-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(job): db job should retain job_runs and user_events for only las…
…t three months (#3640) * feat(job): add daily job to retain job_runs and user_events for only last three months Signed-off-by: Wei Zhang <[email protected]> * [autofix.ci] apply automated fixes * chore: fix review Signed-off-by: Wei Zhang <[email protected]> * chore: fix tests Signed-off-by: Wei Zhang <[email protected]> * chore: return error directly if meet one for now Signed-off-by: Wei Zhang <[email protected]> --------- Signed-off-by: Wei Zhang <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
20e34e2
commit fc64e65
Showing
3 changed files
with
194 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use std::sync::Arc; | ||
|
||
use anyhow::Context; | ||
use chrono::{DateTime, Utc}; | ||
use serde::{Deserialize, Serialize}; | ||
use tabby_db::DbConn; | ||
|
@@ -16,7 +17,7 @@ impl Job for DbMaintainanceJob { | |
|
||
impl DbMaintainanceJob { | ||
pub async fn cron( | ||
_now: DateTime<Utc>, | ||
now: DateTime<Utc>, | ||
context: Arc<dyn ContextService>, | ||
db: DbConn, | ||
) -> tabby_schema::Result<()> { | ||
|
@@ -35,6 +36,159 @@ impl DbMaintainanceJob { | |
|
||
db.delete_unused_source_id_read_access_policy(&active_source_ids) | ||
.await?; | ||
|
||
Self::data_retention(now, &db).await?; | ||
Ok(()) | ||
} | ||
|
||
async fn data_retention(now: DateTime<Utc>, db: &DbConn) -> tabby_schema::Result<()> { | ||
db.delete_job_run_before_three_months(now) | ||
.await | ||
.context("Failed to clean up and retain only the last 3 months of jobs")?; | ||
|
||
db.delete_user_events_before_three_months(now) | ||
.await | ||
.context("Failed to clean up and retain only the last 3 months of user events")?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use chrono::{DateTime, Utc}; | ||
use tabby_db::DbConn; | ||
|
||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_retention_should_delete() { | ||
let db = DbConn::new_in_memory().await.unwrap(); | ||
let cases = vec![ | ||
( | ||
"2024-04-30T12:12:12Z".parse::<DateTime<Utc>>().unwrap(), | ||
"2024-01-30T12:12:11Z".parse::<DateTime<Utc>>().unwrap(), | ||
), | ||
( | ||
"2024-04-30T12:12:12Z".parse::<DateTime<Utc>>().unwrap(), | ||
"2024-01-29T12:12:12Z".parse::<DateTime<Utc>>().unwrap(), | ||
), | ||
( | ||
"2024-05-01T12:12:12Z".parse::<DateTime<Utc>>().unwrap(), | ||
"2024-01-31T12:12:11Z".parse::<DateTime<Utc>>().unwrap(), | ||
), | ||
]; | ||
|
||
let user_id = db | ||
.create_user("[email protected]".to_string(), None, true, None) | ||
.await | ||
.unwrap(); | ||
for (now, created) in cases { | ||
db.create_user_event( | ||
user_id, | ||
"test".to_string(), | ||
created.timestamp_millis() as u128, | ||
"".to_string(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let events = db | ||
.list_user_events( | ||
None, | ||
None, | ||
false, | ||
vec![user_id], | ||
created.checked_sub_days(chrono::Days::new(1)).unwrap(), | ||
now, | ||
) | ||
.await | ||
.unwrap(); | ||
assert_eq!(events.len(), 1); | ||
|
||
DbMaintainanceJob::data_retention(now, &db).await.unwrap(); | ||
|
||
let events = db | ||
.list_user_events( | ||
None, | ||
None, | ||
false, | ||
vec![user_id], | ||
created.checked_sub_days(chrono::Days::new(1)).unwrap(), | ||
now, | ||
) | ||
.await | ||
.unwrap(); | ||
assert_eq!(events.len(), 0); | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_retention_should_not_delete() { | ||
let db = DbConn::new_in_memory().await.unwrap(); | ||
let cases = vec![ | ||
( | ||
"2024-04-30T12:12:12Z".parse::<DateTime<Utc>>().unwrap(), | ||
"2024-01-31T12:12:12Z".parse::<DateTime<Utc>>().unwrap(), | ||
), | ||
( | ||
"2024-04-30T12:12:12Z".parse::<DateTime<Utc>>().unwrap(), | ||
"2024-01-30T12:12:12Z".parse::<DateTime<Utc>>().unwrap(), | ||
), | ||
( | ||
"2024-04-30T12:12:12Z".parse::<DateTime<Utc>>().unwrap(), | ||
"2024-04-30T12:12:11Z".parse::<DateTime<Utc>>().unwrap(), | ||
), | ||
]; | ||
|
||
let user_id = db | ||
.create_user("[email protected]".to_string(), None, true, None) | ||
.await | ||
.unwrap(); | ||
for (now, created) in cases { | ||
db.create_user_event( | ||
user_id, | ||
"test".to_string(), | ||
created.timestamp_millis() as u128, | ||
"".to_string(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let events = db | ||
.list_user_events( | ||
None, | ||
None, | ||
false, | ||
vec![user_id], | ||
created.checked_sub_days(chrono::Days::new(1)).unwrap(), | ||
now, | ||
) | ||
.await | ||
.unwrap(); | ||
assert_eq!(events.len(), 1); | ||
|
||
DbMaintainanceJob::data_retention(now, &db).await.unwrap(); | ||
|
||
let events = db | ||
.list_user_events( | ||
None, | ||
None, | ||
false, | ||
vec![user_id], | ||
created.checked_sub_days(chrono::Days::new(1)).unwrap(), | ||
now, | ||
) | ||
.await | ||
.unwrap(); | ||
assert_eq!(events.len(), 1); | ||
|
||
// clean up for next iteration | ||
db.delete_user_events_before_three_months( | ||
now.checked_add_months(chrono::Months::new(3)).unwrap(), | ||
) | ||
.await | ||
.unwrap(); | ||
} | ||
} | ||
} |