claudy 0.2.1

Modern multi-provider launcher for Claude CLI
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
use crate::domain::analytics::{
    InsightsCacheEfficiency, InsightsCostAnalysis, InsightsDailyCost, InsightsOverview,
    InsightsPeriod, InsightsSummary, SessionCostHighlight,
};
use crate::domain::commands::AnalyticsAction;
use crate::domain::context::Context;
use crate::ports::analytics_ports::AnalyticsStore;

pub fn run_analytics(ctx: &mut Context, action: AnalyticsAction) -> anyhow::Result<i32> {
    match action {
        AnalyticsAction::Dashboard => run_dashboard(ctx),
        AnalyticsAction::Ingest { full, project } => run_ingest(ctx, full, project.as_deref()),
        AnalyticsAction::Recommend => run_recommend(ctx),
        AnalyticsAction::Export {
            format,
            project,
            days,
        } => run_export(ctx, &format, project.as_deref(), days),
        AnalyticsAction::SyncPricing => run_sync_pricing(ctx),
        AnalyticsAction::Insights {
            days,
            from,
            to,
            project,
        } => run_insights(
            ctx,
            days,
            from.as_deref(),
            to.as_deref(),
            project.as_deref(),
        ),
        AnalyticsAction::Recalculate => run_recalculate(ctx),
    }
}

fn run_dashboard(ctx: &mut Context) -> anyhow::Result<i32> {
    #[cfg(feature = "analytics-ui")]
    {
        ctx.output.info("Launching analytics dashboard...");
        crate::adapters::analytics::tauri::launch_dashboard(&ctx.paths)
    }
    #[cfg(not(feature = "analytics-ui"))]
    {
        ctx.output
            .warn("Analytics dashboard requires --features analytics-ui build");
        ctx.output
            .info("Use 'claudy analytics ingest' for CLI-only analytics");
        Ok(1)
    }
}

fn run_ingest(ctx: &mut Context, full: bool, project: Option<&str>) -> anyhow::Result<i32> {
    let db_path = &ctx.paths.analytics_db;

    // Auto-trigger pricing sync before ingestion; network errors are warnings only.
    match dirs::home_dir() {
        None => {
            ctx.output
                .warn("Pricing sync skipped: cannot determine home directory");
        }
        Some(home) => {
            let cp = home.join(".claudy").join("cache").join("models_dev.json");
            let store_result =
                crate::adapters::analytics::sqlite_store::SqliteAnalyticsStore::open(db_path)
                    .and_then(|s| s.initialize_schema().map(|_| s));
            match store_result {
                Ok(store) => {
                    match crate::adapters::analytics::pricing::sync::run_pricing_sync(&store, &cp) {
                        Ok(result) => {
                            for w in &result.warnings {
                                ctx.output.warn(&format!("Pricing sync: {w}"));
                            }
                            ctx.output.info(&format!(
                                "Pricing sync: {} models synced (source: {})",
                                result.models_synced,
                                result.source.label(),
                            ));
                        }
                        Err(_) => {
                            ctx.output
                                .warn("Pricing sync skipped: could not fetch pricing data");
                        }
                    }
                }
                Err(_) => {
                    ctx.output
                        .warn("Pricing sync skipped: analytics store unavailable");
                }
            }
        }
    }

    ctx.output.info("Starting ingestion...");

    let result = crate::adapters::analytics::ingestion::run_ingestion(db_path, full, project)?;

    ctx.output.info(&format!(
        "Ingestion complete in {}ms: {} files scanned, {} ingested | {} sessions, {} turns, {} token records, {} tool calls",
        result.elapsed_ms,
        result.files_scanned,
        result.files_ingested,
        result.sessions_created,
        result.turns_created,
        result.token_records_created,
        result.tool_calls_created,
    ));
    Ok(0)
}

