Skip to content

venkatesh1146/sharingan

Repository files navigation

Market Pulse Engine

A production-grade Python backend using FastAPI, Celery, MongoDB, and Google AI (Gemini) that provides AI-powered market intelligence through a background processing architecture. The API serves pre-computed snapshots for sub-200ms response times.

🎯 Overview

Market Pulse is a market insights system that uses 3 specialized AI agents running as Celery tasks to fetch news, process indices, and generate market snapshots. The API reads only from MongoDBβ€”no real-time AI processing on requestβ€”ensuring fast, consistent responses.

Key Design

  • Background Processing: Celery tasks handle news fetch, AI analysis, and snapshot generation on a schedule
  • Pre-computed Snapshots: API serves cached snapshots from MongoDB; target response time < 200ms
  • 3 Agents: NewsProcessingAgent, SnapshotGenerationAgent, IndicesCollectionAgent
  • Data Sources: CMOTS API for news and world indices; Gemini for sentiment and summaries

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        API Request                           β”‚
β”‚                    GET /api/v1/market-summary                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                  β”‚
                                  β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                       MongoDB                                β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚ market_snapshots β”‚  β”‚ news_articles  β”‚  β”‚indices_timeseriesβ”‚
β”‚  β”‚ (TTL: 15 min)    β”‚  β”‚ (90 day retain)β”‚  β”‚ (90 day TTL)  β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–²β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                  β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Celery Tasks (Background)                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚fetch_news (3m)  β”‚  β”‚gen_snapshot (5m) β”‚  β”‚fetch_indices β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚           β–Ό                    β–Ό                    β–Ό         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚NewsProcessing   β”‚  β”‚SnapshotGenerationβ”‚  β”‚IndicesCollectionβ”‚
β”‚  β”‚Agent (Gemini)   β”‚  β”‚Agent (Gemini)    β”‚  β”‚Agent           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Agents

Agent Location Responsibility
NewsProcessingAgent app/agents/news_processing_agent.py AI sentiment, entity extraction, summary, impact analysis
SnapshotGenerationAgent app/agents/snapshot_agent.py Market outlook, summary bullets, executive summary, trending news
IndicesCollectionAgent app/agents/indices_agent.py Fetch world indices from CMOTS, store in MongoDB, market-hours aware

See ARCHITECTURE.md for data flow, task schedules, and configuration details.

πŸš€ Quick Start

Prerequisites

  • Python 3.11+
  • Google AI API key (Google AI Studio)
  • Redis (Celery broker + cache)
  • MongoDB
  • Docker (optional, for Redis/MongoDB and full stack)

Installation

  1. Clone and enter the project:

    cd sharingan
  2. Create virtual environment:

    python -m venv .venv
    source .venv/bin/activate   # Windows: .venv\Scripts\activate
  3. Install dependencies:

    pip install -r requirements.txt
  4. Configure environment:

    cp .env.example .env
    # Edit .env and .env.local with GOOGLE_AI_API_KEY, MONGODB_URL, REDIS_URL, etc.
  5. Run with the startup script (recommended):

    ./run.sh all

    This starts Redis, MongoDB (via Docker), Celery worker, Celery beat, triggers initial data tasks, and runs the FastAPI server.

Manual Run (without run.sh)

# Start Redis and MongoDB (e.g. via Docker or local install)
# Then:
source .venv/bin/activate
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
# In another terminal: start Celery worker and beat (see run.sh)

Docker Deployment

# All services via docker-compose
./run.sh docker

# With dev/monitoring profile (e.g. Flower)
./run.sh docker-dev

# Or directly:
docker-compose up -d
docker-compose logs -f market-pulse-api
docker-compose down

πŸ“œ run.sh Commands Guide

The project includes a development script run.sh that manages local services. Ensure .venv exists and optionally create .env.local for overrides.

Command Description
./run.sh all Start all services: Redis, MongoDB, Celery worker, Celery beat, trigger initial data population, then run the FastAPI API (foreground). Use for full local dev.
./run.sh debug Same as all but runs the API in debug mode (verbose logging, access log).
./run.sh api Start only the FastAPI server (assumes Redis/MongoDB/Celery already running or not needed for read-only testing).
./run.sh celery Start Celery worker and Celery beat in the background; script stays running and shows logs. Use Ctrl+C to stop.
./run.sh worker Start only the Celery worker in the foreground (single terminal).
./run.sh beat Start only Celery beat in the foreground.
./run.sh flower Start Flower monitoring UI at http://localhost:5555 (foreground).
./run.sh infra Start only infrastructure: Redis and MongoDB (Docker containers).
./run.sh logs Tail Celery worker and beat log files (logs/celery-worker.log, logs/celery-beat.log).
./run.sh populate Trigger initial data population tasks (news fetch, indices fetch, snapshot generation). Requires Celery worker running.
./run.sh stop Stop all background services: Celery worker, Celery beat, Flower, and Docker containers (Redis, MongoDB).
./run.sh status Show status of Redis, MongoDB, Celery worker, Celery beat, and Flower (running/stopped).
./run.sh docker Run the full stack with docker-compose (build and up).
./run.sh docker-dev Run with docker-compose using dev and monitoring profiles.
./run.sh help Print usage and available commands.

Examples:

# Full dev: API + Celery + infra + initial data
./run.sh all

# Only API (e.g. if Celery is already running elsewhere)
./run.sh api

# Only infra, then in another terminal run worker + beat + api
./run.sh infra
./run.sh celery   # in terminal 2
./run.sh api      # in terminal 3

# Check what's running
./run.sh status

# Stop everything
./run.sh stop

Celery logs are written to logs/. Use ./run.sh logs to tail them.

πŸ“‘ API Endpoints

Primary

Method Endpoint Description
GET /api/v1/market-summary Returns the latest pre-computed market snapshot from MongoDB. Target < 200ms. Triggers async snapshot generation if none exists.

Supporting

Method Endpoint Description
POST /api/v1/snapshot/generate Manually trigger snapshot generation.
GET /api/v1/db/stats Database statistics.
POST /api/v1/data/populate Manually trigger data population (news, indices, snapshot).
GET /api/v1/health Health check.
GET /api/v1/agents/status Agent/task status.

Example: Get market summary

curl "http://localhost:8000/api/v1/market-summary"

πŸ“ Project Structure

sharingan/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py                    # FastAPI application
β”‚   β”œβ”€β”€ config.py                  # Configuration (Pydantic Settings)
β”‚   β”œβ”€β”€ agents/                     # AI agents (Celery task logic)
β”‚   β”‚   β”œβ”€β”€ base.py
β”‚   β”‚   β”œβ”€β”€ news_processing_agent.py
β”‚   β”‚   β”œβ”€β”€ snapshot_agent.py
β”‚   β”‚   └── indices_agent.py
β”‚   β”œβ”€β”€ celery_app/
β”‚   β”‚   β”œβ”€β”€ celery_config.py
β”‚   β”‚   └── tasks/
β”‚   β”‚       β”œβ”€β”€ news_tasks.py
β”‚   β”‚       β”œβ”€β”€ snapshot_tasks.py
β”‚   β”‚       β”œβ”€β”€ indices_tasks.py
β”‚   β”‚       └── cleanup_tasks.py
β”‚   β”œβ”€β”€ db/
β”‚   β”‚   β”œβ”€β”€ mongodb.py
β”‚   β”‚   β”œβ”€β”€ models/                 # MongoDB document models
β”‚   β”‚   └── repositories/
β”‚   β”œβ”€β”€ models/                     # Pydantic request/response schemas
β”‚   β”œβ”€β”€ services/                   # Business logic & external APIs
β”‚   β”‚   β”œβ”€β”€ cmots_news_service.py
β”‚   β”‚   β”œβ”€β”€ company_news_service.py
β”‚   β”‚   β”œβ”€β”€ news_processor_service.py
β”‚   β”‚   β”œβ”€β”€ snapshot_generator_service.py
β”‚   β”‚   └── redis_service.py
β”‚   β”œβ”€β”€ constants/                  # Themes, tickers
β”‚   β”œβ”€β”€ middleware/
β”‚   β”œβ”€β”€ prompts/
β”‚   β”œβ”€β”€ tools/
β”‚   └── utils/
β”œβ”€β”€ tests/
β”œβ”€β”€ run.sh                          # Development run script
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ ARCHITECTURE.md
└── README.md

πŸ”§ Configuration

Key environment variables (see app/config.py and .env / .env.local):

Variable Description Default
GOOGLE_AI_API_KEY Google AI (Gemini) API key Required
GEMINI_FAST_MODEL Model for fast tasks gemini-2.0-flash
GEMINI_PRO_MODEL Model for complex tasks gemini-1.5-pro
MONGODB_URL MongoDB connection URL mongodb://localhost:27017
MONGODB_DATABASE Database name market_intelligence
REDIS_URL Redis connection URL redis://localhost:6379
CELERY_BROKER_URL Celery broker (Redis) redis://localhost:6379/0
CELERY_RESULT_BACKEND Celery results backend redis://localhost:6379/1
LOG_LEVEL Logging level INFO
NEWS_FETCH_INTERVAL News task interval (seconds) 180 (3 min)
SNAPSHOT_GENERATION_INTERVAL Snapshot task interval (seconds) 300 (5 min)
INDICES_FETCH_INTERVAL Indices task interval (seconds) 300 (5 min)

πŸ§ͺ Testing

# Run all tests
pytest

# With coverage
pytest --cov=app --cov-report=html

# Specific test file
pytest tests/unit/test_market_data_agent.py

# Integration tests
pytest tests/integration/

πŸ“Š Monitoring

  • Structured logging: JSON-formatted logs with request tracing
  • Health checks: /api/v1/health, /api/v1/agents/status
  • Flower: Celery monitoring at http://localhost:5555 when started via ./run.sh flower or docker-dev profile

πŸ“ License

MIT License

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make changes with tests
  4. Submit a pull request

πŸ“§ Support

For questions or issues, please open a GitHub issue.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •