From 9520ec4a5e71a5e14a36d8396a964141de584487 Mon Sep 17 00:00:00 2001 From: Meng Zhang Date: Fri, 12 Apr 2024 10:46:16 -0700 Subject: [PATCH] =?UTF-8?q?fix(webserver):=20when=20compute=20daily=20stat?= =?UTF-8?q?s=20for=20Other=20languages,=20it=20shou=E2=80=A6=20(#1837)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(webserver): when compute daily stats for Other languages, it should use OR logic operator * [autofix.ci] apply automated fixes * Fix query and add another test case --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: boxbeam --- ee/tabby-db/src/user_completions.rs | 20 +++++++------- ee/tabby-webserver/src/service/analytic.rs | 32 +++++++++------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/ee/tabby-db/src/user_completions.rs b/ee/tabby-db/src/user_completions.rs index 2c2143f69d65..18c5f533dd82 100644 --- a/ee/tabby-db/src/user_completions.rs +++ b/ee/tabby-db/src/user_completions.rs @@ -123,7 +123,7 @@ impl DbConn { end: DateTime, users: Vec, languages: Vec, - not_languages: Vec, + all_languages: Vec, ) -> Result> { let users = users .iter() @@ -135,7 +135,7 @@ impl DbConn { .map(|l| format!("'{}'", l)) .collect::>() .join(","); - let not_languages = not_languages + let all_languages = all_languages .into_iter() .map(|l| format!("'{}'", l)) .collect::>() @@ -145,14 +145,17 @@ impl DbConn { // with `STRFTIME('%s')`. The effect of this is to extract the unix timestamp (seconds) rounded to // the start of the day and group them by that. let res = sqlx::query_as(&format!( - r#" + r#" SELECT CAST(STRFTIME('%s', DATE(created_at)) AS TIMESTAMP) as start, SUM(1) as completions, SUM(selects) as selects - FROM user_completions - WHERE created_at >= ?1 AND created_at < ?2 - AND ({no_selected_users} OR user_id IN ({users})) - AND (({no_selected_languages} OR language IN ({languages})) AND (language NOT IN ({not_languages}))) + FROM ( + SELECT created_at, selects, IIF(language IN ({all_languages}), language, 'other') as language + FROM user_completions + WHERE created_at >= ?1 AND created_at < ?2 + ) + WHERE ({no_selected_users} OR user_id IN ({users})) + AND ({no_selected_languages} OR language IN ({languages})) GROUP BY 1 ORDER BY 1 ASC "#, @@ -161,9 +164,6 @@ impl DbConn { )) .bind(start) .bind(end) - .bind(users) - .bind(languages) - .bind(not_languages) .fetch_all(&self.pool) .await?; Ok(res) diff --git a/ee/tabby-webserver/src/service/analytic.rs b/ee/tabby-webserver/src/service/analytic.rs index 8339a3890799..18dd7fff2f6e 100644 --- a/ee/tabby-webserver/src/service/analytic.rs +++ b/ee/tabby-webserver/src/service/analytic.rs @@ -38,29 +38,15 @@ impl AnalyticService for AnalyticServiceImpl { start: DateTime, end: DateTime, users: Vec, - mut languages: Vec, + languages: Vec, ) -> Result> { let users = convert_ids(users); - let include_other_languages = languages.iter().any(|l| l == &Language::Other); - let not_languages = if include_other_languages { - Some(Language::all_known().flat_map(|l| l.to_strings()).collect()) - } else { - None - }; - - languages.retain(|l| l != &Language::Other); - + let all_languages = Language::all_known().flat_map(|l| l.to_strings()).collect(); let languages = languages.into_iter().flat_map(|l| l.to_strings()).collect(); let stats = self .db - .compute_daily_stats( - start, - end, - users, - languages, - not_languages.unwrap_or_default(), - ) + .compute_daily_stats(start, end, users, languages, all_languages) .await?; let stats = stats .into_iter() @@ -221,11 +207,19 @@ mod tests { let start = end.checked_sub_days(Days::new(100)).unwrap(); let stats = service - .daily_stats(start, end, vec![], vec![Language::Other]) + .daily_stats(start, end, vec![], vec![Language::Rust, Language::Other]) .await .unwrap(); assert_eq!(1, stats.len()); - assert_eq!(1, stats[0].completions); + assert_eq!(2, stats[0].completions); + + let stats2 = service + .daily_stats(start, end, vec![], vec![Language::Other]) + .await + .unwrap(); + + assert_eq!(1, stats2.len()); + assert_eq!(1, stats2[0].completions); } }