Skip to content

Commit

Permalink
Use new session id format
Browse files Browse the repository at this point in the history
  • Loading branch information
goenning committed Nov 3, 2023
1 parent 02540b8 commit a36218a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.2

* Use new session id format

## 0.4.1

* Automatic flush of events on app exit
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tauri-plugin-aptabase"
version = "0.4.1"
version = "0.4.2"
license = "MIT"
description = "Tauri Plugin for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps"
authors = [ "Guilherme Oenning" ]
Expand All @@ -21,4 +21,5 @@ reqwest = { version = "0.11", features = ["json"] }
time = { version = "0.3", features = ["formatting"]}
os_info = "3"
uuid = "1"
rand = "0.8"
log = "0.4"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aptabase/tauri",
"version": "0.4.1",
"version": "0.4.2",
"private": false,
"description": "Tauri Plugin for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
"author": "Guilherme Oenning",
Expand Down
21 changes: 18 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::time::{SystemTime, UNIX_EPOCH};
use rand::Rng;
use serde_json::{json, Value};
use std::{sync::{Arc, Mutex as SyncMutex}, time::Duration};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use uuid::Uuid;

use crate::{
config::Config,
Expand All @@ -10,6 +11,20 @@ use crate::{

static SESSION_TIMEOUT: Duration = Duration::from_secs(4 * 60 * 60);

fn new_session_id() -> String {
let epoch_in_seconds = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("time went backwards")
.as_secs();

let mut rng = rand::thread_rng();
let random: u64 = rng.gen_range(0..=99999999);

let id = epoch_in_seconds * 100_000_000 + random;

return id.to_string();
}

/// A tracking session.
#[derive(Debug, Clone)]
pub struct TrackingSession {
Expand All @@ -20,7 +35,7 @@ pub struct TrackingSession {
impl TrackingSession {
fn new() -> Self {
TrackingSession {
id: Uuid::new_v4().to_string(),
id: new_session_id(),
last_touch_ts: OffsetDateTime::now_utc(),
}
}
Expand Down Expand Up @@ -52,7 +67,7 @@ impl AptabaseClient {
sys_info,
}
}

/// Starts the event dispatcher loop.
pub(crate) fn start_polling(&self, interval: Duration) {
let dispatcher = self.dispatcher.clone();
Expand Down

0 comments on commit a36218a

Please sign in to comment.