-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMakefile
More file actions
286 lines (244 loc) · 8.59 KB
/
Makefile
File metadata and controls
286 lines (244 loc) · 8.59 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
.PHONY: help setup start stop restart logs clean backup restore status health
# Default target
help:
@echo "WarDragon Analytics - Docker Compose Management"
@echo ""
@echo "Available targets:"
@echo " setup - Initial setup (copy .env.example, create directories)"
@echo " start - Start all services"
@echo " stop - Stop all services"
@echo " restart - Restart all services"
@echo " logs - Tail logs from all services"
@echo " status - Show service status"
@echo " health - Check health of all services"
@echo " clean - Remove containers and volumes (WARNING: deletes data!)"
@echo " backup - Backup database to backups/ directory"
@echo " restore - Restore database from backup (set BACKUP_FILE=path)"
@echo " shell-db - Open psql shell to TimescaleDB"
@echo " shell-collector - Open shell in collector container"
@echo " shell-web - Open shell in web container"
@echo ""
@echo "Service-specific logs:"
@echo " logs-collector - Tail collector logs"
@echo " logs-web - Tail web logs"
@echo " logs-grafana - Tail Grafana logs"
@echo " logs-db - Tail TimescaleDB logs"
setup:
@echo "Setting up WarDragon Analytics..."
@if [ ! -f .env ]; then \
cp .env.example .env; \
echo "Created .env file - EDIT THIS FILE with your passwords!"; \
else \
echo ".env already exists, skipping..."; \
fi
@mkdir -p volumes/timescale-data volumes/grafana-data logs/collector config
@chmod 700 volumes/timescale-data volumes/grafana-data
@echo "Created directories"
@echo ""
@echo "Next steps:"
@echo "1. Edit .env and set strong passwords"
@echo "2. Edit config/kits.yaml to define your WarDragon kits"
@echo "3. Run 'make start' to start services"
start:
@echo "Starting WarDragon Analytics..."
docker-compose up -d
@echo "Services starting... Run 'make status' to check status"
@echo "Web UI will be available at http://localhost:8080"
@echo "Grafana will be available at http://localhost:3000"
stop:
@echo "Stopping WarDragon Analytics..."
docker-compose down
restart:
@echo "Restarting WarDragon Analytics..."
docker-compose restart
logs:
docker-compose logs -f
logs-collector:
docker-compose logs -f collector
logs-web:
docker-compose logs -f web
logs-grafana:
docker-compose logs -f grafana
logs-db:
docker-compose logs -f timescaledb
status:
@echo "Service Status:"
@docker-compose ps
health:
@echo "Checking service health..."
@echo ""
@echo "TimescaleDB:"
@docker exec wardragon-timescaledb pg_isready -U wardragon || echo " UNHEALTHY"
@echo ""
@echo "Web API:"
@curl -sf http://localhost:8080/health > /dev/null && echo " HEALTHY" || echo " UNHEALTHY"
@echo ""
@echo "Grafana:"
@curl -sf http://localhost:3000/api/health > /dev/null && echo " HEALTHY" || echo " UNHEALTHY"
@echo ""
@echo "Docker Compose Status:"
@docker-compose ps
clean:
@echo "WARNING: This will remove all containers and volumes (including data)!"
@read -p "Are you sure? Type 'yes' to continue: " confirm; \
if [ "$$confirm" = "yes" ]; then \
docker-compose down -v; \
echo "Containers and volumes removed"; \
else \
echo "Cancelled"; \
fi
backup:
@mkdir -p backups
@BACKUP_FILE=backups/wardragon_backup_$$(date +%Y%m%d_%H%M%S).sql.gz; \
echo "Backing up database to $$BACKUP_FILE..."; \
docker exec wardragon-timescaledb pg_dump -U wardragon wardragon | gzip > $$BACKUP_FILE; \
echo "Backup complete: $$BACKUP_FILE"
restore:
@if [ -z "$(BACKUP_FILE)" ]; then \
echo "ERROR: BACKUP_FILE not specified"; \
echo "Usage: make restore BACKUP_FILE=backups/wardragon_backup_20260119.sql.gz"; \
exit 1; \
fi
@if [ ! -f "$(BACKUP_FILE)" ]; then \
echo "ERROR: Backup file not found: $(BACKUP_FILE)"; \
exit 1; \
fi
@echo "Restoring database from $(BACKUP_FILE)..."
@if echo "$(BACKUP_FILE)" | grep -q ".gz$$"; then \
gunzip -c $(BACKUP_FILE) | docker exec -i wardragon-timescaledb psql -U wardragon wardragon; \
else \
docker exec -i wardragon-timescaledb psql -U wardragon wardragon < $(BACKUP_FILE); \
fi
@echo "Restore complete"
shell-db:
docker exec -it wardragon-timescaledb psql -U wardragon wardragon
shell-collector:
docker exec -it wardragon-collector /bin/bash
shell-web:
docker exec -it wardragon-web /bin/bash
pull:
@echo "Pulling latest images..."
docker-compose pull
build:
@echo "Building application containers..."
docker-compose build
update: pull build restart
@echo "Update complete"
# Development targets
dev-setup:
@if [ ! -f docker-compose.override.yml ]; then \
cp docker-compose.override.yml.example docker-compose.override.yml; \
echo "Created docker-compose.override.yml for development"; \
fi
@make setup
dev-start:
@echo "Starting in development mode..."
@docker-compose up
# Show database stats
db-stats:
@echo "Database Statistics:"
@docker exec wardragon-timescaledb psql -U wardragon wardragon -c "\
SELECT \
schemaname, \
tablename, \
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS size \
FROM pg_tables \
WHERE schemaname = 'public' \
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;"
# Show recent drone detections
db-drones:
@echo "Recent Drone Detections (last 10):"
@docker exec wardragon-timescaledb psql -U wardragon wardragon -c "\
SELECT time, kit_id, drone_id, lat, lon, alt, rid_make, rid_model \
FROM drones \
ORDER BY time DESC \
LIMIT 10;" 2>/dev/null || echo "Table 'drones' does not exist yet"
# Show kit status
db-kits:
@echo "Configured Kits Status:"
@docker exec wardragon-timescaledb psql -U wardragon wardragon -c "\
SELECT kit_id, name, status, last_seen, api_url \
FROM kits \
ORDER BY last_seen DESC;" 2>/dev/null || echo "Table 'kits' does not exist yet"
#################################
# Testing Targets
#################################
.PHONY: test test-unit test-integration test-all coverage test-verbose test-specific install-test-deps
# Install test dependencies
install-test-deps:
@echo "Installing test dependencies..."
@pip install pytest pytest-asyncio pytest-cov pytest-mock pytest-timeout httpx
# Run unit tests only (no Docker required)
test: test-unit
# Run unit tests (fast, no external dependencies)
test-unit:
@echo "Running unit tests..."
@pytest -m unit -v
# Run integration tests (requires Docker Compose)
test-integration:
@echo "Running integration tests..."
@echo "Checking if Docker Compose is running..."
@docker-compose ps | grep -q "Up" || (echo "ERROR: Docker Compose services not running. Start with 'make start'" && exit 1)
@export RUN_INTEGRATION_TESTS=1 && pytest -m integration -v
# Run all tests (unit + integration)
test-all:
@echo "Running all tests..."
@export RUN_INTEGRATION_TESTS=1 && pytest -v
# Run tests with verbose output
test-verbose:
@echo "Running tests with verbose output..."
@pytest -vv -s
# Run specific test file or function
# Usage: make test-specific TEST=tests/test_api.py
# Usage: make test-specific TEST=tests/test_api.py::test_health_endpoint
test-specific:
@if [ -z "$(TEST)" ]; then \
echo "ERROR: TEST parameter required"; \
echo "Usage: make test-specific TEST=tests/test_api.py"; \
exit 1; \
fi
@echo "Running specific test: $(TEST)"
@pytest $(TEST) -v
# Generate coverage report (HTML)
coverage:
@echo "Generating coverage report..."
@pytest -m unit --cov=app --cov-report=html --cov-report=term
@echo ""
@echo "Coverage report generated in htmlcov/index.html"
@echo "Open with: firefox htmlcov/index.html"
# Generate coverage report (all tests including integration)
coverage-all:
@echo "Generating coverage report (all tests)..."
@export RUN_INTEGRATION_TESTS=1 && pytest --cov=app --cov-report=html --cov-report=term
@echo ""
@echo "Coverage report generated in htmlcov/index.html"
# Generate coverage XML (for CI/CD)
coverage-xml:
@echo "Generating XML coverage report..."
@pytest -m unit --cov=app --cov-report=xml --cov-report=term
@echo "Coverage XML generated: coverage.xml"
# Run tests with coverage check (fail if < 70%)
test-coverage:
@echo "Running tests with coverage check..."
@pytest -m unit --cov=app --cov-report=term --cov-fail-under=70
# Clean test artifacts
test-clean:
@echo "Cleaning test artifacts..."
@rm -rf .pytest_cache
@rm -rf htmlcov
@rm -rf .coverage
@rm -f coverage.xml
@rm -f coverage.json
@rm -rf tests/__pycache__
@rm -rf app/__pycache__
@rm -f tests/test-output.log
@echo "Test artifacts cleaned"
# Run tests in watch mode (requires pytest-watch)
# Install: pip install pytest-watch
test-watch:
@echo "Running tests in watch mode..."
@ptw -- -m unit -v
# Show test markers
test-markers:
@echo "Available test markers:"
@pytest --markers | grep "^@pytest.mark" || pytest --markers | grep " @"