From e91d9d89c801c9aa40e1c9a0e98153692cd7b50b Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Tue, 2 Jan 2024 17:19:21 -0500 Subject: [PATCH] Add settings events (#3847) Adds the infractucture for settings events and specifically tracks theme settings. Currently, we only take note of the theme at app open and when the user switches the theme with the theme selector. Changes at the config file are ignored, as putting code that low leads to a lot of chances of reporting theme events when the user hasn't done anything. This change is done in both Zed 1 and Zed 2. I'll open up a PR on zed.dev and adjust the database accordingly. Release Notes: - N/A --- Cargo.lock | 2 ++ crates/client/src/telemetry.rs | 20 ++++++++++++++++++++ crates/client2/src/telemetry.rs | 20 ++++++++++++++++++++ crates/theme_selector/Cargo.toml | 1 + crates/theme_selector/src/theme_selector.rs | 17 +++++++++++++++-- crates/theme_selector2/Cargo.toml | 11 ++++++----- crates/theme_selector2/src/theme_selector.rs | 14 ++++++++++++-- crates/zed/src/main.rs | 5 +++++ crates/zed2/src/main.rs | 5 +++++ 9 files changed, 86 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3226037d51a40f06b24ed0ede79e2ee211c2ed43..93bd488fe873f6c8edea16071387da2acd49f407 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9856,6 +9856,7 @@ dependencies = [ name = "theme_selector" version = "0.1.0" dependencies = [ + "client", "editor", "feature_flags", "fs", @@ -9876,6 +9877,7 @@ dependencies = [ name = "theme_selector2" version = "0.1.0" dependencies = [ + "client2", "editor2", "feature_flags2", "fs2", diff --git a/crates/client/src/telemetry.rs b/crates/client/src/telemetry.rs index a3e7449cf8a4be5a25b4f7eaaa4f3d2379d70d68..0a3b5bf3bce324d1bbbcaa2002abfa47dc713305 100644 --- a/crates/client/src/telemetry.rs +++ b/crates/client/src/telemetry.rs @@ -113,6 +113,11 @@ pub enum ClickhouseEvent { operation: &'static str, milliseconds_since_first_event: i64, }, + Setting { + setting: &'static str, + value: String, + milliseconds_since_first_event: i64, + }, } #[cfg(debug_assertions)] @@ -353,6 +358,21 @@ impl Telemetry { self.report_clickhouse_event(event, telemetry_settings, true) } + pub fn report_setting_event( + self: &Arc, + telemetry_settings: TelemetrySettings, + setting: &'static str, + value: String, + ) { + let event = ClickhouseEvent::Setting { + setting, + value, + milliseconds_since_first_event: self.milliseconds_since_first_event(), + }; + + self.report_clickhouse_event(event, telemetry_settings, false) + } + fn milliseconds_since_first_event(&self) -> i64 { let mut state = self.state.lock(); match state.first_event_datetime { diff --git a/crates/client2/src/telemetry.rs b/crates/client2/src/telemetry.rs index b303e68183d3a263f11ed0759410a43264c7c564..76efd140531ae7eeca2e7468de1feeb49f596fde 100644 --- a/crates/client2/src/telemetry.rs +++ b/crates/client2/src/telemetry.rs @@ -112,6 +112,11 @@ pub enum ClickhouseEvent { operation: &'static str, milliseconds_since_first_event: i64, }, + Setting { + setting: &'static str, + value: String, + milliseconds_since_first_event: i64, + }, } #[cfg(debug_assertions)] @@ -377,6 +382,21 @@ impl Telemetry { self.report_clickhouse_event(event, telemetry_settings, true) } + pub fn report_setting_event( + self: &Arc, + telemetry_settings: TelemetrySettings, + setting: &'static str, + value: String, + ) { + let event = ClickhouseEvent::Setting { + setting, + value, + milliseconds_since_first_event: self.milliseconds_since_first_event(), + }; + + self.report_clickhouse_event(event, telemetry_settings, false) + } + fn milliseconds_since_first_event(&self) -> i64 { let mut state = self.state.lock(); match state.first_event_datetime { diff --git a/crates/theme_selector/Cargo.toml b/crates/theme_selector/Cargo.toml index 7e97d3918606e42cbadd62e354bea5ded0f44e76..fb3feb8d38c6c4c436d65dd343adfa7ee29a459b 100644 --- a/crates/theme_selector/Cargo.toml +++ b/crates/theme_selector/Cargo.toml @@ -9,6 +9,7 @@ path = "src/theme_selector.rs" doctest = false [dependencies] +client = { path = "../client" } editor = { path = "../editor" } fuzzy = { path = "../fuzzy" } fs = { path = "../fs" } diff --git a/crates/theme_selector/src/theme_selector.rs b/crates/theme_selector/src/theme_selector.rs index 1969b0256a3aa5ee9c203ee02b765695bb748bf9..4495413061f70ecbf7fdcfbba7ab127287433411 100644 --- a/crates/theme_selector/src/theme_selector.rs +++ b/crates/theme_selector/src/theme_selector.rs @@ -1,3 +1,4 @@ +use client::{telemetry::Telemetry, TelemetrySettings}; use feature_flags::FeatureFlagAppExt; use fs::Fs; use fuzzy::{match_strings, StringMatch, StringMatchCandidate}; @@ -19,7 +20,8 @@ pub fn init(cx: &mut AppContext) { pub fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext) { workspace.toggle_modal(cx, |workspace, cx| { let fs = workspace.app_state().fs.clone(); - cx.add_view(|cx| ThemeSelector::new(ThemeSelectorDelegate::new(fs, cx), cx)) + let telemetry = workspace.client().telemetry().clone(); + cx.add_view(|cx| ThemeSelector::new(ThemeSelectorDelegate::new(fs, telemetry, cx), cx)) }); } @@ -48,10 +50,15 @@ pub struct ThemeSelectorDelegate { original_theme: Arc, selection_completed: bool, selected_index: usize, + telemetry: Arc, } impl ThemeSelectorDelegate { - fn new(fs: Arc, cx: &mut ViewContext) -> Self { + fn new( + fs: Arc, + telemetry: Arc, + cx: &mut ViewContext, + ) -> Self { let original_theme = theme::current(cx).clone(); let staff_mode = cx.is_staff(); @@ -74,6 +81,7 @@ impl ThemeSelectorDelegate { original_theme: original_theme.clone(), selected_index: 0, selection_completed: false, + telemetry, }; this.select_if_matching(&original_theme.meta.name); this @@ -124,6 +132,11 @@ impl PickerDelegate for ThemeSelectorDelegate { self.selection_completed = true; let theme_name = theme::current(cx).meta.name.clone(); + + let telemetry_settings = *settings::get::(cx); + self.telemetry + .report_setting_event(telemetry_settings, "theme", theme_name.to_string()); + update_settings_file::(self.fs.clone(), cx, |settings| { settings.theme = Some(theme_name); }); diff --git a/crates/theme_selector2/Cargo.toml b/crates/theme_selector2/Cargo.toml index 853a53af68f25aa0ff12f4cdd7c232b65e3828c8..da9e3a4d16ca8334f03789d3fd0f8ae152f18aa2 100644 --- a/crates/theme_selector2/Cargo.toml +++ b/crates/theme_selector2/Cargo.toml @@ -9,17 +9,18 @@ path = "src/theme_selector.rs" doctest = false [dependencies] +client = { package = "client2", path = "../client2" } editor = { package = "editor2", path = "../editor2" } -fuzzy = { package = "fuzzy2", path = "../fuzzy2" } +feature_flags = { package = "feature_flags2", path = "../feature_flags2" } fs = { package = "fs2", path = "../fs2" } +fuzzy = { package = "fuzzy2", path = "../fuzzy2" } gpui = { package = "gpui2", path = "../gpui2" } -ui = { package = "ui2", path = "../ui2" } picker = { package = "picker2", path = "../picker2" } -theme = { package = "theme2", path = "../theme2" } settings = { package = "settings2", path = "../settings2" } -feature_flags = { package = "feature_flags2", path = "../feature_flags2" } -workspace = { package = "workspace2", path = "../workspace2" } +theme = { package = "theme2", path = "../theme2" } +ui = { package = "ui2", path = "../ui2" } util = { path = "../util" } +workspace = { package = "workspace2", path = "../workspace2" } log.workspace = true parking_lot.workspace = true postage.workspace = true diff --git a/crates/theme_selector2/src/theme_selector.rs b/crates/theme_selector2/src/theme_selector.rs index e0cdc455557ed948d3f69aba1fa74809579cc794..776c39ca6754b8cefcc8f26c8fee0c6b0f9dfadf 100644 --- a/crates/theme_selector2/src/theme_selector.rs +++ b/crates/theme_selector2/src/theme_selector.rs @@ -1,3 +1,4 @@ +use client::{telemetry::Telemetry, TelemetrySettings}; use feature_flags::FeatureFlagAppExt; use fs::Fs; use fuzzy::{match_strings, StringMatch, StringMatchCandidate}; @@ -6,7 +7,7 @@ use gpui::{ VisualContext, WeakView, }; use picker::{Picker, PickerDelegate}; -use settings::{update_settings_file, SettingsStore}; +use settings::{update_settings_file, Settings, SettingsStore}; use std::sync::Arc; use theme::{Theme, ThemeMeta, ThemeRegistry, ThemeSettings}; use ui::{prelude::*, v_stack, ListItem}; @@ -26,9 +27,10 @@ pub fn init(cx: &mut AppContext) { pub fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext) { let fs = workspace.app_state().fs.clone(); + let telemetry = workspace.client().telemetry().clone(); workspace.toggle_modal(cx, |cx| { ThemeSelector::new( - ThemeSelectorDelegate::new(cx.view().downgrade(), fs, cx), + ThemeSelectorDelegate::new(cx.view().downgrade(), fs, telemetry, cx), cx, ) }); @@ -88,6 +90,7 @@ pub struct ThemeSelectorDelegate { original_theme: Arc, selection_completed: bool, selected_index: usize, + telemetry: Arc, view: WeakView, } @@ -95,6 +98,7 @@ impl ThemeSelectorDelegate { fn new( weak_view: WeakView, fs: Arc, + telemetry: Arc, cx: &mut ViewContext, ) -> Self { let original_theme = cx.theme().clone(); @@ -124,6 +128,7 @@ impl ThemeSelectorDelegate { original_theme: original_theme.clone(), selected_index: 0, selection_completed: false, + telemetry, view: weak_view, }; this.select_if_matching(&original_theme.name); @@ -177,6 +182,11 @@ impl PickerDelegate for ThemeSelectorDelegate { self.selection_completed = true; let theme_name = cx.theme().name.clone(); + + let telemetry_settings = TelemetrySettings::get_global(cx).clone(); + self.telemetry + .report_setting_event(telemetry_settings, "theme", theme_name.to_string()); + update_settings_file::(self.fs.clone(), cx, move |settings| { settings.theme = Some(theme_name.to_string()); }); diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index 20b93ae6bb4f3ddf0368748fd949ddb5c6b83e63..9c0c9639b667daae4a82edc656723d1dc05646a3 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -168,6 +168,11 @@ fn main() { client.telemetry().start(installation_id, session_id, cx); let telemetry_settings = *settings::get::(cx); + client.telemetry().report_setting_event( + telemetry_settings, + "theme", + theme::current(cx).meta.name.to_string(), + ); let event_operation = match existing_installation_id_found { Some(false) => "first open", _ => "open", diff --git a/crates/zed2/src/main.rs b/crates/zed2/src/main.rs index f4d9aa2510f4739659f4749fd877d9ea8de76c98..909680610f799ecc9a24bcd14025f147346247d0 100644 --- a/crates/zed2/src/main.rs +++ b/crates/zed2/src/main.rs @@ -177,6 +177,11 @@ fn main() { client.telemetry().start(installation_id, session_id, cx); let telemetry_settings = *client::TelemetrySettings::get_global(cx); + client.telemetry().report_setting_event( + telemetry_settings, + "theme", + cx.theme().name.to_string(), + ); let event_operation = match existing_installation_id_found { Some(false) => "first open", _ => "open",