# sayan112207/ai_tracker architecture

> Architecture diagram of the GitHub repository sayan112207/ai_tracker, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sayan112207/ai_tracker
- Repository: https://github.com/sayan112207/ai_tracker

## Overview

1. **Repository shape**
   This is a single-machine MVP with two top-level applications: a **Python backend** and a **React frontend**. The backend owns all capture, processing, storage, and analytics logic; the frontend is a thin dashboard that reads from the backend API.

2. **Primary end-to-end flow**
   The core pipeline is: **user triggers a capture** in the dashboard → **FastAPI** enqueues/runs capture jobs → **provider adapters** collect live AI responses → **raw artifacts** are written to local storage → **processing** extracts entities/mentions/citations and sentiment/framing → **ranking/aggregation** computes visibility metrics → **analytics API** serves read models back to the dashboard.

3. **Backend boundary: API vs. worker-like orchestration**
   `backend/main.py` and `backend/api/*` expose the HTTP surface. The API is not just read-only; it also initiates captures, reprocessing, and project actions. The capture pipeline behaves like an embedded worker/orchestrator rather than a separate service.

4. **Capture layer and provider adapters**
   `backend/capture/orchestrator.py` coordinates capture batches across providers in `backend/providers/*`. There are two provider styles:
   - **Browser providers**: ChatGPT, Gemini, Perplexity using Playwright/patchright-style automation and stealth handling.
   - **Search/API providers**: Google AI Overview and Google AI Mode via SerpAPI.
   The provider boundary matters because browser providers produce DOM/screenshot artifacts, while SerpAPI providers return structured result data without browser state.

5. **Local artifact storage**
   `backend/storage/*` writes an **immutable raw JSON record per run** plus associated HTML and screenshot files under `storage/raw`, `storage/processed`, `storage/html`, and `storage/screenshots`. This makes raw capture replayable and allows reprocessing without scraping again. Cleanup is a separate concern handled by `backend/storage/cleanup.py` and the `scripts/cleanup.py` utility.

6. **Processing pipeline**
   `backend/processing/pipeline.py` chains the analysis stages: normalization, NER, and sentiment. `normalizer.py` and `ner.py` extract and canonicalize brand mentions and citations; `sentiment.py` adds optional local brand-intelligence signals. The important architectural boundary is that this stage consumes saved artifacts and produces derived data, rather than re-contacting providers.

7. **Ranking and analytics**
   `backend/ranking/engine.py` turns processed mentions/citations into a **visibility score** and related metrics. `backend/analytics/service.py` exposes read-oriented views for trends, provider breakdowns, competitor analysis, and run histories. This keeps scoring logic separate from presentation, and analytics separate from raw capture semantics.

8. **Persistence model**
   The backend uses **SQLite** via SQLAlchemy (`backend/database/session.py`, `backend/models/db_models.py`). The database stores the durable structured records: projects, runs, mentions, competitors, and derived analytics state. The local JSON artifacts remain the source of truth for capture evidence, while SQLite holds queryable summary data for the dashboard.

9. **LLM-assisted features**
   `backend/llm/*` and `backend/providers/gemini/*` support optional Gemini-powered features such as prompt suggestions and competitor auto-detection. These are additive and non-critical: if the Gemini key is absent, the capture/processing pipeline still works. This creates a clear separation between core deterministic pipeline and optional enrichment.

10. **Frontend role**
   The frontend in `frontend/src/*` is a Vite/React dashboard with charts and operational views. It does not compute visibility itself; it polls or fetches API endpoints for overview metrics, prompt runs, competitors, providers, history, and live capture progress. The `CaptureProgress` and orchestration views mirror backend job state.

11. **Operational concerns baked into the architecture**
   The repo includes built-in support for **caching**, **rate limiting**, **retries**, and a **circuit breaker** around provider execution. These are important to diagram because they sit between the orchestrator and provider adapters, protecting the system from repeated failures and reducing unnecessary scraping.

12. **Supporting scripts and bootstrap**
   `scripts/init_db.py`, `scripts/seed_example.py`, and `scripts/login_provider.py` are environment/bootstrap utilities rather than runtime subsystems. They initialize SQLite, seed demo content, and optionally persist browser session cookies for signed-in provider use.

13. **What to emphasize in an architecture diagram**
   The most important boundaries are:
   - **UI vs backend**
   - **API/orchestrator vs provider adapters**
   - **raw immutable artifacts vs processed/queryable data**
   - **capture-time computation vs analytics/read models**
   - **core pipeline vs optional Gemini enrichment**
   That separation is the main design characteristic of this repo.

