-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
246 lines (201 loc) Β· 14.7 KB
/
Copy pathMakefile
File metadata and controls
246 lines (201 loc) Β· 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Backtrader Optimization System Makefile
.PHONY: help install run stop restart clean logs status redis-cli db db-stop db-restart down-all nuke run-nodb celery celery-stop celery-status flower test-dashboard test-perf test-quick
# Use a local empty Docker config to bypass credential helpers when pulling base images
DOCKER_CONFIG ?= $(PWD)/.docker-empty
# Default target
.DEFAULT_GOAL := help
# Colors for output
GREEN := \033[0;32m
YELLOW := \033[1;33m
RED := \033[0;31m
NC := \033[0m # No Color
help: ## Show this help message
@echo "$(GREEN)Backtrader Optimization System$(NC)"
@echo "================================"
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "$(YELLOW)%-15s$(NC) %s\n", $$1, $$2}' $(MAKEFILE_LIST)
install: ## Install dependencies
@echo "$(GREEN)Installing dependencies...$(NC)"
@pip install -r deployment/requirements/requirements.txt
@pip install -r deployment/requirements/requirements-redis.txt
@pip install -r deployment/requirements/requirements-celery.txt
@echo "$(GREEN)β
Dependencies installed (including Celery and enhanced optimization)$(NC)"
run: ## Start full stack including AI Agents, Vector DB, API, Redis, Frontend, DB+Adminer, Celery workers, and Monitoring
@echo "$(GREEN)π Starting full stack with AI Agents, Vector DB, Celery workers and monitoring...$(NC)"
@mkdir -p $(DOCKER_CONFIG) && [ -f $(DOCKER_CONFIG)/config.json ] || printf '{}' > $(DOCKER_CONFIG)/config.json
@mkdir -p monitoring/loki monitoring/promtail monitoring/grafana/provisioning/datasources monitoring/grafana/dashboards
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml -f deployment/docker/docker-compose.celery.yml -f deployment/docker/docker-compose.monitoring.yml up -d --build db db_admin vectordb agents api redis frontend celery_worker_cpu celery_worker_optimization celery_worker_optimization_2 celery_worker_optimization_3 celery_worker_optimization_4 celery_worker_validation flower loki promtail grafana seq
@echo "$(GREEN)β¨ Core Services:$(NC)"
@echo "$(GREEN) API: http://localhost:8000/docs$(NC)"
@echo "$(GREEN) Frontend: http://localhost:5173$(NC)"
@echo "$(GREEN) AI Agents: http://localhost:8001/docs$(NC)"
@echo "$(GREEN) Vector DB UI: http://localhost:6334$(NC)"
@echo "$(GREEN)π Monitoring:$(NC)"
@echo "$(GREEN) Flower (Celery): http://localhost:5555$(NC)"
@echo "$(GREEN) Adminer (DB): http://localhost:8081$(NC)"
@echo "$(GREEN) Grafana: http://localhost:3000 (admin/backtrader123)$(NC)"
@echo "$(GREEN) Seq (Logs): http://localhost:5341$(NC)"
@echo "$(YELLOW)π€ AI Agents: Ready (add API key to .env)$(NC)"
@echo "$(YELLOW)βοΈ Workers: 2 CPU, 4 optimization$(NC)"
run-nodb: ## Alias for run (DB excluded); kept for clarity
@$(MAKE) run
stop: ## Stop all services except DB (API, Agents, Redis, Frontend, Celery, Monitoring)
@echo "$(YELLOW)π Stopping all services except DB...$(NC)"
-@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml -f deployment/docker/docker-compose.celery.yml -f deployment/docker/docker-compose.monitoring.yml stop api agents vectordb redis frontend celery_worker_cpu celery_worker_optimization celery_worker_optimization_2 celery_worker_optimization_3 celery_worker_optimization_4 celery_worker_validation flower loki promtail grafana seq 2>/dev/null || true
@echo "$(GREEN)β
Stopped all services (DB still running)$(NC)"
restart: ## Restart API, Redis, and Frontend (DB unchanged)
@echo "$(YELLOW)π Restarting api, redis, frontend...$(NC)"
-@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml restart api redis frontend 2>/dev/null || true
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml up -d --build api redis frontend
@echo "$(GREEN)β
Restarted api, redis, frontend$(NC)"
db: ## Start or ensure DB (and Adminer) are running and persistent
@echo "$(YELLOW)π Ensuring DB services are up...$(NC)"
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml up -d db db_admin
@echo "$(GREEN)β
DB: localhost:5432 β’ Adminer: http://localhost:8081$(NC)"
db-stop: ## Stop DB and Adminer (data persists)
@echo "$(YELLOW)π Stopping DB and Adminer (volumes persist)...$(NC)"
-@docker compose -f deployment/docker/docker-compose.yml stop db db_admin 2>/dev/null || true
@echo "$(GREEN)β
DB services stopped (data preserved)$(NC)"
db-restart: ## Restart DB and Adminer (data persists)
@echo "$(YELLOW)π Restarting DB and Adminer...$(NC)"
-@docker compose -f deployment/docker/docker-compose.yml restart db db_admin 2>/dev/null || true
@docker compose -f deployment/docker/docker-compose.yml up -d db db_admin
@echo "$(GREEN)β
DB services restarted$(NC)"
redis-start: ## Start only Redis
@echo "$(YELLOW)π§ Starting Redis...$(NC)"
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml up -d redis
@sleep 3
@echo "$(GREEN)β
Redis started$(NC)"
redis-stop: ## Stop only Redis
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml stop redis
api: ## Start only FastAPI
@echo "$(YELLOW)π Starting FastAPI (compose)...$(NC)"
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml up -d api
status: ## Check service status
@echo "$(GREEN)Service Status:$(NC)"
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml ps
logs: ## Tail logs for all running services
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml logs --tail=50 -f
redis-cli: ## Connect to Redis CLI
@docker compose -f deployment/docker/docker-compose.yml exec redis redis-cli
down-all: ## Stop and remove all services including AI (volumes preserved)
@echo "$(YELLOW)π Bringing down entire stack (keeping volumes)...$(NC)"
-@docker compose -f deployment/docker/docker-compose.yml -f deployment/docker/docker-compose.celery.yml -f deployment/docker/docker-compose.monitoring.yml down --remove-orphans 2>/dev/null || true
@echo "$(GREEN)β
Stack down (volumes kept, including vector DB data)$(NC)"
nuke: ## HARD RESET: stop and remove all containers and volumes (DB & Vector DB wiped!)
@echo "$(RED)π₯ HARD RESET: Removing containers and volumes (DB & Vector DB will be wiped)$(NC)"
@echo "$(RED)β οΈ WARNING: This will delete all indexed data in the vector database!$(NC)"
@read -p "Are you sure? Type 'yes' to continue: " confirm && [ "$$confirm" = "yes" ] || (echo "Cancelled" && exit 1)
-@docker compose -f deployment/docker/docker-compose.yml -f deployment/docker/docker-compose.celery.yml -f deployment/docker/docker-compose.monitoring.yml down -v --remove-orphans 2>/dev/null || true
-@docker system prune -af 2>/dev/null || true
-@docker volume prune -f 2>/dev/null || true
-@docker builder prune -af 2>/dev/null || true
@echo "$(GREEN)β
Full cleanup complete$(NC)"
clean: down-all ## Backward-compatible alias for non-destructive cleanup
# Shortcuts
up: run
down: stop
ps: status
# Frontend
frontend-install: ## Install frontend dependencies
@cd frontend && corepack enable || true
@cd frontend && npm install
frontend-dev: ## Start frontend dev server
@cd frontend && npm run dev
frontend-run: ## Start frontend dev server in background (http://localhost:5173)
-@pkill -f "vite" 2>/dev/null || true
-@fuser -k 5173/tcp 2>/dev/null || true
@cd frontend && (nohup npm run dev -- --host 0.0.0.0 --port 5173 > ../frontend-dev.log 2>&1 &)
@echo "$(GREEN)Frontend started at http://localhost:5173 (logs: frontend-dev.log)$(NC)"
# Celery Commands
celery: ## Start Celery workers (CPU and optimization)
@echo "$(GREEN)π Starting Celery workers...$(NC)"
@mkdir -p $(DOCKER_CONFIG) && [ -f $(DOCKER_CONFIG)/config.json ] || printf '{}' > $(DOCKER_CONFIG)/config.json
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml -f deployment/docker/docker-compose.celery.yml up -d celery_worker_cpu celery_worker_optimization celery_worker_optimization_2 celery_worker_optimization_3 celery_worker_optimization_4
@echo "$(GREEN)β
Celery workers started$(NC)"
celery-gpu: ## Start GPU worker for ML tasks (requires NVIDIA Docker)
@echo "$(GREEN)π Starting GPU worker...$(NC)"
@docker compose -f deployment/docker/docker-compose.yml -f deployment/docker/docker-compose.celery.yml up -d celery_worker_gpu
@echo "$(GREEN)β
GPU worker started$(NC)"
celery-stop: ## Stop all Celery workers
@echo "$(YELLOW)π Stopping Celery workers...$(NC)"
-@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml -f deployment/docker/docker-compose.celery.yml stop celery_worker_cpu celery_worker_optimization celery_worker_optimization_2 celery_worker_optimization_3 celery_worker_optimization_4 celery_worker_gpu flower 2>/dev/null || true
@echo "$(GREEN)β
Celery workers stopped$(NC)"
celery-status: ## Check Celery worker status
@echo "$(GREEN)Celery Worker Status:$(NC)"
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml -f deployment/docker/docker-compose.celery.yml ps | grep celery
celery-scale: ## Scale CPU workers (usage: make celery-scale N=4)
@echo "$(GREEN)π Scaling CPU workers to $(N)...$(NC)"
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml -f deployment/docker/docker-compose.celery.yml up -d --scale celery_worker_cpu=$(N)
@echo "$(GREEN)β
Scaled to $(N) CPU workers$(NC)"
flower: ## Open Flower monitoring dashboard
@echo "$(GREEN)πΈ Opening Flower dashboard...$(NC)"
@open http://localhost:5555 2>/dev/null || xdg-open http://localhost:5555 2>/dev/null || echo "Open http://localhost:5555 in your browser"
celery-logs: ## Tail Celery worker logs
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml -f deployment/docker/docker-compose.celery.yml logs --tail=50 -f celery_worker_cpu celery_worker_optimization celery_worker_optimization_2 celery_worker_optimization_3 celery_worker_optimization_4
# Monitoring Commands
grafana: ## Open Grafana dashboard
@echo "$(GREEN)π Opening Grafana dashboard...$(NC)"
@open http://localhost:3000 2>/dev/null || xdg-open http://localhost:3000 2>/dev/null || echo "Open http://localhost:3000 in your browser (admin/backtrader123)"
seq: ## Open Seq structured logging UI
@echo "$(GREEN)π Opening Seq dashboard...$(NC)"
@open http://localhost:5341 2>/dev/null || xdg-open http://localhost:5341 2>/dev/null || echo "Open http://localhost:5341 in your browser"
monitoring-status: ## Check monitoring stack status
@echo "$(GREEN)Monitoring Stack Status:$(NC)"
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml -f deployment/docker/docker-compose.monitoring.yml ps | grep -E "loki|promtail|grafana|seq"
monitoring-logs: ## Tail monitoring stack logs
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml -f deployment/docker/docker-compose.monitoring.yml logs --tail=50 -f loki promtail grafana seq
test-dashboard: ## Run comprehensive dashboard performance tests
@echo "$(GREEN)π§ͺ Running Dashboard Performance Tests...$(NC)"
@./test_dashboard_performance.sh
test-perf: ## Quick performance test (original vs optimized)
@echo "$(GREEN)β‘ Quick Performance Test$(NC)"
@echo "Testing original endpoint (might be slow)..."
@time curl -s "http://localhost:8000/persist/results?limit=50" > /dev/null || echo "Original endpoint failed"
@echo ""
@echo "Testing optimized endpoint (should be fast)..."
@time curl -s "http://localhost:8000/persist/results/batch?limit=50" > /dev/null || echo "Optimized endpoint not available"
@echo ""
@echo "Testing cache hit (should be very fast)..."
@time curl -s "http://localhost:8000/persist/results/batch?limit=50" > /dev/null || echo "Cache test failed"
test-quick: ## Ultra-quick dashboard test (just check if endpoints work)
@echo "$(GREEN)β Testing Dashboard Endpoints$(NC)"
@echo -n "Health check: "
@curl -s "http://localhost:8000/health" | grep -q "ok" && echo "$(GREEN)β$(NC)" || echo "$(RED)β$(NC)"
@echo -n "Original endpoint: "
@curl -s "http://localhost:8000/persist/results?limit=1" | grep -q "results" && echo "$(GREEN)β$(NC)" || echo "$(RED)β$(NC)"
@echo -n "Optimized batch endpoint: "
@curl -s "http://localhost:8000/persist/results/batch?limit=1" | grep -q "results" && echo "$(GREEN)β$(NC)" || echo "$(RED)β$(NC)"
@echo -n "Cache stats endpoint: "
@curl -s "http://localhost:8000/persist/cache/stats" | grep -q "enabled" && echo "$(GREEN)β$(NC)" || echo "$(YELLOW)β (Redis not configured)$(NC)"
@echo -n "Database connection: "
@docker exec backtrader-db-1 psql -U bt -d backtrader -c "SELECT 1" > /dev/null 2>&1 && echo "$(GREEN)β$(NC)" || echo "$(RED)β$(NC)"
# AI Agents Commands
agents: ## Start AI Agents and Vector DB services
@echo "$(GREEN)π€ Starting AI Agents and Vector Database...$(NC)"
@DOCKER_CONFIG=$(DOCKER_CONFIG) docker compose -f deployment/docker/docker-compose.yml up -d --build vectordb agents
@echo "$(GREEN)β
AI Agents: http://localhost:8001$(NC)"
@echo "$(GREEN)β
Vector DB: http://localhost:6334$(NC)"
@echo "$(YELLOW)Note: Add your API key to .env file$(NC)"
agents-stop: ## Stop AI Agents and Vector DB
@echo "$(YELLOW)π Stopping AI Agents and Vector DB...$(NC)"
-@docker compose -f deployment/docker/docker-compose.yml stop agents vectordb 2>/dev/null || true
@echo "$(GREEN)β
AI services stopped$(NC)"
agents-logs: ## Tail AI Agents logs
@docker compose -f deployment/docker/docker-compose.yml logs --tail=50 -f agents
agents-index: ## Manually trigger codebase indexing
@echo "$(GREEN)π Indexing codebase in vector database...$(NC)"
@curl -X POST http://localhost:8001/index/codebase
@echo "$(GREEN)β
Indexing complete$(NC)"
agents-status: ## Check AI Agents and Vector DB status
@echo "$(GREEN)AI Services Status:$(NC)"
@echo -n "AI Agents API: "
@curl -s "http://localhost:8001/health" | grep -q "healthy" && echo "$(GREEN)β Running$(NC)" || echo "$(RED)β Not running$(NC)"
@echo -n "Vector Database: "
@curl -s "http://localhost:6333/health" | grep -q "ok" && echo "$(GREEN)β Running$(NC)" || echo "$(RED)β Not running$(NC)"
@echo -n "Index status: "
@curl -s "http://localhost:8001/index/status" | grep -q '"indexed":true' && echo "$(GREEN)β Indexed$(NC)" || echo "$(YELLOW)β Not indexed (run: make agents-index)$(NC)"
agents-test: ## Test AI Agents functionality
@echo "$(GREEN)π§ͺ Testing AI Agents...$(NC)"
@./scripts/test_agents.sh 2>/dev/null || (echo "$(YELLOW)Test script not found. Creating...$(NC)" && \
echo '#!/bin/bash\ncurl -s http://localhost:8001/health | grep -q "healthy" && echo "β AI Agents working" || echo "β AI Agents not running"' > scripts/test_agents.sh && \
chmod +x scripts/test_agents.sh && ./scripts/test_agents.sh)