Skip to content

Commit eb425a2

Browse files
authored
Merge pull request #275 from ClickHouse/stock-data-demo
Add stock data demo
2 parents 5d56c80 + 29fa6d2 commit eb425a2

31 files changed

+13544
-0
lines changed

stock-data-demo/.gitignore

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Environment variables
2+
.env*
3+
!.env.example
4+
env.local
5+
6+
# Dependencies
7+
node_modules/
8+
npm-debug.log*
9+
yarn-debug.log*
10+
yarn-error.log*
11+
12+
# Production builds
13+
.next/
14+
out/
15+
dist/
16+
build/
17+
18+
# Runtime data
19+
*.pid
20+
*.seed
21+
*.pid.lock
22+
23+
# Logs
24+
logs
25+
*.log
26+
27+
# Coverage directory used by tools like istanbul
28+
coverage/
29+
30+
# IDE files
31+
.vscode/
32+
.idea/
33+
*.swp
34+
*.swo
35+
36+
# OS generated files
37+
.DS_Store
38+
.DS_Store?
39+
._*
40+
.Spotlight-V100
41+
.Trashes
42+
ehthumbs.db
43+
Thumbs.db
44+
45+
# Temporary files
46+
*.tmp
47+
*.temp
48+
49+
# Archives
50+
*.tar.gz
51+
stock-data.tar.gz
52+
53+
# Frontend backup
54+
frontend-backup/
55+
56+
# Application logs
57+
app.log

