feat(config): add retention period to eol rule

- add retention functionality to EolRule
- parse retention string to calculate EOL deadline
- generate 'before:' query based on retention period
This commit is contained in:
Jeremiah Russell
2025-10-10 22:40:22 +01:00
committed by Jeremiah Russell
parent 9fa268340f
commit d48abdc3ad

View File

@@ -1,8 +1,9 @@
use std::{collections::BTreeSet, fmt}; use std::{collections::BTreeSet, fmt};
use chrono::{Datelike, Local, TimeDelta, TimeZone};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{Retention, eol_action::EolAction}; use crate::{MessageAge, Retention, eol_action::EolAction};
/// End of life rules /// End of life rules
#[derive(Debug, Serialize, Deserialize, Default, Clone)] #[derive(Debug, Serialize, Deserialize, Default, Clone)]
@@ -133,8 +134,51 @@ impl EolRule {
) )
} }
pub(crate) fn eol_query(&self) -> String { pub(crate) fn eol_query(&self) -> Option<String> {
format!("before: {}", chrono::Local::now().format("%Y-%m-%d")) let today = chrono::Local::now();
let message_age = MessageAge::parse(&self.retention)?;
let deadline = match message_age {
MessageAge::Days(c) => {
let delta = TimeDelta::days(c);
today.checked_sub_signed(delta).unwrap()
}
MessageAge::Weeks(c) => {
let delta = TimeDelta::weeks(c);
today.checked_sub_signed(delta).unwrap()
}
MessageAge::Months(c) => {
let day = today.day();
let month = today.month();
let year = today.year();
let mut years = c as i32 / 12;
let months = c % 12;
let mut new_month = month - months as u32;
if new_month < 1 {
years += 1;
new_month += 12;
}
let new_year = year - years;
Local
.with_ymd_and_hms(new_year, new_month, day, 0, 0, 0)
.unwrap()
}
MessageAge::Years(c) => {
let day = today.day();
let month = today.month();
let year = today.year();
let new_year = year - c as i32;
Local
.with_ymd_and_hms(new_year, month, day, 0, 0, 0)
.unwrap()
}
};
Some(format!("before: {}", deadline.format("%Y-%m-%d")))
} }
} }