-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
410 lines (334 loc) · 19 KB
/
Makefile
File metadata and controls
410 lines (334 loc) · 19 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
.PHONY: help build clean test lint lint-fix install generate mobile wasm \
install-tools install-sebuf buf-generate buf-lint \
build-server build-sink docker-up docker-down docker-build \
test-unit test-e2e test-coverage
# Default target
.DEFAULT_GOAL := help
# Development API key seeded in docker/postgres/init-causality-server.sql
DEV_API_KEY := deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef
# =============================================================================
# Help
# =============================================================================
help: ## Show this help message
@echo "Causality Event Processing System"
@echo ""
@echo "Available commands:"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}'
# =============================================================================
# Core Development
# =============================================================================
build: build-server build-sink build-reaction ## Build all binaries
build-server: ## Build HTTP server binary
@echo "Building HTTP server..."
@mkdir -p bin
@go build -o bin/causality-server ./cmd/server
build-sink: ## Build warehouse sink binary
@echo "Building warehouse sink..."
@mkdir -p bin
@go build -o bin/warehouse-sink ./cmd/warehouse-sink
build-reaction: ## Build reaction engine binary
@echo "Building reaction engine..."
@mkdir -p bin
@go build -o bin/reaction-engine ./cmd/reaction-engine
clean: ## Clean build artifacts
@echo "Cleaning..."
@rm -rf bin/ coverage/ api/openapi/
run-server: build-server ## Run HTTP server locally
@echo "Running HTTP server..."
@./bin/causality-server
run-sink: build-sink ## Run warehouse sink locally
@echo "Running warehouse sink..."
@./bin/warehouse-sink
run-reaction: build-reaction ## Run reaction engine locally
@echo "Running reaction engine..."
@./bin/reaction-engine
# =============================================================================
# Testing
# =============================================================================
test: test-unit ## Run all tests
test-unit: ## Run unit tests
@echo "Running unit tests..."
@go test -v ./...
test-coverage: ## Run tests with coverage
@echo "Running tests with coverage..."
@mkdir -p coverage
@go test -coverprofile=coverage/coverage.out ./...
@go tool cover -html=coverage/coverage.out -o coverage/coverage.html
@echo "Coverage report: coverage/coverage.html"
test-e2e: ## Run end-to-end tests (requires docker-compose)
@echo "Running E2E tests..."
@go test -v -tags=e2e ./tests/e2e/...
bench: ## Run benchmarks
@echo "Running benchmarks..."
@go test -bench=. -benchmem ./...
# =============================================================================
# Code Quality
# =============================================================================
lint: ## Run linter
@echo "Running linter..."
@golangci-lint run ./...
lint-fix: ## Run linter with auto-fix
@echo "Running linter with auto-fix..."
@golangci-lint run --fix ./...
fmt: ## Format code
@echo "Formatting code..."
@go fmt ./...
vet: ## Run go vet
@echo "Running go vet..."
@go vet ./...
check: fmt vet lint ## Run all code quality checks
# =============================================================================
# Dependencies & Tools
# =============================================================================
install: install-tools ## Install all dependencies and tools
@echo "Installing Go dependencies..."
@go mod tidy
install-tools: ## Install development tools
@echo "Installing development tools..."
@go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
@go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
@go install github.com/bufbuild/buf/cmd/buf@latest
@go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest
install-sebuf: ## Install sebuf plugins for HTTP handler generation
@echo "Installing sebuf plugins..."
@go install github.com/SebastienMelki/sebuf/cmd/protoc-gen-go-http@latest
@go install github.com/SebastienMelki/sebuf/cmd/protoc-gen-openapiv3@latest
# =============================================================================
# Protocol Buffers
# =============================================================================
buf-generate: ## Generate protobuf code with buf
@echo "Generating protobuf code..."
@buf generate
buf-lint: ## Lint proto files
@echo "Linting proto files..."
@buf lint
buf-breaking: ## Check for breaking changes
@echo "Checking for breaking changes..."
@buf breaking --against '.git#branch=main'
generate: buf-generate ## Generate all code (alias for buf-generate)
# =============================================================================
# Docker
# =============================================================================
docker-build: ## Build Docker images
@echo "Building Docker images..."
@docker-compose build
docker-up: ## Start local development environment
@echo "Starting local environment..."
@docker-compose up -d
@echo ""
@echo "Services started:"
@echo " - HTTP Server: http://localhost:8080"
@echo " - NATS: localhost:4222 (monitoring: http://localhost:8222)"
@echo " - MinIO: http://localhost:9001 (minioadmin/minioadmin)"
@echo " - Trino: http://localhost:8085"
@echo " - Redash: http://localhost:5050"
@echo " - Hive Metastore: localhost:9083"
@echo " - PostgreSQL: localhost:5432"
docker-down: ## Stop local development environment
@echo "Stopping local environment..."
@docker-compose down
docker-restart: ## Restart all services
@docker-compose down
@docker-compose up -d
docker-logs: ## View logs from all services
@docker-compose logs -f
docker-logs-server: ## View server logs
@docker-compose logs -f causality-server
docker-logs-sink: ## View warehouse sink logs
@docker-compose logs -f warehouse-sink
docker-logs-trino: ## View Trino logs
@docker-compose logs -f trino
docker-logs-reaction: ## View reaction engine logs
@docker-compose logs -f reaction-engine
docker-clean: ## Remove all containers and volumes
@echo "Cleaning Docker resources..."
@docker-compose down -v --remove-orphans
docker-rebuild: docker-clean docker-build docker-up ## Clean rebuild everything
docker-ps: ## Show running containers
@docker-compose ps
# =============================================================================
# Trino & Data
# =============================================================================
trino-cli: ## Open Trino CLI
@docker exec -it causality-trino trino
trino-init: ## Create Trino schema and tables
@echo "Creating Trino schema and tables..."
@docker exec causality-trino trino --execute "CREATE SCHEMA IF NOT EXISTS hive.causality WITH (location = 's3a://causality-events/')"
@docker exec causality-trino trino --execute "CREATE TABLE IF NOT EXISTS hive.causality.events (id VARCHAR, device_id VARCHAR, timestamp_ms BIGINT, correlation_id VARCHAR, event_category VARCHAR, event_type VARCHAR, platform VARCHAR, os_version VARCHAR, app_version VARCHAR, build_number VARCHAR, device_model VARCHAR, manufacturer VARCHAR, screen_width INTEGER, screen_height INTEGER, locale VARCHAR, timezone VARCHAR, network_type VARCHAR, carrier VARCHAR, is_jailbroken BOOLEAN, is_emulator BOOLEAN, sdk_version VARCHAR, payload_json VARCHAR, app_id VARCHAR, year INTEGER, month INTEGER, day INTEGER, hour INTEGER) WITH (format = 'PARQUET', partitioned_by = ARRAY['app_id', 'year', 'month', 'day', 'hour'], external_location = 's3a://causality-events/events/')"
@echo "Tables created successfully"
trino-sync: ## Sync Trino partitions from S3
@echo "Syncing partitions..."
@docker exec causality-trino trino --execute "CALL hive.system.sync_partition_metadata('causality', 'events', 'FULL')"
trino-query: ## Query recent events (usage: make trino-query SQL="SELECT * FROM hive.causality.events LIMIT 10")
@docker exec causality-trino trino --execute "$(SQL)"
trino-count: ## Count events in warehouse
@docker exec causality-trino trino --execute "SELECT count(*) as total_events FROM hive.causality.events"
trino-stats: ## Show event statistics
@docker exec causality-trino trino --execute "SELECT event_category, event_type, count(*) as count FROM hive.causality.events GROUP BY event_category, event_type ORDER BY count DESC"
# =============================================================================
# Testing & Events
# =============================================================================
test-event: ## Send a single test event
@NOW=$$(date +%s)000; \
curl -s -X POST http://localhost:8080/v1/events/ingest \
-H "Content-Type: application/json" \
-H "X-API-Key: $(DEV_API_KEY)" \
-d '{"event":{"appId":"dev-app","deviceId":"device-001","timestampMs":'$$NOW',"screenView":{"screenName":"HomeScreen"}}}' | jq .
test-batch: ## Send a batch of test events
@NOW=$$(date +%s)000; \
curl -s -X POST http://localhost:8080/v1/events/batch \
-H "Content-Type: application/json" \
-H "X-API-Key: $(DEV_API_KEY)" \
-d '{"events":[{"appId":"dev-app","deviceId":"d1","timestampMs":'$$NOW',"screenView":{"screenName":"Home"}},{"appId":"dev-app","deviceId":"d2","timestampMs":'$$NOW',"buttonTap":{"buttonId":"btn-login","screenName":"Home"}},{"appId":"dev-app","deviceId":"d3","timestampMs":'$$NOW',"userLogin":{"userId":"user-123","method":"email"}}]}' | jq .
test-load: ## Send 100 test events for load testing
@echo "Sending 100 test events..."
@NOW=$$(date +%s)000; \
for i in $$(seq 1 10); do \
curl -s -X POST http://localhost:8080/v1/events/batch \
-H "Content-Type: application/json" \
-H "X-API-Key: $(DEV_API_KEY)" \
-d '{"events":[{"appId":"dev-app","deviceId":"d'$$i'","timestampMs":'$$NOW',"screenView":{"screenName":"Screen'$$i'"}},{"appId":"dev-app","deviceId":"d'$$i'","timestampMs":'$$NOW',"buttonTap":{"buttonId":"btn-'$$i'","screenName":"Screen'$$i'"}},{"appId":"dev-app","deviceId":"d'$$i'","timestampMs":'$$NOW',"userLogin":{"userId":"user-'$$i'","method":"email"}},{"appId":"dev-app","deviceId":"d'$$i'","timestampMs":'$$NOW',"productView":{"productId":"prod-'$$i'","productName":"Product '$$i'","priceCents":999}},{"appId":"dev-app","deviceId":"d'$$i'","timestampMs":'$$NOW',"addToCart":{"productId":"prod-'$$i'","quantity":1,"priceCents":999}},{"appId":"dev-app","deviceId":"d'$$i'","timestampMs":'$$NOW',"purchaseComplete":{"orderId":"order-'$$i'","totalCents":999,"currency":"USD"}},{"appId":"dev-app","deviceId":"d'$$i'","timestampMs":'$$NOW',"appStart":{"isColdStart":true,"launchDurationMs":150}},{"appId":"dev-app","deviceId":"d'$$i'","timestampMs":'$$NOW',"customEvent":{"eventName":"test_event_'$$i'","stringParams":{"key":"value"}}},{"appId":"dev-app","deviceId":"d'$$i'","timestampMs":'$$NOW',"screenExit":{"screenName":"Screen'$$i'","durationMs":5000}},{"appId":"dev-app","deviceId":"d'$$i'","timestampMs":'$$NOW',"networkChange":{}}]}' > /dev/null; \
done
@echo "Done! Sent 100 events"
test-random: ## Send random events with variation (for realistic graphs)
@echo "Sending random events..."
@for round in $$(seq 1 20); do \
NOW=$$(date +%s)000; \
device=$$((RANDOM % 100)); \
screen_count=$$((RANDOM % 10 + 1)); \
for s in $$(seq 1 $$screen_count); do \
curl -s -X POST http://localhost:8080/v1/events/ingest -H "Content-Type: application/json" -H "X-API-Key: $(DEV_API_KEY)" \
-d '{"event":{"appId":"dev-app","deviceId":"device-'$$device'","timestampMs":'$$NOW',"screenView":{"screenName":"Screen'$$((RANDOM % 20))'"}}}' > /dev/null; \
done; \
btn_count=$$((RANDOM % 8)); \
for b in $$(seq 1 $$btn_count); do \
curl -s -X POST http://localhost:8080/v1/events/ingest -H "Content-Type: application/json" -H "X-API-Key: $(DEV_API_KEY)" \
-d '{"event":{"appId":"dev-app","deviceId":"device-'$$device'","timestampMs":'$$NOW',"buttonTap":{"buttonId":"btn-'$$((RANDOM % 15))'","screenName":"Screen'$$((RANDOM % 20))'"}}}' > /dev/null; \
done; \
if [ $$((RANDOM % 3)) -eq 0 ]; then \
curl -s -X POST http://localhost:8080/v1/events/ingest -H "Content-Type: application/json" -H "X-API-Key: $(DEV_API_KEY)" \
-d '{"event":{"appId":"dev-app","deviceId":"device-'$$device'","timestampMs":'$$NOW',"userLogin":{"userId":"user-'$$((RANDOM % 50))'","method":"email"}}}' > /dev/null; \
fi; \
if [ $$((RANDOM % 4)) -eq 0 ]; then \
curl -s -X POST http://localhost:8080/v1/events/ingest -H "Content-Type: application/json" -H "X-API-Key: $(DEV_API_KEY)" \
-d '{"event":{"appId":"dev-app","deviceId":"device-'$$device'","timestampMs":'$$NOW',"productView":{"productId":"prod-'$$((RANDOM % 30))'","productName":"Product","priceCents":'$$((RANDOM % 10000 + 100))'}}}' > /dev/null; \
fi; \
if [ $$((RANDOM % 6)) -eq 0 ]; then \
curl -s -X POST http://localhost:8080/v1/events/ingest -H "Content-Type: application/json" -H "X-API-Key: $(DEV_API_KEY)" \
-d '{"event":{"appId":"dev-app","deviceId":"device-'$$device'","timestampMs":'$$NOW',"addToCart":{"productId":"prod-'$$((RANDOM % 30))'","quantity":'$$((RANDOM % 5 + 1))',"priceCents":'$$((RANDOM % 10000 + 100))'}}}' > /dev/null; \
fi; \
if [ $$((RANDOM % 10)) -eq 0 ]; then \
curl -s -X POST http://localhost:8080/v1/events/ingest -H "Content-Type: application/json" -H "X-API-Key: $(DEV_API_KEY)" \
-d '{"event":{"appId":"dev-app","deviceId":"device-'$$device'","timestampMs":'$$NOW',"purchaseComplete":{"orderId":"order-'$$RANDOM'","totalCents":'$$((RANDOM % 50000 + 500))',"currency":"USD"}}}' > /dev/null; \
fi; \
curl -s -X POST http://localhost:8080/v1/events/ingest -H "Content-Type: application/json" -H "X-API-Key: $(DEV_API_KEY)" \
-d '{"event":{"appId":"dev-app","deviceId":"device-'$$device'","timestampMs":'$$NOW',"appStart":{"isColdStart":'$$([ $$((RANDOM % 2)) -eq 0 ] && echo true || echo false)',"launchDurationMs":'$$((RANDOM % 2000 + 100))'}}}' > /dev/null; \
echo -n "."; \
done
@echo ""
@echo "Done! Sent random events with variation"
health: ## Check server health
@curl -s http://localhost:8080/health | jq .
ready: ## Check server readiness
@curl -s http://localhost:8080/ready | jq .
# =============================================================================
# NATS
# =============================================================================
nats-streams: ## List NATS streams with message counts
@curl -s "http://localhost:8222/jsz?streams=true" | jq '{total_messages: .messages, total_bytes: .bytes, streams: [.account_details[].stream_detail[] | {name, messages: .state.messages, bytes: .state.bytes, consumers: .state.consumer_count}]}'
nats-consumers: ## List NATS consumers
@curl -s "http://localhost:8222/jsz?consumers=true" | jq '.streams[].consumers'
nats-info: ## Show NATS server info
@curl -s "http://localhost:8222/varz" | jq '{server_id, version, max_connections, subscriptions, messages_in: .in_msgs, messages_out: .out_msgs}'
# =============================================================================
# MinIO
# =============================================================================
minio-ls: ## List objects in MinIO bucket
@docker exec causality-minio mc ls local/causality-events --recursive
minio-size: ## Show bucket size
@docker exec causality-minio mc du local/causality-events
# =============================================================================
# Mobile SDK (gomobile)
# =============================================================================
.PHONY: mobile-setup mobile-ios mobile-android mobile-all mobile-clean mobile-test
mobile-setup: ## Install gomobile and initialize
@echo "Installing gomobile..."
@go install golang.org/x/mobile/cmd/gomobile@latest
@gomobile init
mobile-ios: ## Build iOS SDK (.xcframework)
@./scripts/gomobile-build.sh ios
mobile-android: ## Build Android SDK (.aar)
@./scripts/gomobile-build.sh android
mobile-all: ## Build both iOS and Android SDKs
@./scripts/gomobile-build.sh all
mobile-clean: ## Clean mobile build artifacts
@echo "Cleaning mobile build artifacts..."
@rm -rf build/mobile
mobile-test: ## Run mobile SDK tests and verify no CGO
@echo "Running mobile SDK tests..."
@go test ./sdk/mobile/... -v -race
@echo "Verifying no CGO dependencies..."
@go list -f '{{.CgoFiles}}' ./sdk/mobile/... | grep -v '^\[\]$$' && echo "ERROR: CGO files found!" && exit 1 || echo "OK: No CGO dependencies"
# =============================================================================
# WebAssembly SDK
# =============================================================================
wasm: ## Build WebAssembly SDK
@echo "Building WASM..."
@mkdir -p wasm
@GOOS=js GOARCH=wasm go build -o wasm/causality.wasm ./wasm
@cp "$$(go env GOROOT)/misc/wasm/wasm_exec.js" wasm/
# =============================================================================
# Utilities
# =============================================================================
verify: ## Verify module dependencies
@echo "Verifying dependencies..."
@go mod verify
tidy: ## Tidy and verify module dependencies
@echo "Tidying dependencies..."
@go mod tidy
@go mod verify
update-deps: ## Update all dependencies
@echo "Updating dependencies..."
@go get -u ./...
@go mod tidy
# =============================================================================
# Quick Start
# =============================================================================
quickstart: install docker-up ## Quick start: install deps and start environment
@echo ""
@echo "Quick start complete!"
@echo ""
@echo "Wait for services to be ready, then run:"
@echo " make health - Check server health"
@echo " make test-event - Send a test event"
@echo " make test-load - Send 100 test events"
@echo " make trino-init - Create Trino tables"
@echo " make trino-sync - Sync partitions"
@echo " make trino-stats - View event statistics"
dev: docker-clean docker-up dev-wait trino-init ## Start dev environment with tables (clean start)
@echo "Dev environment ready!"
@echo " make test-event - Send test event"
@echo " make trino-cli - Open Trino CLI"
dev-wait: ## Wait for services to be healthy
@echo "Waiting for services to be ready..."
@until curl -s http://localhost:8080/health > /dev/null 2>&1; do sleep 2; done
@echo " ✓ HTTP Server ready"
@echo "Waiting for Trino to fully initialize..."
@until docker exec causality-trino trino --execute "SELECT 1" > /dev/null 2>&1; do sleep 3; done
@echo " ✓ Trino ready"
@echo "All services ready!"
dev-logs: docker-logs ## Tail all logs (alias)
dev-rebuild: ## Rebuild and restart only the Go services (uses cache)
@docker-compose up -d --build --force-recreate causality-server warehouse-sink reaction-engine
dev-rebuild-clean: ## Rebuild Go services from scratch (no cache)
@echo "Rebuilding Go services without cache..."
@docker-compose build --no-cache causality-server warehouse-sink reaction-engine
@docker-compose up -d --force-recreate causality-server warehouse-sink reaction-engine
@echo "Waiting for services..."
@sleep 5
@echo "Services rebuilt and restarted"
api-docs: ## Open OpenAPI spec
@echo "OpenAPI spec: api/openapi/EventService.openapi.yaml"
@cat api/openapi/EventService.openapi.yaml
.PHONY: all
all: install generate build test ## Build everything