vibe_coding_tracker 0.10.2

Vibe Coding Tracker - AI coding assistant telemetry/usage parser, aggregate JSONL events into CodeAnalysis results
Documentation
use crate::cli::TimeRange;
use crate::constants::{FastHashMap, capacity};
use crate::models::{CodeAnalysis, ExtensionType, ProviderActiveDays};
use crate::session::parser::parse_session_file_as;
use crate::session::state::ParseMode;
use crate::utils::{
    COPILOT_SESSION_MAX_DEPTH, collect_files_with_max_depth, is_claude_session_file,
    is_codex_session_file, is_copilot_session_file, is_gemini_session_file,
};
use anyhow::Result;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::Path;

/// Single row of aggregated metrics grouped by model
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AggregatedAnalysisRow {
    pub model: String,
    pub edit_lines: usize,
    pub read_lines: usize,
    pub write_lines: usize,
    pub bash_count: usize,
    pub edit_count: usize,
    pub read_count: usize,
    pub todo_write_count: usize,
    pub write_count: usize,
}

/// Analysis results with provider active day counts for daily averages
pub struct AnalysisData {
    /// Rows aggregated across *all* providers, keyed by model name.
    ///
    /// Drives the main per-model table. Same-named models from different
    /// providers (e.g. Copilot CLI + Claude Code both using
    /// `claude-sonnet-4-6`) share a single row here.
    pub rows: Vec<AggregatedAnalysisRow>,
    /// Same aggregation, but partitioned by **source directory** rather
    /// than by model name. Drives the per-provider summary footer so
    /// Copilot-originated sessions cannot be mis-attributed to Claude Code
    /// just because their model name starts with `claude-`.
    pub per_provider: PerProviderAnalysisRows,
    pub provider_days: ProviderActiveDays,
}

/// Per-provider aggregated analysis rows, keyed by source directory.
#[derive(Debug, Default, Clone)]
pub struct PerProviderAnalysisRows {
    pub claude: Vec<AggregatedAnalysisRow>,
    pub codex: Vec<AggregatedAnalysisRow>,
    pub copilot: Vec<AggregatedAnalysisRow>,
    pub gemini: Vec<AggregatedAnalysisRow>,
}

/// Aggregate file-operation metrics across every provider's session files,
/// keyed by model.
///
/// Scans Claude, Codex, Copilot, and Gemini session directories, aggregates
/// tool-call counts and line counts by model, and returns sorted results
/// with provider active-day counts.
pub fn aggregate_sessions_by_model(time_range: TimeRange) -> Result<AnalysisData> {
    let paths = crate::utils::resolve_paths()?;
    let mut aggregated: FastHashMap<String, AggregatedAnalysisRow> =
        FastHashMap::with_capacity(capacity::MODEL_COMBINATIONS);

    let mut claude_aggregated: FastHashMap<String, AggregatedAnalysisRow> =
        FastHashMap::with_capacity(capacity::MODELS_PER_SESSION);
    let mut codex_aggregated: FastHashMap<String, AggregatedAnalysisRow> =
        FastHashMap::with_capacity(capacity::MODELS_PER_SESSION);
    let mut copilot_aggregated: FastHashMap<String, AggregatedAnalysisRow> =
        FastHashMap::with_capacity(capacity::MODELS_PER_SESSION);
    let mut gemini_aggregated: FastHashMap<String, AggregatedAnalysisRow> =
        FastHashMap::with_capacity(capacity::MODELS_PER_SESSION);

    let mut claude_dates: HashSet<String> = HashSet::new();
    let mut codex_dates: HashSet<String> = HashSet::new();
    let mut copilot_dates: HashSet<String> = HashSet::new();
    let mut gemini_dates: HashSet<String> = HashSet::new();

    if paths.claude_session_dir.exists() {
        // Walks the projects tree recursively, so top-level `<session>.jsonl` logs
        // and `<session>/subagents/agent-*.jsonl` logs are both collected here.
        aggregate_sessions_in_directory(
            &paths.claude_session_dir,
            ExtensionType::ClaudeCode,
            &mut aggregated,
            &mut claude_aggregated,
            &mut claude_dates,
            is_claude_session_file,
            time_range,
            None,
        )?;
    }

    if paths.codex_session_dir.exists() {
        aggregate_sessions_in_directory(
            &paths.codex_session_dir,
            ExtensionType::Codex,
            &mut aggregated,
            &mut codex_aggregated,
            &mut codex_dates,
            is_codex_session_file,
            time_range,
            None,
        )?;
    }

    if paths.copilot_session_dir.exists() {
        // `events.jsonl` always lives exactly two levels under
        // `session-state/`; see the rationale in `usage::calculator`.
        aggregate_sessions_in_directory(
            &paths.copilot_session_dir,
            ExtensionType::Copilot,
            &mut aggregated,
            &mut copilot_aggregated,
            &mut copilot_dates,
            is_copilot_session_file,
            time_range,
            Some(COPILOT_SESSION_MAX_DEPTH),
        )?;
    }

    if paths.gemini_session_dir.exists() {
        aggregate_sessions_in_directory(
            &paths.gemini_session_dir,
            ExtensionType::Gemini,
            &mut aggregated,
            &mut gemini_aggregated,
            &mut gemini_dates,
            is_gemini_session_file,
            time_range,
            None,
        )?;
    }

    let mut all_dates: HashSet<&String> = HashSet::new();
    all_dates.extend(claude_dates.iter());
    all_dates.extend(codex_dates.iter());
    all_dates.extend(copilot_dates.iter());
    all_dates.extend(gemini_dates.iter());

    let provider_days = ProviderActiveDays {
        claude: claude_dates.len(),
        codex: codex_dates.len(),
        copilot: copilot_dates.len(),
        gemini: gemini_dates.len(),
        total: all_dates.len(),
    };

    let mut results: Vec<AggregatedAnalysisRow> = aggregated.into_values().collect();
    results.sort_unstable_by(|a, b| a.model.cmp(&b.model));

    let per_provider = PerProviderAnalysisRows {
        claude: into_sorted_rows(claude_aggregated),
        codex: into_sorted_rows(codex_aggregated),
        copilot: into_sorted_rows(copilot_aggregated),
        gemini: into_sorted_rows(gemini_aggregated),
    };

    Ok(AnalysisData {
        rows: results,
        per_provider,
        provider_days,
    })
}

