feat: add persistent lifetime stats and sync achievements

- Add lifetime stats persistence to Rust backend
- Sync achievement state between frontend and backend on startup
- Add commands for loading/saving stats to disk
- Expand achievement definitions with 150+ new achievements
- Fix stats store to properly track total vs session metrics
This commit is contained in:
2026-01-25 20:06:36 -08:00
committed by Naomi Carrigan
parent 8a19f35922
commit 42c3b4ee83
7 changed files with 1462 additions and 106 deletions
+13
View File
@@ -102,6 +102,19 @@ pub async fn get_usage_stats(
manager.get_usage_stats(&conversation_id)
}
/// Load persisted lifetime stats from store (no bridge required)
#[tauri::command]
pub async fn get_persisted_stats(app: AppHandle) -> Result<UsageStats, String> {
let mut stats = UsageStats::new();
// Load persisted stats if available
if let Some(persisted) = crate::stats::load_stats(&app).await {
stats.apply_persisted(persisted);
}
Ok(stats)
}
#[tauri::command]
pub async fn validate_directory(
path: String,
+1
View File
@@ -95,6 +95,7 @@ pub fn run() {
get_config,
save_config,
get_usage_stats,
get_persisted_stats,
load_saved_achievements,
answer_question,
send_windows_notification,
+106
View File
@@ -3,6 +3,7 @@ use chrono::{Local, Timelike};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Instant;
use tauri_plugin_store::StoreExt;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct UsageStats {
@@ -241,6 +242,111 @@ pub struct StatsUpdateEvent {
pub stats: UsageStats,
}
/// Serializable struct for persisting only lifetime (total) stats
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PersistedStats {
pub total_input_tokens: u64,
pub total_output_tokens: u64,
pub total_cost_usd: f64,
pub messages_exchanged: u64,
pub code_blocks_generated: u64,
pub files_edited: u64,
pub files_created: u64,
pub tools_usage: HashMap<String, u64>,
pub sessions_started: u64,
pub consecutive_days: u64,
pub total_days_used: u64,
pub morning_sessions: u64,
pub night_sessions: u64,
pub last_session_date: Option<String>,
}
impl From<&UsageStats> for PersistedStats {
fn from(stats: &UsageStats) -> Self {
PersistedStats {
total_input_tokens: stats.total_input_tokens,
total_output_tokens: stats.total_output_tokens,
total_cost_usd: stats.total_cost_usd,
messages_exchanged: stats.messages_exchanged,
code_blocks_generated: stats.code_blocks_generated,
files_edited: stats.files_edited,
files_created: stats.files_created,
tools_usage: stats.tools_usage.clone(),
sessions_started: stats.sessions_started,
consecutive_days: stats.consecutive_days,
total_days_used: stats.total_days_used,
morning_sessions: stats.morning_sessions,
night_sessions: stats.night_sessions,
last_session_date: stats.last_session_date.clone(),
}
}
}
impl UsageStats {
/// Apply persisted stats to restore lifetime totals
pub fn apply_persisted(&mut self, persisted: PersistedStats) {
self.total_input_tokens = persisted.total_input_tokens;
self.total_output_tokens = persisted.total_output_tokens;
self.total_cost_usd = persisted.total_cost_usd;
self.messages_exchanged = persisted.messages_exchanged;
self.code_blocks_generated = persisted.code_blocks_generated;
self.files_edited = persisted.files_edited;
self.files_created = persisted.files_created;
self.tools_usage = persisted.tools_usage;
self.sessions_started = persisted.sessions_started;
self.consecutive_days = persisted.consecutive_days;
self.total_days_used = persisted.total_days_used;
self.morning_sessions = persisted.morning_sessions;
self.night_sessions = persisted.night_sessions;
self.last_session_date = persisted.last_session_date;
}
}
/// Save lifetime stats to persistent store
pub async fn save_stats(app: &tauri::AppHandle, stats: &UsageStats) -> Result<(), String> {
let store = app.store("stats.json").map_err(|e| e.to_string())?;
let persisted = PersistedStats::from(stats);
println!("Saving stats: {:?}", persisted);
store.set(
"lifetime_stats",
serde_json::to_value(persisted).map_err(|e| e.to_string())?,
);
store.save().map_err(|e| e.to_string())?;
println!("Stats saved successfully");
Ok(())
}
/// Load lifetime stats from persistent store
pub async fn load_stats(app: &tauri::AppHandle) -> Option<PersistedStats> {
println!("Loading stats from store...");
let store = match app.store("stats.json") {
Ok(s) => s,
Err(e) => {
println!("Failed to open stats store: {}", e);
return None;
}
};
if let Some(stats_value) = store.get("lifetime_stats") {
println!("Found lifetime stats in store: {:?}", stats_value);
if let Ok(persisted) = serde_json::from_value::<PersistedStats>(stats_value.clone()) {
println!("Loaded lifetime stats successfully");
return Some(persisted);
} else {
println!("Failed to parse lifetime stats");
}
} else {
println!("No lifetime stats found in store");
}
None
}
#[cfg(test)]
mod tests {
use super::*;
+38 -3
View File
@@ -112,7 +112,7 @@ impl WslBridge {
return Err("Process already running".to_string());
}
// Load saved achievements when starting a new session
// Load saved achievements and stats when starting a new session
let app_clone = app.clone();
let stats = self.stats.clone();
tauri::async_runtime::spawn(async move {
@@ -122,7 +122,17 @@ impl WslBridge {
"Loaded {} unlocked achievements",
achievements.unlocked.len()
);
stats.write().achievements = achievements;
println!("Loading saved stats...");
let persisted_stats = crate::stats::load_stats(&app_clone).await;
let mut stats_guard = stats.write();
stats_guard.achievements = achievements;
if let Some(persisted) = persisted_stats {
println!("Applying persisted lifetime stats");
stats_guard.apply_persisted(persisted);
}
});
let working_dir = &options.working_dir;
@@ -440,6 +450,18 @@ impl WslBridge {
self.session_id = None;
self.mcp_config_file = None; // Temp file is automatically deleted when dropped
// Save lifetime stats before resetting session
let stats_snapshot = self.stats.read().clone();
let app_clone = app.clone();
tauri::async_runtime::spawn(async move {
println!("Saving stats on session stop...");
if let Err(e) = crate::stats::save_stats(&app_clone, &stats_snapshot).await {
eprintln!("Failed to save stats: {}", e);
} else {
println!("Stats saved successfully on session stop");
}
});
// Reset session stats on explicit disconnect
self.stats.write().reset_session();
@@ -733,10 +755,23 @@ fn process_json_line(
let current_stats = stats.read().clone();
let stats_event = StatsUpdateEvent {
stats: current_stats,
stats: current_stats.clone(),
};
let _ = app.emit("claude:stats", stats_event);
// Save stats periodically (every 10 messages to avoid excessive disk writes)
if current_stats.session_messages_exchanged.is_multiple_of(10)
&& current_stats.session_messages_exchanged > 0
{
let app_handle = app.clone();
tauri::async_runtime::spawn(async move {
println!("Periodic stats save (every 10 messages)...");
if let Err(e) = crate::stats::save_stats(&app_handle, &current_stats).await {
eprintln!("Failed to save stats: {}", e);
}
});
}
// Only emit error results - success content is already sent via Assistant message
if subtype != "success" {
if let Some(text) = result {