-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ai-help): delete ai history after six months (#437)
- Loading branch information
Showing
8 changed files
with
171 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::db::schema::ai_help_history; | ||
use crate::db::Pool; | ||
use crate::diesel::QueryDsl; | ||
use crate::{api::error::ApiError, settings::SETTINGS}; | ||
use actix_web::web::Data; | ||
use chrono::Utc; | ||
use diesel::{ExpressionMethods, RunQueryDsl}; | ||
use std::ops::Sub; | ||
use std::time::Duration; | ||
|
||
/// This removes old AI history records from the database. It is meant to be called from a | ||
/// cron job calling the respective endpoint in the admin API. | ||
pub async fn do_delete_old_ai_history(pool: Data<Pool>) -> Result<(), ApiError> { | ||
let mut conn = pool.get()?; | ||
let history_deletion_period_in_sec = SETTINGS | ||
.ai | ||
.as_ref() | ||
.map(|ai| ai.history_deletion_period_in_sec) | ||
.ok_or(ApiError::Generic( | ||
"ai.history_deletion_period_in_sec missing from configuration".to_string(), | ||
))?; | ||
|
||
let oldest_timestamp = Utc::now() | ||
.sub(Duration::from_secs(history_deletion_period_in_sec)) | ||
.naive_utc(); | ||
|
||
let affected_rows = diesel::delete( | ||
ai_help_history::table.filter(ai_help_history::updated_at.lt(oldest_timestamp)), | ||
) | ||
.execute(&mut conn)?; | ||
info!( | ||
"Deleted old AI history before {oldest_timestamp}: {affected_rows} old record(s) deleted." | ||
); | ||
Ok(()) | ||
} |
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 @@ | ||
pub mod ai_explain; | ||
pub mod ai_help; | ||
pub mod ai_history; | ||
pub mod documents; | ||
pub mod error; | ||
pub mod fxa_webhook; | ||
|
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