feat: add configurable GFS retention per orbit
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use chrono::{DateTime, Datelike, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashSet;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -42,6 +43,64 @@ pub struct SnapshotRef {
|
||||
pub taken_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct RetentionPolicy {
|
||||
pub keep_daily: u32,
|
||||
pub keep_weekly: u32,
|
||||
pub keep_monthly: u32,
|
||||
pub keep_yearly: u32,
|
||||
}
|
||||
|
||||
impl RetentionPolicy {
|
||||
pub fn has_tiers(&self) -> bool {
|
||||
*self != Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep the newest snapshot in each of the most recent occupied UTC periods.
|
||||
pub fn evaluate_gfs(policy: &RetentionPolicy, mut snapshots: Vec<SnapshotRef>) -> Vec<Uuid> {
|
||||
if !policy.has_tiers() {
|
||||
return Vec::new();
|
||||
}
|
||||
snapshots.sort_unstable_by_key(|s| std::cmp::Reverse((s.taken_at, s.id)));
|
||||
let mut keep = HashSet::new();
|
||||
keep_periods(&snapshots, &mut keep, policy.keep_daily, |at| {
|
||||
at.date_naive()
|
||||
});
|
||||
keep_periods(&snapshots, &mut keep, policy.keep_weekly, |at| {
|
||||
at.iso_week()
|
||||
});
|
||||
keep_periods(&snapshots, &mut keep, policy.keep_monthly, |at| {
|
||||
(at.year(), at.month())
|
||||
});
|
||||
keep_periods(&snapshots, &mut keep, policy.keep_yearly, |at| at.year());
|
||||
snapshots
|
||||
.into_iter()
|
||||
.filter_map(|s| (!keep.contains(&s.id)).then_some(s.id))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn keep_periods<K: PartialEq>(
|
||||
snapshots: &[SnapshotRef],
|
||||
keep: &mut HashSet<Uuid>,
|
||||
mut count: u32,
|
||||
period: impl Fn(DateTime<Utc>) -> K,
|
||||
) {
|
||||
let mut last = None;
|
||||
for s in snapshots {
|
||||
if count == 0 {
|
||||
break;
|
||||
}
|
||||
let key = period(s.taken_at);
|
||||
if last.as_ref() != Some(&key) {
|
||||
keep.insert(s.id);
|
||||
count -= 1;
|
||||
last = Some(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns snapshot IDs to forget (newest-first input).
|
||||
pub fn evaluate_decay(policy: &DecayPolicy, mut snapshots: Vec<SnapshotRef>) -> Vec<Uuid> {
|
||||
snapshots.sort_by(|a, b| b.taken_at.cmp(&a.taken_at));
|
||||
@@ -96,12 +155,92 @@ pub fn evaluate_decay(policy: &DecayPolicy, mut snapshots: Vec<SnapshotRef>) ->
|
||||
.collect()
|
||||
}
|
||||
|
||||
use chrono::Datelike;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use chrono::Duration;
|
||||
use chrono::{Duration, TimeZone, Timelike};
|
||||
|
||||
#[test]
|
||||
fn gfs_keeps_the_newest_in_each_period_and_combines_tiers() {
|
||||
let policy = RetentionPolicy {
|
||||
keep_daily: 7,
|
||||
keep_weekly: 2,
|
||||
keep_monthly: 6,
|
||||
keep_yearly: 3,
|
||||
};
|
||||
let newest = Utc.with_ymd_and_hms(2026, 1, 17, 20, 0, 0).unwrap();
|
||||
let snapshots: Vec<_> = (0..1460)
|
||||
.rev()
|
||||
.flat_map(|day| {
|
||||
[
|
||||
newest - Duration::days(day) - Duration::hours(12),
|
||||
newest - Duration::days(day),
|
||||
]
|
||||
.map(|taken_at| SnapshotRef {
|
||||
id: Uuid::new_v4(),
|
||||
taken_at,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let expired: std::collections::HashSet<_> = evaluate_gfs(&policy, snapshots.clone())
|
||||
.into_iter()
|
||||
.collect();
|
||||
let kept: Vec<_> = snapshots
|
||||
.iter()
|
||||
.filter(|s| !expired.contains(&s.id))
|
||||
.collect();
|
||||
let dates: Vec<_> = kept
|
||||
.iter()
|
||||
.map(|s| s.taken_at.date_naive().to_string())
|
||||
.collect();
|
||||
assert_eq!(
|
||||
dates,
|
||||
[
|
||||
"2024-12-31",
|
||||
"2025-08-31",
|
||||
"2025-09-30",
|
||||
"2025-10-31",
|
||||
"2025-11-30",
|
||||
"2025-12-31",
|
||||
"2026-01-11",
|
||||
"2026-01-12",
|
||||
"2026-01-13",
|
||||
"2026-01-14",
|
||||
"2026-01-15",
|
||||
"2026-01-16",
|
||||
"2026-01-17"
|
||||
]
|
||||
);
|
||||
assert!(kept.iter().all(|s| s.taken_at.hour() == 20));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gfs_weeks_start_monday_and_skip_empty_weeks() {
|
||||
let sunday = Utc.with_ymd_and_hms(2025, 1, 5, 23, 0, 0).unwrap();
|
||||
let snapshots: Vec<_> = [-49, -1, 0, 1]
|
||||
.map(|hours| SnapshotRef {
|
||||
id: Uuid::new_v4(),
|
||||
taken_at: sunday + Duration::hours(hours),
|
||||
})
|
||||
.into();
|
||||
let policy = RetentionPolicy {
|
||||
keep_weekly: 2,
|
||||
..RetentionPolicy::default()
|
||||
};
|
||||
assert_eq!(
|
||||
evaluate_gfs(&policy, snapshots.clone()),
|
||||
[snapshots[1].id, snapshots[0].id]
|
||||
);
|
||||
let sparse = vec![
|
||||
snapshots[3].clone(),
|
||||
SnapshotRef {
|
||||
id: snapshots[0].id,
|
||||
taken_at: sunday - Duration::weeks(3),
|
||||
},
|
||||
];
|
||||
assert!(evaluate_gfs(&policy, sparse).is_empty());
|
||||
assert!(evaluate_gfs(&RetentionPolicy::default(), snapshots).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_n_dailies() {
|
||||
|
||||
Reference in New Issue
Block a user