Skip to content

Commit

Permalink
store crashed session as 0 duration
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwicky committed Oct 23, 2024
1 parent ba30026 commit 1a4a22a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
14 changes: 12 additions & 2 deletions common/gateway-stats-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ impl PersistentStatsStorage {
.await?)
}

pub async fn get_unique_users(&self, date: Date) -> Result<i32, StatsStorageError> {
Ok(self.session_manager.get_unique_users(date).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 delete_unique_users(&self, before_date: Date) -> Result<(), StatsStorageError> {
Expand Down Expand Up @@ -156,6 +156,16 @@ impl PersistentStatsStorage {
.map(Into::into))
}

pub async fn get_all_active_sessions(&self) -> Result<Vec<ActiveSession>, StatsStorageError> {
Ok(self
.session_manager
.get_all_active_sessions()
.await?
.into_iter()
.map(Into::into)
.collect())
}

pub async fn get_started_sessions_count(
&self,
start_date: Date,
Expand Down
8 changes: 7 additions & 1 deletion common/gateway-stats-storage/src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl SessionManager {
Ok(())
}

pub(crate) async fn get_unique_users(&self, date: Date) -> Result<i32> {
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 = ?",
date
Expand Down Expand Up @@ -133,6 +133,12 @@ impl SessionManager {
.await
}

pub(crate) async fn get_all_active_sessions(&self) -> Result<Vec<StoredActiveSession>> {
sqlx::query_as("SELECT start_time, typ FROM sessions_active")
.fetch_all(&self.connection_pool)
.await
}

pub(crate) async fn get_started_sessions_count(&self, start_date: Date) -> Result<i32> {
Ok(sqlx::query!(
"SELECT COUNT(*) as count FROM sessions_active WHERE date(start_time) = ?",
Expand Down
17 changes: 15 additions & 2 deletions gateway/src/node/statistics/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-only

use nym_credentials_interface::TicketType;
use nym_gateway_stats_storage::models::FinishedSession;
use nym_gateway_stats_storage::PersistentStatsStorage;
use nym_gateway_stats_storage::{error::StatsStorageError, models::ActiveSession};
use nym_node_http_api::state::metrics::SharedSessionStats;
Expand Down Expand Up @@ -90,8 +91,20 @@ impl SessionStatsHandler {
let yesterday = OffsetDateTime::now_utc().date() - Duration::DAY;
//publish yesterday's data if any
self.publish_stats(yesterday).await?;
//store "active" sessions as duration 0
for active_session in self.storage.get_all_active_sessions().await? {
self.storage
.insert_finished_session(
self.current_day,
FinishedSession {
duration: Duration::ZERO,
typ: active_session.typ,
},
)
.await?
}
//cleanup active sessions
self.storage.cleanup_active_sessions().await?; //store them with duration 0
self.storage.cleanup_active_sessions().await?;

//delete old entries
self.delete_old_stats(yesterday - Duration::DAY).await?;
Expand All @@ -101,7 +114,7 @@ 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(stats_date).await?;
let user_count = self.storage.get_unique_users_count(stats_date).await?;
let session_started = self.storage.get_started_sessions_count(stats_date).await? as u32;
{
let mut shared_state = self.shared_session_stats.write().await;
Expand Down

0 comments on commit 1a4a22a

Please sign in to comment.