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