mithril_client/cardano_database_client/
statistics.rs1use std::sync::Arc;
2
3use crate::{MithrilResult, cardano_database_client::CardanoDatabaseAggregatorRequest};
4
5pub struct InternalStatisticsSender {
6 pub(super) aggregator_requester: Arc<dyn CardanoDatabaseAggregatorRequest>,
7}
8
9impl InternalStatisticsSender {
10 pub fn new(aggregator_requester: Arc<dyn CardanoDatabaseAggregatorRequest>) -> Self {
12 Self {
13 aggregator_requester,
14 }
15 }
16
17 pub async fn add_statistics(
19 &self,
20 full_restoration: bool,
21 include_ancillary: bool,
22 number_of_immutable_files_restored: u64,
23 ) -> MithrilResult<()> {
24 if include_ancillary {
25 self.aggregator_requester
26 .increment_ancillary_downloaded_statistic()
27 .await?;
28 };
29
30 if full_restoration {
31 self.aggregator_requester
32 .increment_cardano_database_complete_restoration_statistic()
33 .await?;
34 } else {
35 self.aggregator_requester
36 .increment_cardano_database_partial_restoration_statistic()
37 .await?;
38 };
39
40 if number_of_immutable_files_restored > 0 {
41 self.aggregator_requester
42 .increment_immutables_snapshot_restored_statistic(
43 number_of_immutable_files_restored as u32,
44 )
45 .await?;
46 }
47
48 Ok(())
49 }
50}
51
52#[cfg(test)]
53mod tests {
54 use mockall::predicate::eq;
55
56 use crate::cardano_database_client::CardanoDatabaseClientDependencyInjector;
57
58 #[tokio::test]
59 async fn add_statistics_with_ancillary() {
60 let include_ancillary = true;
61 let client = CardanoDatabaseClientDependencyInjector::new()
62 .with_aggregator_requester_mock_config(|requester| {
63 requester
64 .expect_increment_ancillary_downloaded_statistic()
65 .times(1)
66 .return_once(|| Ok(()));
67
68 requester
69 .expect_increment_cardano_database_partial_restoration_statistic()
70 .returning(|| Ok(()));
71 requester
72 .expect_increment_immutables_snapshot_restored_statistic()
73 .returning(|_| Ok(()));
74 })
75 .build_cardano_database_client();
76
77 client.add_statistics(false, include_ancillary, 99).await.unwrap();
78 }
79
80 #[tokio::test]
81 async fn add_statistics_without_ancillary() {
82 let include_ancillary = false;
83 let client = CardanoDatabaseClientDependencyInjector::new()
84 .with_aggregator_requester_mock_config(|requester| {
85 requester.expect_increment_ancillary_downloaded_statistic().never();
86
87 requester
88 .expect_increment_cardano_database_partial_restoration_statistic()
89 .returning(|| Ok(()));
90 requester
91 .expect_increment_immutables_snapshot_restored_statistic()
92 .returning(|_| Ok(()));
93 })
94 .build_cardano_database_client();
95
96 client.add_statistics(false, include_ancillary, 99).await.unwrap();
97 }
98
99 #[tokio::test]
100 async fn add_statistics_with_full_restoration() {
101 let full_restoration = true;
102 let client = CardanoDatabaseClientDependencyInjector::new()
103 .with_aggregator_requester_mock_config(|requester| {
104 requester
105 .expect_increment_cardano_database_complete_restoration_statistic()
106 .returning(|| Ok(()))
107 .times(1);
108 requester
109 .expect_increment_cardano_database_partial_restoration_statistic()
110 .never();
111
112 requester
113 .expect_increment_ancillary_downloaded_statistic()
114 .returning(|| Ok(()));
115 requester
116 .expect_increment_immutables_snapshot_restored_statistic()
117 .returning(|_| Ok(()));
118 })
119 .build_cardano_database_client();
120
121 client.add_statistics(full_restoration, false, 99).await.unwrap();
122 }
123
124 #[tokio::test]
125 async fn add_statistics_with_partial_restoration() {
126 let full_restoration = false;
127 let client = CardanoDatabaseClientDependencyInjector::new()
128 .with_aggregator_requester_mock_config(|requester| {
129 requester
130 .expect_increment_cardano_database_partial_restoration_statistic()
131 .returning(|| Ok(()))
132 .times(1);
133 requester
134 .expect_increment_cardano_database_complete_restoration_statistic()
135 .never();
136
137 requester
138 .expect_increment_immutables_snapshot_restored_statistic()
139 .returning(|_| Ok(()));
140 })
141 .build_cardano_database_client();
142
143 client.add_statistics(full_restoration, false, 99).await.unwrap();
144 }
145
146 #[tokio::test]
147 async fn add_statistics_increments_immutable_files_restored() {
148 let immutable_files_restored = 123456;
149 let client = CardanoDatabaseClientDependencyInjector::new()
150 .with_aggregator_requester_mock_config(|requester| {
151 requester
152 .expect_increment_immutables_snapshot_restored_statistic()
153 .times(1)
154 .with(eq(immutable_files_restored as u32))
155 .returning(|_| Ok(()));
156
157 requester
158 .expect_increment_cardano_database_partial_restoration_statistic()
159 .returning(|| Ok(()));
160 })
161 .build_cardano_database_client();
162
163 client
164 .add_statistics(false, false, immutable_files_restored)
165 .await
166 .unwrap();
167 }
168
169 #[tokio::test]
170 async fn add_statistics_does_not_increment_immutable_files_restored_when_none_restored() {
171 let immutable_files_restored = 0;
172 let client = CardanoDatabaseClientDependencyInjector::new()
173 .with_aggregator_requester_mock_config(|requester| {
174 requester
175 .expect_increment_immutables_snapshot_restored_statistic()
176 .never()
177 .returning(|_| Ok(()));
178
179 requester
180 .expect_increment_cardano_database_partial_restoration_statistic()
181 .returning(|| Ok(()));
182 })
183 .build_cardano_database_client();
184
185 client
186 .add_statistics(false, false, immutable_files_restored)
187 .await
188 .unwrap();
189 }
190}