-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
467 lines (407 loc) · 19.7 KB
/
Copy pathMakefile
File metadata and controls
467 lines (407 loc) · 19.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# DataOrb Makefile for Raspberry Pi
# Usage: make [target] or just 'make' for interactive menu
# Use bash for better echo support
SHELL := /bin/bash
# Default target - show interactive menu
.DEFAULT_GOAL := menu
# Default app directory for Pi deployment
APP_DIR ?= /home/pianalytics/dataorb
# Node memory limit for Pi builds
NODE_MEM ?= 512
# Python interpreter
PYTHON ?= python3
# Colors for output
RED := \033[0;31m
GREEN := \033[0;32m
YELLOW := \033[1;33m
BLUE := \033[0;34m
PURPLE := \033[0;35m
CYAN := \033[0;36m
WHITE := \033[1;37m
NC := \033[0m # No Color
# =============================================================================
# Interactive Menu
# =============================================================================
.PHONY: menu
menu: ## Show interactive menu
@clear
@echo ""
@printf "$(BLUE)╔══════════════════════════════════════════════════════════════╗$(NC)\n"
@printf "$(BLUE)║$(WHITE) DataOrb Build System - Interactive Menu $(BLUE)║$(NC)\n"
@printf "$(BLUE)╠══════════════════════════════════════════════════════════════╣$(NC)\n"
@printf "$(BLUE)║$(NC) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(CYAN)Rebuild Operations:$(NC) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)1)$(NC) Complete rebuild (clean + install + build) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)2)$(NC) Rebuild frontend only $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)3)$(NC) Rebuild backend only $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(CYAN)Development:$(NC) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)4)$(NC) Start development servers $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)5)$(NC) Build locally (not on Pi) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)6)$(NC) Deploy to Pi $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(CYAN)Service Management:$(NC) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)7)$(NC) Check status $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)8)$(NC) Restart services $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)9)$(NC) View logs $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)10)$(NC) Follow logs (live) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(CYAN)Quality & Testing:$(NC) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)11)$(NC) Run linting $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)12)$(NC) Auto-format code $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)13)$(NC) Run tests (backend + frontend + lint) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)14)$(NC) Run backend tests only $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)15)$(NC) Run frontend tests only $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)16)$(NC) Run E2E tests (Playwright) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(CYAN)Other:$(NC) $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)17)$(NC) Show all make targets $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)18)$(NC) Clean everything $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(GREEN)q)$(NC) Quit $(BLUE)║$(NC)\n"
@printf "$(BLUE)║$(NC) $(BLUE)║$(NC)\n"
@printf "$(BLUE)╚══════════════════════════════════════════════════════════════╝$(NC)\n"
@echo ""
@printf "Enter choice: "
@read choice; \
case $$choice in \
1) $(MAKE) rebuild ;; \
2) $(MAKE) frontend ;; \
3) $(MAKE) backend ;; \
4) $(MAKE) dev ;; \
5) $(MAKE) build-local ;; \
6) echo "Enter PI_HOST (e.g., 192.168.1.154):"; read pi_host; \
echo "Enter PI_USER (e.g., pianalytics):"; read pi_user; \
$(MAKE) deploy PI_HOST=$$pi_host PI_USER=$$pi_user ;; \
7) $(MAKE) status ;; \
8) $(MAKE) restart-services ;; \
9) $(MAKE) logs ;; \
10) $(MAKE) logs-follow ;; \
11) $(MAKE) lint ;; \
12) $(MAKE) format ;; \
13) $(MAKE) test ;; \
14) $(MAKE) test-backend ;; \
15) $(MAKE) test-frontend ;; \
16) $(MAKE) test-e2e ;; \
17) $(MAKE) help ;; \
18) $(MAKE) clean ;; \
q|Q) echo "Goodbye!" ;; \
*) echo "Invalid choice. Please run 'make menu' again." ;; \
esac
.PHONY: help
help: ## Show this help message
@echo "DataOrb Build System"
@echo "===================="
@echo ""
@echo "Usage: make [target]"
@echo ""
@echo "Available targets:"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " $(GREEN)%-20s$(NC) %s\n", $$1, $$2}'
@echo ""
@echo "Examples:"
@echo " make rebuild # Complete rebuild on Pi"
@echo " make frontend # Rebuild frontend only"
@echo " make backend # Rebuild backend only"
@echo " make test # Run all tests"
# =============================================================================
# Complete Rebuild
# =============================================================================
.PHONY: rebuild
rebuild: clean-all install-all build-all restart-services ## Complete clean rebuild of frontend and backend
@echo -e "$(GREEN)✓ Complete rebuild finished successfully!$(NC)"
@$(MAKE) status
.PHONY: clean-all
clean-all: clean-frontend clean-backend ## Clean all dependencies and builds
@echo -e "$(GREEN)✓ All cleaned$(NC)"
.PHONY: install-all
install-all: install-frontend install-backend ## Install all dependencies
@echo -e "$(GREEN)✓ All dependencies installed$(NC)"
.PHONY: build-all
build-all: build-frontend build-backend ## Build all components (with linting)
@echo -e "$(GREEN)✓ All components built and verified$(NC)"
# =============================================================================
# Frontend Targets
# =============================================================================
.PHONY: frontend
frontend: clean-frontend install-frontend build-frontend ## Clean rebuild of frontend only
@echo -e "$(GREEN)✓ Frontend rebuild complete$(NC)"
.PHONY: clean-frontend
clean-frontend: ## Clean frontend dependencies and build
@echo -e "$(YELLOW)Cleaning frontend...$(NC)"
@cd frontend && rm -rf node_modules build
@echo " ✓ Removed node_modules and build"
.PHONY: install-frontend
install-frontend: ## Install frontend dependencies
@echo -e "$(YELLOW)Installing frontend dependencies...$(NC)"
@cd frontend && npm install
@echo " ✓ Frontend dependencies installed"
.PHONY: build-frontend
build-frontend: lint-frontend ## Build frontend with memory constraints for Pi (runs linting first)
@echo -e "$(YELLOW)Building frontend with NODE_OPTIONS=--max-old-space-size=$(NODE_MEM)$(NC)"
@cd frontend && \
NODE_OPTIONS="--max-old-space-size=$(NODE_MEM)" \
GENERATE_SOURCEMAP=false \
CI=false \
npm run build
@if [ -f "frontend/build/index.html" ]; then \
echo -e "$(GREEN) ✓ Frontend build successful$(NC)"; \
else \
echo -e "$(RED) ✗ Frontend build failed!$(NC)"; \
exit 1; \
fi
.PHONY: dev-frontend
dev-frontend: ## Run frontend in development mode with file watching
@echo -e "$(YELLOW)Starting frontend dev server...$(NC)"
@cd frontend && npm start
# =============================================================================
# Backend Targets
# =============================================================================
.PHONY: backend
backend: clean-backend install-backend build-backend ## Clean rebuild of backend only (with linting)
@echo -e "$(GREEN)✓ Backend rebuild and verification complete$(NC)"
.PHONY: clean-backend
clean-backend: ## Clean backend virtual environment and cache
@echo -e "$(YELLOW)Cleaning backend...$(NC)"
@cd backend && rm -rf venv __pycache__ *.pyc .mypy_cache
@find backend -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
@find backend -type d -name ".mypy_cache" -exec rm -rf {} + 2>/dev/null || true
@find backend -name "*.pyc" -delete 2>/dev/null || true
@echo " ✓ Removed venv, Python cache, and mypy cache"
.PHONY: install-backend
install-backend: ## Install backend dependencies in virtual environment
@echo -e "$(YELLOW)Installing backend dependencies...$(NC)"
@cd backend && $(PYTHON) -m venv venv
@cd backend && . venv/bin/activate && pip install --upgrade pip
@cd backend && . venv/bin/activate && pip install -r requirements.txt
@if [ -f "backend/requirements-dev.txt" ]; then \
cd backend && . venv/bin/activate && pip install -r requirements-dev.txt; \
echo " ✓ Backend dev dependencies installed"; \
fi
@echo " ✓ Backend dependencies installed"
.PHONY: build-backend
build-backend: lint-backend ## Verify backend code quality (runs linting)
@echo -e "$(YELLOW)Checking backend code quality...$(NC)"
@echo -e "$(GREEN) ✓ Backend code quality verified$(NC)"
.PHONY: dev-backend
dev-backend: ## Run backend in development mode with auto-reload
@echo -e "$(YELLOW)Starting backend dev server...$(NC)"
@cd backend && . venv/bin/activate && FLASK_DEBUG=1 $(PYTHON) app.py
# =============================================================================
# Service Management
# =============================================================================
.PHONY: restart-services
restart-services: restart-backend restart-display ## Restart all systemd services
@echo -e "$(GREEN)✓ All services restarted$(NC)"
.PHONY: restart
restart: restart-services ## Alias for restart-services
.PHONY: restart-backend
restart-backend: ## Restart backend systemd service
@echo -e "$(YELLOW)Restarting backend service...$(NC)"
@if systemctl is-active --quiet pi-analytics-backend; then \
sudo systemctl restart pi-analytics-backend; \
sleep 2; \
if systemctl is-active --quiet pi-analytics-backend; then \
echo -e "$(GREEN) ✓ Backend service restarted$(NC)"; \
else \
echo -e "$(RED) ✗ Backend service failed to start$(NC)"; \
sudo systemctl status pi-analytics-backend --no-pager; \
fi \
else \
echo " Backend service not running, starting it..."; \
sudo systemctl start pi-analytics-backend; \
fi
.PHONY: restart-display
restart-display: ## Restart display systemd service
@echo -e "$(YELLOW)Restarting display service...$(NC)"
@if systemctl is-active --quiet pi-analytics-display; then \
sudo systemctl restart pi-analytics-display; \
sleep 2; \
if systemctl is-active --quiet pi-analytics-display; then \
echo -e "$(GREEN) ✓ Display service restarted$(NC)"; \
else \
echo -e "$(RED) ✗ Display service failed to start$(NC)"; \
sudo systemctl status pi-analytics-display --no-pager; \
fi \
else \
echo " Display service not running, starting it..."; \
sudo systemctl start pi-analytics-display; \
fi
.PHONY: stop-services
stop-services: ## Stop all systemd services
@echo -e "$(YELLOW)Stopping services...$(NC)"
@sudo systemctl stop pi-analytics-backend pi-analytics-display 2>/dev/null || true
@echo -e "$(GREEN) ✓ Services stopped$(NC)"
.PHONY: start-services
start-services: ## Start all systemd services
@echo -e "$(YELLOW)Starting services...$(NC)"
@sudo systemctl start pi-analytics-backend pi-analytics-display
@echo -e "$(GREEN) ✓ Services started$(NC)"
# =============================================================================
# Testing and Quality
# =============================================================================
.PHONY: test
test: test-backend test-frontend lint ## Run all tests and linting
.PHONY: test-all
test-all: test-backend test-frontend test-e2e lint ## Run all tests including E2E
.PHONY: test-backend
test-backend: ## Run backend tests
@echo -e "$(YELLOW)Running backend tests...$(NC)"
@cd backend && . venv/bin/activate && python -m pytest tests/ -v || echo " No tests found"
.PHONY: test-frontend
test-frontend: ## Run frontend tests
@echo -e "$(YELLOW)Running frontend tests...$(NC)"
@cd frontend && npx react-scripts test --watchAll=false --passWithNoTests
.PHONY: test-e2e
test-e2e: ## Run E2E Playwright tests (requires built frontend + running server)
@echo -e "$(YELLOW)Running E2E tests...$(NC)"
@cd e2e && npx playwright test
.PHONY: lint
lint: lint-frontend lint-backend ## Run linting on frontend and backend
.PHONY: lint-frontend
lint-frontend: ## Run ESLint and stylelint on frontend
@echo -e "$(YELLOW)Linting frontend code...$(NC)"
@echo " Running ESLint (JavaScript/TypeScript)..."
@cd frontend && npm run lint || \
(echo -e "$(RED) ✗ ESLint found issues$(NC)" && exit 1)
@echo " Running stylelint (CSS)..."
@cd frontend && npm run lint:css || \
(echo -e "$(RED) ✗ stylelint found issues. Run 'make lint-css-fix' to auto-fix.$(NC)" && exit 1)
@echo -e "$(GREEN) ✓ Frontend linting passed$(NC)"
.PHONY: lint-css
lint-css: ## Run stylelint on CSS files only
@echo -e "$(YELLOW)Linting CSS files...$(NC)"
@cd frontend && npm run lint:css
.PHONY: lint-css-fix
lint-css-fix: ## Auto-fix CSS linting issues
@echo -e "$(YELLOW)Auto-fixing CSS issues...$(NC)"
@cd frontend && npm run lint:css:fix
.PHONY: lint-backend
lint-backend: ## Run flake8, black, and mypy on backend
@echo -e "$(YELLOW)Linting backend Python code...$(NC)"
@echo " Running flake8..."
@cd backend && . venv/bin/activate && \
flake8 app.py config_manager.py ota_manager.py themes.py --max-line-length=100 --extend-ignore=E203,W503 || \
(echo -e "$(RED) ✗ flake8 found issues$(NC)" && exit 1)
@echo " Running black..."
@cd backend && . venv/bin/activate && \
black --check app.py config_manager.py ota_manager.py themes.py --line-length=100 --no-cache || \
(echo -e "$(RED) ✗ black found formatting issues. Run 'make format-backend' to fix.$(NC)" && exit 1)
@echo " Running mypy..."
@cd backend && . venv/bin/activate && \
mypy app.py config_manager.py ota_manager.py themes.py --ignore-missing-imports || \
(echo -e "$(RED) ✗ mypy found type issues$(NC)" && exit 1)
@echo -e "$(GREEN) ✓ Backend linting passed$(NC)"
.PHONY: format
format: format-frontend format-backend ## Auto-format all code
.PHONY: format-frontend
format-frontend: ## Auto-format frontend code with Prettier
@echo -e "$(YELLOW)Formatting frontend...$(NC)"
@cd frontend && npm run format
.PHONY: format-backend
format-backend: ## Auto-format backend code with Black
@echo -e "$(YELLOW)Formatting backend...$(NC)"
@cd backend && . venv/bin/activate && \
black app.py config_manager.py ota_manager.py themes.py --line-length=100 --no-cache
# =============================================================================
# Status and Monitoring
# =============================================================================
.PHONY: status
status: ## Check status of all services and components
@echo "======================================"
@echo "DataOrb Services Status"
@echo "======================================"
@echo ""
@echo "Backend Service:"
@if systemctl is-active --quiet pi-analytics-backend 2>/dev/null; then \
echo -e "$(GREEN) ✓ Running$(NC)"; \
else \
echo -e "$(RED) ✗ Not running$(NC)"; \
fi
@echo ""
@echo "Display Service:"
@if systemctl is-active --quiet pi-analytics-display 2>/dev/null; then \
echo -e "$(GREEN) ✓ Running$(NC)"; \
else \
echo -e "$(RED) ✗ Not running$(NC)"; \
fi
@echo ""
@echo "Backend API:"
@if curl -s -f -o /dev/null -w "%{http_code}" http://localhost:5000/api/health | grep -q 200; then \
echo -e "$(GREEN) ✓ Responding$(NC)"; \
else \
echo -e "$(RED) ✗ Not responding$(NC)"; \
fi
@echo ""
@echo "Frontend Build:"
@if [ -f "frontend/build/index.html" ]; then \
echo -e "$(GREEN) ✓ Built$(NC)"; \
stat -c " Last built: %y" frontend/build/index.html | cut -d'.' -f1; \
else \
echo -e "$(RED) ✗ Not built$(NC)"; \
fi
@echo ""
@echo "Config Auto-Reload:"
@CONFIG_VERSION=$$(curl -s http://localhost:5000/api/config/version 2>/dev/null | grep -oE '"version":"[^"]+' | cut -d'"' -f4); \
if [ -n "$$CONFIG_VERSION" ]; then \
echo -e "$(GREEN) ✓ Enabled (version: $$CONFIG_VERSION)$(NC)"; \
else \
echo -e "$(RED) ✗ Not available$(NC)"; \
fi
@echo ""
@echo "Network:"
@echo " IP: $$(hostname -I | awk '{print $$1}')"
@echo ""
@echo "Access URLs:"
@echo " Dashboard: http://$$(hostname -I | awk '{print $$1}'):5000"
@echo " Config: http://$$(hostname -I | awk '{print $$1}'):5000/config"
@echo ""
@echo "======================================"
.PHONY: logs
logs: ## Show logs from systemd services
@echo -e "$(YELLOW)Backend logs:$(NC)"
@sudo journalctl -u pi-analytics-backend -n 20 --no-pager
@echo ""
@echo -e "$(YELLOW)Display logs:$(NC)"
@sudo journalctl -u pi-analytics-display -n 20 --no-pager
.PHONY: logs-follow
logs-follow: ## Follow logs from systemd services
@sudo journalctl -u pi-analytics-backend -u pi-analytics-display -f
# =============================================================================
# Development Helpers
# =============================================================================
.PHONY: dev
dev: ## Run both frontend and backend in development mode (requires two terminals)
@echo -e "$(YELLOW)Starting development servers...$(NC)"
@echo "Run these commands in separate terminals:"
@echo " 1. make dev-backend"
@echo " 2. make dev-frontend"
.PHONY: build-local
build-local: ## Build frontend locally (not on Pi)
@echo -e "$(YELLOW)Building frontend locally...$(NC)"
@cd frontend && npm run build
@echo -e "$(GREEN)✓ Frontend built locally$(NC)"
.PHONY: deploy
deploy: build-local ## Build locally and deploy to Pi
@if [ -z "$(PI_HOST)" ]; then \
echo -e "$(RED)Error: PI_HOST not set$(NC)"; \
echo "Usage: make deploy PI_HOST=192.168.1.154 PI_USER=pianalytics"; \
exit 1; \
fi
@echo -e "$(YELLOW)Deploying to $(PI_USER)@$(PI_HOST)...$(NC)"
@ssh $(PI_USER)@$(PI_HOST) "mkdir -p ~/dataorb/frontend"
@scp -r frontend/build $(PI_USER)@$(PI_HOST):~/dataorb/frontend/
@echo -e "$(GREEN)✓ Deployed to $(PI_HOST)$(NC)"
@echo "Restart backend on Pi: ssh $(PI_USER)@$(PI_HOST) 'sudo systemctl restart pi-analytics-backend'"
# =============================================================================
# Maintenance
# =============================================================================
.PHONY: update
update: ## Pull latest changes from git
@echo -e "$(YELLOW)Pulling latest changes...$(NC)"
@git pull
@echo -e "$(GREEN)✓ Updated from git$(NC)"
.PHONY: clean
clean: clean-all ## Clean everything (alias for clean-all)
.PHONY: install
install: install-all ## Install everything (alias for install-all)
.PHONY: build
build: build-all ## Build everything (alias for build-all)