stock-data-demo/README.md

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
# Stock Data Demo Platform
2+
3+
A unified, high-throughput stock data demo platform that combines real-time data ingestion with advanced visualization capabilities. This integrated system handles WebSocket data from Polygon.io, stores it in ClickHouse, and provides both administrative controls and sophisticated stock data visualization.
4+
5+
## **Getting Started**
6+
7+
### **Prerequisites**
8+
9+
- Node.js 18+
10+
- Access to ClickHouse cluster
11+
- Polygon.io API credentials
12+
13+
### **Installation & Startup**
14+
15+
```bash
16+
# Install all dependencies (both backend and frontend)
17+
npm install
18+
19+
# Start the integrated platform
20+
npm start
21+
22+
# For development with hot reloading
23+
npm run dev
24+
```
25+
26+
### **Access Points**
27+
28+
- **Landing Page**: `http://localhost:34567/`
29+
- **Stock Charts UI**: `http://localhost:34567/stocks` _(live candlestick charts)_
30+
- **Admin Controls**: `http://localhost:34567/admin`
31+
- **Health API**: `http://localhost:34567/health`
32+
- **Metrics API**: `http://localhost:34567/metrics`
33+
34+
## 🏗️ **Application Structure**
35+
36+
```
37+
stock-data/
38+
├── app.js # Main Express server (ingestion + routing)
39+
├── package.json # Combined dependencies
40+
├── public/ # Static assets and admin UI
41+
│ ├── index.html # Unified landing page
42+
│ ├── admin.html # Ingestion monitoring UI
43+
│ ├── styles.css # Admin UI styles
44+
│ └── app.js # Admin UI JavaScript
45+
├── frontend/ # Next.js stock visualization app
46+
│ ├── src/
47+
│ │ ├── components/ # Stock UI components
48+
│ │ │ ├── liveDataTable.tsx
49+
│ │ │ └── liveStockChart.tsx
50+
│ │ ├── queryHandlers/ # ClickHouse query logic
51+
│ │ ├── types/ # TypeScript definitions
52+
│ │ └── app/ # Next.js app structure
53+
│ ├── next.config.mjs # Next.js configuration
54+
│ └── tsconfig.json # TypeScript configuration
55+
└── README.md # This documentation
56+
```
57+
58+
## **Configuration**
59+
60+
### **Environment Variables (.env)**
61+
62+
The application uses several environment variables to configure its behavior. Create a `.env` file in the root directory with the following variables:
63+
64+
```env
65+
# ClickHouse Configuration (Writer)
66+
CLICKHOUSE_HOST=https://your-clickhouse-host:8443
67+
CLICKHOUSE_USERNAME=your_username
68+
CLICKHOUSE_PASSWORD=your_password
69+
70+
# ClickHouse Configuration (Frontend - Reader)
71+
NEXT_PUBLIC_CLICKHOUSE_HOST=https://your-clickhouse-host:8443
72+
NEXT_PUBLIC_CLICKHOUSE_USERNAME=your_username
73+
NEXT_PUBLIC_CLICKHOUSE_PASSWORD=your_password
74+
75+
# Confluent Cloud Kafka Configuration
76+
KAFKA_BROKER=your-kafka-broker:9092
77+
KAFKA_USERNAME=your_kafka_username
78+
KAFKA_PASSWORD=your_kafka_password
79+
KAFKA_ENABLED=false # Set to 'true' to enable Kafka integration
80+
81+
# Polygon.io API
82+
POLYGON_API_KEY=your_polygon_api_key
83+
```
84+
85+
### **Application Settings**
86+
87+
Key configurable parameters in the `StockDataIngester` class:
88+
89+
```javascript
90+
this.maxBatchSize = 1000; // Records per batch
91+
this.maxMemoryUsage = 100 * 1024 * 1024; // 100MB memory limit
92+
this.batchTimeout = 5000; // 5 second flush interval
93+
this.maxConcurrentInserts = 10; // Max parallel ClickHouse inserts
94+
this.maxReconnectAttempts = 10; // WebSocket reconnection attempts
95+
```
96+
97+
## **Platform Capabilities**
98+
99+
### **Data Ingestion Service**
100+
101+
- **Throughput**: 100,000+ messages per second
102+
- **Memory Management**: Auto-managed with 100MB default limit
103+
- **Latency**: Sub-second batch processing
104+
- **Availability**: Automatic recovery from most failure scenarios
105+
- **Data Types**: Real-time trades and quotes from all US equities
106+
107+
### **Visualization Frontend**
108+
109+
- **Real-time Updates**: Live data refreshes every 100ms
110+
- **Interactive Charts**: Candlestick charts with volume indicators
111+
- **Time Window Selection**: 4 different aggregation periods (5min to 1day) with smart refresh rates
112+
- **Chart Performance**: Optimized refresh intervals from 500ms to 60s based on time window
113+
- **Data Quality**: Direct ClickHouse integration for minimal latency
114+
- **UI Framework**: Modern React with ClickHouse UI components
115+
116+
### **Administrative Interface**
117+
118+
- **Live Monitoring**: Real-time connection status and throughput metrics
119+
- **Control Panel**: Start, stop, pause, and restart data ingestion
120+
- **Performance Tracking**: Memory usage, queue lengths, processing rates
121+
- **Connection History**: Detailed logs of connection events and failures
122+
123+
## **Integration Architecture**
124+
125+
### **Data Flow**
126+
127+
1. **WebSocket Ingestion** → Polygon.io real-time feeds
128+
2. **Data Processing** → Filtering, transformation, and batching
129+
3. **Database Storage** → ClickHouse with batch inserts
130+
4. **UI Data Access** → Direct ClickHouse queries from frontend
131+
5. **Administrative Monitoring** → Express API endpoints
132+
133+
### **Technology Stack**
134+
135+
- **Backend**: Node.js + Express (JavaScript)
136+
- **Frontend**: Next.js + React (TypeScript)
137+
- **Database**: ClickHouse
138+
- **UI Components**: ClickHouse UI + Custom Components
139+
- **Charts**: Chart.js with financial data extensions and real-time streaming
140+
- **Time Windows**: Multiple aggregation periods (1s, 1m, 2m, 1h buckets) with adaptive refresh rates
141+
- **Real-time**: WebSocket connections + polling
142+
143+
## **Control API**
144+
145+
```bash
146+
# Pause data ingestion
147+
curl -X POST http://localhost:34567/control/pause
148+
149+
# Resume data ingestion
150+
curl -X POST http://localhost:34567/control/start
151+
152+
# Stop ingestion completely
153+
curl -X POST http://localhost:34567/control/stop
154+
155+
# Restart with clean reconnection
156+
curl -X POST http://localhost:34567/control/restart
157+
```
158+
159+
## **Monitoring & Metrics**
160+
161+
### **Health Check Response**
162+
163+
```json
164+
{
165+
"status": "connected",
166+
"connected": true,
167+
"paused": false,
168+
"uptime": 1234.5,
169+
"totalUptime": 5678.9,
170+
"tradesInserted": 125847,
171+
"quotesInserted": 98234,
172+
"totalRecordsInserted": 224081,
173+
"processedMessages": 15678,
174+
"droppedMessages": 23,
175+
"memoryUsageMB": 45,
176+
"runningInserts": 3,
177+
"queueLength": 0,
178+
"connectionHistory": [...],
179+
"statusMessage": "Active - Receiving data"
180+
}
181+
```
182+
183+
## **Development**
184+
185+
### **Frontend Development**
186+
187+
```bash
188+
# Build frontend for production
189+
npm run frontend:build
190+
191+
# Run frontend in development mode
192+
npm run frontend:dev
193+
194+
# Run only the backend
195+
npm run backend
196+
```

0 commit comments

Comments
 (0)