Skip to content

Commit

Permalink
feat: add ws stats task id (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiroaisen authored Mar 12, 2024
2 parents f1fb384 + e985b78 commit ed68b8a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions defs/db/WsStatsConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { DateTime } from "../DateTime.js";
export type WsStatsConnection = {
_id: string;
st: string;
task_id: string;
sd: string | null | undefined;
dp: string;
du: number | null | undefined;
Expand Down
13 changes: 12 additions & 1 deletion rs/packages/api/src/ws_stats/routes/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ impl WsConnectionHandler {
}
};

let task_id = WsStatsConnection::random_task_id();
let reconnections: u16;
let connection_id: String;
let created_at: DateTime;
Expand All @@ -176,6 +177,7 @@ impl WsConnectionHandler {

let connection = WsStatsConnection {
id: connection_id.clone(),
task_id: task_id.clone(),
station_id: station_id.clone(),
start_deployment_id: Some(deployment_id.clone()),
current_deployment_id: deployment_id,
Expand Down Expand Up @@ -209,6 +211,7 @@ impl WsConnectionHandler {

let update = doc! {
"$set": {
WsStatsConnection::KEY_TASK_ID: &task_id,
WsStatsConnection::KEY_CURRENT_DEPLOYMENT_ID: &deployment_id,
WsStatsConnection::KEY_IS_OPEN: true,
WsStatsConnection::KEY_CLOSED_AT: null,
Expand Down Expand Up @@ -351,6 +354,12 @@ impl WsConnectionHandler {

let duration_ms = ((*DateTime::now() - *created_at).as_seconds_f64() * 1000.0).round();

// only update if the task id is the same (not taken from another connection)
let filter = doc! {
WsStatsConnection::KEY_ID: &connection_id,
WsStatsConnection::KEY_TASK_ID: &task_id,
};

let update = doc! {
"$set": {
WsStatsConnection::KEY_IS_OPEN: false,
Expand All @@ -359,7 +368,9 @@ impl WsConnectionHandler {
}
};

let _ = WsStatsConnection::update_by_id(&connection_id, update).await;
let _ = WsStatsConnection::cl()
.update_one(filter, update, None)
.await;

log::info!(
target: "ws-stats",
Expand Down
1 change: 0 additions & 1 deletion rs/packages/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use schemars::JsonSchema;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_util::DateTime;
use std::borrow::Borrow;
use std::collections::HashSet;
use ts_rs::TS;

pub mod error;
Expand Down
10 changes: 10 additions & 0 deletions rs/packages/db/src/models/ws_stats_connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub struct WsStatsConnection {
#[serde(rename = "st")]
pub station_id: String,

#[serde(default)]
pub task_id: String,

#[serde(rename = "sd")]
pub start_deployment_id: Option<String>,

Expand Down Expand Up @@ -74,6 +77,13 @@ pub struct WsStatsConnection {
pub abnormally_closed: bool,
}

impl WsStatsConnection {
/// create a random task id, this task id is always used with the id field, so its only unique within the same document
pub fn random_task_id() -> String {
uid::uid(6)
}
}

impl Model for WsStatsConnection {
const CL_NAME: &'static str = "ws_stats_connection";
const UID_LEN: usize = 12;
Expand Down

0 comments on commit ed68b8a

Please sign in to comment.