-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(job): add daily job to retain job_runs and user_events for only …
…last three months Signed-off-by: Wei Zhang <[email protected]>
- Loading branch information
Showing
4 changed files
with
182 additions
and
1 deletion.
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,9 +1,10 @@ | ||
use std::sync::Arc; | ||
|
||
use chrono::{DateTime, Utc}; | ||
use chrono::{DateTime, Months, Utc}; | ||
use serde::{Deserialize, Serialize}; | ||
use tabby_db::DbConn; | ||
use tabby_schema::context::ContextService; | ||
use tracing::error; | ||
|
||
use super::helper::Job; | ||
|
||
|
@@ -37,4 +38,157 @@ impl DbMaintainanceJob { | |
.await?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn retention(now: DateTime<Utc>, db: DbConn) -> tabby_schema::Result<()> { | ||
if let Some(three_months_ago) = now.checked_sub_months(Months::new(3)) { | ||
if let Err(e) = db.delete_jobs_before(three_months_ago).await { | ||
error!( | ||
"Failed to clean up and retain only the last 3 months of jobs: {:?}", | ||
e | ||
); | ||
} | ||
|
||
if let Err(e) = db.delete_user_events_before(three_months_ago).await { | ||
error!( | ||
"Failed to clean up and retain only the last 3 months of user events: {:?}", | ||
e | ||
); | ||
} | ||
}; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use chrono::{DateTime, Utc}; | ||
use tabby_db::DbConn; | ||
|
||
#[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::retention(now, db.clone()).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::retention(now, db.clone()).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(now).await.unwrap(); | ||
} | ||
} | ||
} |
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