mithril_client/cardano_database_client/
statistics.rs

1use std::sync::Arc;
2
3use crate::{aggregator_client::{AggregatorClient, AggregatorRequest}, MithrilResult};
4
5pub struct InternalStaticticsSender {
6    pub(super) aggregator_client: Arc<dyn AggregatorClient>,
7}
8
9impl InternalStaticticsSender {
10    /// Constructs a new `InternalStaticticsSender`.
11    pub fn new(aggregator_client: Arc<dyn AggregatorClient>) -> Self {
12        Self { aggregator_client }
13    }
14
15    /// Increments the aggregator Cardano database snapshot download statistics
16    pub async fn add_statistics(
17        &self,
18        full_restoration: bool,
19        include_ancillary: bool,
20        number_of_immutable_files_restored: u64,
21    ) -> MithrilResult<()> {
22        if include_ancillary {
23            self
24                .aggregator_client
25                .post_content(AggregatorRequest::IncrementCardanoDatabaseAncillaryStatistic)
26                .await?;
27        };
28
29        let restoration_request = if full_restoration {
30            AggregatorRequest::IncrementCardanoDatabaseCompleteRestorationStatistic
31        } else {
32            AggregatorRequest::IncrementCardanoDatabasePartialRestorationStatistic
33        };
34
35        self
36            .aggregator_client
37            .post_content(restoration_request)
38            .await?;
39
40        self
41            .aggregator_client
42            .post_content(AggregatorRequest::IncrementCardanoDatabaseImmutablesRestoredStatistic {
43                number_of_immutables: number_of_immutable_files_restored.to_string(),
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
80            .add_statistics(false, include_ancillary, 99)
81            .await
82            .unwrap();
83    }
84
85    #[tokio::test]
86    async fn add_statistics_without_ancillary() {
87        let include_ancillary = false;
88        let client = CardanoDatabaseClientDependencyInjector::new()
89            .with_aggregator_client_mock_config(|http_client| {
90                http_client
91                .expect_post_content()
92                .with(eq(
93                    AggregatorRequest::IncrementCardanoDatabaseAncillaryStatistic,
94                ))
95                .never();
96
97                http_client
98                .expect_post_content()
99                .returning(|_| Ok("whatever".to_string()));
100            })
101            .build_cardano_database_client();
102
103        client
104            .add_statistics(false, include_ancillary, 99)
105            .await
106            .unwrap();
107    }
108
109    #[tokio::test]
110    async fn add_statistics_with_full_restoration() {
111        let full_restoration = true;
112        let client = CardanoDatabaseClientDependencyInjector::new()
113            .with_aggregator_client_mock_config(|http_client| {
114                http_client
115                .expect_post_content()
116                .with(eq(
117                    AggregatorRequest::IncrementCardanoDatabaseCompleteRestorationStatistic,
118                ))
119                .times(1)
120                .return_once(|_| Ok("whatever".to_string()));
121
122                http_client
123                .expect_post_content()
124                .with(eq(
125                    AggregatorRequest::IncrementCardanoDatabasePartialRestorationStatistic,
126                ))
127                .never();
128
129                http_client
130                    .expect_post_content()
131                    .returning(|_| Ok("whatever".to_string()));
132            })
133            .build_cardano_database_client();
134
135        client
136            .add_statistics(full_restoration, false, 99)
137            .await
138            .unwrap();
139    }
140
141    #[tokio::test]
142    async fn add_statistics_with_partial_restoration() {
143        let full_restoration = false;
144        let client = CardanoDatabaseClientDependencyInjector::new()
145            .with_aggregator_client_mock_config(|http_client| {
146                http_client
147                .expect_post_content()
148                .with(eq(
149                    AggregatorRequest::IncrementCardanoDatabasePartialRestorationStatistic,
150                ))
151                .times(1)
152                .return_once(|_| Ok("whatever".to_string()));
153
154                http_client
155                .expect_post_content()
156                .with(eq(
157                    AggregatorRequest::IncrementCardanoDatabaseCompleteRestorationStatistic,
158                ))
159                .never();
160
161                http_client
162                    .expect_post_content()
163                    .returning(|_| Ok("whatever".to_string()));
164            })
165            .build_cardano_database_client();
166
167        client
168            .add_statistics(full_restoration, false, 99)
169            .await
170            .unwrap();
171    }
172
173    #[tokio::test]
174    async fn add_statistics_increments_immutable_files_restored() {
175        let immutable_files_restored = 123456;
176        let client = CardanoDatabaseClientDependencyInjector::new()
177            .with_aggregator_client_mock_config(|http_client| {
178                http_client
179                .expect_post_content()
180                .with(eq(
181                    AggregatorRequest::IncrementCardanoDatabaseImmutablesRestoredStatistic {
182                        number_of_immutables: immutable_files_restored.to_string(),
183                    },
184                ))
185                .times(1)
186                .return_once(|_| Ok("whatever".to_string()));
187
188                http_client
189                .expect_post_content()
190                .returning(|_| Ok("whatever".to_string()));
191            })
192            .build_cardano_database_client();
193
194        client.add_statistics(false, false, immutable_files_restored).await.unwrap();
195    }
196
197    #[tokio::test]
198    async fn add_statistics_do_not_increments_immutable_files_restored_when_no_immutable_files_restored() {
199        let immutable_files_restored = 0;
200        let client = CardanoDatabaseClientDependencyInjector::new()
201            .with_aggregator_client_mock_config(|http_client| {
202                http_client
203                .expect_post_content()
204                .with(predicate::function(|req| matches!(req, AggregatorRequest::IncrementCardanoDatabaseImmutablesRestoredStatistic { .. })))
205                .never();
206            
207
208                http_client
209                .expect_post_content()
210                .returning(|_| Ok("whatever".to_string()));
211            })
212            .build_cardano_database_client();
213
214        client.add_statistics(false, false, immutable_files_restored).await.unwrap();
215    }
216}