Link signups to users in telemetry via a stored device_id
Co-authored-by: Joseph Lyons <joseph@zed.dev>
This commit is contained in:
parent
04baccbea6
commit
4784dbe498
7 changed files with 124 additions and 109 deletions
|
@ -14,9 +14,11 @@ use async_tungstenite::tungstenite::{
|
|||
};
|
||||
use futures::{future::LocalBoxFuture, FutureExt, SinkExt, StreamExt, TryStreamExt};
|
||||
use gpui::{
|
||||
actions, serde_json::Value, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle,
|
||||
AnyWeakViewHandle, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle,
|
||||
MutableAppContext, Task, View, ViewContext, ViewHandle,
|
||||
actions,
|
||||
serde_json::{json, Value},
|
||||
AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, AnyWeakViewHandle, AppContext,
|
||||
AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext, Task, View, ViewContext,
|
||||
ViewHandle,
|
||||
};
|
||||
use http::HttpClient;
|
||||
use lazy_static::lazy_static;
|
||||
|
@ -52,13 +54,29 @@ lazy_static! {
|
|||
|
||||
pub const ZED_SECRET_CLIENT_TOKEN: &str = "618033988749894";
|
||||
|
||||
actions!(client, [Authenticate]);
|
||||
actions!(client, [Authenticate, TestTelemetry]);
|
||||
|
||||
pub fn init(rpc: Arc<Client>, cx: &mut MutableAppContext) {
|
||||
cx.add_global_action(move |_: &Authenticate, cx| {
|
||||
let rpc = rpc.clone();
|
||||
cx.spawn(|cx| async move { rpc.authenticate_and_connect(true, &cx).log_err().await })
|
||||
pub fn init(client: Arc<Client>, cx: &mut MutableAppContext) {
|
||||
cx.add_global_action({
|
||||
let client = client.clone();
|
||||
move |_: &Authenticate, cx| {
|
||||
let client = client.clone();
|
||||
cx.spawn(
|
||||
|cx| async move { client.authenticate_and_connect(true, &cx).log_err().await },
|
||||
)
|
||||
.detach();
|
||||
}
|
||||
});
|
||||
cx.add_global_action({
|
||||
let client = client.clone();
|
||||
move |_: &TestTelemetry, _| {
|
||||
client.log_event(
|
||||
"test_telemetry",
|
||||
json!({
|
||||
"test_property": "test_value"
|
||||
}),
|
||||
)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{http::HttpClient, ZED_SECRET_CLIENT_TOKEN};
|
||||
use crate::{http::HttpClient, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL};
|
||||
use gpui::{
|
||||
executor::Background,
|
||||
serde_json::{self, value::Map, Value},
|
||||
|
@ -22,7 +22,6 @@ pub struct Telemetry {
|
|||
|
||||
#[derive(Default)]
|
||||
struct TelemetryState {
|
||||
metrics_id: Option<i32>,
|
||||
device_id: Option<String>,
|
||||
app_version: Option<AppVersion>,
|
||||
os_version: Option<AppVersion>,
|
||||
|
@ -33,7 +32,6 @@ struct TelemetryState {
|
|||
#[derive(Serialize)]
|
||||
struct RecordEventParams {
|
||||
token: &'static str,
|
||||
metrics_id: Option<i32>,
|
||||
device_id: Option<String>,
|
||||
app_version: Option<String>,
|
||||
os_version: Option<String>,
|
||||
|
@ -48,8 +46,13 @@ struct Event {
|
|||
properties: Option<Map<String, Value>>,
|
||||
}
|
||||
|
||||
const MAX_QUEUE_LEN: usize = 30;
|
||||
const EVENTS_URI: &'static str = "https://zed.dev/api/telemetry";
|
||||
#[cfg(debug_assertions)]
|
||||
const MAX_QUEUE_LEN: usize = 1;
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
const MAX_QUEUE_LEN: usize = 10;
|
||||
|
||||
const EVENTS_URI: &'static str = "api/telemetry";
|
||||
const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(30);
|
||||
|
||||
impl Telemetry {
|
||||
|
@ -61,7 +64,6 @@ impl Telemetry {
|
|||
state: Mutex::new(TelemetryState {
|
||||
os_version: platform.os_version().log_err(),
|
||||
app_version: platform.app_version().log_err(),
|
||||
metrics_id: None,
|
||||
device_id: None,
|
||||
queue: Default::default(),
|
||||
flush_task: Default::default(),
|
||||
|
@ -69,10 +71,6 @@ impl Telemetry {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn set_metrics_id(&self, metrics_id: Option<i32>) {
|
||||
self.state.lock().metrics_id = metrics_id;
|
||||
}
|
||||
|
||||
pub fn log_event(self: &Arc<Self>, kind: &str, properties: Value) {
|
||||
let mut state = self.state.lock();
|
||||
state.queue.push(Event {
|
||||
|
@ -88,6 +86,7 @@ impl Telemetry {
|
|||
},
|
||||
});
|
||||
if state.queue.len() >= MAX_QUEUE_LEN {
|
||||
drop(state);
|
||||
self.flush();
|
||||
} else {
|
||||
let this = self.clone();
|
||||
|
@ -105,7 +104,6 @@ impl Telemetry {
|
|||
let client = self.client.clone();
|
||||
let app_version = state.app_version;
|
||||
let os_version = state.os_version;
|
||||
let metrics_id = state.metrics_id;
|
||||
let device_id = state.device_id.clone();
|
||||
state.flush_task.take();
|
||||
self.executor
|
||||
|
@ -115,11 +113,13 @@ impl Telemetry {
|
|||
events,
|
||||
app_version: app_version.map(|v| v.to_string()),
|
||||
os_version: os_version.map(|v| v.to_string()),
|
||||
metrics_id,
|
||||
device_id,
|
||||
})
|
||||
.log_err()?;
|
||||
let request = Request::post(EVENTS_URI).body(body.into()).log_err()?;
|
||||
let request = Request::post(format!("{}/{}", *ZED_SERVER_URL, EVENTS_URI))
|
||||
.header("Content-Type", "application/json")
|
||||
.body(body.into())
|
||||
.log_err()?;
|
||||
client.send(request).await.log_err();
|
||||
Some(())
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue