Remove immediate flush mode

Allow flush method to be called publicly. This is a better, simpler solution, that allows for better control over flushing.
This commit is contained in:
Joseph T. Lyons 2024-01-06 20:27:30 -05:00
parent 520c433af5
commit 5344296c9a
4 changed files with 27 additions and 36 deletions

View file

@ -193,7 +193,8 @@ impl Telemetry {
// TestAppContext ends up calling this function on shutdown and it panics when trying to find the TelemetrySettings // TestAppContext ends up calling this function on shutdown and it panics when trying to find the TelemetrySettings
#[cfg(not(any(test, feature = "test-support")))] #[cfg(not(any(test, feature = "test-support")))]
fn shutdown_telemetry(self: &Arc<Self>) -> impl Future<Output = ()> { fn shutdown_telemetry(self: &Arc<Self>) -> impl Future<Output = ()> {
self.report_app_event("close", true); self.report_app_event("close");
self.flush_clickhouse_events();
Task::ready(()) Task::ready(())
} }
@ -283,7 +284,7 @@ impl Telemetry {
milliseconds_since_first_event: self.milliseconds_since_first_event(), milliseconds_since_first_event: self.milliseconds_since_first_event(),
}; };
self.report_clickhouse_event(event, false) self.report_clickhouse_event(event)
} }
pub fn report_copilot_event( pub fn report_copilot_event(
@ -299,7 +300,7 @@ impl Telemetry {
milliseconds_since_first_event: self.milliseconds_since_first_event(), milliseconds_since_first_event: self.milliseconds_since_first_event(),
}; };
self.report_clickhouse_event(event, false) self.report_clickhouse_event(event)
} }
pub fn report_assistant_event( pub fn report_assistant_event(
@ -315,7 +316,7 @@ impl Telemetry {
milliseconds_since_first_event: self.milliseconds_since_first_event(), milliseconds_since_first_event: self.milliseconds_since_first_event(),
}; };
self.report_clickhouse_event(event, false) self.report_clickhouse_event(event)
} }
pub fn report_call_event( pub fn report_call_event(
@ -331,7 +332,7 @@ impl Telemetry {
milliseconds_since_first_event: self.milliseconds_since_first_event(), milliseconds_since_first_event: self.milliseconds_since_first_event(),
}; };
self.report_clickhouse_event(event, false) self.report_clickhouse_event(event)
} }
pub fn report_cpu_event(self: &Arc<Self>, usage_as_percentage: f32, core_count: u32) { pub fn report_cpu_event(self: &Arc<Self>, usage_as_percentage: f32, core_count: u32) {
@ -341,7 +342,7 @@ impl Telemetry {
milliseconds_since_first_event: self.milliseconds_since_first_event(), milliseconds_since_first_event: self.milliseconds_since_first_event(),
}; };
self.report_clickhouse_event(event, false) self.report_clickhouse_event(event)
} }
pub fn report_memory_event( pub fn report_memory_event(
@ -355,16 +356,16 @@ impl Telemetry {
milliseconds_since_first_event: self.milliseconds_since_first_event(), milliseconds_since_first_event: self.milliseconds_since_first_event(),
}; };
self.report_clickhouse_event(event, false) self.report_clickhouse_event(event)
} }
pub fn report_app_event(self: &Arc<Self>, operation: &'static str, immediate_flush: bool) { pub fn report_app_event(self: &Arc<Self>, operation: &'static str) {
let event = ClickhouseEvent::App { let event = ClickhouseEvent::App {
operation, operation,
milliseconds_since_first_event: self.milliseconds_since_first_event(), milliseconds_since_first_event: self.milliseconds_since_first_event(),
}; };
self.report_clickhouse_event(event, immediate_flush) self.report_clickhouse_event(event)
} }
pub fn report_setting_event(self: &Arc<Self>, setting: &'static str, value: String) { pub fn report_setting_event(self: &Arc<Self>, setting: &'static str, value: String) {
@ -374,7 +375,7 @@ impl Telemetry {
milliseconds_since_first_event: self.milliseconds_since_first_event(), milliseconds_since_first_event: self.milliseconds_since_first_event(),
}; };
self.report_clickhouse_event(event, false) self.report_clickhouse_event(event)
} }
fn milliseconds_since_first_event(&self) -> i64 { fn milliseconds_since_first_event(&self) -> i64 {
@ -391,7 +392,7 @@ impl Telemetry {
} }
} }
fn report_clickhouse_event(self: &Arc<Self>, event: ClickhouseEvent, immediate_flush: bool) { fn report_clickhouse_event(self: &Arc<Self>, event: ClickhouseEvent) {
let mut state = self.state.lock(); let mut state = self.state.lock();
if !state.settings.metrics { if !state.settings.metrics {
@ -404,7 +405,7 @@ impl Telemetry {
.push(ClickhouseEventWrapper { signed_in, event }); .push(ClickhouseEventWrapper { signed_in, event });
if state.installation_id.is_some() { if state.installation_id.is_some() {
if immediate_flush || state.clickhouse_events_queue.len() >= MAX_QUEUE_LEN { if state.clickhouse_events_queue.len() >= MAX_QUEUE_LEN {
drop(state); drop(state);
self.flush_clickhouse_events(); self.flush_clickhouse_events();
} else { } else {
@ -430,7 +431,7 @@ impl Telemetry {
self.state.lock().is_staff self.state.lock().is_staff
} }
fn flush_clickhouse_events(self: &Arc<Self>) { pub fn flush_clickhouse_events(self: &Arc<Self>) {
let mut state = self.state.lock(); let mut state = self.state.lock();
state.first_event_datetime = None; state.first_event_datetime = None;
let mut events = mem::take(&mut state.clickhouse_events_queue); let mut events = mem::take(&mut state.clickhouse_events_queue);

View file

@ -86,7 +86,7 @@ impl Render for WelcomePage {
.full_width() .full_width()
.on_click(cx.listener(|this, _, cx| { .on_click(cx.listener(|this, _, cx| {
this.telemetry this.telemetry
.report_app_event("welcome page button: theme", false); .report_app_event("welcome page button: theme");
this.workspace this.workspace
.update(cx, |workspace, cx| { .update(cx, |workspace, cx| {
theme_selector::toggle( theme_selector::toggle(
@ -103,7 +103,7 @@ impl Render for WelcomePage {
.full_width() .full_width()
.on_click(cx.listener(|this, _, cx| { .on_click(cx.listener(|this, _, cx| {
this.telemetry this.telemetry
.report_app_event("welcome page button: keymap", false); .report_app_event("welcome page button: keymap");
this.workspace this.workspace
.update(cx, |workspace, cx| { .update(cx, |workspace, cx| {
base_keymap_picker::toggle( base_keymap_picker::toggle(
@ -119,10 +119,8 @@ impl Render for WelcomePage {
Button::new("install-cli", "Install the CLI") Button::new("install-cli", "Install the CLI")
.full_width() .full_width()
.on_click(cx.listener(|this, _, cx| { .on_click(cx.listener(|this, _, cx| {
this.telemetry.report_app_event( this.telemetry
"welcome page button: install cli", .report_app_event("welcome page button: install cli");
false,
);
cx.app_mut() cx.app_mut()
.spawn( .spawn(
|cx| async move { install_cli::install_cli(&cx).await }, |cx| async move { install_cli::install_cli(&cx).await },
@ -153,10 +151,8 @@ impl Render for WelcomePage {
) )
.on_click(cx.listener( .on_click(cx.listener(
move |this, selection, cx| { move |this, selection, cx| {
this.telemetry.report_app_event( this.telemetry
"welcome page button: vim", .report_app_event("welcome page button: vim");
false,
);
this.update_settings::<VimModeSetting>( this.update_settings::<VimModeSetting>(
selection, selection,
cx, cx,
@ -183,7 +179,6 @@ impl Render for WelcomePage {
move |this, selection, cx| { move |this, selection, cx| {
this.telemetry.report_app_event( this.telemetry.report_app_event(
"welcome page button: user telemetry", "welcome page button: user telemetry",
false,
); );
this.update_settings::<TelemetrySettings>( this.update_settings::<TelemetrySettings>(
selection, selection,
@ -222,7 +217,6 @@ impl Render for WelcomePage {
move |this, selection, cx| { move |this, selection, cx| {
this.telemetry.report_app_event( this.telemetry.report_app_event(
"welcome page button: crash diagnostics", "welcome page button: crash diagnostics",
false,
); );
this.update_settings::<TelemetrySettings>( this.update_settings::<TelemetrySettings>(
selection, selection,
@ -254,7 +248,7 @@ impl WelcomePage {
pub fn new(workspace: &Workspace, cx: &mut ViewContext<Workspace>) -> View<Self> { pub fn new(workspace: &Workspace, cx: &mut ViewContext<Workspace>) -> View<Self> {
let this = cx.new_view(|cx| { let this = cx.new_view(|cx| {
cx.on_release(|this: &mut Self, _, _| { cx.on_release(|this: &mut Self, _, _| {
this.telemetry.report_app_event("close welcome page", false); this.telemetry.report_app_event("close welcome page");
}) })
.detach(); .detach();

View file

@ -1258,9 +1258,7 @@ impl Workspace {
} }
pub fn open(&mut self, _: &Open, cx: &mut ViewContext<Self>) { pub fn open(&mut self, _: &Open, cx: &mut ViewContext<Self>) {
self.client() self.client().telemetry().report_app_event("open project");
.telemetry()
.report_app_event("open project", false);
let paths = cx.prompt_for_paths(PathPromptOptions { let paths = cx.prompt_for_paths(PathPromptOptions {
files: true, files: true,
directories: true, directories: true,

View file

@ -175,13 +175,11 @@ fn main() {
telemetry.start(installation_id, session_id, cx); telemetry.start(installation_id, session_id, cx);
telemetry.report_setting_event("theme", cx.theme().name.to_string()); telemetry.report_setting_event("theme", cx.theme().name.to_string());
telemetry.report_setting_event("keymap", BaseKeymap::get_global(cx).to_string()); telemetry.report_setting_event("keymap", BaseKeymap::get_global(cx).to_string());
telemetry.report_app_event( telemetry.report_app_event(match existing_installation_id_found {
match existing_installation_id_found {
Some(false) => "first open", Some(false) => "first open",
_ => "open", _ => "open",
}, });
true, telemetry.flush_clickhouse_events();
);
let app_state = Arc::new(AppState { let app_state = Arc::new(AppState {
languages: languages.clone(), languages: languages.clone(),