Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Product Data] Better unique user count on gateways #5084

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 2 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions common/gateway-stats-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ impl PersistentStatsStorage {
.await?)
}

pub async fn get_unique_users_count(&self, date: Date) -> Result<i32, StatsStorageError> {
Ok(self.session_manager.get_unique_users_count(date).await?)
pub async fn get_unique_users(&self, date: Date) -> Result<Vec<String>, StatsStorageError> {
Ok(self.session_manager.get_unique_users(date).await?)
}

pub async fn delete_unique_users(&self, before_date: Date) -> Result<(), StatsStorageError> {
Expand Down
11 changes: 5 additions & 6 deletions common/gateway-stats-storage/src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,13 @@ impl SessionManager {
Ok(())
}

pub(crate) async fn get_unique_users_count(&self, date: Date) -> Result<i32> {
Ok(sqlx::query!(
"SELECT COUNT(*) as count FROM sessions_unique_users WHERE day = ?",
pub(crate) async fn get_unique_users(&self, date: Date) -> Result<Vec<String>> {
sqlx::query_scalar!(
"SELECT client_address as count FROM sessions_unique_users WHERE day = ?",
date
)
.fetch_one(&self.connection_pool)
.await?
.count)
.fetch_all(&self.connection_pool)
.await
}

pub(crate) async fn delete_unique_users(&self, before_date: Date) -> Result<()> {
Expand Down
1 change: 1 addition & 0 deletions gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ once_cell = { workspace = true }
rand = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sha2 = { workspace = true }
si-scale = { workspace = true }
subtle-encoding = { workspace = true, features = ["bech32-preview"] }
thiserror = { workspace = true }
Expand Down
10 changes: 8 additions & 2 deletions gateway/src/node/statistics/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use nym_gateway_stats_storage::PersistentStatsStorage;
use nym_gateway_stats_storage::{error::StatsStorageError, models::ActiveSession};
use nym_node_http_api::state::metrics::SharedSessionStats;
use nym_sphinx::DestinationAddressBytes;
use sha2::{Digest, Sha256};
use time::{Date, Duration, OffsetDateTime};

use nym_statistics_common::events::SessionEvent;
Expand Down Expand Up @@ -114,12 +115,17 @@ impl SessionStatsHandler {
//update shared state once a day has passed, with data from the previous day
async fn publish_stats(&mut self, stats_date: Date) -> Result<(), StatsStorageError> {
let finished_sessions = self.storage.get_finished_sessions(stats_date).await?;
let user_count = self.storage.get_unique_users_count(stats_date).await?;
let unique_users = self.storage.get_unique_users(stats_date).await?;
let unique_users_hash = unique_users
.into_iter()
.map(|address| format!("{:x}", Sha256::digest(address)))
.collect::<Vec<_>>();
let session_started = self.storage.get_started_sessions_count(stats_date).await? as u32;
{
let mut shared_state = self.shared_session_stats.write().await;
shared_state.update_time = stats_date;
shared_state.unique_active_users = user_count as u32;
shared_state.unique_active_users_count = unique_users_hash.len() as u32;
shared_state.unique_active_users_hashes = unique_users_hash;
shared_state.session_started = session_started;
shared_state.sessions = finished_sessions.iter().map(|s| s.serialize()).collect();
}
Expand Down
9 changes: 6 additions & 3 deletions nym-node/nym-node-http-api/src/state/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ type FinishedSessions = Vec<(u64, String)>;
#[derive(Debug, Clone)]
pub struct SessionStatsState {
pub update_time: Date,
pub unique_active_users: u32,
pub unique_active_users_count: u32,
pub unique_active_users_hashes: Vec<String>,
pub session_started: u32,
pub sessions: FinishedSessions,
}
Expand All @@ -174,7 +175,8 @@ impl SessionStatsState {
.collect();
SessionStats {
update_time: self.update_time.with_time(time!(0:00)).assume_utc(),
unique_active_users: self.unique_active_users,
unique_active_users: self.unique_active_users_count,
unique_active_users_hashes: self.unique_active_users_hashes.clone(),
sessions,
sessions_started: self.session_started,
sessions_finished: self.sessions.len() as u32,
Expand All @@ -186,7 +188,8 @@ impl Default for SessionStatsState {
fn default() -> Self {
SessionStatsState {
update_time: OffsetDateTime::UNIX_EPOCH.date(),
unique_active_users: 0,
unique_active_users_count: 0,
unique_active_users_hashes: Default::default(),
session_started: 0,
sessions: Default::default(),
}
Expand Down
2 changes: 2 additions & 0 deletions nym-node/nym-node-requests/src/api/v1/metrics/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub struct SessionStats {

pub unique_active_users: u32,

pub unique_active_users_hashes: Vec<String>,

pub sessions: Vec<Session>,

pub sessions_started: u32,
Expand Down
Loading