fn run_recalculate(ctx: &mut Context) -> anyhow::Result<i32> {
    let db_path = &ctx.paths.analytics_db;
    ctx.output.info("Syncing pricing data...");
    let store = crate::adapters::analytics::sqlite_store::SqliteAnalyticsStore::open(db_path)?;
    store.initialize_schema()?;

    let cache_path = std::path::Path::new(&ctx.paths.cache_dir).join("models_dev.json");
    crate::adapters::analytics::pricing::sync::run_pricing_sync(&store, &cache_path)?;

    ctx.output
        .info("Recalculating costs with updated pricing...");
    let updated = store.recalculate_costs()?;

    ctx.output.info(&format!(
        "Recalculated {} token usage records and updated session totals.",
        updated
    ));
    Ok(0)
}

fn run_recommend(ctx: &mut Context) -> anyhow::Result<i32> {
    let db_path = &ctx.paths.analytics_db;
    let store = crate::adapters::analytics::sqlite_store::SqliteAnalyticsStore::open(db_path)?;
    store.initialize_schema()?;

    let recs = crate::adapters::analytics::recommendations::generate(&store)?;

    if recs.is_empty() {
        ctx.output.info(
            "No recommendations at this time. Run 'claudy analytics ingest' first to collect data.",
        );
        return Ok(0);
    }

    ctx.output.info(&format!("{} recommendations:", recs.len()));
    for (i, rec) in recs.iter().enumerate() {
        let severity = match &rec.severity {
            crate::domain::analytics::Severity::Info => "INFO",
            crate::domain::analytics::Severity::Warning => "WARN",
            crate::domain::analytics::Severity::Critical => "CRIT",
        };
        ctx.output.info(&format!(
            "  {}. [{}] {}{}",
            i + 1,
            severity,
            rec.title,
            rec.description,
        ));
        if let Some(action) = &rec.action {
            ctx.output.info(&format!("     Action: {}", action));
        }
    }
    Ok(0)
}

fn run_export(
    ctx: &mut Context,
    format: &str,
    project: Option<&str>,
    _days: u32,
) -> anyhow::Result<i32> {
    let db_path = &ctx.paths.analytics_db;
    let store = crate::adapters::analytics::sqlite_store::SqliteAnalyticsStore::open(db_path)?;

    let project_id = project
        .map(|p| store.get_project_by_encoded_dir(p))
        .transpose()?
        .flatten()
        .map(|p| p.id);

    let sessions = store.get_sessions(1000, None, project_id)?;

    match format {
        "json" => {
            let json = serde_json::to_string_pretty(&sessions)?;
            println!("{}", json);
        }
        "csv" => {
            println!(
                "session_uuid,project_id,cwd,model,started_at,ended_at,total_turns,total_cost_usd,total_duration_ms"
            );
            for s in &sessions {
                println!(
                    "{},{},{},{},{},{},{},{},{}",
                    s.session_uuid,
                    s.project_id,
                    s.cwd.as_deref().unwrap_or(""),
                    s.model.as_deref().unwrap_or(""),
                    s.started_at.as_deref().unwrap_or(""),
                    s.ended_at.as_deref().unwrap_or(""),
                    s.total_turns,
                    s.total_cost_usd,
                    s.total_duration_ms,
                );
            }
        }
        _ => {
            ctx.output.warn(&format!(
                "Unknown format '{}'. Use 'json' or 'csv'.",
                format
            ));
            return Ok(1);
        }
    }
    Ok(0)
}

fn run_sync_pricing(ctx: &mut Context) -> anyhow::Result<i32> {
    let db_path = &ctx.paths.analytics_db;
    let store = crate::adapters::analytics::sqlite_store::SqliteAnalyticsStore::open(db_path)?;
    store.initialize_schema()?;

    let cache_path = dirs::home_dir()
        .map(|h| h.join(".claudy").join("cache").join("models_dev.json"))
        .ok_or_else(|| anyhow::anyhow!("cannot determine home directory"))?;

    let result = crate::adapters::analytics::pricing::sync::run_pricing_sync(&store, &cache_path)?;

    for warning in &result.warnings {
        println!("Warning: {warning}");
    }

    println!(
        "Pricing sync complete: {} models synced (source: {})",
        result.models_synced,
        result.source.label(),
    );

    Ok(0)
}

