chore: rustfmt the workspace and make clippy -D warnings pass
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -61,8 +61,13 @@ impl RetentionPolicy {
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
#[serde(tag = "mode", rename_all = "snake_case", deny_unknown_fields)]
|
||||
pub enum CaptureRetention {
|
||||
Age { days: u16 },
|
||||
Count { #[serde(default = "fallback_count")] keep: u16 },
|
||||
Age {
|
||||
days: u16,
|
||||
},
|
||||
Count {
|
||||
#[serde(default = "fallback_count")]
|
||||
keep: u16,
|
||||
},
|
||||
}
|
||||
|
||||
fn fallback_count() -> u16 {
|
||||
@@ -90,13 +95,20 @@ pub fn evaluate_capture_retention(
|
||||
now: DateTime<Utc>,
|
||||
) -> Vec<Uuid> {
|
||||
snapshots.sort_unstable_by_key(|snapshot| std::cmp::Reverse((snapshot.taken_at, snapshot.id)));
|
||||
snapshots.into_iter().enumerate().skip(1).filter_map(|(index, snapshot)| {
|
||||
let expired = match policy {
|
||||
CaptureRetention::Age { days } => now.signed_duration_since(snapshot.taken_at).num_days() >= i64::from(days),
|
||||
CaptureRetention::Count { keep } => index >= usize::from(keep),
|
||||
};
|
||||
expired.then_some(snapshot.id)
|
||||
}).collect()
|
||||
snapshots
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.skip(1)
|
||||
.filter_map(|(index, snapshot)| {
|
||||
let expired = match policy {
|
||||
CaptureRetention::Age { days } => {
|
||||
now.signed_duration_since(snapshot.taken_at).num_days() >= i64::from(days)
|
||||
}
|
||||
CaptureRetention::Count { keep } => index >= usize::from(keep),
|
||||
};
|
||||
expired.then_some(snapshot.id)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Keep the newest snapshot in each of the most recent occupied UTC periods.
|
||||
@@ -175,7 +187,10 @@ pub fn evaluate_decay(policy: &DecayPolicy, mut snapshots: Vec<SnapshotRef>) ->
|
||||
let mut months: Vec<(i32, u32, SnapshotRef)> = Vec::new();
|
||||
for s in &snapshots {
|
||||
let key = (s.taken_at.year(), s.taken_at.month());
|
||||
if let Some(pos) = months.iter().position(|(y, m, _)| *y == key.0 && *m == key.1) {
|
||||
if let Some(pos) = months
|
||||
.iter()
|
||||
.position(|(y, m, _)| *y == key.0 && *m == key.1)
|
||||
{
|
||||
if s.taken_at > months[pos].2.taken_at {
|
||||
months[pos] = (key.0, key.1, s.clone());
|
||||
}
|
||||
@@ -204,14 +219,23 @@ mod tests {
|
||||
#[test]
|
||||
fn capture_retention_keeps_one_old_archive_or_the_latest_five() {
|
||||
let now = Utc::now();
|
||||
let snapshots: Vec<_> = (0..6).map(|i| SnapshotRef {
|
||||
id: Uuid::new_v4(),
|
||||
taken_at: now - Duration::days(15 + i),
|
||||
}).collect();
|
||||
assert_eq!(evaluate_capture_retention(CaptureRetention::default(), snapshots.clone(), now),
|
||||
snapshots[1..].iter().map(|snap| snap.id).collect::<Vec<_>>());
|
||||
assert_eq!(evaluate_capture_retention(CaptureRetention::Count { keep: 5 }, snapshots.clone(), now),
|
||||
[snapshots[5].id]);
|
||||
let snapshots: Vec<_> = (0..6)
|
||||
.map(|i| SnapshotRef {
|
||||
id: Uuid::new_v4(),
|
||||
taken_at: now - Duration::days(15 + i),
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(
|
||||
evaluate_capture_retention(CaptureRetention::default(), snapshots.clone(), now),
|
||||
snapshots[1..]
|
||||
.iter()
|
||||
.map(|snap| snap.id)
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
assert_eq!(
|
||||
evaluate_capture_retention(CaptureRetention::Count { keep: 5 }, snapshots.clone(), now),
|
||||
[snapshots[5].id]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user