events.rs

   1use super::ips_file::IpsFile;
   2use crate::api::CloudflareIpCountryHeader;
   3use crate::clickhouse::write_to_table;
   4use crate::{api::slack, AppState, Error, Result};
   5use anyhow::{anyhow, Context};
   6use aws_sdk_s3::primitives::ByteStream;
   7use axum::{
   8    body::Bytes,
   9    headers::Header,
  10    http::{HeaderMap, HeaderName, StatusCode},
  11    routing::post,
  12    Extension, Router, TypedHeader,
  13};
  14use rpc::ExtensionMetadata;
  15use semantic_version::SemanticVersion;
  16use serde::{Serialize, Serializer};
  17use sha2::{Digest, Sha256};
  18use std::sync::{Arc, OnceLock};
  19use telemetry_events::{
  20    ActionEvent, AppEvent, AssistantEvent, CallEvent, CpuEvent, EditEvent, EditorEvent, Event,
  21    EventRequestBody, EventWrapper, ExtensionEvent, InlineCompletionEvent, MemoryEvent, Panic,
  22    ReplEvent, SettingEvent,
  23};
  24use uuid::Uuid;
  25
  26const CRASH_REPORTS_BUCKET: &str = "zed-crash-reports";
  27
  28pub fn router() -> Router {
  29    Router::new()
  30        .route("/telemetry/events", post(post_events))
  31        .route("/telemetry/crashes", post(post_crash))
  32        .route("/telemetry/panics", post(post_panic))
  33        .route("/telemetry/hangs", post(post_hang))
  34}
  35
  36pub struct ZedChecksumHeader(Vec<u8>);
  37
  38impl Header for ZedChecksumHeader {
  39    fn name() -> &'static HeaderName {
  40        static ZED_CHECKSUM_HEADER: OnceLock<HeaderName> = OnceLock::new();
  41        ZED_CHECKSUM_HEADER.get_or_init(|| HeaderName::from_static("x-zed-checksum"))
  42    }
  43
  44    fn decode<'i, I>(values: &mut I) -> Result<Self, axum::headers::Error>
  45    where
  46        Self: Sized,
  47        I: Iterator<Item = &'i axum::http::HeaderValue>,
  48    {
  49        let checksum = values
  50            .next()
  51            .ok_or_else(axum::headers::Error::invalid)?
  52            .to_str()
  53            .map_err(|_| axum::headers::Error::invalid())?;
  54
  55        let bytes = hex::decode(checksum).map_err(|_| axum::headers::Error::invalid())?;
  56        Ok(Self(bytes))
  57    }
  58
  59    fn encode<E: Extend<axum::http::HeaderValue>>(&self, _values: &mut E) {
  60        unimplemented!()
  61    }
  62}
  63
  64pub async fn post_crash(
  65    Extension(app): Extension<Arc<AppState>>,
  66    headers: HeaderMap,
  67    body: Bytes,
  68) -> Result<()> {
  69    let report = IpsFile::parse(&body)?;
  70    let version_threshold = SemanticVersion::new(0, 123, 0);
  71
  72    let bundle_id = &report.header.bundle_id;
  73    let app_version = &report.app_version();
  74
  75    if bundle_id == "dev.zed.Zed-Dev" {
  76        log::error!("Crash uploads from {} are ignored.", bundle_id);
  77        return Ok(());
  78    }
  79
  80    if app_version.is_none() || app_version.unwrap() < version_threshold {
  81        log::error!(
  82            "Crash uploads from {} are ignored.",
  83            report.header.app_version
  84        );
  85        return Ok(());
  86    }
  87    let app_version = app_version.unwrap();
  88
  89    if let Some(blob_store_client) = app.blob_store_client.as_ref() {
  90        let response = blob_store_client
  91            .head_object()
  92            .bucket(CRASH_REPORTS_BUCKET)
  93            .key(report.header.incident_id.clone() + ".ips")
  94            .send()
  95            .await;
  96
  97        if response.is_ok() {
  98            log::info!("We've already uploaded this crash");
  99            return Ok(());
 100        }
 101
 102        blob_store_client
 103            .put_object()
 104            .bucket(CRASH_REPORTS_BUCKET)
 105            .key(report.header.incident_id.clone() + ".ips")
 106            .acl(aws_sdk_s3::types::ObjectCannedAcl::PublicRead)
 107            .body(ByteStream::from(body.to_vec()))
 108            .send()
 109            .await
 110            .map_err(|e| log::error!("Failed to upload crash: {}", e))
 111            .ok();
 112    }
 113
 114    let recent_panic_on: Option<i64> = headers
 115        .get("x-zed-panicked-on")
 116        .and_then(|h| h.to_str().ok())
 117        .and_then(|s| s.parse().ok());
 118
 119    let installation_id = headers
 120        .get("x-zed-installation-id")
 121        .and_then(|h| h.to_str().ok())
 122        .map(|s| s.to_string())
 123        .unwrap_or_default();
 124
 125    let mut recent_panic = None;
 126
 127    if let Some(recent_panic_on) = recent_panic_on {
 128        let crashed_at = match report.timestamp() {
 129            Ok(t) => Some(t),
 130            Err(e) => {
 131                log::error!("Can't parse {}: {}", report.header.timestamp, e);
 132                None
 133            }
 134        };
 135        if crashed_at.is_some_and(|t| (t.timestamp_millis() - recent_panic_on).abs() <= 30000) {
 136            recent_panic = headers.get("x-zed-panic").and_then(|h| h.to_str().ok());
 137        }
 138    }
 139
 140    let description = report.description(recent_panic);
 141    let summary = report.backtrace_summary();
 142
 143    tracing::error!(
 144        service = "client",
 145        version = %report.header.app_version,
 146        os_version = %report.header.os_version,
 147        bundle_id = %report.header.bundle_id,
 148        incident_id = %report.header.incident_id,
 149        installation_id = %installation_id,
 150        description = %description,
 151        backtrace = %summary,
 152        "crash report"
 153    );
 154
 155    if let Some(slack_panics_webhook) = app.config.slack_panics_webhook.clone() {
 156        let payload = slack::WebhookBody::new(|w| {
 157            w.add_section(|s| s.text(slack::Text::markdown(description)))
 158                .add_section(|s| {
 159                    s.add_field(slack::Text::markdown(format!(
 160                        "*Version:*\n{} ({})",
 161                        bundle_id, app_version
 162                    )))
 163                    .add_field({
 164                        let hostname = app.config.blob_store_url.clone().unwrap_or_default();
 165                        let hostname = hostname.strip_prefix("https://").unwrap_or_else(|| {
 166                            hostname.strip_prefix("http://").unwrap_or_default()
 167                        });
 168
 169                        slack::Text::markdown(format!(
 170                            "*Incident:*\n<https://{}.{}/{}.ips|{}…>",
 171                            CRASH_REPORTS_BUCKET,
 172                            hostname,
 173                            report.header.incident_id,
 174                            report
 175                                .header
 176                                .incident_id
 177                                .chars()
 178                                .take(8)
 179                                .collect::<String>(),
 180                        ))
 181                    })
 182                })
 183                .add_rich_text(|r| r.add_preformatted(|p| p.add_text(summary)))
 184        });
 185        let payload_json = serde_json::to_string(&payload).map_err(|err| {
 186            log::error!("Failed to serialize payload to JSON: {err}");
 187            Error::Internal(anyhow!(err))
 188        })?;
 189
 190        reqwest::Client::new()
 191            .post(slack_panics_webhook)
 192            .header("Content-Type", "application/json")
 193            .body(payload_json)
 194            .send()
 195            .await
 196            .map_err(|err| {
 197                log::error!("Failed to send payload to Slack: {err}");
 198                Error::Internal(anyhow!(err))
 199            })?;
 200    }
 201
 202    Ok(())
 203}
 204
 205pub async fn post_hang(
 206    Extension(app): Extension<Arc<AppState>>,
 207    TypedHeader(ZedChecksumHeader(checksum)): TypedHeader<ZedChecksumHeader>,
 208    body: Bytes,
 209) -> Result<()> {
 210    let Some(expected) = calculate_json_checksum(app.clone(), &body) else {
 211        return Err(Error::http(
 212            StatusCode::INTERNAL_SERVER_ERROR,
 213            "events not enabled".into(),
 214        ))?;
 215    };
 216
 217    if checksum != expected {
 218        return Err(Error::http(
 219            StatusCode::BAD_REQUEST,
 220            "invalid checksum".into(),
 221        ))?;
 222    }
 223
 224    let incident_id = Uuid::new_v4().to_string();
 225
 226    // dump JSON into S3 so we can get frame offsets if we need to.
 227    if let Some(blob_store_client) = app.blob_store_client.as_ref() {
 228        blob_store_client
 229            .put_object()
 230            .bucket(CRASH_REPORTS_BUCKET)
 231            .key(incident_id.clone() + ".hang.json")
 232            .acl(aws_sdk_s3::types::ObjectCannedAcl::PublicRead)
 233            .body(ByteStream::from(body.to_vec()))
 234            .send()
 235            .await
 236            .map_err(|e| log::error!("Failed to upload crash: {}", e))
 237            .ok();
 238    }
 239
 240    let report: telemetry_events::HangReport = serde_json::from_slice(&body).map_err(|err| {
 241        log::error!("can't parse report json: {err}");
 242        Error::Internal(anyhow!(err))
 243    })?;
 244
 245    let mut backtrace = "Possible hang detected on main thread:".to_string();
 246    let unknown = "<unknown>".to_string();
 247    for frame in report.backtrace.iter() {
 248        backtrace.push_str(&format!("\n{}", frame.symbols.first().unwrap_or(&unknown)));
 249    }
 250
 251    tracing::error!(
 252        service = "client",
 253        version = %report.app_version.unwrap_or_default().to_string(),
 254        os_name = %report.os_name,
 255        os_version = report.os_version.unwrap_or_default().to_string(),
 256        incident_id = %incident_id,
 257        installation_id = %report.installation_id.unwrap_or_default(),
 258        backtrace = %backtrace,
 259        "hang report");
 260
 261    Ok(())
 262}
 263
 264pub async fn post_panic(
 265    Extension(app): Extension<Arc<AppState>>,
 266    TypedHeader(ZedChecksumHeader(checksum)): TypedHeader<ZedChecksumHeader>,
 267    body: Bytes,
 268) -> Result<()> {
 269    let Some(expected) = calculate_json_checksum(app.clone(), &body) else {
 270        return Err(Error::http(
 271            StatusCode::INTERNAL_SERVER_ERROR,
 272            "events not enabled".into(),
 273        ))?;
 274    };
 275
 276    if checksum != expected {
 277        return Err(Error::http(
 278            StatusCode::BAD_REQUEST,
 279            "invalid checksum".into(),
 280        ))?;
 281    }
 282
 283    let report: telemetry_events::PanicRequest = serde_json::from_slice(&body)
 284        .map_err(|_| Error::http(StatusCode::BAD_REQUEST, "invalid json".into()))?;
 285    let panic = report.panic;
 286
 287    if panic.os_name == "Linux" && panic.os_version == Some("1.0.0".to_string()) {
 288        return Err(Error::http(
 289            StatusCode::BAD_REQUEST,
 290            "invalid os version".into(),
 291        ))?;
 292    }
 293
 294    tracing::error!(
 295        service = "client",
 296        version = %panic.app_version,
 297        os_name = %panic.os_name,
 298        os_version = %panic.os_version.clone().unwrap_or_default(),
 299        installation_id = %panic.installation_id.clone().unwrap_or_default(),
 300        description = %panic.payload,
 301        backtrace = %panic.backtrace.join("\n"),
 302        "panic report"
 303    );
 304
 305    let backtrace = if panic.backtrace.len() > 25 {
 306        let total = panic.backtrace.len();
 307        format!(
 308            "{}\n   and {} more",
 309            panic
 310                .backtrace
 311                .iter()
 312                .take(20)
 313                .cloned()
 314                .collect::<Vec<_>>()
 315                .join("\n"),
 316            total - 20
 317        )
 318    } else {
 319        panic.backtrace.join("\n")
 320    };
 321
 322    if !report_to_slack(&panic) {
 323        return Ok(());
 324    }
 325
 326    let backtrace_with_summary = panic.payload + "\n" + &backtrace;
 327
 328    if let Some(slack_panics_webhook) = app.config.slack_panics_webhook.clone() {
 329        let payload = slack::WebhookBody::new(|w| {
 330            w.add_section(|s| s.text(slack::Text::markdown("Panic request".to_string())))
 331                .add_section(|s| {
 332                    s.add_field(slack::Text::markdown(format!(
 333                        "*Version:*\n {} ",
 334                        panic.app_version
 335                    )))
 336                    .add_field({
 337                        slack::Text::markdown(format!(
 338                            "*OS:*\n{} {}",
 339                            panic.os_name,
 340                            panic.os_version.unwrap_or_default()
 341                        ))
 342                    })
 343                })
 344                .add_rich_text(|r| r.add_preformatted(|p| p.add_text(backtrace_with_summary)))
 345        });
 346        let payload_json = serde_json::to_string(&payload).map_err(|err| {
 347            log::error!("Failed to serialize payload to JSON: {err}");
 348            Error::Internal(anyhow!(err))
 349        })?;
 350
 351        reqwest::Client::new()
 352            .post(slack_panics_webhook)
 353            .header("Content-Type", "application/json")
 354            .body(payload_json)
 355            .send()
 356            .await
 357            .map_err(|err| {
 358                log::error!("Failed to send payload to Slack: {err}");
 359                Error::Internal(anyhow!(err))
 360            })?;
 361    }
 362
 363    Ok(())
 364}
 365
 366fn report_to_slack(panic: &Panic) -> bool {
 367    if panic.payload.contains("ERROR_SURFACE_LOST_KHR") {
 368        return false;
 369    }
 370
 371    if panic.payload.contains("ERROR_INITIALIZATION_FAILED") {
 372        return false;
 373    }
 374
 375    if panic
 376        .payload
 377        .contains("GPU has crashed, and no debug information is available")
 378    {
 379        return false;
 380    }
 381
 382    true
 383}
 384
 385pub async fn post_events(
 386    Extension(app): Extension<Arc<AppState>>,
 387    TypedHeader(ZedChecksumHeader(checksum)): TypedHeader<ZedChecksumHeader>,
 388    country_code_header: Option<TypedHeader<CloudflareIpCountryHeader>>,
 389    body: Bytes,
 390) -> Result<()> {
 391    let Some(clickhouse_client) = app.clickhouse_client.clone() else {
 392        Err(Error::http(
 393            StatusCode::NOT_IMPLEMENTED,
 394            "not supported".into(),
 395        ))?
 396    };
 397
 398    let Some(expected) = calculate_json_checksum(app.clone(), &body) else {
 399        return Err(Error::http(
 400            StatusCode::INTERNAL_SERVER_ERROR,
 401            "events not enabled".into(),
 402        ))?;
 403    };
 404
 405    let checksum_matched = checksum == expected;
 406
 407    let request_body: telemetry_events::EventRequestBody =
 408        serde_json::from_slice(&body).map_err(|err| {
 409            log::error!("can't parse event json: {err}");
 410            Error::Internal(anyhow!(err))
 411        })?;
 412
 413    let mut to_upload = ToUpload::default();
 414    let Some(last_event) = request_body.events.last() else {
 415        return Err(Error::http(StatusCode::BAD_REQUEST, "no events".into()))?;
 416    };
 417    let country_code = country_code_header.map(|h| h.to_string());
 418
 419    let first_event_at = chrono::Utc::now()
 420        - chrono::Duration::milliseconds(last_event.milliseconds_since_first_event);
 421
 422    for wrapper in &request_body.events {
 423        match &wrapper.event {
 424            Event::Editor(event) => to_upload.editor_events.push(EditorEventRow::from_event(
 425                event.clone(),
 426                wrapper,
 427                &request_body,
 428                first_event_at,
 429                country_code.clone(),
 430                checksum_matched,
 431            )),
 432            Event::InlineCompletion(event) => {
 433                to_upload
 434                    .inline_completion_events
 435                    .push(InlineCompletionEventRow::from_event(
 436                        event.clone(),
 437                        wrapper,
 438                        &request_body,
 439                        first_event_at,
 440                        country_code.clone(),
 441                        checksum_matched,
 442                    ))
 443            }
 444            Event::Call(event) => to_upload.call_events.push(CallEventRow::from_event(
 445                event.clone(),
 446                wrapper,
 447                &request_body,
 448                first_event_at,
 449                checksum_matched,
 450            )),
 451            Event::Assistant(event) => {
 452                to_upload
 453                    .assistant_events
 454                    .push(AssistantEventRow::from_event(
 455                        event.clone(),
 456                        wrapper,
 457                        &request_body,
 458                        first_event_at,
 459                        checksum_matched,
 460                    ))
 461            }
 462            Event::Cpu(event) => to_upload.cpu_events.push(CpuEventRow::from_event(
 463                event.clone(),
 464                wrapper,
 465                &request_body,
 466                first_event_at,
 467                checksum_matched,
 468            )),
 469            Event::Memory(event) => to_upload.memory_events.push(MemoryEventRow::from_event(
 470                event.clone(),
 471                wrapper,
 472                &request_body,
 473                first_event_at,
 474                checksum_matched,
 475            )),
 476            Event::App(event) => to_upload.app_events.push(AppEventRow::from_event(
 477                event.clone(),
 478                wrapper,
 479                &request_body,
 480                first_event_at,
 481                checksum_matched,
 482            )),
 483            Event::Setting(event) => to_upload.setting_events.push(SettingEventRow::from_event(
 484                event.clone(),
 485                wrapper,
 486                &request_body,
 487                first_event_at,
 488                checksum_matched,
 489            )),
 490            Event::Edit(event) => to_upload.edit_events.push(EditEventRow::from_event(
 491                event.clone(),
 492                wrapper,
 493                &request_body,
 494                first_event_at,
 495                checksum_matched,
 496            )),
 497            Event::Action(event) => to_upload.action_events.push(ActionEventRow::from_event(
 498                event.clone(),
 499                wrapper,
 500                &request_body,
 501                first_event_at,
 502                checksum_matched,
 503            )),
 504            Event::Extension(event) => {
 505                let metadata = app
 506                    .db
 507                    .get_extension_version(&event.extension_id, &event.version)
 508                    .await?;
 509                to_upload
 510                    .extension_events
 511                    .push(ExtensionEventRow::from_event(
 512                        event.clone(),
 513                        wrapper,
 514                        &request_body,
 515                        metadata,
 516                        first_event_at,
 517                        checksum_matched,
 518                    ))
 519            }
 520            Event::Repl(event) => to_upload.repl_events.push(ReplEventRow::from_event(
 521                event.clone(),
 522                wrapper,
 523                &request_body,
 524                first_event_at,
 525                checksum_matched,
 526            )),
 527        }
 528    }
 529
 530    to_upload
 531        .upload(&clickhouse_client)
 532        .await
 533        .map_err(|err| Error::Internal(anyhow!(err)))?;
 534
 535    Ok(())
 536}
 537
 538#[derive(Default)]
 539struct ToUpload {
 540    editor_events: Vec<EditorEventRow>,
 541    inline_completion_events: Vec<InlineCompletionEventRow>,
 542    assistant_events: Vec<AssistantEventRow>,
 543    call_events: Vec<CallEventRow>,
 544    cpu_events: Vec<CpuEventRow>,
 545    memory_events: Vec<MemoryEventRow>,
 546    app_events: Vec<AppEventRow>,
 547    setting_events: Vec<SettingEventRow>,
 548    extension_events: Vec<ExtensionEventRow>,
 549    edit_events: Vec<EditEventRow>,
 550    action_events: Vec<ActionEventRow>,
 551    repl_events: Vec<ReplEventRow>,
 552}
 553
 554impl ToUpload {
 555    pub async fn upload(&self, clickhouse_client: &clickhouse::Client) -> anyhow::Result<()> {
 556        const EDITOR_EVENTS_TABLE: &str = "editor_events";
 557        write_to_table(EDITOR_EVENTS_TABLE, &self.editor_events, clickhouse_client)
 558            .await
 559            .with_context(|| format!("failed to upload to table '{EDITOR_EVENTS_TABLE}'"))?;
 560
 561        const INLINE_COMPLETION_EVENTS_TABLE: &str = "inline_completion_events";
 562        write_to_table(
 563            INLINE_COMPLETION_EVENTS_TABLE,
 564            &self.inline_completion_events,
 565            clickhouse_client,
 566        )
 567        .await
 568        .with_context(|| format!("failed to upload to table '{INLINE_COMPLETION_EVENTS_TABLE}'"))?;
 569
 570        const ASSISTANT_EVENTS_TABLE: &str = "assistant_events";
 571        write_to_table(
 572            ASSISTANT_EVENTS_TABLE,
 573            &self.assistant_events,
 574            clickhouse_client,
 575        )
 576        .await
 577        .with_context(|| format!("failed to upload to table '{ASSISTANT_EVENTS_TABLE}'"))?;
 578
 579        const CALL_EVENTS_TABLE: &str = "call_events";
 580        write_to_table(CALL_EVENTS_TABLE, &self.call_events, clickhouse_client)
 581            .await
 582            .with_context(|| format!("failed to upload to table '{CALL_EVENTS_TABLE}'"))?;
 583
 584        const CPU_EVENTS_TABLE: &str = "cpu_events";
 585        write_to_table(CPU_EVENTS_TABLE, &self.cpu_events, clickhouse_client)
 586            .await
 587            .with_context(|| format!("failed to upload to table '{CPU_EVENTS_TABLE}'"))?;
 588
 589        const MEMORY_EVENTS_TABLE: &str = "memory_events";
 590        write_to_table(MEMORY_EVENTS_TABLE, &self.memory_events, clickhouse_client)
 591            .await
 592            .with_context(|| format!("failed to upload to table '{MEMORY_EVENTS_TABLE}'"))?;
 593
 594        const APP_EVENTS_TABLE: &str = "app_events";
 595        write_to_table(APP_EVENTS_TABLE, &self.app_events, clickhouse_client)
 596            .await
 597            .with_context(|| format!("failed to upload to table '{APP_EVENTS_TABLE}'"))?;
 598
 599        const SETTING_EVENTS_TABLE: &str = "setting_events";
 600        write_to_table(
 601            SETTING_EVENTS_TABLE,
 602            &self.setting_events,
 603            clickhouse_client,
 604        )
 605        .await
 606        .with_context(|| format!("failed to upload to table '{SETTING_EVENTS_TABLE}'"))?;
 607
 608        const EXTENSION_EVENTS_TABLE: &str = "extension_events";
 609        write_to_table(
 610            EXTENSION_EVENTS_TABLE,
 611            &self.extension_events,
 612            clickhouse_client,
 613        )
 614        .await
 615        .with_context(|| format!("failed to upload to table '{EXTENSION_EVENTS_TABLE}'"))?;
 616
 617        const EDIT_EVENTS_TABLE: &str = "edit_events";
 618        write_to_table(EDIT_EVENTS_TABLE, &self.edit_events, clickhouse_client)
 619            .await
 620            .with_context(|| format!("failed to upload to table '{EDIT_EVENTS_TABLE}'"))?;
 621
 622        const ACTION_EVENTS_TABLE: &str = "action_events";
 623        write_to_table(ACTION_EVENTS_TABLE, &self.action_events, clickhouse_client)
 624            .await
 625            .with_context(|| format!("failed to upload to table '{ACTION_EVENTS_TABLE}'"))?;
 626
 627        const REPL_EVENTS_TABLE: &str = "repl_events";
 628        write_to_table(REPL_EVENTS_TABLE, &self.repl_events, clickhouse_client)
 629            .await
 630            .with_context(|| format!("failed to upload to table '{REPL_EVENTS_TABLE}'"))?;
 631
 632        Ok(())
 633    }
 634}
 635
 636pub fn serialize_country_code<S>(country_code: &str, serializer: S) -> Result<S::Ok, S::Error>
 637where
 638    S: Serializer,
 639{
 640    if country_code.len() != 2 {
 641        use serde::ser::Error;
 642        return Err(S::Error::custom(
 643            "country_code must be exactly 2 characters",
 644        ));
 645    }
 646
 647    let country_code = country_code.as_bytes();
 648
 649    serializer.serialize_u16(((country_code[1] as u16) << 8) + country_code[0] as u16)
 650}
 651
 652#[derive(Serialize, Debug, clickhouse::Row)]
 653pub struct EditorEventRow {
 654    system_id: String,
 655    installation_id: String,
 656    session_id: Option<String>,
 657    metrics_id: String,
 658    operation: String,
 659    app_version: String,
 660    file_extension: String,
 661    os_name: String,
 662    os_version: String,
 663    release_channel: String,
 664    signed_in: bool,
 665    vim_mode: bool,
 666    #[serde(serialize_with = "serialize_country_code")]
 667    country_code: String,
 668    region_code: String,
 669    city: String,
 670    time: i64,
 671    copilot_enabled: bool,
 672    copilot_enabled_for_language: bool,
 673    architecture: String,
 674    is_staff: Option<bool>,
 675    major: Option<i32>,
 676    minor: Option<i32>,
 677    patch: Option<i32>,
 678    checksum_matched: bool,
 679    is_via_ssh: bool,
 680}
 681
 682impl EditorEventRow {
 683    fn from_event(
 684        event: EditorEvent,
 685        wrapper: &EventWrapper,
 686        body: &EventRequestBody,
 687        first_event_at: chrono::DateTime<chrono::Utc>,
 688        country_code: Option<String>,
 689        checksum_matched: bool,
 690    ) -> Self {
 691        let semver = body.semver();
 692        let time =
 693            first_event_at + chrono::Duration::milliseconds(wrapper.milliseconds_since_first_event);
 694
 695        Self {
 696            app_version: body.app_version.clone(),
 697            major: semver.map(|v| v.major() as i32),
 698            minor: semver.map(|v| v.minor() as i32),
 699            patch: semver.map(|v| v.patch() as i32),
 700            checksum_matched,
 701            release_channel: body.release_channel.clone().unwrap_or_default(),
 702            os_name: body.os_name.clone(),
 703            os_version: body.os_version.clone().unwrap_or_default(),
 704            architecture: body.architecture.clone(),
 705            system_id: body.system_id.clone().unwrap_or_default(),
 706            installation_id: body.installation_id.clone().unwrap_or_default(),
 707            session_id: body.session_id.clone(),
 708            metrics_id: body.metrics_id.clone().unwrap_or_default(),
 709            is_staff: body.is_staff,
 710            time: time.timestamp_millis(),
 711            operation: event.operation,
 712            file_extension: event.file_extension.unwrap_or_default(),
 713            signed_in: wrapper.signed_in,
 714            vim_mode: event.vim_mode,
 715            copilot_enabled: event.copilot_enabled,
 716            copilot_enabled_for_language: event.copilot_enabled_for_language,
 717            country_code: country_code.unwrap_or("XX".to_string()),
 718            region_code: "".to_string(),
 719            city: "".to_string(),
 720            is_via_ssh: event.is_via_ssh,
 721        }
 722    }
 723}
 724
 725#[derive(Serialize, Debug, clickhouse::Row)]
 726pub struct InlineCompletionEventRow {
 727    installation_id: String,
 728    session_id: Option<String>,
 729    provider: String,
 730    suggestion_accepted: bool,
 731    app_version: String,
 732    file_extension: String,
 733    os_name: String,
 734    os_version: String,
 735    release_channel: String,
 736    signed_in: bool,
 737    #[serde(serialize_with = "serialize_country_code")]
 738    country_code: String,
 739    region_code: String,
 740    city: String,
 741    time: i64,
 742    is_staff: Option<bool>,
 743    major: Option<i32>,
 744    minor: Option<i32>,
 745    patch: Option<i32>,
 746    checksum_matched: bool,
 747}
 748
 749impl InlineCompletionEventRow {
 750    fn from_event(
 751        event: InlineCompletionEvent,
 752        wrapper: &EventWrapper,
 753        body: &EventRequestBody,
 754        first_event_at: chrono::DateTime<chrono::Utc>,
 755        country_code: Option<String>,
 756        checksum_matched: bool,
 757    ) -> Self {
 758        let semver = body.semver();
 759        let time =
 760            first_event_at + chrono::Duration::milliseconds(wrapper.milliseconds_since_first_event);
 761
 762        Self {
 763            app_version: body.app_version.clone(),
 764            major: semver.map(|v| v.major() as i32),
 765            minor: semver.map(|v| v.minor() as i32),
 766            patch: semver.map(|v| v.patch() as i32),
 767            checksum_matched,
 768            release_channel: body.release_channel.clone().unwrap_or_default(),
 769            os_name: body.os_name.clone(),
 770            os_version: body.os_version.clone().unwrap_or_default(),
 771            installation_id: body.installation_id.clone().unwrap_or_default(),
 772            session_id: body.session_id.clone(),
 773            is_staff: body.is_staff,
 774            time: time.timestamp_millis(),
 775            file_extension: event.file_extension.unwrap_or_default(),
 776            signed_in: wrapper.signed_in,
 777            country_code: country_code.unwrap_or("XX".to_string()),
 778            region_code: "".to_string(),
 779            city: "".to_string(),
 780            provider: event.provider,
 781            suggestion_accepted: event.suggestion_accepted,
 782        }
 783    }
 784}
 785
 786#[derive(Serialize, Debug, clickhouse::Row)]
 787pub struct CallEventRow {
 788    // AppInfoBase
 789    app_version: String,
 790    major: Option<i32>,
 791    minor: Option<i32>,
 792    patch: Option<i32>,
 793    release_channel: String,
 794    os_name: String,
 795    os_version: String,
 796    checksum_matched: bool,
 797
 798    // ClientEventBase
 799    installation_id: String,
 800    session_id: Option<String>,
 801    is_staff: Option<bool>,
 802    time: i64,
 803
 804    // CallEventRow
 805    operation: String,
 806    room_id: Option<u64>,
 807    channel_id: Option<u64>,
 808}
 809
 810impl CallEventRow {
 811    fn from_event(
 812        event: CallEvent,
 813        wrapper: &EventWrapper,
 814        body: &EventRequestBody,
 815        first_event_at: chrono::DateTime<chrono::Utc>,
 816        checksum_matched: bool,
 817    ) -> Self {
 818        let semver = body.semver();
 819        let time =
 820            first_event_at + chrono::Duration::milliseconds(wrapper.milliseconds_since_first_event);
 821
 822        Self {
 823            app_version: body.app_version.clone(),
 824            major: semver.map(|v| v.major() as i32),
 825            minor: semver.map(|v| v.minor() as i32),
 826            patch: semver.map(|v| v.patch() as i32),
 827            checksum_matched,
 828            release_channel: body.release_channel.clone().unwrap_or_default(),
 829            os_name: body.os_name.clone(),
 830            os_version: body.os_version.clone().unwrap_or_default(),
 831            installation_id: body.installation_id.clone().unwrap_or_default(),
 832            session_id: body.session_id.clone(),
 833            is_staff: body.is_staff,
 834            time: time.timestamp_millis(),
 835            operation: event.operation,
 836            room_id: event.room_id,
 837            channel_id: event.channel_id,
 838        }
 839    }
 840}
 841
 842#[derive(Serialize, Debug, clickhouse::Row)]
 843pub struct AssistantEventRow {
 844    // AppInfoBase
 845    app_version: String,
 846    major: Option<i32>,
 847    minor: Option<i32>,
 848    patch: Option<i32>,
 849    checksum_matched: bool,
 850    release_channel: String,
 851    os_name: String,
 852    os_version: String,
 853
 854    // ClientEventBase
 855    installation_id: Option<String>,
 856    session_id: Option<String>,
 857    is_staff: Option<bool>,
 858    time: i64,
 859
 860    // AssistantEventRow
 861    conversation_id: String,
 862    kind: String,
 863    phase: String,
 864    model: String,
 865    response_latency_in_ms: Option<i64>,
 866    error_message: Option<String>,
 867}
 868
 869impl AssistantEventRow {
 870    fn from_event(
 871        event: AssistantEvent,
 872        wrapper: &EventWrapper,
 873        body: &EventRequestBody,
 874        first_event_at: chrono::DateTime<chrono::Utc>,
 875        checksum_matched: bool,
 876    ) -> Self {
 877        let semver = body.semver();
 878        let time =
 879            first_event_at + chrono::Duration::milliseconds(wrapper.milliseconds_since_first_event);
 880
 881        Self {
 882            app_version: body.app_version.clone(),
 883            major: semver.map(|v| v.major() as i32),
 884            minor: semver.map(|v| v.minor() as i32),
 885            patch: semver.map(|v| v.patch() as i32),
 886            checksum_matched,
 887            release_channel: body.release_channel.clone().unwrap_or_default(),
 888            os_name: body.os_name.clone(),
 889            os_version: body.os_version.clone().unwrap_or_default(),
 890            installation_id: body.installation_id.clone(),
 891            session_id: body.session_id.clone(),
 892            is_staff: body.is_staff,
 893            time: time.timestamp_millis(),
 894            conversation_id: event.conversation_id.unwrap_or_default(),
 895            kind: event.kind.to_string(),
 896            phase: event.phase.to_string(),
 897            model: event.model,
 898            response_latency_in_ms: event
 899                .response_latency
 900                .map(|latency| latency.as_millis() as i64),
 901            error_message: event.error_message,
 902        }
 903    }
 904}
 905
 906#[derive(Debug, clickhouse::Row, Serialize)]
 907pub struct CpuEventRow {
 908    installation_id: Option<String>,
 909    session_id: Option<String>,
 910    is_staff: Option<bool>,
 911    usage_as_percentage: f32,
 912    core_count: u32,
 913    app_version: String,
 914    release_channel: String,
 915    os_name: String,
 916    os_version: String,
 917    time: i64,
 918    // pub normalized_cpu_usage: f64, MATERIALIZED
 919    major: Option<i32>,
 920    minor: Option<i32>,
 921    patch: Option<i32>,
 922    checksum_matched: bool,
 923}
 924
 925impl CpuEventRow {
 926    fn from_event(
 927        event: CpuEvent,
 928        wrapper: &EventWrapper,
 929        body: &EventRequestBody,
 930        first_event_at: chrono::DateTime<chrono::Utc>,
 931        checksum_matched: bool,
 932    ) -> Self {
 933        let semver = body.semver();
 934        let time =
 935            first_event_at + chrono::Duration::milliseconds(wrapper.milliseconds_since_first_event);
 936
 937        Self {
 938            app_version: body.app_version.clone(),
 939            major: semver.map(|v| v.major() as i32),
 940            minor: semver.map(|v| v.minor() as i32),
 941            patch: semver.map(|v| v.patch() as i32),
 942            checksum_matched,
 943            release_channel: body.release_channel.clone().unwrap_or_default(),
 944            os_name: body.os_name.clone(),
 945            os_version: body.os_version.clone().unwrap_or_default(),
 946            installation_id: body.installation_id.clone(),
 947            session_id: body.session_id.clone(),
 948            is_staff: body.is_staff,
 949            time: time.timestamp_millis(),
 950            usage_as_percentage: event.usage_as_percentage,
 951            core_count: event.core_count,
 952        }
 953    }
 954}
 955
 956#[derive(Serialize, Debug, clickhouse::Row)]
 957pub struct MemoryEventRow {
 958    // AppInfoBase
 959    app_version: String,
 960    major: Option<i32>,
 961    minor: Option<i32>,
 962    patch: Option<i32>,
 963    checksum_matched: bool,
 964    release_channel: String,
 965    os_name: String,
 966    os_version: String,
 967
 968    // ClientEventBase
 969    installation_id: Option<String>,
 970    session_id: Option<String>,
 971    is_staff: Option<bool>,
 972    time: i64,
 973
 974    // MemoryEventRow
 975    memory_in_bytes: u64,
 976    virtual_memory_in_bytes: u64,
 977}
 978
 979impl MemoryEventRow {
 980    fn from_event(
 981        event: MemoryEvent,
 982        wrapper: &EventWrapper,
 983        body: &EventRequestBody,
 984        first_event_at: chrono::DateTime<chrono::Utc>,
 985        checksum_matched: bool,
 986    ) -> Self {
 987        let semver = body.semver();
 988        let time =
 989            first_event_at + chrono::Duration::milliseconds(wrapper.milliseconds_since_first_event);
 990
 991        Self {
 992            app_version: body.app_version.clone(),
 993            major: semver.map(|v| v.major() as i32),
 994            minor: semver.map(|v| v.minor() as i32),
 995            patch: semver.map(|v| v.patch() as i32),
 996            checksum_matched,
 997            release_channel: body.release_channel.clone().unwrap_or_default(),
 998            os_name: body.os_name.clone(),
 999            os_version: body.os_version.clone().unwrap_or_default(),
1000            installation_id: body.installation_id.clone(),
1001            session_id: body.session_id.clone(),
1002            is_staff: body.is_staff,
1003            time: time.timestamp_millis(),
1004            memory_in_bytes: event.memory_in_bytes,
1005            virtual_memory_in_bytes: event.virtual_memory_in_bytes,
1006        }
1007    }
1008}
1009
1010#[derive(Serialize, Debug, clickhouse::Row)]
1011pub struct AppEventRow {
1012    // AppInfoBase
1013    app_version: String,
1014    major: Option<i32>,
1015    minor: Option<i32>,
1016    patch: Option<i32>,
1017    checksum_matched: bool,
1018    release_channel: String,
1019    os_name: String,
1020    os_version: String,
1021
1022    // ClientEventBase
1023    installation_id: Option<String>,
1024    session_id: Option<String>,
1025    is_staff: Option<bool>,
1026    time: i64,
1027
1028    // AppEventRow
1029    operation: String,
1030}
1031
1032impl AppEventRow {
1033    fn from_event(
1034        event: AppEvent,
1035        wrapper: &EventWrapper,
1036        body: &EventRequestBody,
1037        first_event_at: chrono::DateTime<chrono::Utc>,
1038        checksum_matched: bool,
1039    ) -> Self {
1040        let semver = body.semver();
1041        let time =
1042            first_event_at + chrono::Duration::milliseconds(wrapper.milliseconds_since_first_event);
1043
1044        Self {
1045            app_version: body.app_version.clone(),
1046            major: semver.map(|v| v.major() as i32),
1047            minor: semver.map(|v| v.minor() as i32),
1048            patch: semver.map(|v| v.patch() as i32),
1049            checksum_matched,
1050            release_channel: body.release_channel.clone().unwrap_or_default(),
1051            os_name: body.os_name.clone(),
1052            os_version: body.os_version.clone().unwrap_or_default(),
1053            installation_id: body.installation_id.clone(),
1054            session_id: body.session_id.clone(),
1055            is_staff: body.is_staff,
1056            time: time.timestamp_millis(),
1057            operation: event.operation,
1058        }
1059    }
1060}
1061
1062#[derive(Serialize, Debug, clickhouse::Row)]
1063pub struct SettingEventRow {
1064    // AppInfoBase
1065    app_version: String,
1066    major: Option<i32>,
1067    minor: Option<i32>,
1068    patch: Option<i32>,
1069    checksum_matched: bool,
1070    release_channel: String,
1071    os_name: String,
1072    os_version: String,
1073
1074    // ClientEventBase
1075    installation_id: Option<String>,
1076    session_id: Option<String>,
1077    is_staff: Option<bool>,
1078    time: i64,
1079    // SettingEventRow
1080    setting: String,
1081    value: String,
1082}
1083
1084impl SettingEventRow {
1085    fn from_event(
1086        event: SettingEvent,
1087        wrapper: &EventWrapper,
1088        body: &EventRequestBody,
1089        first_event_at: chrono::DateTime<chrono::Utc>,
1090        checksum_matched: bool,
1091    ) -> Self {
1092        let semver = body.semver();
1093        let time =
1094            first_event_at + chrono::Duration::milliseconds(wrapper.milliseconds_since_first_event);
1095
1096        Self {
1097            app_version: body.app_version.clone(),
1098            major: semver.map(|v| v.major() as i32),
1099            minor: semver.map(|v| v.minor() as i32),
1100            checksum_matched,
1101            patch: semver.map(|v| v.patch() as i32),
1102            release_channel: body.release_channel.clone().unwrap_or_default(),
1103            os_name: body.os_name.clone(),
1104            os_version: body.os_version.clone().unwrap_or_default(),
1105            installation_id: body.installation_id.clone(),
1106            session_id: body.session_id.clone(),
1107            is_staff: body.is_staff,
1108            time: time.timestamp_millis(),
1109            setting: event.setting,
1110            value: event.value,
1111        }
1112    }
1113}
1114
1115#[derive(Serialize, Debug, clickhouse::Row)]
1116pub struct ExtensionEventRow {
1117    // AppInfoBase
1118    app_version: String,
1119    major: Option<i32>,
1120    minor: Option<i32>,
1121    patch: Option<i32>,
1122    checksum_matched: bool,
1123    release_channel: String,
1124    os_name: String,
1125    os_version: String,
1126
1127    // ClientEventBase
1128    installation_id: Option<String>,
1129    session_id: Option<String>,
1130    is_staff: Option<bool>,
1131    time: i64,
1132
1133    // ExtensionEventRow
1134    extension_id: Arc<str>,
1135    extension_version: Arc<str>,
1136    dev: bool,
1137    schema_version: Option<i32>,
1138    wasm_api_version: Option<String>,
1139}
1140
1141impl ExtensionEventRow {
1142    fn from_event(
1143        event: ExtensionEvent,
1144        wrapper: &EventWrapper,
1145        body: &EventRequestBody,
1146        extension_metadata: Option<ExtensionMetadata>,
1147        first_event_at: chrono::DateTime<chrono::Utc>,
1148        checksum_matched: bool,
1149    ) -> Self {
1150        let semver = body.semver();
1151        let time =
1152            first_event_at + chrono::Duration::milliseconds(wrapper.milliseconds_since_first_event);
1153
1154        Self {
1155            app_version: body.app_version.clone(),
1156            major: semver.map(|v| v.major() as i32),
1157            minor: semver.map(|v| v.minor() as i32),
1158            patch: semver.map(|v| v.patch() as i32),
1159            checksum_matched,
1160            release_channel: body.release_channel.clone().unwrap_or_default(),
1161            os_name: body.os_name.clone(),
1162            os_version: body.os_version.clone().unwrap_or_default(),
1163            installation_id: body.installation_id.clone(),
1164            session_id: body.session_id.clone(),
1165            is_staff: body.is_staff,
1166            time: time.timestamp_millis(),
1167            extension_id: event.extension_id,
1168            extension_version: event.version,
1169            dev: extension_metadata.is_none(),
1170            schema_version: extension_metadata
1171                .as_ref()
1172                .and_then(|metadata| metadata.manifest.schema_version),
1173            wasm_api_version: extension_metadata.as_ref().and_then(|metadata| {
1174                metadata
1175                    .manifest
1176                    .wasm_api_version
1177                    .as_ref()
1178                    .map(|version| version.to_string())
1179            }),
1180        }
1181    }
1182}
1183
1184#[derive(Serialize, Debug, clickhouse::Row)]
1185pub struct ReplEventRow {
1186    // AppInfoBase
1187    app_version: String,
1188    major: Option<i32>,
1189    minor: Option<i32>,
1190    patch: Option<i32>,
1191    checksum_matched: bool,
1192    release_channel: String,
1193    os_name: String,
1194    os_version: String,
1195
1196    // ClientEventBase
1197    installation_id: Option<String>,
1198    session_id: Option<String>,
1199    is_staff: Option<bool>,
1200    time: i64,
1201
1202    // ReplEventRow
1203    kernel_language: String,
1204    kernel_status: String,
1205    repl_session_id: String,
1206}
1207
1208impl ReplEventRow {
1209    fn from_event(
1210        event: ReplEvent,
1211        wrapper: &EventWrapper,
1212        body: &EventRequestBody,
1213        first_event_at: chrono::DateTime<chrono::Utc>,
1214        checksum_matched: bool,
1215    ) -> Self {
1216        let semver = body.semver();
1217        let time =
1218            first_event_at + chrono::Duration::milliseconds(wrapper.milliseconds_since_first_event);
1219
1220        Self {
1221            app_version: body.app_version.clone(),
1222            major: semver.map(|v| v.major() as i32),
1223            minor: semver.map(|v| v.minor() as i32),
1224            patch: semver.map(|v| v.patch() as i32),
1225            checksum_matched,
1226            release_channel: body.release_channel.clone().unwrap_or_default(),
1227            os_name: body.os_name.clone(),
1228            os_version: body.os_version.clone().unwrap_or_default(),
1229            installation_id: body.installation_id.clone(),
1230            session_id: body.session_id.clone(),
1231            is_staff: body.is_staff,
1232            time: time.timestamp_millis(),
1233            kernel_language: event.kernel_language,
1234            kernel_status: event.kernel_status,
1235            repl_session_id: event.repl_session_id,
1236        }
1237    }
1238}
1239
1240#[derive(Serialize, Debug, clickhouse::Row)]
1241pub struct EditEventRow {
1242    // AppInfoBase
1243    app_version: String,
1244    major: Option<i32>,
1245    minor: Option<i32>,
1246    patch: Option<i32>,
1247    checksum_matched: bool,
1248    release_channel: String,
1249    os_name: String,
1250    os_version: String,
1251
1252    // ClientEventBase
1253    installation_id: Option<String>,
1254    // Note: This column name has a typo in the ClickHouse table.
1255    #[serde(rename = "sesssion_id")]
1256    session_id: Option<String>,
1257    is_staff: Option<bool>,
1258    time: i64,
1259
1260    // EditEventRow
1261    period_start: i64,
1262    period_end: i64,
1263    environment: String,
1264    is_via_ssh: bool,
1265}
1266
1267impl EditEventRow {
1268    fn from_event(
1269        event: EditEvent,
1270        wrapper: &EventWrapper,
1271        body: &EventRequestBody,
1272        first_event_at: chrono::DateTime<chrono::Utc>,
1273        checksum_matched: bool,
1274    ) -> Self {
1275        let semver = body.semver();
1276        let time =
1277            first_event_at + chrono::Duration::milliseconds(wrapper.milliseconds_since_first_event);
1278
1279        let period_start = time - chrono::Duration::milliseconds(event.duration);
1280        let period_end = time;
1281
1282        Self {
1283            app_version: body.app_version.clone(),
1284            major: semver.map(|v| v.major() as i32),
1285            minor: semver.map(|v| v.minor() as i32),
1286            patch: semver.map(|v| v.patch() as i32),
1287            checksum_matched,
1288            release_channel: body.release_channel.clone().unwrap_or_default(),
1289            os_name: body.os_name.clone(),
1290            os_version: body.os_version.clone().unwrap_or_default(),
1291            installation_id: body.installation_id.clone(),
1292            session_id: body.session_id.clone(),
1293            is_staff: body.is_staff,
1294            time: time.timestamp_millis(),
1295            period_start: period_start.timestamp_millis(),
1296            period_end: period_end.timestamp_millis(),
1297            environment: event.environment,
1298            is_via_ssh: event.is_via_ssh,
1299        }
1300    }
1301}
1302
1303#[derive(Serialize, Debug, clickhouse::Row)]
1304pub struct ActionEventRow {
1305    // AppInfoBase
1306    app_version: String,
1307    major: Option<i32>,
1308    minor: Option<i32>,
1309    patch: Option<i32>,
1310    checksum_matched: bool,
1311    release_channel: String,
1312    os_name: String,
1313    os_version: String,
1314
1315    // ClientEventBase
1316    installation_id: Option<String>,
1317    // Note: This column name has a typo in the ClickHouse table.
1318    #[serde(rename = "sesssion_id")]
1319    session_id: Option<String>,
1320    is_staff: Option<bool>,
1321    time: i64,
1322    // ActionEventRow
1323    source: String,
1324    action: String,
1325}
1326
1327impl ActionEventRow {
1328    fn from_event(
1329        event: ActionEvent,
1330        wrapper: &EventWrapper,
1331        body: &EventRequestBody,
1332        first_event_at: chrono::DateTime<chrono::Utc>,
1333        checksum_matched: bool,
1334    ) -> Self {
1335        let semver = body.semver();
1336        let time =
1337            first_event_at + chrono::Duration::milliseconds(wrapper.milliseconds_since_first_event);
1338
1339        Self {
1340            app_version: body.app_version.clone(),
1341            major: semver.map(|v| v.major() as i32),
1342            minor: semver.map(|v| v.minor() as i32),
1343            patch: semver.map(|v| v.patch() as i32),
1344            checksum_matched,
1345            release_channel: body.release_channel.clone().unwrap_or_default(),
1346            os_name: body.os_name.clone(),
1347            os_version: body.os_version.clone().unwrap_or_default(),
1348            installation_id: body.installation_id.clone(),
1349            session_id: body.session_id.clone(),
1350            is_staff: body.is_staff,
1351            time: time.timestamp_millis(),
1352            source: event.source,
1353            action: event.action,
1354        }
1355    }
1356}
1357
1358pub fn calculate_json_checksum(app: Arc<AppState>, json: &impl AsRef<[u8]>) -> Option<Vec<u8>> {
1359    let checksum_seed = app.config.zed_client_checksum_seed.as_ref()?;
1360
1361    let mut summer = Sha256::new();
1362    summer.update(checksum_seed);
1363    summer.update(json);
1364    summer.update(checksum_seed);
1365    Some(summer.finalize().into_iter().collect())
1366}