diff --git a/common/statistics/src/lib.rs b/common/statistics/src/lib.rs index 9e107fc119..5423106648 100644 --- a/common/statistics/src/lib.rs +++ b/common/statistics/src/lib.rs @@ -34,12 +34,16 @@ pub enum StatsData { #[derive(Clone, Debug, Deserialize, Serialize)] pub struct StatsGatewayData { + pub gateway_id: String, pub inbox_count: u32, } impl StatsGatewayData { - pub fn new(inbox_count: u32) -> Self { - StatsGatewayData { inbox_count } + pub fn new(gateway_id: String, inbox_count: u32) -> Self { + StatsGatewayData { + gateway_id, + inbox_count, + } } } diff --git a/gateway/src/node/mod.rs b/gateway/src/node/mod.rs index d539dafb1a..01a60c7fde 100644 --- a/gateway/src/node/mod.rs +++ b/gateway/src/node/mod.rs @@ -333,6 +333,7 @@ where if self.config.get_enabled_statistics() { let statistics_service_url = self.config.get_statistics_service_url(); let stats_collector = GatewayStatisticsCollector::new( + self.identity_keypair.public_key().to_base58_string(), active_clients_store.clone(), statistics_service_url, ); diff --git a/gateway/src/node/statistics/collector.rs b/gateway/src/node/statistics/collector.rs index 18f290d006..13d3a6d0ee 100644 --- a/gateway/src/node/statistics/collector.rs +++ b/gateway/src/node/statistics/collector.rs @@ -14,13 +14,19 @@ use statistics_common::{ use crate::node::client_handling::active_clients::ActiveClientsStore; pub(crate) struct GatewayStatisticsCollector { + gateway_id: String, active_clients_store: ActiveClientsStore, statistics_service_url: Url, } impl GatewayStatisticsCollector { - pub fn new(active_clients_store: ActiveClientsStore, statistics_service_url: Url) -> Self { + pub fn new( + gateway_id: String, + active_clients_store: ActiveClientsStore, + statistics_service_url: Url, + ) -> Self { GatewayStatisticsCollector { + gateway_id, active_clients_store, statistics_service_url, } @@ -34,8 +40,10 @@ impl StatisticsCollector for GatewayStatisticsCollector { interval: Duration, timestamp: DateTime, ) -> StatsMessage { - let inbox_count = self.active_clients_store.size() as u32; - let stats_data = vec![StatsData::Gateway(StatsGatewayData { inbox_count })]; + let stats_data = vec![StatsData::Gateway(StatsGatewayData::new( + self.gateway_id.clone(), + self.active_clients_store.size() as u32, + ))]; StatsMessage { stats_data, interval_seconds: interval.as_secs() as u32, diff --git a/service-providers/network-statistics/migrations/20220512120000_mixnet_statistics.sql b/service-providers/network-statistics/migrations/20220512120000_mixnet_statistics.sql index 5e6b2aa348..da13b953ca 100644 --- a/service-providers/network-statistics/migrations/20220512120000_mixnet_statistics.sql +++ b/service-providers/network-statistics/migrations/20220512120000_mixnet_statistics.sql @@ -16,6 +16,7 @@ CREATE TABLE service_statistics CREATE TABLE gateway_statistics ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + gateway_id VARCHAR NOT NULL, inbox_count INTEGER NOT NULL, timestamp DATETIME NOT NULL ); \ No newline at end of file diff --git a/service-providers/network-statistics/src/api/routes.rs b/service-providers/network-statistics/src/api/routes.rs index 7316d2c375..0a1c558128 100644 --- a/service-providers/network-statistics/src/api/routes.rs +++ b/service-providers/network-statistics/src/api/routes.rs @@ -35,6 +35,7 @@ pub struct ServiceStatistic { #[derive(Clone, Serialize, Deserialize, Debug)] pub struct GatewayStatistic { + pub gateway_id: String, pub inbox_count: u32, pub timestamp: String, } @@ -70,6 +71,7 @@ pub(crate) async fn post_all_statistics( .into_iter() .map(|data| { GenericStatistic::Gateway(GatewayStatistic { + gateway_id: data.gateway_id, inbox_count: data.inbox_count as u32, timestamp: data.timestamp.to_string(), }) diff --git a/service-providers/network-statistics/src/storage/manager.rs b/service-providers/network-statistics/src/storage/manager.rs index 07e0cc751b..fa554edab4 100644 --- a/service-providers/network-statistics/src/storage/manager.rs +++ b/service-providers/network-statistics/src/storage/manager.rs @@ -47,16 +47,19 @@ impl StorageManager { /// /// # Arguments /// + /// * `gateway_id`: The gateway identity that collected the statstics. /// * `inbox_count`: Number of clients of a gateway. /// * `interval_seconds`: Duration in seconds in which the data was gathered. /// * `timestamp`: The moment in time when the data started being collected. pub(super) async fn insert_gateway_statistics( &self, + gateway_id: String, inbox_count: u32, timestamp: DateTime, ) -> Result<(), sqlx::Error> { sqlx::query!( - "INSERT INTO gateway_statistics(inbox_count, timestamp) VALUES (?, ?)", + "INSERT INTO gateway_statistics(gateway_id, inbox_count, timestamp) VALUES (?, ?, ?)", + gateway_id, inbox_count, timestamp, ) diff --git a/service-providers/network-statistics/src/storage/mod.rs b/service-providers/network-statistics/src/storage/mod.rs index 6ae4e08646..ea468c8606 100644 --- a/service-providers/network-statistics/src/storage/mod.rs +++ b/service-providers/network-statistics/src/storage/mod.rs @@ -71,7 +71,11 @@ impl NetworkStatisticsStorage { } statistics_common::StatsData::Gateway(gateway_data) => { self.manager - .insert_gateway_statistics(gateway_data.inbox_count, timestamp) + .insert_gateway_statistics( + gateway_data.gateway_id, + gateway_data.inbox_count, + timestamp, + ) .await? } } diff --git a/service-providers/network-statistics/src/storage/models.rs b/service-providers/network-statistics/src/storage/models.rs index f04a333495..d179914540 100644 --- a/service-providers/network-statistics/src/storage/models.rs +++ b/service-providers/network-statistics/src/storage/models.rs @@ -17,6 +17,7 @@ pub(crate) struct ServiceStatistics { pub(crate) struct GatewayStatistics { #[allow(dead_code)] pub(crate) id: i64, + pub(crate) gateway_id: String, pub(crate) inbox_count: i64, pub(crate) timestamp: NaiveDateTime, }