Skip to content

Commit

Permalink
fix(webserver): when compute daily stats for Other languages, it shou… (
Browse files Browse the repository at this point in the history
TabbyML#1837)

* 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 <[email protected]>
  • Loading branch information
3 people authored Apr 12, 2024
1 parent 14eca32 commit 9520ec4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
20 changes: 10 additions & 10 deletions ee/tabby-db/src/user_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl DbConn {
end: DateTime<Utc>,
users: Vec<i64>,
languages: Vec<String>,
not_languages: Vec<String>,
all_languages: Vec<String>,
) -> Result<Vec<UserCompletionDailyStatsDAO>> {
let users = users
.iter()
Expand All @@ -135,7 +135,7 @@ impl DbConn {
.map(|l| format!("'{}'", l))
.collect::<Vec<_>>()
.join(",");
let not_languages = not_languages
let all_languages = all_languages
.into_iter()
.map(|l| format!("'{}'", l))
.collect::<Vec<_>>()
Expand All @@ -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
"#,
Expand All @@ -161,9 +164,6 @@ impl DbConn {
))
.bind(start)
.bind(end)
.bind(users)
.bind(languages)
.bind(not_languages)
.fetch_all(&self.pool)
.await?;
Ok(res)
Expand Down
32 changes: 13 additions & 19 deletions ee/tabby-webserver/src/service/analytic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,15 @@ impl AnalyticService for AnalyticServiceImpl {
start: DateTime<Utc>,
end: DateTime<Utc>,
users: Vec<ID>,
mut languages: Vec<Language>,
languages: Vec<Language>,
) -> Result<Vec<CompletionStats>> {
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()
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 9520ec4

Please sign in to comment.