From 821ca2dead8118767a96f040a95dbcb267cea632 Mon Sep 17 00:00:00 2001 From: Magnus F Date: Thu, 23 Nov 2023 16:46:07 +0100 Subject: [PATCH] fix: Skip creating `usage_anonymous_id` when TABBY_DISABLE_USAGE_COLLECTION is set (#864) * dont write file if tracking is disabled * remove comment, id is optional * Update usage.rs --------- Co-authored-by: Meng Zhang --- crates/tabby-common/src/usage.rs | 49 +++++++++++++++++--------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/crates/tabby-common/src/usage.rs b/crates/tabby-common/src/usage.rs index 0ea04a7..5d3a8c9 100644 --- a/crates/tabby-common/src/usage.rs +++ b/crates/tabby-common/src/usage.rs @@ -11,7 +11,7 @@ static USAGE_API_ENDPOINT: &str = "https://app.tabbyml.com/api/usage"; struct UsageTracker { id: String, - client: Option, + client: Client, } impl UsageTracker { @@ -42,32 +42,27 @@ impl UsageTracker { } let id = fs::read_to_string(usage_id_file()).expect("Failed to read usage id"); - let client = if std::env::var("TABBY_DISABLE_USAGE_COLLECTION").is_ok() { - None - } else { - Some(Client::new()) - }; - - Self { id, client } + Self { + id, + client: Client::new(), + } } async fn capture(&self, event: &str, properties: T) where T: Serialize, { - if let Some(client) = &self.client { - let payload = Payload { - distinct_id: self.id.as_ref(), - event, - properties, - }; - client - .post(USAGE_API_ENDPOINT) - .json(&payload) - .send() - .await - .ok(); - } + let payload = Payload { + distinct_id: &self.id, + event, + properties, + }; + self.client + .post(USAGE_API_ENDPOINT) + .json(&payload) + .send() + .await + .ok(); } } @@ -80,12 +75,20 @@ struct Payload<'a, T> { } lazy_static! { - static ref TRACKER: UsageTracker = UsageTracker::new(); + static ref TRACKER: Option = { + if std::env::var("TABBY_DISABLE_USAGE_COLLECTION").is_ok() { + None + } else { + Some(UsageTracker::new()) + } + }; } pub async fn capture(event: &str, properties: T) where T: Serialize, { - TRACKER.capture(event, properties).await + if let Some(tracker) = TRACKER.as_ref() { + tracker.capture(event, properties).await; + } }