File size: 18,118 Bytes
a21c316 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | use rusqlite::{params, Connection};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
/// Aggregated token statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenStatsAggregated {
pub period: String, // e.g., "2024-01-15 14:00" for hourly, "2024-01-15" for daily
pub total_input_tokens: u64,
pub total_output_tokens: u64,
pub total_tokens: u64,
pub request_count: u64,
}
/// Per-account token statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountTokenStats {
pub account_email: String,
pub total_input_tokens: u64,
pub total_output_tokens: u64,
pub total_tokens: u64,
pub request_count: u64,
}
/// Summary statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenStatsSummary {
pub total_input_tokens: u64,
pub total_output_tokens: u64,
pub total_tokens: u64,
pub total_requests: u64,
pub unique_accounts: u64,
}
/// Per-model token statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelTokenStats {
pub model: String,
pub total_input_tokens: u64,
pub total_output_tokens: u64,
pub total_tokens: u64,
pub request_count: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelTrendPoint {
pub period: String,
pub model_data: std::collections::HashMap<String, u64>,
}
/// Account trend data point (for stacked area chart)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountTrendPoint {
pub period: String,
pub account_data: std::collections::HashMap<String, u64>,
}
pub(crate) fn get_db_path() -> Result<PathBuf, String> {
let data_dir = crate::modules::account::get_data_dir()?;
Ok(data_dir.join("token_stats.db"))
}
fn connect_db() -> Result<Connection, String> {
let db_path = get_db_path()?;
let conn = Connection::open(db_path).map_err(|e| e.to_string())?;
// Enable WAL mode for better concurrency
conn.pragma_update(None, "journal_mode", "WAL")
.map_err(|e| e.to_string())?;
conn.pragma_update(None, "busy_timeout", 5000)
.map_err(|e| e.to_string())?;
conn.pragma_update(None, "synchronous", "NORMAL")
.map_err(|e| e.to_string())?;
Ok(conn)
}
/// Initialize the token stats database
pub fn init_db() -> Result<(), String> {
let conn = connect_db()?;
// Create main usage table
conn.execute(
"CREATE TABLE IF NOT EXISTS token_usage (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp INTEGER NOT NULL,
account_email TEXT NOT NULL,
model TEXT NOT NULL,
input_tokens INTEGER NOT NULL DEFAULT 0,
output_tokens INTEGER NOT NULL DEFAULT 0,
total_tokens INTEGER NOT NULL DEFAULT 0
)",
[],
)
.map_err(|e| e.to_string())?;
// Create indexes for efficient queries
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_token_timestamp ON token_usage (timestamp DESC)",
[],
)
.map_err(|e| e.to_string())?;
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_token_account ON token_usage (account_email)",
[],
)
.map_err(|e| e.to_string())?;
// Create hourly aggregation table for fast queries
conn.execute(
"CREATE TABLE IF NOT EXISTS token_stats_hourly (
hour_bucket TEXT NOT NULL,
account_email TEXT NOT NULL,
total_input_tokens INTEGER NOT NULL DEFAULT 0,
total_output_tokens INTEGER NOT NULL DEFAULT 0,
total_tokens INTEGER NOT NULL DEFAULT 0,
request_count INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (hour_bucket, account_email)
)",
[],
)
.map_err(|e| e.to_string())?;
Ok(())
}
/// Record token usage from a request
pub fn record_usage(
account_email: &str,
model: &str,
input_tokens: u32,
output_tokens: u32,
) -> Result<(), String> {
let conn = connect_db()?;
let timestamp = chrono::Local::now().timestamp();
let total_tokens = input_tokens + output_tokens;
// Insert into raw usage table
conn.execute(
"INSERT INTO token_usage (timestamp, account_email, model, input_tokens, output_tokens, total_tokens)
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
params![timestamp, account_email, model, input_tokens, output_tokens, total_tokens],
).map_err(|e| e.to_string())?;
let hour_bucket = chrono::Local::now().format("%Y-%m-%d %H:00").to_string();
conn.execute(
"INSERT INTO token_stats_hourly (hour_bucket, account_email, total_input_tokens, total_output_tokens, total_tokens, request_count)
VALUES (?1, ?2, ?3, ?4, ?5, 1)
ON CONFLICT(hour_bucket, account_email) DO UPDATE SET
total_input_tokens = total_input_tokens + ?3,
total_output_tokens = total_output_tokens + ?4,
total_tokens = total_tokens + ?5,
request_count = request_count + 1",
params![hour_bucket, account_email, input_tokens, output_tokens, total_tokens],
).map_err(|e| e.to_string())?;
Ok(())
}
/// Get hourly aggregated stats for a time range
pub fn get_hourly_stats(hours: i64) -> Result<Vec<TokenStatsAggregated>, String> {
let conn = connect_db()?;
let cutoff = chrono::Local::now() - chrono::Duration::hours(hours);
let cutoff_bucket = cutoff.format("%Y-%m-%d %H:00").to_string();
let mut stmt = conn
.prepare(
"SELECT hour_bucket,
SUM(total_input_tokens) as input,
SUM(total_output_tokens) as output,
SUM(total_tokens) as total,
SUM(request_count) as count
FROM token_stats_hourly
WHERE hour_bucket >= ?1
GROUP BY hour_bucket
ORDER BY hour_bucket ASC",
)
.map_err(|e| e.to_string())?;
let rows = stmt
.query_map([cutoff_bucket], |row| {
Ok(TokenStatsAggregated {
period: row.get(0)?,
total_input_tokens: row.get(1)?,
total_output_tokens: row.get(2)?,
total_tokens: row.get(3)?,
request_count: row.get(4)?,
})
})
.map_err(|e| e.to_string())?;
let mut result = Vec::new();
for row in rows {
result.push(row.map_err(|e| e.to_string())?);
}
Ok(result)
}
/// Get daily aggregated stats for a time range
pub fn get_daily_stats(days: i64) -> Result<Vec<TokenStatsAggregated>, String> {
let conn = connect_db()?;
let cutoff = chrono::Local::now() - chrono::Duration::days(days);
let cutoff_bucket = cutoff.format("%Y-%m-%d").to_string();
let mut stmt = conn
.prepare(
"SELECT substr(hour_bucket, 1, 10) as day_bucket,
SUM(total_input_tokens) as input,
SUM(total_output_tokens) as output,
SUM(total_tokens) as total,
SUM(request_count) as count
FROM token_stats_hourly
WHERE substr(hour_bucket, 1, 10) >= ?1
GROUP BY day_bucket
ORDER BY day_bucket ASC",
)
.map_err(|e| e.to_string())?;
let rows = stmt
.query_map([cutoff_bucket], |row| {
Ok(TokenStatsAggregated {
period: row.get(0)?,
total_input_tokens: row.get(1)?,
total_output_tokens: row.get(2)?,
total_tokens: row.get(3)?,
request_count: row.get(4)?,
})
})
.map_err(|e| e.to_string())?;
let mut result = Vec::new();
for row in rows {
result.push(row.map_err(|e| e.to_string())?);
}
Ok(result)
}
/// Get weekly aggregated stats
pub fn get_weekly_stats(weeks: i64) -> Result<Vec<TokenStatsAggregated>, String> {
let conn = connect_db()?;
let cutoff = chrono::Local::now() - chrono::Duration::weeks(weeks);
let cutoff_timestamp = cutoff.timestamp();
let mut stmt = conn
.prepare(
"SELECT strftime('%Y-W%W', datetime(timestamp, 'unixepoch', 'localtime')) as week_bucket,
SUM(input_tokens) as input,
SUM(output_tokens) as output,
SUM(total_tokens) as total,
COUNT(*) as count
FROM token_usage
WHERE timestamp >= ?1
GROUP BY week_bucket
ORDER BY week_bucket ASC",
)
.map_err(|e| e.to_string())?;
let rows = stmt
.query_map([cutoff_timestamp], |row| {
Ok(TokenStatsAggregated {
period: row.get(0)?,
total_input_tokens: row.get(1)?,
total_output_tokens: row.get(2)?,
total_tokens: row.get(3)?,
request_count: row.get(4)?,
})
})
.map_err(|e| e.to_string())?;
let mut result = Vec::new();
for row in rows {
result.push(row.map_err(|e| e.to_string())?);
}
Ok(result)
}
/// Get per-account statistics for a time range
pub fn get_account_stats(hours: i64) -> Result<Vec<AccountTokenStats>, String> {
let conn = connect_db()?;
let cutoff = chrono::Local::now() - chrono::Duration::hours(hours);
let cutoff_bucket = cutoff.format("%Y-%m-%d %H:00").to_string();
let mut stmt = conn
.prepare(
"SELECT account_email,
SUM(total_input_tokens) as input,
SUM(total_output_tokens) as output,
SUM(total_tokens) as total,
SUM(request_count) as count
FROM token_stats_hourly
WHERE hour_bucket >= ?1
GROUP BY account_email
ORDER BY total DESC",
)
.map_err(|e| e.to_string())?;
let rows = stmt
.query_map([cutoff_bucket], |row| {
Ok(AccountTokenStats {
account_email: row.get(0)?,
total_input_tokens: row.get(1)?,
total_output_tokens: row.get(2)?,
total_tokens: row.get(3)?,
request_count: row.get(4)?,
})
})
.map_err(|e| e.to_string())?;
let mut result = Vec::new();
for row in rows {
result.push(row.map_err(|e| e.to_string())?);
}
Ok(result)
}
/// Get summary statistics for a time range
pub fn get_summary_stats(hours: i64) -> Result<TokenStatsSummary, String> {
let conn = connect_db()?;
let cutoff = chrono::Local::now() - chrono::Duration::hours(hours);
let cutoff_bucket = cutoff.format("%Y-%m-%d %H:00").to_string();
let (total_input, total_output, total, requests): (u64, u64, u64, u64) = conn
.query_row(
"SELECT COALESCE(SUM(total_input_tokens), 0),
COALESCE(SUM(total_output_tokens), 0),
COALESCE(SUM(total_tokens), 0),
COALESCE(SUM(request_count), 0)
FROM token_stats_hourly
WHERE hour_bucket >= ?1",
[&cutoff_bucket],
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
)
.map_err(|e| e.to_string())?;
let unique_accounts: u64 = conn
.query_row(
"SELECT COUNT(DISTINCT account_email) FROM token_stats_hourly WHERE hour_bucket >= ?1",
[&cutoff_bucket],
|row| row.get(0),
)
.map_err(|e| e.to_string())?;
Ok(TokenStatsSummary {
total_input_tokens: total_input,
total_output_tokens: total_output,
total_tokens: total,
total_requests: requests,
unique_accounts,
})
}
pub fn get_model_stats(hours: i64) -> Result<Vec<ModelTokenStats>, String> {
let conn = connect_db()?;
let cutoff = chrono::Local::now().timestamp() - (hours * 3600);
let mut stmt = conn
.prepare(
"SELECT model,
SUM(input_tokens) as input,
SUM(output_tokens) as output,
SUM(total_tokens) as total,
COUNT(*) as count
FROM token_usage
WHERE timestamp >= ?1
GROUP BY model
ORDER BY total DESC",
)
.map_err(|e| e.to_string())?;
let rows = stmt
.query_map([cutoff], |row| {
Ok(ModelTokenStats {
model: row.get(0)?,
total_input_tokens: row.get(1)?,
total_output_tokens: row.get(2)?,
total_tokens: row.get(3)?,
request_count: row.get(4)?,
})
})
.map_err(|e| e.to_string())?;
let mut result = Vec::new();
for row in rows {
result.push(row.map_err(|e| e.to_string())?);
}
Ok(result)
}
pub fn get_model_trend_hourly(hours: i64) -> Result<Vec<ModelTrendPoint>, String> {
let conn = connect_db()?;
let cutoff = chrono::Local::now().timestamp() - (hours * 3600);
let mut stmt = conn
.prepare(
"SELECT strftime('%Y-%m-%d %H:00', datetime(timestamp, 'unixepoch', 'localtime')) as hour_bucket,
model,
SUM(total_tokens) as total
FROM token_usage
WHERE timestamp >= ?1
GROUP BY hour_bucket, model
ORDER BY hour_bucket ASC",
)
.map_err(|e| e.to_string())?;
let mut trend_map: std::collections::BTreeMap<String, std::collections::HashMap<String, u64>> =
std::collections::BTreeMap::new();
let rows = stmt
.query_map([cutoff], |row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
row.get::<_, u64>(2)?,
))
})
.map_err(|e| e.to_string())?;
for row in rows {
let (period, model, total) = row.map_err(|e| e.to_string())?;
trend_map.entry(period).or_default().insert(model, total);
}
Ok(trend_map
.into_iter()
.map(|(period, model_data)| ModelTrendPoint { period, model_data })
.collect())
}
pub fn get_model_trend_daily(days: i64) -> Result<Vec<ModelTrendPoint>, String> {
let conn = connect_db()?;
let cutoff = chrono::Local::now().timestamp() - (days * 24 * 3600);
let mut stmt = conn
.prepare(
"SELECT strftime('%Y-%m-%d', datetime(timestamp, 'unixepoch', 'localtime')) as day_bucket,
model,
SUM(total_tokens) as total
FROM token_usage
WHERE timestamp >= ?1
GROUP BY day_bucket, model
ORDER BY day_bucket ASC",
)
.map_err(|e| e.to_string())?;
let mut trend_map: std::collections::BTreeMap<String, std::collections::HashMap<String, u64>> =
std::collections::BTreeMap::new();
let rows = stmt
.query_map([cutoff], |row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
row.get::<_, u64>(2)?,
))
})
.map_err(|e| e.to_string())?;
for row in rows {
let (period, model, total) = row.map_err(|e| e.to_string())?;
trend_map.entry(period).or_default().insert(model, total);
}
Ok(trend_map
.into_iter()
.map(|(period, model_data)| ModelTrendPoint { period, model_data })
.collect())
}
pub fn get_account_trend_hourly(hours: i64) -> Result<Vec<AccountTrendPoint>, String> {
let conn = connect_db()?;
let cutoff = chrono::Local::now().timestamp() - (hours * 3600);
let mut stmt = conn
.prepare(
"SELECT strftime('%Y-%m-%d %H:00', datetime(timestamp, 'unixepoch', 'localtime')) as hour_bucket,
account_email,
SUM(total_tokens) as total
FROM token_usage
WHERE timestamp >= ?1
GROUP BY hour_bucket, account_email
ORDER BY hour_bucket ASC",
)
.map_err(|e| e.to_string())?;
let mut trend_map: std::collections::BTreeMap<String, std::collections::HashMap<String, u64>> =
std::collections::BTreeMap::new();
let rows = stmt
.query_map([cutoff], |row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
row.get::<_, u64>(2)?,
))
})
.map_err(|e| e.to_string())?;
for row in rows {
let (period, account, total) = row.map_err(|e| e.to_string())?;
trend_map.entry(period).or_default().insert(account, total);
}
Ok(trend_map
.into_iter()
.map(|(period, account_data)| AccountTrendPoint {
period,
account_data,
})
.collect())
}
pub fn get_account_trend_daily(days: i64) -> Result<Vec<AccountTrendPoint>, String> {
let conn = connect_db()?;
let cutoff = chrono::Local::now().timestamp() - (days * 24 * 3600);
let mut stmt = conn
.prepare(
"SELECT strftime('%Y-%m-%d', datetime(timestamp, 'unixepoch', 'localtime')) as day_bucket,
account_email,
SUM(total_tokens) as total
FROM token_usage
WHERE timestamp >= ?1
GROUP BY day_bucket, account_email
ORDER BY day_bucket ASC",
)
.map_err(|e| e.to_string())?;
let mut trend_map: std::collections::BTreeMap<String, std::collections::HashMap<String, u64>> =
std::collections::BTreeMap::new();
let rows = stmt
.query_map([cutoff], |row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
row.get::<_, u64>(2)?,
))
})
.map_err(|e| e.to_string())?;
for row in rows {
let (period, account, total) = row.map_err(|e| e.to_string())?;
trend_map.entry(period).or_default().insert(account, total);
}
Ok(trend_map
.into_iter()
.map(|(period, account_data)| AccountTrendPoint {
period,
account_data,
})
.collect())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_record_and_query() {
// This would need a test database setup
// For now, just verify the module compiles
assert!(true);
}
}
|