src — analytics

Module: src-analytics Cohesion: 0.80 Members: 0

src — analytics

This document provides a comprehensive overview of the src/analytics module, detailing its purpose, architecture, key components, and integration points within the codebase.

Analytics Module Overview

The src/analytics module serves as the central hub for all analytical capabilities within the application. Its primary goal is to provide insights into user behavior, system performance, resource consumption (especially LLM API costs), and codebase health. This includes:

The module employs various strategies for data collection and storage, ranging from in-memory real-time dashboards to file-backed persistence and a robust SQLite-based system for long-term analytics.

Core Concepts

Several core concepts underpin the analytics module:

  1. Event-Driven Tracking: Most analytics components react to discrete events (e.g., message, tool_call, session_start) to update their internal state and metrics.
  2. Layered Persistence:

  1. Cost Centralization: LLM model pricing is sourced from src/config/model-pricing.ts, ensuring consistent cost calculations across all analytics components.
  2. Code Analysis: Leveraging Git history and static code analysis to provide insights into the codebase itself, independent of runtime behavior.
  3. Observability: Providing standard interfaces (like Prometheus) for external monitoring systems.

Architecture and Data Flow

The analytics module is composed of several specialized sub-modules, each handling a specific aspect of data collection, analysis, or reporting.

graph TD
    subgraph "Application Events"
        A[User Interactions]
        B[LLM API Calls]
        C[Tool Executions]
        D[File System Changes]
        E[Git Commits]
    end

    subgraph "Analytics Module"
        subgraph "Runtime Tracking"
            F[MetricsDashboard]
            G[AnalyticsDashboard]
            H[BudgetAlertManager]
            I[CostPredictor]
            J[ToolAnalytics]
            K[ROI Tracker]
        end
        subgraph "Persistent & Reporting"
            L[PersistentAnalytics]
            M[PrometheusExporter]
            N[Code Evolution]
            O[Codebase Heatmap]
            P[Complexity Analyzer]
        end
    end

    A,B,C,D -- Emit Events --> F,G,J,K
    G -- Triggers --> H
    F,G,J,K -- Aggregate Data --> L
    F,G,J,K -- Expose Metrics --> M

    L -- Provides Historical Data --> I
    E -- Analyzes --> N,O
    D -- Analyzes --> P

    I -- Predicts Cost --> B
    L -- Stores Aggregates --> SQLite DB
    G,J,K -- Persist Data --> Local JSON Files
    M -- Scraped By --> Prometheus/Grafana

Key Distinctions between Dashboards:

Key Components

1. BudgetAlertManager (src/analytics/budget-alerts.ts)

2. Code Evolution Report (src/analytics/code-evolution.ts)

3. Codebase Heatmap (src/analytics/codebase-heatmap.ts)

4. Complexity Analyzer (src/analytics/complexity-analyzer.ts)

5. CostPredictor (src/analytics/cost-predictor.ts)

6. AnalyticsDashboard (src/analytics/dashboard.ts)

7. MetricsDashboard (src/analytics/metrics-dashboard.ts)

8. PersistentAnalytics (src/analytics/persistent-analytics.ts)

9. PrometheusExporter (src/analytics/prometheus-exporter.ts)

10. ROITracker (src/analytics/roi-tracker.ts)

11. ToolAnalytics (src/analytics/tool-analytics.ts) - Inferred from index.ts and Call Graph

Integration Points

The analytics module is deeply integrated throughout the application:

Usage Patterns

Developers typically interact with the analytics module in a few ways:

  1. Recording Events:

  1. Retrieving Metrics/Reports:

  1. Configuration:

  1. Observability:

Contribution Guidelines

When contributing to the analytics module: