# mo-ri-ch/marketpulseai architecture

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

- Interactive diagram: https://gitdiagram.com/mo-ri-ch/marketpulseai
- Repository: https://github.com/mo-ri-ch/marketpulseai

## Overview

1. **Top-level shape**
   - This repo is a two-tier application: a **FastAPI backend** in `backend/` and a **Next.js + TypeScript frontend** in `frontend/`.
   - `docker-compose.yml` and the two Dockerfiles indicate the system is designed to run as separate containers with a clear API/UI boundary.

2. **User-facing UI**
   - The frontend uses the **Next.js App Router** (`frontend/src/app`) with a login page and a main dashboard page.
   - The dashboard is composed from focused panels: `WatchlistPanel`, `BreakingNews`, `AISummaryPanel`, `MarketSummary`, `SentimentPanel`, `AlertsPanel`, and `StockAnalysisModal`.
   - This strongly suggests a single-screen information dashboard where each panel is fed by backend data rather than client-side computation.

3. **Backend API layer**
   - `backend/main.py` is the FastAPI entrypoint; `backend/routers/watchlist.py` is the only explicit router in the tree, so the exposed API surface is centered on watchlist operations.
   - `backend/auth.py` provides authentication support, while `backend/schemas.py` and `backend/models.py` define the request/response and persistence shapes shared across endpoints.
   - The backend is therefore organized as a classic API service: route handlers on top, schema validation in the middle, ORM models underneath.

4. **Persistence and migrations**
   - `backend/database.py` likely owns the SQLAlchemy engine/session setup, and `backend/alembic/` contains migration support.
   - The presence of `alembic/versions/9a07dd54f78b_initial_schema.py` indicates a versioned relational schema, with SQLite used locally and PostgreSQL expected in production per the README.
   - This is the durable state boundary for users, watchlists, and any stored analysis metadata.

5. **Authentication and personalization**
   - The README describes **JWT-based auth** and user-linked watchlists; the code layout supports that via `auth.py`, `models.py`, and `routers/watchlist.py`.
   - Personalization is a backend concern: a user authenticates, then the dashboard can query/update that user’s watchlists and related alerts.
   - This is the main tenant boundary in the system: data is not just global market data, but also account-scoped settings.

6. **Crawler subsystem**
   - `backend/crawlers/` is a dedicated ingestion layer with `base.py`, `primary.py`, `secondary.py`, `extractor.py`, `sources.py`, and `agent.py`.
   - The structure implies source-specific collectors, a shared extraction contract, and a source registry or configuration layer.
   - Based on the README, this subsystem gathers articles/posts from financial news, official filings, and community sources before any AI processing happens.

7. **Normalization and deduplication**
   - The crawler output is not used raw; `backend/ai/deduplicator.py` and the README’s “merge identical stories” behavior indicate a normalization step that groups the same event from multiple publishers.
   - This is an important architectural boundary because it converts noisy, source-specific items into a unified story entity with multiple source links.
   - For diagramming, treat this as the pivot point between ingestion and analysis.

8. **AI analysis pipeline**
   - `backend/ai/pipeline.py` is the orchestration layer for downstream analysis, with separate modules for `summarizer.py`, `sentiment.py`, and `alerts_evaluator.py`.
   - The README also references Gemini integration plus heuristic fallback behavior, so AI analysis is an external-service-backed capability with a local fallback path.
   - The analysis flow appears to be: grouped articles in, summary/sentiment/alert signals out.

9. **Data products exposed to the UI**
   - The frontend component names map directly to backend-derived products: breaking news, watchlist changes, sentiment scores, alert states, and stock-specific analysis.
   - The dashboard is therefore fed by several logically distinct backend outputs: raw or grouped news, aggregated sentiment, watchlist state, and ticker-focused prediction/summary results.
   - For architecture diagrams, these should be shown as separate read models even if they originate from the same crawl pipeline.

10. **Operational and runtime concerns**
   - `backend/requirements.txt` plus `backend/requirements_crawl.txt` suggest the backend has a core API runtime and an additional crawling/analysis dependency set.
   - `logger_config.py` indicates centralized logging, which matters for tracing crawl and AI pipeline failures.
   - `backend/.env.example` implies environment-driven configuration for database, JWT secrets, Gemini API key, and email/SMTP alerts.

11. **Deployment boundary**
   - Each side is containerized independently, and the README points to **Railway/Render/AWS ECS** for backend deployment and **Vercel** for the frontend.
   - This means the frontend and backend can be scaled and deployed separately, with the browser talking to the backend over HTTP rather than through server-side coupling.

12. **Best way to draw the diagram**
   - Draw four main boxes: **Frontend UI**, **FastAPI API/Auth**, **Crawler + AI processing**, and **Relational DB**.
   - Put **Gemini API / external source websites / SMTP** outside the system boundary as external dependencies.
   - Show the main flow as: sources → crawlers/extractor → deduplication → AI analysis → persisted records/API responses → dashboard panels, with auth/watchlist requests flowing directly between frontend and backend.

Main entry points: [`backend/main.py`](https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/main.py) (API App), [`backend/ai`](https://github.com/mo-ri-ch/marketpulseai/tree/master/backend/ai) (Analysis Modules), [`backend/models.py`](https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/models.py) (Models), [`backend/database.py`](https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/database.py) (Database)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend UI"]
  node_frontend_app["App Router<br/>Next.js app"]
  node_frontend_login["Login Page<br/>UI page<br/>[page.tsx]"]
  node_frontend_dashboard["Dashboard<br/>UI page<br/>[page.tsx]"]
  node_frontend_panels["Dashboard Panels<br/>UI components"]
end

subgraph group_backend["Backend Service"]
  node_backend_main["API App<br/>FastAPI entrypoint<br/>[main.py]"]
  node_backend_auth["Auth<br/>[auth.py]"]
  node_backend_watchlist["Watchlist API<br/>router<br/>[watchlist.py]"]
  node_backend_contracts["Schemas<br/>contracts<br/>[schemas.py]"]
  node_backend_models[("Models<br/>[models.py]")]
  node_backend_db[("Database<br/>SQLAlchemy setup<br/>[database.py]")]
  node_backend_migrations["Migrations<br/>Alembic"]
end

subgraph group_pipeline["Processing Pipeline"]
  node_crawl_sources["Source Registry<br/>crawler config<br/>[sources.py]"]
  node_crawl_extractors["Crawlers<br/>ingestion layer"]
  node_ai_dedupe{{"Deduplicator<br/>normalization<br/>[deduplicator.py]"}}
  node_ai_pipeline["AI Pipeline<br/>analysis orchestration<br/>[pipeline.py]"]
  node_ai_outputs["Analysis Modules<br/>AI processors"]
end

subgraph group_external["External Dependencies"]
  node_external_sources["News Sources<br/>external websites"]
  node_external_gemini(("Gemini API<br/>external model"))
  node_external_smtp(("Email/SMTP<br/>external service"))
end

node_frontend_login -->|"sign in"| node_backend_auth
node_frontend_dashboard -->|"renders"| node_frontend_panels
node_frontend_panels -->|"fetches data"| node_backend_main
node_frontend_app -->|"API client"| node_backend_main
node_backend_main -->|"uses"| node_backend_auth
node_backend_main -->|"mounts"| node_backend_watchlist
node_backend_main -->|"validates"| node_backend_contracts
node_backend_watchlist -->|"persists"| node_backend_models
node_backend_models -->|"stores in"| node_backend_db
node_backend_db -->|"managed by"| node_backend_migrations
node_crawl_sources -->|"reads from"| node_external_sources
node_crawl_extractors -->|"targets"| node_crawl_sources
node_crawl_extractors -->|"feeds"| node_ai_dedupe
node_ai_dedupe -->|"prepares"| node_ai_pipeline
node_ai_pipeline -->|"runs"| node_ai_outputs
node_ai_outputs -->|"calls"| node_external_gemini
node_ai_outputs -->|"sends alerts"| node_external_smtp
node_ai_outputs -->|"writes results"| node_backend_models
node_backend_main -->|"queries"| node_backend_db
node_backend_main -->|"serves insights"| node_ai_outputs

click node_frontend_app "https://github.com/mo-ri-ch/marketpulseai/tree/master/frontend/src/app"
click node_frontend_login "https://github.com/mo-ri-ch/marketpulseai/blob/master/frontend/src/app/login/page.tsx"
click node_frontend_dashboard "https://github.com/mo-ri-ch/marketpulseai/blob/master/frontend/src/app/page.tsx"
click node_frontend_panels "https://github.com/mo-ri-ch/marketpulseai/tree/master/frontend/src/components"
click node_backend_main "https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/main.py"
click node_backend_auth "https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/auth.py"
click node_backend_watchlist "https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/routers/watchlist.py"
click node_backend_contracts "https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/schemas.py"
click node_backend_models "https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/models.py"
click node_backend_db "https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/database.py"
click node_backend_migrations "https://github.com/mo-ri-ch/marketpulseai/tree/master/backend/alembic"
click node_crawl_sources "https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/crawlers/sources.py"
click node_crawl_extractors "https://github.com/mo-ri-ch/marketpulseai/tree/master/backend/crawlers"
click node_ai_dedupe "https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/ai/deduplicator.py"
click node_ai_pipeline "https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/ai/pipeline.py"
click node_ai_outputs "https://github.com/mo-ri-ch/marketpulseai/tree/master/backend/ai"

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_login,node_frontend_dashboard,node_frontend_panels toneBlue
class node_backend_main,node_backend_auth,node_backend_watchlist,node_backend_contracts,node_backend_models,node_backend_db,node_backend_migrations toneAmber
class node_crawl_sources,node_crawl_extractors,node_ai_dedupe,node_ai_pipeline,node_ai_outputs toneMint
class node_external_sources,node_external_gemini,node_external_smtp toneRose
```

## Components

### Frontend UI

Next.js App Router dashboard and login experience

- [App Router](https://github.com/mo-ri-ch/marketpulseai/tree/master/frontend/src/app): Next.js app · Frontend shell and route composition · `frontend/src/app`
- [Login Page](https://github.com/mo-ri-ch/marketpulseai/blob/master/frontend/src/app/login/page.tsx): UI page · Authentication screen · `frontend/src/app/login/page.tsx`
- [Dashboard](https://github.com/mo-ri-ch/marketpulseai/blob/master/frontend/src/app/page.tsx): UI page · Main information dashboard · `frontend/src/app/page.tsx`
- [Dashboard Panels](https://github.com/mo-ri-ch/marketpulseai/tree/master/frontend/src/components): UI components · Watchlist, news, summary, sentiment, alerts, and stock analysis panels · `frontend/src/components`

### Backend Service

FastAPI API, auth, persistence, and watchlist endpoints

- [API App](https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/main.py): FastAPI entrypoint · Application startup and router wiring · `backend/main.py`
- [Auth](https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/auth.py): JWT auth · Authentication and user session support · `backend/auth.py`
- [Watchlist API](https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/routers/watchlist.py): router · User watchlist operations · `backend/routers/watchlist.py`
- [Schemas](https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/schemas.py): contracts · Request and response shapes · `backend/schemas.py`
- [Models](https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/models.py): ORM models · Persistent domain entities · `backend/models.py`
- [Database](https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/database.py): SQLAlchemy setup · Engine and session management · `backend/database.py`
- [Migrations](https://github.com/mo-ri-ch/marketpulseai/tree/master/backend/alembic): Alembic · Versioned relational schema · `backend/alembic`

### Processing Pipeline

Crawler ingestion, deduplication, and AI analysis

- [Source Registry](https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/crawlers/sources.py): crawler config · Primary and secondary source definitions · `backend/crawlers/sources.py`
- [Crawlers](https://github.com/mo-ri-ch/marketpulseai/tree/master/backend/crawlers): ingestion layer · Source-specific collectors and extraction flow · `backend/crawlers`
- [Deduplicator](https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/ai/deduplicator.py): normalization · Merges identical stories into unified events · `backend/ai/deduplicator.py`
- [AI Pipeline](https://github.com/mo-ri-ch/marketpulseai/blob/master/backend/ai/pipeline.py): analysis orchestration · Coordinates summarization, sentiment, and alerts · `backend/ai/pipeline.py`
- [Analysis Modules](https://github.com/mo-ri-ch/marketpulseai/tree/master/backend/ai): AI processors · Summaries, sentiment, and alert evaluation · `backend/ai`

### External Dependencies

Outside services and data sources

- **News Sources**: external websites · Financial news, filings, and community sources
- **Gemini API**: external model · Hosted LLM used for analysis with fallback behavior
- **Email/SMTP**: external service · Alert delivery integration

## Connections

- Login Page → Auth: sign in · User authentication requests
- Dashboard → Dashboard Panels: renders · Dashboard page is composed from focused panels
- Dashboard Panels → API App: fetches data · Panels consume backend read models and API responses
- App Router → API App: API client · Frontend talks to the backend over HTTP
- API App → Auth: uses · API entrypoint wires authentication support
- API App → Watchlist API: mounts · Watchlist router is exposed through the API
- API App → Schemas: validates · Request and response payloads are shaped by schemas
- Watchlist API → Models: persists · Watchlist endpoints operate on ORM entities
- Models → Database: stores in · ORM models are backed by the database layer
- Database → Migrations: managed by · Schema changes are versioned with Alembic
- Source Registry → News Sources: reads from · Crawler source definitions point at outside publishers
- Crawlers → Source Registry: targets · Collectors use the configured source registry
- Crawlers → Deduplicator: feeds · Raw extracted items are normalized and merged
- Deduplicator → AI Pipeline: prepares · Grouped stories are handed to analysis orchestration
- AI Pipeline → Analysis Modules: runs · Pipeline executes summarization, sentiment, and alerts
- Analysis Modules → Gemini API: calls · LLM-backed analysis uses Gemini when available
- Analysis Modules → Email/SMTP: sends alerts · Alert results can be delivered via email
- Analysis Modules → Models: writes results · Analysis output becomes persisted application data
- API App → Database: queries · API reads and writes application state
- API App → Analysis Modules: serves insights · API exposes derived news, sentiment, and alert data
