Skip to main content

canic_backup/restore/apply/journal/
counts.rs

1use super::{
2    RestoreApplyDryRunOperation, RestoreApplyJournalError, RestoreApplyJournalOperation,
3    RestoreApplyOperationKind, RestoreApplyOperationState, validate_apply_journal_count,
4};
5use serde::{Deserialize, Serialize};
6
7///
8/// RestoreApplyJournalStateCounts
9///
10
11#[derive(Clone, Debug, Default, Eq, PartialEq)]
12pub(super) struct RestoreApplyJournalStateCounts {
13    pub(super) pending: usize,
14    pub(super) ready: usize,
15    pub(super) blocked: usize,
16    pub(super) completed: usize,
17    pub(super) failed: usize,
18}
19
20impl RestoreApplyJournalStateCounts {
21    // Count operation states from concrete journal operation rows.
22    pub(super) fn from_operations(operations: &[RestoreApplyJournalOperation]) -> Self {
23        let mut counts = Self::default();
24        for operation in operations {
25            match operation.state {
26                RestoreApplyOperationState::Pending => counts.pending += 1,
27                RestoreApplyOperationState::Ready => counts.ready += 1,
28                RestoreApplyOperationState::Blocked => counts.blocked += 1,
29                RestoreApplyOperationState::Completed => counts.completed += 1,
30                RestoreApplyOperationState::Failed => counts.failed += 1,
31            }
32        }
33        counts
34    }
35}
36
37///
38/// RestoreApplyOperationKindCounts
39///
40
41#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
42pub struct RestoreApplyOperationKindCounts {
43    pub snapshot_uploads: usize,
44    pub snapshot_loads: usize,
45    pub member_verifications: usize,
46    pub fleet_verifications: usize,
47    pub verification_operations: usize,
48}
49
50impl RestoreApplyOperationKindCounts {
51    /// Count restore apply journal operations by runner operation kind.
52    #[must_use]
53    pub fn from_operations(operations: &[RestoreApplyJournalOperation]) -> Self {
54        let mut counts = Self::default();
55        for operation in operations {
56            counts.record(&operation.operation);
57        }
58        counts
59    }
60
61    /// Validate this count object against concrete operations.
62    pub fn validate_matches(&self, expected: &Self) -> Result<(), RestoreApplyJournalError> {
63        validate_apply_journal_count(
64            "operation_counts.snapshot_uploads",
65            self.snapshot_uploads,
66            expected.snapshot_uploads,
67        )?;
68        validate_apply_journal_count(
69            "operation_counts.snapshot_loads",
70            self.snapshot_loads,
71            expected.snapshot_loads,
72        )?;
73        validate_apply_journal_count(
74            "operation_counts.member_verifications",
75            self.member_verifications,
76            expected.member_verifications,
77        )?;
78        validate_apply_journal_count(
79            "operation_counts.fleet_verifications",
80            self.fleet_verifications,
81            expected.fleet_verifications,
82        )?;
83        validate_apply_journal_count(
84            "operation_counts.verification_operations",
85            self.verification_operations,
86            expected.verification_operations,
87        )
88    }
89
90    /// Count restore apply dry-run operations by runner operation kind.
91    #[must_use]
92    pub fn from_dry_run_operations(operations: &[RestoreApplyDryRunOperation]) -> Self {
93        let mut counts = Self::default();
94        for operation in operations {
95            counts.record(&operation.operation);
96        }
97        counts
98    }
99
100    // Record one operation kind in the aggregate count object.
101    const fn record(&mut self, operation: &RestoreApplyOperationKind) {
102        match operation {
103            RestoreApplyOperationKind::UploadSnapshot => self.snapshot_uploads += 1,
104            RestoreApplyOperationKind::LoadSnapshot => self.snapshot_loads += 1,
105            RestoreApplyOperationKind::VerifyMember => {
106                self.member_verifications += 1;
107                self.verification_operations += 1;
108            }
109            RestoreApplyOperationKind::VerifyFleet => {
110                self.fleet_verifications += 1;
111                self.verification_operations += 1;
112            }
113        }
114    }
115}