Main entry points: [`backend/capture/orchestrator.py`](https://github.com/sayan112207/ai_tracker/blob/main/backend/capture/orchestrator.py) (Orchestrator), [`backend/processing/pipeline.py`](https://github.com/sayan112207/ai_tracker/blob/main/backend/processing/pipeline.py) (Pipeline), [`backend/api/routes.py`](https://github.com/sayan112207/ai_tracker/blob/main/backend/api/routes.py) (Routes), [`frontend/src/App.jsx`](https://github.com/sayan112207/ai_tracker/blob/main/frontend/src/App.jsx) (Dashboard)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["Dashboard<br/>React app<br/>[App.jsx]"]
  node_frontend_api["API Client<br/>HTTP client<br/>[api.js]"]
  node_frontend_overview["Overview<br/>[Overview.jsx]"]
  node_frontend_progress["Progress"]
  node_frontend_charts["Charts<br/>Viz layer<br/>[Charts.jsx]"]
end

subgraph group_api["API Layer"]
  node_backend_main["Backend App<br/>FastAPI entry<br/>[main.py]"]
  node_backend_routes["Routes<br/>HTTP API<br/>[routes.py]"]
  node_analytics_service["Analytics API<br/>read models<br/>[service.py]"]
end

subgraph group_capture["Capture"]
  node_capture_orchestrator["Orchestrator<br/>capture flow<br/>[orchestrator.py]"]
  node_provider_base["Provider API<br/>adapter base<br/>[base.py]"]
  node_provider_browser["Browser Providers<br/>adapters<br/>[provider.py]"]
  node_provider_search["Search Providers<br/>adapters<br/>[provider.py]"]
end

subgraph group_processing["Processing"]
  node_processing_pipeline["Pipeline<br/>analysis flow<br/>[pipeline.py]"]
  node_normalizer["Normalizer<br/>text extraction<br/>[normalizer.py]"]
  node_ner["NER<br/>entity extraction<br/>[ner.py]"]
  node_sentiment["Sentiment<br/>signal analysis<br/>[sentiment.py]"]
  node_ranking_engine["Visibility Engine<br/>scoring<br/>[engine.py]"]
end

subgraph group_data["Data"]
  node_raw_store["Raw Store<br/>artifact store<br/>[raw_store.py]"]
  node_db_models[("DB Models<br/>SQLAlchemy models<br/>[db_models.py]")]
  node_db_session[("SQLite Session<br/>database session<br/>[session.py]")]
end

subgraph group_optional["Optional Enrichment"]
  node_llm_service["LLM Service<br/>optional enrichment<br/>[service.py]"]
  node_gemini_provider["Gemini Helper<br/>LLM provider<br/>[gemini.py]"]
end

subgraph group_ops["Operations"]
  node_cleanup["Cleanup<br/>maintenance<br/>[cleanup.py]"]
end

node_frontend_app -->|"fetches"| node_frontend_api
node_frontend_app -->|"renders"| node_frontend_overview
node_frontend_app -->|"shows"| node_frontend_progress
node_frontend_app -->|"visualizes"| node_frontend_charts
node_frontend_api -->|"HTTP"| node_backend_routes
node_backend_main -->|"mounts"| node_backend_routes
node_backend_routes -->|"starts jobs"| node_capture_orchestrator
node_backend_routes -->|"serves reads"| node_analytics_service
node_capture_orchestrator -->|"uses contract"| node_provider_base
node_capture_orchestrator -->|"collects"| node_provider_browser
node_capture_orchestrator -->|"collects"| node_provider_search
node_capture_orchestrator -->|"writes artifacts"| node_raw_store
node_raw_store -->|"feeds"| node_processing_pipeline
node_processing_pipeline -->|"stage"| node_normalizer
node_processing_pipeline -->|"stage"| node_ner
node_processing_pipeline -->|"stage"| node_sentiment
node_processing_pipeline -->|"outputs to"| node_ranking_engine
node_ranking_engine -->|"persists"| node_db_session
node_db_session -->|"maps"| node_db_models
node_analytics_service -->|"queries"| node_db_models
node_analytics_service -->|"depends on"| node_ranking_engine
node_llm_service -->|"delegates"| node_gemini_provider
node_llm_service -.->|"enriches"| node_capture_orchestrator
node_cleanup -.->|"prunes"| node_raw_store
node_cleanup -.->|"cleans"| node_db_session
node_frontend_progress -->|"polls"| node_backend_routes

click node_frontend_app "https://github.com/sayan112207/ai_tracker/blob/main/frontend/src/App.jsx"
click node_frontend_api "https://github.com/sayan112207/ai_tracker/blob/main/frontend/src/services/api.js"
click node_frontend_overview "https://github.com/sayan112207/ai_tracker/blob/main/frontend/src/pages/Overview.jsx"
click node_frontend_progress "https://github.com/sayan112207/ai_tracker/blob/main/frontend/src/components/CaptureProgress.jsx"
click node_frontend_charts "https://github.com/sayan112207/ai_tracker/blob/main/frontend/src/charts/Charts.jsx"
click node_backend_main "https://github.com/sayan112207/ai_tracker/blob/main/backend/main.py"
click node_backend_routes "https://github.com/sayan112207/ai_tracker/blob/main/backend/api/routes.py"
click node_capture_orchestrator "https://github.com/sayan112207/ai_tracker/blob/main/backend/capture/orchestrator.py"
click node_provider_base "https://github.com/sayan112207/ai_tracker/blob/main/backend/providers/base.py"
click node_provider_browser "https://github.com/sayan112207/ai_tracker/blob/main/backend/providers/chatgpt/provider.py"
click node_provider_search "https://github.com/sayan112207/ai_tracker/blob/main/backend/providers/google_ai/provider.py"
click node_raw_store "https://github.com/sayan112207/ai_tracker/blob/main/backend/storage/raw_store.py"
click node_db_models "https://github.com/sayan112207/ai_tracker/blob/main/backend/models/db_models.py"
click node_db_session "https://github.com/sayan112207/ai_tracker/blob/main/backend/database/session.py"
click node_processing_pipeline "https://github.com/sayan112207/ai_tracker/blob/main/backend/processing/pipeline.py"
click node_normalizer "https://github.com/sayan112207/ai_tracker/blob/main/backend/processing/normalizer.py"
click node_ner "https://github.com/sayan112207/ai_tracker/blob/main/backend/processing/ner.py"
click node_sentiment "https://github.com/sayan112207/ai_tracker/blob/main/backend/processing/sentiment.py"
click node_ranking_engine "https://github.com/sayan112207/ai_tracker/blob/main/backend/ranking/engine.py"
click node_analytics_service "https://github.com/sayan112207/ai_tracker/blob/main/backend/analytics/service.py"
click node_llm_service "https://github.com/sayan112207/ai_tracker/blob/main/backend/llm/service.py"
click node_gemini_provider "https://github.com/sayan112207/ai_tracker/blob/main/backend/llm/gemini.py"
click node_cleanup "https://github.com/sayan112207/ai_tracker/blob/main/backend/storage/cleanup.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_app,node_frontend_api,node_frontend_overview,node_frontend_progress,node_frontend_charts,node_cleanup toneBlue
class node_backend_main,node_backend_routes,node_analytics_service toneAmber
class node_capture_orchestrator,node_provider_base,node_provider_browser,node_provider_search toneMint
class node_processing_pipeline,node_normalizer,node_ner,node_sentiment,node_ranking_engine toneRose
class node_raw_store,node_db_models,node_db_session toneIndigo
class node_llm_service,node_gemini_provider toneTeal
```

## Components

### Frontend

React dashboard and API client

- [Dashboard](https://github.com/sayan112207/ai_tracker/blob/main/frontend/src/App.jsx): React app · Main UI shell for the dashboard · `frontend/src/App.jsx`
- [API Client](https://github.com/sayan112207/ai_tracker/blob/main/frontend/src/services/api.js): HTTP client · Frontend data access layer · `frontend/src/services/api.js`
- [Overview](https://github.com/sayan112207/ai_tracker/blob/main/frontend/src/pages/Overview.jsx): View · Primary metrics and summary view · `frontend/src/pages/Overview.jsx`
- [Progress](https://github.com/sayan112207/ai_tracker/blob/main/frontend/src/components/CaptureProgress.jsx): Component · Live capture and orchestration status · `frontend/src/components/CaptureProgress.jsx`
- [Charts](https://github.com/sayan112207/ai_tracker/blob/main/frontend/src/charts/Charts.jsx): Viz layer · Metrics and trend visualizations · `frontend/src/charts/Charts.jsx`

### API Layer

HTTP entrypoint and orchestration surface

- [Backend App](https://github.com/sayan112207/ai_tracker/blob/main/backend/main.py): FastAPI entry · Application bootstrap and server startup · `backend/main.py`
- [Routes](https://github.com/sayan112207/ai_tracker/blob/main/backend/api/routes.py): HTTP API · Read and action endpoints for projects, runs, and capture · `backend/api/routes.py`
- [Analytics API](https://github.com/sayan112207/ai_tracker/blob/main/backend/analytics/service.py): read models · Trend, provider, competitor, and history views · `backend/analytics/service.py`

### Capture

Provider orchestration and collection

- [Orchestrator](https://github.com/sayan112207/ai_tracker/blob/main/backend/capture/orchestrator.py): capture flow · Coordinates capture batches and provider execution · `backend/capture/orchestrator.py`
- [Provider API](https://github.com/sayan112207/ai_tracker/blob/main/backend/providers/base.py): adapter base · Common provider contract and execution surface · `backend/providers/base.py`
- [Browser Providers](https://github.com/sayan112207/ai_tracker/blob/main/backend/providers/chatgpt/provider.py): adapters · ChatGPT, Gemini, and Perplexity browser automation · `backend/providers/chatgpt/provider.py`
- [Search Providers](https://github.com/sayan112207/ai_tracker/blob/main/backend/providers/google_ai/provider.py): adapters · Google AI Overview and AI Mode via SerpAPI · `backend/providers/google_ai/provider.py`

### Processing

Artifact-based normalization and analysis

- [Pipeline](https://github.com/sayan112207/ai_tracker/blob/main/backend/processing/pipeline.py): analysis flow · Normalization, NER, and sentiment stages · `backend/processing/pipeline.py`
- [Normalizer](https://github.com/sayan112207/ai_tracker/blob/main/backend/processing/normalizer.py): text extraction · Canonicalizes brands, mentions, and citations · `backend/processing/normalizer.py`
- [NER](https://github.com/sayan112207/ai_tracker/blob/main/backend/processing/ner.py): entity extraction · Extracts entities and references from artifacts · `backend/processing/ner.py`
- [Sentiment](https://github.com/sayan112207/ai_tracker/blob/main/backend/processing/sentiment.py): signal analysis · Optional framing and brand-intelligence signals · `backend/processing/sentiment.py`
- [Visibility Engine](https://github.com/sayan112207/ai_tracker/blob/main/backend/ranking/engine.py): scoring · Computes visibility score and related metrics · `backend/ranking/engine.py`

### Data

Persistence and storage boundaries

- [Raw Store](https://github.com/sayan112207/ai_tracker/blob/main/backend/storage/raw_store.py): artifact store · Immutable raw JSON, HTML, and screenshot artifacts · `backend/storage/raw_store.py`
- [DB Models](https://github.com/sayan112207/ai_tracker/blob/main/backend/models/db_models.py): SQLAlchemy models · Projects, runs, mentions, competitors, and derived state · `backend/models/db_models.py`
- [SQLite Session](https://github.com/sayan112207/ai_tracker/blob/main/backend/database/session.py): database session · SQLAlchemy-backed SQLite connection management · `backend/database/session.py`

### Optional Enrichment

Non-critical Gemini-assisted features

- [LLM Service](https://github.com/sayan112207/ai_tracker/blob/main/backend/llm/service.py): optional enrichment · Prompt suggestions and competitor auto-detection · `backend/llm/service.py`
- [Gemini Helper](https://github.com/sayan112207/ai_tracker/blob/main/backend/llm/gemini.py): LLM provider · Gemini-backed optional features · `backend/llm/gemini.py`

### Operations

Maintenance and bootstrap utilities

- [Cleanup](https://github.com/sayan112207/ai_tracker/blob/main/backend/storage/cleanup.py): maintenance · Artifact and storage cleanup utilities · `backend/storage/cleanup.py`

## Connections

- Dashboard → API Client: fetches · UI reads data through the API client
- Dashboard → Overview: renders · Dashboard shell hosts overview and operational views
- Dashboard → Progress: shows · Displays live capture state
- Dashboard → Charts: visualizes · Charts present metrics and trends
- API Client → Routes: HTTP · Frontend calls backend endpoints
- Backend App → Routes: mounts · Server exposes route layer
- Routes → Orchestrator: starts jobs · API initiates capture and reprocessing actions
- Routes → Analytics API: serves reads · API exposes query-oriented views
- Orchestrator → Provider API: uses contract · Orchestration executes through provider abstraction
- Orchestrator → Browser Providers: collects · Browser-based capture adapters
- Orchestrator → Search Providers: collects · SerpAPI-based adapters
- Orchestrator → Raw Store: writes artifacts · Persist immutable capture evidence
- Raw Store → Pipeline: feeds · Saved artifacts are reprocessed offline
- Pipeline → Normalizer: stage · Normalize extracted content
- Pipeline → NER: stage · Extract entities and citations
- Pipeline → Sentiment: stage · Add optional sentiment/framing signals
- Pipeline → Visibility Engine: outputs to · Derived mentions and citations drive scoring
- Visibility Engine → SQLite Session: persists · Scores and summaries are stored in SQLite
- SQLite Session → DB Models: maps · SQLAlchemy persistence model layer
- Analytics API → DB Models: queries · Read models are built from durable records
- Analytics API → Visibility Engine: depends on · Analytics surfaces visibility metrics
- LLM Service → Gemini Helper: delegates · Optional Gemini-backed enrichment
- LLM Service → Orchestrator: enriches · Adds non-critical assistance to capture workflows
- Cleanup → Raw Store: prunes · Maintenance removes stale artifacts
- Cleanup → SQLite Session: cleans · May also reconcile durable records
- Progress → Routes: polls · Progress view tracks orchestration state