fn into_sorted_rows(map: FastHashMap<String, AggregatedAnalysisRow>) -> Vec<AggregatedAnalysisRow> {
    let mut v: Vec<AggregatedAnalysisRow> = map.into_values().collect();
    v.sort_unstable_by(|a, b| a.model.cmp(&b.model));
    v
}

#[allow(clippy::too_many_arguments)] // per-provider helper; struct-wrapping the args would hurt readability
fn aggregate_sessions_in_directory<P, F>(
    dir: P,
    provider: ExtensionType,
    aggregated: &mut FastHashMap<String, AggregatedAnalysisRow>,
    provider_aggregated: &mut FastHashMap<String, AggregatedAnalysisRow>,
    unique_dates: &mut HashSet<String>,
    filter_fn: F,
    time_range: TimeRange,
    max_depth: Option<usize>,
) -> Result<()>
where
    P: AsRef<Path>,
    F: Copy + Fn(&Path) -> bool + Sync + Send,
{
    let dir = dir.as_ref();
    let files = collect_files_with_max_depth(dir, filter_fn, time_range, max_depth)?;

    // Aggregated analysis only reads counters — no need for `write_file_details`
    // bodies or `edit_file_details` strings. Run in `UsageOnly` and skip the
    // global cache so each file's analysis drops as soon as we've scraped the
    // tool counts and usage totals. Provider is fixed by the source directory.
    let file_aggregations: Vec<(String, CodeAnalysis)> = files
        .par_iter()
        .filter_map(|file_info| {
            match parse_session_file_as(&file_info.path, provider, ParseMode::UsageOnly) {
                Ok(analysis) => Some((file_info.modified_date.clone(), analysis)),
                Err(e) => {
                    eprintln!(
                        "Warning: Failed to parse {}: {}",
                        file_info.path.display(),
                        e
                    );
                    None
                }
            }
        })
        .collect();

    // Merge parallel results sequentially (this part is fast). Every
    // per-model row is accumulated into *both* the cross-provider
    // `aggregated` map (drives the main table) and the per-provider
    // `provider_aggregated` map (drives the per-provider footer) so
    // the display layer does not have to infer provenance from the
    // model name.
    for (date, analysis) in file_aggregations {
        unique_dates.insert(date);
        aggregate_analysis_result(aggregated, &analysis);
        aggregate_analysis_result(provider_aggregated, &analysis);
    }

    Ok(())
}

fn aggregate_analysis_result(
    aggregated: &mut FastHashMap<String, AggregatedAnalysisRow>,
    analysis: &CodeAnalysis,
) {
    for record in &analysis.records {
        for model in record.conversation_usage.keys() {
            if model.contains("<synthetic>") {
                continue;
            }

            let entry = aggregated
                .entry(model.clone())
                .or_insert_with(|| AggregatedAnalysisRow {
                    model: model.clone(),
                    edit_lines: 0,
                    read_lines: 0,
                    write_lines: 0,
                    bash_count: 0,
                    edit_count: 0,
                    read_count: 0,
                    todo_write_count: 0,
                    write_count: 0,
                });

            entry.edit_lines += record.total_edit_lines;
            entry.read_lines += record.total_read_lines;
            entry.write_lines += record.total_write_lines;

            entry.bash_count += record.tool_call_counts.bash;
            entry.edit_count += record.tool_call_counts.edit;
            entry.read_count += record.tool_call_counts.read;
            entry.todo_write_count += record.tool_call_counts.todo_write;
            entry.write_count += record.tool_call_counts.write;
        }
    }
}