wip
This commit is contained in:
parent
4784dbe498
commit
da36eb3b41
4 changed files with 67 additions and 33 deletions
|
@ -1,8 +1,8 @@
|
||||||
use crate::{http::HttpClient, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL};
|
use crate::{http::HttpClient, ZED_SECRET_CLIENT_TOKEN};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
executor::Background,
|
executor::Background,
|
||||||
serde_json::{self, value::Map, Value},
|
serde_json::{self, value::Map, Value},
|
||||||
AppContext, AppVersion, Task,
|
AppContext, Task,
|
||||||
};
|
};
|
||||||
use isahc::Request;
|
use isahc::Request;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
@ -12,38 +12,48 @@ use std::{
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
time::{Duration, SystemTime, UNIX_EPOCH},
|
time::{Duration, SystemTime, UNIX_EPOCH},
|
||||||
};
|
};
|
||||||
use util::ResultExt;
|
use util::{post_inc, ResultExt};
|
||||||
|
|
||||||
pub struct Telemetry {
|
pub struct Telemetry {
|
||||||
client: Arc<dyn HttpClient>,
|
client: Arc<dyn HttpClient>,
|
||||||
executor: Arc<Background>,
|
executor: Arc<Background>,
|
||||||
|
session_id: u128,
|
||||||
state: Mutex<TelemetryState>,
|
state: Mutex<TelemetryState>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct TelemetryState {
|
struct TelemetryState {
|
||||||
device_id: Option<String>,
|
user_id: Option<Arc<str>>,
|
||||||
app_version: Option<AppVersion>,
|
device_id: Option<Arc<str>>,
|
||||||
os_version: Option<AppVersion>,
|
app_version: Option<Arc<str>>,
|
||||||
queue: Vec<Event>,
|
os_version: Option<Arc<str>>,
|
||||||
|
os_name: &'static str,
|
||||||
|
queue: Vec<AmplitudeEvent>,
|
||||||
|
next_event_id: usize,
|
||||||
flush_task: Option<Task<()>>,
|
flush_task: Option<Task<()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const AMPLITUDE_EVENTS_URL: &'static str = "https//api2.amplitude.com/batch";
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct RecordEventParams {
|
struct AmplitudeEventBatch {
|
||||||
token: &'static str,
|
api_key: &'static str,
|
||||||
device_id: Option<String>,
|
events: Vec<AmplitudeEvent>,
|
||||||
app_version: Option<String>,
|
|
||||||
os_version: Option<String>,
|
|
||||||
events: Vec<Event>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Event {
|
struct AmplitudeEvent {
|
||||||
#[serde(rename = "type")]
|
user_id: Option<Arc<str>>,
|
||||||
kind: String,
|
device_id: Option<Arc<str>>,
|
||||||
|
event_type: String,
|
||||||
|
event_properties: Option<Map<String, Value>>,
|
||||||
|
user_properties: Option<Map<String, Value>>,
|
||||||
|
os_name: &'static str,
|
||||||
|
os_version: Option<Arc<str>>,
|
||||||
|
app_version: Option<Arc<str>>,
|
||||||
|
event_id: usize,
|
||||||
|
session_id: u128,
|
||||||
time: u128,
|
time: u128,
|
||||||
properties: Option<Map<String, Value>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
|
@ -52,7 +62,6 @@ const MAX_QUEUE_LEN: usize = 1;
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
const MAX_QUEUE_LEN: usize = 10;
|
const MAX_QUEUE_LEN: usize = 10;
|
||||||
|
|
||||||
const EVENTS_URI: &'static str = "api/telemetry";
|
|
||||||
const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(30);
|
const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(30);
|
||||||
|
|
||||||
impl Telemetry {
|
impl Telemetry {
|
||||||
|
@ -61,30 +70,52 @@ impl Telemetry {
|
||||||
Arc::new(Self {
|
Arc::new(Self {
|
||||||
client,
|
client,
|
||||||
executor: cx.background().clone(),
|
executor: cx.background().clone(),
|
||||||
|
session_id: SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap()
|
||||||
|
.as_millis(),
|
||||||
state: Mutex::new(TelemetryState {
|
state: Mutex::new(TelemetryState {
|
||||||
os_version: platform.os_version().log_err(),
|
os_version: platform
|
||||||
app_version: platform.app_version().log_err(),
|
.os_version()
|
||||||
|
.log_err()
|
||||||
|
.map(|v| v.to_string().into()),
|
||||||
|
os_name: platform.os_name().into(),
|
||||||
|
app_version: platform
|
||||||
|
.app_version()
|
||||||
|
.log_err()
|
||||||
|
.map(|v| v.to_string().into()),
|
||||||
device_id: None,
|
device_id: None,
|
||||||
queue: Default::default(),
|
queue: Default::default(),
|
||||||
flush_task: Default::default(),
|
flush_task: Default::default(),
|
||||||
|
next_event_id: 0,
|
||||||
|
user_id: None,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn log_event(self: &Arc<Self>, kind: &str, properties: Value) {
|
pub fn log_event(self: &Arc<Self>, kind: &str, properties: Value) {
|
||||||
let mut state = self.state.lock();
|
let mut state = self.state.lock();
|
||||||
state.queue.push(Event {
|
let event = AmplitudeEvent {
|
||||||
kind: kind.to_string(),
|
event_type: kind.to_string(),
|
||||||
time: SystemTime::now()
|
time: SystemTime::now()
|
||||||
.duration_since(UNIX_EPOCH)
|
.duration_since(UNIX_EPOCH)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_millis(),
|
.as_millis(),
|
||||||
properties: if let Value::Object(properties) = properties {
|
session_id: self.session_id,
|
||||||
|
event_properties: if let Value::Object(properties) = properties {
|
||||||
Some(properties)
|
Some(properties)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
});
|
user_properties: None,
|
||||||
|
user_id: state.user_id.clone(),
|
||||||
|
device_id: state.device_id.clone(),
|
||||||
|
os_name: state.os_name,
|
||||||
|
os_version: state.os_version.clone(),
|
||||||
|
app_version: state.app_version.clone(),
|
||||||
|
event_id: post_inc(&mut state.next_event_id),
|
||||||
|
};
|
||||||
|
state.queue.push(event);
|
||||||
if state.queue.len() >= MAX_QUEUE_LEN {
|
if state.queue.len() >= MAX_QUEUE_LEN {
|
||||||
drop(state);
|
drop(state);
|
||||||
self.flush();
|
self.flush();
|
||||||
|
@ -102,21 +133,15 @@ impl Telemetry {
|
||||||
let mut state = self.state.lock();
|
let mut state = self.state.lock();
|
||||||
let events = mem::take(&mut state.queue);
|
let events = mem::take(&mut state.queue);
|
||||||
let client = self.client.clone();
|
let client = self.client.clone();
|
||||||
let app_version = state.app_version;
|
|
||||||
let os_version = state.os_version;
|
|
||||||
let device_id = state.device_id.clone();
|
|
||||||
state.flush_task.take();
|
state.flush_task.take();
|
||||||
self.executor
|
self.executor
|
||||||
.spawn(async move {
|
.spawn(async move {
|
||||||
let body = serde_json::to_vec(&RecordEventParams {
|
let body = serde_json::to_vec(&AmplitudeEventBatch {
|
||||||
token: ZED_SECRET_CLIENT_TOKEN,
|
api_key: ZED_SECRET_CLIENT_TOKEN,
|
||||||
events,
|
events,
|
||||||
app_version: app_version.map(|v| v.to_string()),
|
|
||||||
os_version: os_version.map(|v| v.to_string()),
|
|
||||||
device_id,
|
|
||||||
})
|
})
|
||||||
.log_err()?;
|
.log_err()?;
|
||||||
let request = Request::post(format!("{}/{}", *ZED_SERVER_URL, EVENTS_URI))
|
let request = Request::post(AMPLITUDE_EVENTS_URL)
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.body(body.into())
|
.body(body.into())
|
||||||
.log_err()?;
|
.log_err()?;
|
||||||
|
|
|
@ -69,6 +69,7 @@ pub trait Platform: Send + Sync {
|
||||||
fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf>;
|
fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf>;
|
||||||
fn app_path(&self) -> Result<PathBuf>;
|
fn app_path(&self) -> Result<PathBuf>;
|
||||||
fn app_version(&self) -> Result<AppVersion>;
|
fn app_version(&self) -> Result<AppVersion>;
|
||||||
|
fn os_name(&self) -> &'static str;
|
||||||
fn os_version(&self) -> Result<AppVersion>;
|
fn os_version(&self) -> Result<AppVersion>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -750,6 +750,10 @@ impl platform::Platform for MacPlatform {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn os_name(&self) -> &'static str {
|
||||||
|
"macOS"
|
||||||
|
}
|
||||||
|
|
||||||
fn os_version(&self) -> Result<crate::AppVersion> {
|
fn os_version(&self) -> Result<crate::AppVersion> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let process_info = NSProcessInfo::processInfo(nil);
|
let process_info = NSProcessInfo::processInfo(nil);
|
||||||
|
|
|
@ -197,6 +197,10 @@ impl super::Platform for Platform {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn os_name(&self) -> &'static str {
|
||||||
|
"test"
|
||||||
|
}
|
||||||
|
|
||||||
fn os_version(&self) -> Result<AppVersion> {
|
fn os_version(&self) -> Result<AppVersion> {
|
||||||
Ok(AppVersion {
|
Ok(AppVersion {
|
||||||
major: 1,
|
major: 1,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue