An end-to-end data engineering pipeline for ingesting, cleaning, integrating, and storing trading and portfolio data in a containerized PostgreSQL warehouse. The platform includes real-time streaming with Kafka, distributed analytics with Spark, interactive visualizations, and an AI-powered natural language to SQL query interface.
- Docker and Docker Compose installed
- At least 8GB of available RAM
- Ports 8082, 8501, 8502, 5432, 9092 available
-
Navigate to the app directory:
cd app -
Create environment file (if not exists):
cp .env.example .env
Edit
.envand set required variables:GEMINI_API_KEY: Your Google Gemini API key for AI agentPOSTGRES_USER,POSTGRES_PASSWORD,POSTGRES_DB: Database credentials
-
Start all services:
docker-compose up -d
-
Wait for services to be healthy:
# Check service status docker-compose ps # Wait for Airflow to initialize (may take 2-3 minutes) docker-compose logs -f airflow-init
-
Start Spark Master and Worker manually (if needed):
# Start Spark Master docker exec spark-master /usr/local/spark/sbin/start-master.sh # Start Spark Worker docker exec spark-worker /usr/local/spark/sbin/start-worker.sh spark://spark-master:7077
The pipeline runs automatically via Airflow DAG. To trigger it:
- Access Airflow UI (see section below)
- Unpause the DAG:
- Find
stock_portfolio_pipeline_AAin the DAG list - Toggle the pause/unpause button to enable it
- Find
- Trigger the DAG:
- Click on the DAG name
- Click "Play" button to trigger a new run
- Or wait for scheduled execution (runs daily at midnight)
The pipeline executes in the following order:
- Data Cleaning & Integration: Handles missing values, outliers, and integrates datasets
- Encoding & Stream Preparation: Encodes categorical data and prepares streaming subset
- Kafka Streaming: Produces and consumes real-time data streams
- Spark Analytics: Runs distributed analytics and SQL queries
- Data Visualization: Prepares data and starts visualization dashboard
- AI Agent Query Pipeline: Processes natural language queries and starts AI query interface
- Via Airflow UI: See real-time task status and logs
- Via Docker logs:
docker-compose logs -f airflow-worker
http://localhost:8082
- Username:
airflow - Password:
airflow
-
DAG Management:
- View all available DAGs
- Pause/unpause DAGs
- Trigger manual DAG runs
- View DAG execution history
-
Task Monitoring:
- Real-time task status (success, failed, running, queued)
- Task execution logs
- Task duration and retry information
- XCom data exchange between tasks
-
Pipeline Visualization:
- Graph view showing task dependencies
- Gantt chart for execution timeline
- Tree view for execution history
If you cannot access the Airflow UI:
-
Check if the service is running:
docker ps | grep airflow-webserver -
Check logs:
docker-compose logs airflow-webserver
-
Verify port mapping:
docker port stock_airflow_webserver
Should show:
8080/tcp -> 0.0.0.0:8082 -
Restart the webserver:
docker-compose restart airflow-webserver
http://localhost:8501
The dashboard is automatically started by the Airflow pipeline after the data_visualization.start_visualization_service task completes. This happens after:
- Data cleaning and integration
- Encoding and stream preparation
- Kafka streaming
- Spark analytics
- Visualization data preparation
- π Volume by Ticker: Trading volume analysis by stock
- πΉ Price Trends: Stock price trends by sector over time
- π Buy vs Sell: Transaction type comparison
- π Weekly Activity: Trading patterns by day of week
- π₯ Customer Distribution: Customer transaction analysis
For detailed dashboard documentation, see VISUALIZATION.md
http://localhost:8502
The AI Query Interface is automatically started by the Airflow pipeline after the ai_agent_query_pipeline.launch_query_interface task completes. This happens after the AI agent processes the initial query.
-
Access the Query Interface:
- Open
http://localhost:8502in your browser - You'll see the NL2SQL Query Interface
- Open
-
Submit a Natural Language Query:
- Enter a question in natural language, for example:
- "What was the total trading volume for technology stocks?"
- "Show me the top 10 customers by transaction count"
- "What is the average stock price by sector?"
- "How many buy transactions occurred on weekends?"
- Enter a question in natural language, for example:
-
View Results:
- The AI agent will convert your query to SQL
- Execute the query against the database
- Show the generated SQL query
-
View Agent Metrics:
- Check the sidebar for agent statistics
- Total queries, success rate, average execution time
- View query history and logs
You can also test by editing the query file:
-
Edit the query file:
# On Windows (PowerShell) notepad app\agents\user_query.txt # On Linux/Mac nano app/agents/user_query.txt
-
Add your query:
What was the total trading volume for technology stocks? -
Trigger the AI agent task in Airflow:
- Go to Airflow UI
- Find
ai_agent_query_pipeline.process_with_ai_agenttask - Trigger it manually or wait for pipeline execution
-
View results:
- Check
app/agents/AGENT_LOGS.JSONfor query logs - Check
app/agents/logs/for detailed logs - Check
app/agents/generated_results/for CSV exports
- Check
If the AI agent is not working:
-
Check if GEMINI_API_KEY is set:
docker exec stock_airflow_worker env | grep GEMINI_API_KEY
-
Check agent logs:
docker exec stock_airflow_worker cat /opt/airflow/logs/ai_query_interface.log -
Verify the query interface is running:
docker exec stock_airflow_worker pgrep -f 'streamlit run.*ai_query_interface.py'
-
Check agent task logs in Airflow:
- Go to Airflow UI
- Find
ai_agent_query_pipeline.process_with_ai_agenttask - View task logs for errors
The integrated dataset contains the following columns:
- transaction_id: Unique identifier for each trading transaction in the system.
- timestamp: Date and time when the transaction was executed.
- customer_id: Unique identifier for the customer who executed the transaction.
- stock_ticker: Stock symbol representing the traded security (e.g., AAPL, GOOGL).
- transaction_type: Type of transaction (e.g., BUY, SELL).
- quantity: Number of shares traded in the transaction.
- average_trade_size: Average number of shares typically traded by this customer
- stock_price: Price per share at the time of transaction.
- total_trade_amount: Total monetary value of the transaction calculated as stock_price Γ quantity.
- customer_account_type: Type of customer account (e.g., Individual, Corporate, Institutional).
- day_name: Name of the weekday when the transaction occurred (e.g., Monday, Tuesday).
- is_weekend: Boolean flag indicating whether the transaction occurred on a weekend.
- is_holiday: Boolean flag indicating whether the transaction occurred on a market holiday.
- stock_liquidity_tier: Classification of stock liquidity (e.g., High, Medium, Low).
- stock_sector: Economic sector to which the stock belongs (e.g., Technology, Healthcare).
- stock_industry: Specific industry classification within the sector (e.g., Software, Pharmaceuticals).
For a detailed visual representation of the system architecture, see ARCHITECTURE.md.
The architecture diagram shows:
- Data sources and ingestion
- Pipeline stages and data flow
- Service components and their interactions
- Storage layers and outputs
- User-facing interfaces
- Architecture Diagram: See ARCHITECTURE.md for the complete system architecture
- Dashboard Documentation: See app/VISUALIZATION.md for detailed dashboard features
- Pipeline Architecture: Check DAG definition in
app/src/dags/stock_portfolio_pipeline.py - Task Functions: See
app/src/dags/dag_tasks.pyfor implementation details