Skip to content

Commit 7d5a683

Browse files
authored
Merge pull request #1244 from input-output-hk/ensemble/1127-fix-record-statistics
Fix record download statistics
2 parents 54faa24 + 560781b commit 7d5a683

File tree

12 files changed

+208
-21
lines changed

12 files changed

+208
-21
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-aggregator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-aggregator"
3-
version = "0.3.94"
3+
version = "0.3.95"
44
description = "A Mithril Aggregator server"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-aggregator/src/http_server/routes/statistics_routes.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ fn post_statistics(
2626
mod handlers {
2727
use std::{convert::Infallible, sync::Arc};
2828

29-
use mithril_common::messages::SnapshotMessage;
29+
use mithril_common::messages::SnapshotDownloadMessage;
3030
use reqwest::StatusCode;
3131

3232
use crate::event_store::{EventMessage, TransmitterService};
3333
use crate::http_server::routes::reply;
3434

3535
pub async fn post_snapshot_statistics(
36-
snapshot_message: SnapshotMessage,
36+
snapshot_download_message: SnapshotDownloadMessage,
3737
event_transmitter: Arc<TransmitterService<EventMessage>>,
3838
) -> Result<impl warp::Reply, Infallible> {
3939
let headers: Vec<(&str, &str)> = Vec::new();
4040

4141
match event_transmitter.send_event_message(
4242
"HTTP::statistics",
4343
"snapshot_downloaded",
44-
&snapshot_message,
44+
&snapshot_download_message,
4545
headers,
4646
) {
4747
Err(e) => Ok(reply::internal_server_error(e)),
@@ -54,7 +54,7 @@ mod handlers {
5454
mod tests {
5555
use super::*;
5656

57-
use mithril_common::messages::SnapshotMessage;
57+
use mithril_common::messages::SnapshotDownloadMessage;
5858
use mithril_common::test_utils::apispec::APISpec;
5959

6060
use warp::{http::Method, test::request};
@@ -82,14 +82,14 @@ mod tests {
8282
let mut builder = DependenciesBuilder::new(config);
8383
let mut rx = builder.get_event_transmitter_receiver().await.unwrap();
8484
let dependency_manager = builder.build_dependency_container().await.unwrap();
85-
let snapshot_message = SnapshotMessage::dummy();
85+
let snapshot_download_message = SnapshotDownloadMessage::dummy();
8686

8787
let method = Method::POST.as_str();
8888
let path = "/statistics/snapshot";
8989

9090
let response = request()
9191
.method(method)
92-
.json(&snapshot_message)
92+
.json(&snapshot_download_message)
9393
.path(&format!("/{SERVER_BASE_PATH}{path}"))
9494
.reply(&setup_router(Arc::new(dependency_manager)))
9595
.await;
@@ -99,7 +99,7 @@ mod tests {
9999
method,
100100
path,
101101
"application/json",
102-
&snapshot_message,
102+
&snapshot_download_message,
103103
&response,
104104
);
105105

mithril-client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-client"
3-
version = "0.4.1"
3+
version = "0.4.2"
44
description = "A Mithril Client"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-client/src/aggregator_client/http_client.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ impl AggregatorHTTPClient {
159159
/// Issue a POST HTTP request.
160160
#[async_recursion]
161161
async fn post(&self, url: &str, json: &str) -> Result<Response, AggregatorHTTPClientError> {
162-
let request_builder = Client::new().post(url.to_owned()).json(json);
162+
debug!("POST url='{url}' json='{json}'.");
163+
let request_builder = Client::new().post(url.to_owned()).body(json.to_owned());
163164
let current_api_version = self
164165
.compute_current_api_version()
165166
.await
@@ -177,7 +178,7 @@ impl AggregatorHTTPClient {
177178
})?;
178179

179180
match response.status() {
180-
StatusCode::OK => Ok(response),
181+
StatusCode::OK | StatusCode::CREATED => Ok(response),
181182
StatusCode::PRECONDITION_FAILED => {
182183
if self.discard_current_api_version().await.is_some()
183184
&& !self.api_versions.read().await.is_empty()

mithril-client/src/aggregator_client/snapshot_client.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ use thiserror::Error;
66

77
use mithril_common::{
88
entities::Snapshot,
9-
messages::{SnapshotListItemMessage, SnapshotListMessage, SnapshotMessage},
9+
messages::{SnapshotListItemMessage, SnapshotListMessage, SnapshotMessage, ToMessageAdapter},
1010
StdResult,
1111
};
1212

13-
use crate::aggregator_client::AggregatorClient;
1413
use crate::utils::DownloadProgressReporter;
14+
use crate::{
15+
aggregator_client::AggregatorClient, message_adapters::ToSnapshotDownloadMessageAdapter,
16+
};
1517

1618
/// Error for the Snapshot client
1719
#[derive(Error, Debug)]
@@ -77,7 +79,9 @@ impl SnapshotClient {
7779
{
7880
Ok(()) => {
7981
// the snapshot download does not fail if the statistic call fails.
80-
let _ = self.add_statistics(snapshot).await;
82+
if let Err(e) = self.add_statistics(snapshot).await {
83+
warn!("Could not POST snapshot download statistics: {e:?}");
84+
}
8185

8286
Ok(())
8387
}
@@ -101,7 +105,8 @@ impl SnapshotClient {
101105
/// Increments Aggregator's download statistics
102106
pub async fn add_statistics(&self, snapshot: &Snapshot) -> StdResult<()> {
103107
let url = "statistics/snapshot";
104-
let json = serde_json::to_string(snapshot)?;
108+
let snapshot_download_message = ToSnapshotDownloadMessageAdapter::adapt(snapshot);
109+
let json = serde_json::to_string(&snapshot_download_message)?;
105110
let _response = self.http_client.post_content(url, &json).await?;
106111

107112
Ok(())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
mod from_certificate_message_adapter;
22
mod from_mithril_stake_distribution_message;
33
mod from_snapshot_message;
4+
mod to_snapshot_download_message;
45

56
pub use from_certificate_message_adapter::FromCertificateMessageAdapter;
67
pub use from_mithril_stake_distribution_message::FromMithrilStakeDistributionMessageAdapter;
78
pub use from_snapshot_message::FromSnapshotMessageAdapter;
9+
pub use to_snapshot_download_message::ToSnapshotDownloadMessageAdapter;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use mithril_common::entities::Snapshot;
2+
use mithril_common::messages::{SnapshotDownloadMessage, ToMessageAdapter};
3+
4+
/// Adapter to convert [Snapshot] to [SnapshotDownloadMessage] instances
5+
pub struct ToSnapshotDownloadMessageAdapter;
6+
7+
impl ToMessageAdapter<&Snapshot, SnapshotDownloadMessage> for ToSnapshotDownloadMessageAdapter {
8+
/// Method to trigger the conversion
9+
fn adapt(snapshot: &Snapshot) -> SnapshotDownloadMessage {
10+
SnapshotDownloadMessage {
11+
digest: snapshot.digest.clone(),
12+
beacon: snapshot.beacon.clone(),
13+
size: snapshot.size,
14+
locations: snapshot.locations.clone(),
15+
compression_algorithm: snapshot.compression_algorithm,
16+
cardano_node_version: snapshot.cardano_node_version.clone(),
17+
}
18+
}
19+
}
20+
21+
#[cfg(test)]
22+
mod tests {
23+
use mithril_common::test_utils::fake_data;
24+
25+
use super::*;
26+
27+
#[test]
28+
fn adapt_ok() {
29+
let mut snapshot = fake_data::snapshots(1)[0].to_owned();
30+
snapshot.digest = "digest123".to_string();
31+
let snapshot_download_message = ToSnapshotDownloadMessageAdapter::adapt(&snapshot);
32+
33+
assert_eq!("digest123".to_string(), snapshot_download_message.digest);
34+
}
35+
}

mithril-common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-common"
3-
version = "0.2.114"
3+
version = "0.2.115"
44
authors = { workspace = true }
55
edition = { workspace = true }
66
documentation = { workspace = true }

mithril-common/src/messages/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod mithril_stake_distribution_list;
1111
mod register_signature;
1212
mod register_signer;
1313
mod snapshot;
14+
mod snapshot_download;
1415
mod snapshot_list;
1516

1617
pub use certificate::CertificateMessage;
@@ -28,4 +29,5 @@ pub use mithril_stake_distribution_list::{
2829
pub use register_signature::RegisterSignatureMessage;
2930
pub use register_signer::RegisterSignerMessage;
3031
pub use snapshot::SnapshotMessage;
32+
pub use snapshot_download::SnapshotDownloadMessage;
3133
pub use snapshot_list::{SnapshotListItemMessage, SnapshotListMessage};

0 commit comments

Comments
 (0)