fn run_insights(
    ctx: &mut Context,
    days: u32,
    from: Option<&str>,
    to: Option<&str>,
    project: Option<&str>,
) -> anyhow::Result<i32> {
    use chrono::{Local, NaiveDate, TimeDelta};

    let (effective_days, from_str, to_str) = match (from, to) {
        (Some(f), Some(t)) => {
            let from_date = NaiveDate::parse_from_str(f, "%Y-%m-%d")
                .map_err(|e| anyhow::anyhow!("Invalid --from date: {e}. Use YYYY-MM-DD."))?;
            let to_date = NaiveDate::parse_from_str(t, "%Y-%m-%d")
                .map_err(|e| anyhow::anyhow!("Invalid --to date: {e}. Use YYYY-MM-DD."))?;
            let diff = (to_date - from_date).num_days();
            if diff <= 0 {
                anyhow::bail!("--from must be before --to");
            }
            (diff as u32, f.to_string(), t.to_string())
        }
        (Some(_), None) | (None, Some(_)) => {
            anyhow::bail!("Both --from and --to must be provided together, or use --days");
        }
        (None, None) => {
            let today = Local::now().date_naive();
            let from_date = today - TimeDelta::days(days as i64);
            (
                days,
                from_date.format("%Y-%m-%d").to_string(),
                today.format("%Y-%m-%d").to_string(),
            )
        }
    };

    let db_path = &ctx.paths.analytics_db;
    let store = crate::adapters::analytics::sqlite_store::SqliteAnalyticsStore::open(db_path)?;
    store.initialize_schema()?;

    let project_id = project
        .map(|p| store.get_project_by_encoded_dir(p))
        .transpose()?
        .flatten()
        .map(|p| p.id);

    let dashboard = store.aggregate_dashboard_stats(effective_days, project_id)?;
    let cost_metrics = store.aggregate_cost_metrics(effective_days, project_id)?;
    let token_trends = store.aggregate_token_trends(effective_days, project_id)?;
    let tool_dist = store.aggregate_tool_distribution(Some(effective_days), project_id)?;

    let daily_costs: Vec<InsightsDailyCost> = token_trends
        .into_iter()
        .map(|p| InsightsDailyCost {
            date: p.date,
            cost_usd: p.total_cost_usd,
            sessions: p.session_count,
            model: p.model,
        })
        .collect();

    let mut top_tools = tool_dist;
    top_tools.sort_by_key(|b| std::cmp::Reverse(b.call_count));
    top_tools.truncate(10);

    let notable_sessions: Vec<SessionCostHighlight> = {
        let all_sessions = store.get_sessions(100, Some(effective_days), project_id)?;
        let mut sorted: Vec<_> = all_sessions.into_iter().collect();
        sorted.sort_by(|a, b| {
            b.total_cost_usd
                .partial_cmp(&a.total_cost_usd)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        let projects = store.list_projects()?;
        let proj_map: std::collections::HashMap<i64, String> = projects
            .into_iter()
            .map(|p| (p.id, p.display_name))
            .collect();

        sorted
            .into_iter()
            .take(5)
            .map(|s| SessionCostHighlight {
                session_uuid: s.session_uuid,
                project_name: proj_map.get(&s.project_id).cloned().unwrap_or_default(),
                cost_usd: s.total_cost_usd,
                turns: s.total_turns,
                model: s.model,
                started_at: s.started_at,
            })
            .collect()
    };

    let summary = InsightsSummary {
        period: InsightsPeriod {
            from: from_str,
            to: to_str,
            days: effective_days,
        },
        overview: InsightsOverview {
            total_sessions: dashboard.total_sessions,
            total_cost_usd: dashboard.total_cost_usd,
            total_turns: dashboard.total_turns,
            avg_tokens_per_session: dashboard.avg_tokens_per_session,
            most_used_model: dashboard.most_used_model,
        },
        daily_costs,
        model_distribution: cost_metrics.by_model,
        tool_usage: top_tools,
        notable_sessions,
        cost_analysis: InsightsCostAnalysis {
            total_cost_usd: cost_metrics.total_cost_usd,
            avg_cost_per_session: cost_metrics.avg_cost_per_session,
            avg_cost_per_turn: cost_metrics.avg_cost_per_turn,
            weekly_avg_cost: cost_metrics.weekly_avg_cost,
            cache_savings_usd: cost_metrics.cache_savings_usd,
            estimated_cost_portion: cost_metrics.estimated_cost_portion,
        },
        cache_efficiency: InsightsCacheEfficiency {
            hit_ratio: dashboard.cache_hit_ratio,
            savings_usd: cost_metrics.cache_savings_usd,
        },
    };

    let json = serde_json::to_string(&summary)?;
    println!("{json}");
    Ok(0)
}

#[cfg(test)]
mod tests {
    use crate::adapters::analytics::pricing::sync::run_pricing_sync;
    use crate::adapters::analytics::sqlite_store::SqliteAnalyticsStore;
    use crate::ports::analytics_ports::{AnalyticsStore, PricingStore};
    use tempfile::{NamedTempFile, TempDir};

    fn write_models_dev_cache(dir: &TempDir) -> std::path::PathBuf {
        let cache_path = dir.path().join("models_dev.json");
        let json = r#"[{"id":"claude-haiku-4-5","name":"Claude Haiku 4.5","cost":{"input":0.80,"output":4.00,"cache_read":0.08,"cache_write":1.00}}]"#;
        std::fs::write(&cache_path, json).unwrap();
        cache_path
    }

    /// Verifies the pricing-sync contract used by run_ingest's auto-trigger path:
    /// run_pricing_sync with a valid cache file must sync ≥1 model into the store (R7, AC7).
    #[test]
    fn test_pricing_sync_contract_used_by_ingest() {
        let tmp_dir = TempDir::new().unwrap();
        let cache_path = write_models_dev_cache(&tmp_dir);
        let db_file = NamedTempFile::new().unwrap();
        let store = SqliteAnalyticsStore::open(db_file.path().to_str().unwrap()).unwrap();
        store.initialize_schema().unwrap();

        let result = run_pricing_sync(&store, &cache_path).unwrap();

        assert!(
            result.models_synced >= 1,
            "at least one model should be synced"
        );
        assert!(
            result.warnings.is_empty(),
            "no warnings expected with valid cache"
        );

        let rows = store.list_model_pricing().unwrap();
        assert!(
            !rows.is_empty(),
            "model_pricing table must not be empty after sync"
        );
    }

    /// AC7: when run_ingest's auto-sync path succeeds, the model_pricing table is populated.
    /// Exercises the full sync → store path that run_ingest wires up internally.
    #[test]
    fn test_auto_sync_path_populates_model_pricing_table() {
        let tmp_dir = TempDir::new().unwrap();
        let cache_path = write_models_dev_cache(&tmp_dir);
        let db_file = NamedTempFile::new().unwrap();
        let store = SqliteAnalyticsStore::open(db_file.path().to_str().unwrap()).unwrap();
        store.initialize_schema().unwrap();

        run_pricing_sync(&store, &cache_path).unwrap();

        let rows = store.list_model_pricing().unwrap();
        assert!(
            !rows.is_empty(),
            "auto-sync must populate model_pricing (AC7)"
        );
        assert!(
            rows.iter().any(|r| r.model_id == "claude-haiku-4-5"),
            "synced model must appear in model_pricing table"
        );
    }
}