Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(db): don't count inactive users in stats #1939

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

## Fixes and Improvements
* Changed the default model filename from `q8_0.v2.gguf` to `model.gguf` in MODEL_SPEC.md
* Inactive users are no longer counted in reports
wsxiaoys marked this conversation as resolved.
Show resolved Hide resolved

# v0.10.0

Expand Down
14 changes: 7 additions & 7 deletions ee/tabby-db/src/user_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ impl DbConn {
.join(",");
Ok(sqlx::query_as(&format!(
r#"
SELECT CAST(STRFTIME('%s', DATE(created_at)) AS TIMESTAMP) as start,
SELECT CAST(STRFTIME('%s', DATE(user_completions.created_at)) AS TIMESTAMP) as start,
language,
SUM(1) as completions,
SUM(selects) as selects,
SUM(views) as views
FROM user_completions
WHERE created_at >= DATE('now', '-1 year')
FROM user_completions JOIN users ON user_id = users.id AND users.active
WHERE user_completions.created_at >= DATE('now', '-1 year')
AND ({users_empty} OR user_id IN ({users}))
GROUP BY 1, 2
ORDER BY 1, 2 ASC
Expand Down Expand Up @@ -155,13 +155,13 @@ impl DbConn {
SUM(views) as views
FROM (
SELECT user_id,
CAST((STRFTIME('%s', created_at) - STRFTIME('%s', ?1)) / 3600 / 24 AS INT) as days_since_start,
created_at,
CAST((STRFTIME('%s', user_completions.created_at) - STRFTIME('%s', ?1)) / 3600 / 24 AS INT) as days_since_start,
user_completions.created_at,
selects,
views,
IIF(language IN ({all_languages}), language, 'other') as language
FROM user_completions
WHERE created_at >= ?1 AND created_at < ?2
FROM user_completions JOIN users ON user_id = users.id AND users.active
WHERE user_completions.created_at >= ?1 AND user_completions.created_at < ?2
)
WHERE ({no_selected_users} OR user_id IN ({users}))
AND ({no_selected_languages} OR language IN ({languages}))
Expand Down
32 changes: 32 additions & 0 deletions ee/tabby-webserver/src/service/analytic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,36 @@ mod tests {
assert_eq!(1, stats2.len());
assert_eq!(1, stats2[0].completions);
}

#[tokio::test]
async fn test_inactive_user_gets_no_stats() {
let db = DbConn::new_in_memory().await.unwrap();
let service = new_analytic_service(db.clone());

let id = db
.create_user("testuser".into(), None, false)
.await
.unwrap();

db.create_user_completion(timestamp(), id, "completion_id".into(), "rust".into())
.await
.unwrap();

db.update_user_active(id, false).await.unwrap();

assert!(service
.daily_stats_in_past_year(vec![id.as_id()])
.await
.unwrap()
.is_empty());

let end = Utc::now();
let start = end.checked_sub_days(Days::new(100)).unwrap();

assert!(service
.daily_stats(start, end, vec![id.as_id()], vec![])
.await
.unwrap()
.is_empty());
}
}
Loading