telemetry.rs

 1use anyhow::{Context, Result};
 2use serde::Serialize;
 3
 4use crate::clickhouse::write_to_table;
 5
 6#[derive(Serialize, Debug, clickhouse::Row)]
 7pub struct LlmUsageEventRow {
 8    pub time: i64,
 9    pub user_id: i32,
10    pub is_staff: bool,
11    pub plan: String,
12    pub model: String,
13    pub provider: String,
14    pub input_token_count: u64,
15    pub output_token_count: u64,
16    pub requests_this_minute: u64,
17    pub tokens_this_minute: u64,
18    pub tokens_this_day: u64,
19    pub input_tokens_this_month: u64,
20    pub output_tokens_this_month: u64,
21    pub spending_this_month: u64,
22    pub lifetime_spending: u64,
23}
24
25#[derive(Serialize, Debug, clickhouse::Row)]
26pub struct LlmRateLimitEventRow {
27    pub time: i64,
28    pub user_id: i32,
29    pub is_staff: bool,
30    pub plan: String,
31    pub model: String,
32    pub provider: String,
33    pub usage_measure: String,
34    pub requests_this_minute: u64,
35    pub tokens_this_minute: u64,
36    pub tokens_this_day: u64,
37    pub users_in_recent_minutes: u64,
38    pub users_in_recent_days: u64,
39    pub max_requests_per_minute: u64,
40    pub max_tokens_per_minute: u64,
41    pub max_tokens_per_day: u64,
42}
43
44pub async fn report_llm_usage(client: &clickhouse::Client, row: LlmUsageEventRow) -> Result<()> {
45    const LLM_USAGE_EVENTS_TABLE: &str = "llm_usage_events";
46    write_to_table(LLM_USAGE_EVENTS_TABLE, &[row], client)
47        .await
48        .with_context(|| format!("failed to upload to table '{LLM_USAGE_EVENTS_TABLE}'"))?;
49    Ok(())
50}
51
52pub async fn report_llm_rate_limit(
53    client: &clickhouse::Client,
54    row: LlmRateLimitEventRow,
55) -> Result<()> {
56    const LLM_RATE_LIMIT_EVENTS_TABLE: &str = "llm_rate_limit_events";
57    write_to_table(LLM_RATE_LIMIT_EVENTS_TABLE, &[row], client)
58        .await
59        .with_context(|| format!("failed to upload to table '{LLM_RATE_LIMIT_EVENTS_TABLE}'"))?;
60    Ok(())
61}