mithril_client/cardano_database_client/
statistics.rs1use std::sync::Arc;
2
3use crate::{
4 aggregator_client::{AggregatorClient, AggregatorRequest},
5 MithrilResult,
6};
7
8pub struct InternalStatisticsSender {
9 pub(super) aggregator_client: Arc<dyn AggregatorClient>,
10}
11
12impl InternalStatisticsSender {
13 pub fn new(aggregator_client: Arc<dyn AggregatorClient>) -> Self {
15 Self { aggregator_client }
16 }
17
18 pub async fn add_statistics(
20 &self,
21 full_restoration: bool,
22 include_ancillary: bool,
23 number_of_immutable_files_restored: u64,
24 ) -> MithrilResult<()> {
25 if include_ancillary {
26 self.aggregator_client
27 .post_content(AggregatorRequest::IncrementCardanoDatabaseAncillaryStatistic)
28 .await?;
29 };
30
31 let restoration_request = if full_restoration {
32 AggregatorRequest::IncrementCardanoDatabaseCompleteRestorationStatistic
33 } else {
34 AggregatorRequest::IncrementCardanoDatabasePartialRestorationStatistic
35 };
36
37 self.aggregator_client
38 .post_content(restoration_request)
39 .await?;
40
41 self.aggregator_client
42 .post_content(
43 AggregatorRequest::IncrementCardanoDatabaseImmutablesRestoredStatistic {
44 number_of_immutables: number_of_immutable_files_restored,
45 },
46 )
47 .await?;
48
49 Ok(())
50 }
51}
52
53#[cfg(test)]
54mod tests {
55 use mockall::predicate::{self, eq};
56
57 use crate::{
58 aggregator_client::AggregatorRequest,
59 cardano_database_client::CardanoDatabaseClientDependencyInjector,
60 };
61
62 #[tokio::test]
63 async fn add_statistics_with_ancillary() {
64 let include_ancillary = true;
65 let client = CardanoDatabaseClientDependencyInjector::new()
66 .with_aggregator_client_mock_config(|http_client| {
67 http_client
68 .expect_post_content()
69 .with(eq(
70 AggregatorRequest::IncrementCardanoDatabaseAncillaryStatistic,
71 ))
72 .times(1)
73 .return_once(|_| Ok("whatever".to_string()));
74
75 http_client
76 .expect_post_content()
77 .returning(|_| Ok("whatever".to_string()));
78 })
79 .build_cardano_database_client();
80
81 client
82 .add_statistics(false, include_ancillary, 99)
83 .await
84 .unwrap();
85 }
86
87 #[tokio::test]
88 async fn add_statistics_without_ancillary() {
89 let include_ancillary = false;
90 let client = CardanoDatabaseClientDependencyInjector::new()
91 .with_aggregator_client_mock_config(|http_client| {
92 http_client
93 .expect_post_content()
94 .with(eq(
95 AggregatorRequest::IncrementCardanoDatabaseAncillaryStatistic,
96 ))
97 .never();
98
99 http_client
100 .expect_post_content()
101 .returning(|_| Ok("whatever".to_string()));
102 })
103 .build_cardano_database_client();
104
105 client
106 .add_statistics(false, include_ancillary, 99)
107 .await
108 .unwrap();
109 }
110
111 #[tokio::test]
112 async fn add_statistics_with_full_restoration() {
113 let full_restoration = true;
114 let client = CardanoDatabaseClientDependencyInjector::new()
115 .with_aggregator_client_mock_config(|http_client| {
116 http_client
117 .expect_post_content()
118 .with(eq(
119 AggregatorRequest::IncrementCardanoDatabaseCompleteRestorationStatistic,
120 ))
121 .times(1)
122 .return_once(|_| Ok("whatever".to_string()));
123
124 http_client
125 .expect_post_content()
126 .with(eq(
127 AggregatorRequest::IncrementCardanoDatabasePartialRestorationStatistic,
128 ))
129 .never();
130
131 http_client
132 .expect_post_content()
133 .returning(|_| Ok("whatever".to_string()));
134 })
135 .build_cardano_database_client();
136
137 client
138 .add_statistics(full_restoration, false, 99)
139 .await
140 .unwrap();
141 }
142
143 #[tokio::test]
144 async fn add_statistics_with_partial_restoration() {
145 let full_restoration = false;
146 let client = CardanoDatabaseClientDependencyInjector::new()
147 .with_aggregator_client_mock_config(|http_client| {
148 http_client
149 .expect_post_content()
150 .with(eq(
151 AggregatorRequest::IncrementCardanoDatabasePartialRestorationStatistic,
152 ))
153 .times(1)
154 .return_once(|_| Ok("whatever".to_string()));
155
156 http_client
157 .expect_post_content()
158 .with(eq(
159 AggregatorRequest::IncrementCardanoDatabaseCompleteRestorationStatistic,
160 ))
161 .never();
162
163 http_client
164 .expect_post_content()
165 .returning(|_| Ok("whatever".to_string()));
166 })
167 .build_cardano_database_client();
168
169 client
170 .add_statistics(full_restoration, false, 99)
171 .await
172 .unwrap();
173 }
174
175 #[tokio::test]
176 async fn add_statistics_increments_immutable_files_restored() {
177 let immutable_files_restored = 123456;
178 let client = CardanoDatabaseClientDependencyInjector::new()
179 .with_aggregator_client_mock_config(|http_client| {
180 http_client
181 .expect_post_content()
182 .with(eq(
183 AggregatorRequest::IncrementCardanoDatabaseImmutablesRestoredStatistic {
184 number_of_immutables: immutable_files_restored,
185 },
186 ))
187 .times(1)
188 .return_once(|_| Ok("whatever".to_string()));
189
190 http_client
191 .expect_post_content()
192 .returning(|_| Ok("whatever".to_string()));
193 })
194 .build_cardano_database_client();
195
196 client
197 .add_statistics(false, false, immutable_files_restored)
198 .await
199 .unwrap();
200 }
201
202 #[tokio::test]
203 async fn add_statistics_does_not_increment_immutable_files_restored_when_none_restored() {
204 let immutable_files_restored = 0;
205 let client = CardanoDatabaseClientDependencyInjector::new()
206 .with_aggregator_client_mock_config(|http_client| {
207 http_client
208 .expect_post_content()
209 .with(predicate::function(|req| matches!(req, AggregatorRequest::IncrementCardanoDatabaseImmutablesRestoredStatistic { .. })))
210 .never();
211
212 http_client
213 .expect_post_content()
214 .returning(|_| Ok("whatever".to_string()));
215 })
216 .build_cardano_database_client();
217
218 client
219 .add_statistics(false, false, immutable_files_restored)
220 .await
221 .unwrap();
222 }
223}