-
Notifications
You must be signed in to change notification settings - Fork 1
369 lines (338 loc) · 14.1 KB
/
Copy pathci.yml
File metadata and controls
369 lines (338 loc) · 14.1 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
name: CI
on:
push:
branches: ["main", "claude/**", "feature/**"]
pull_request:
branches: ["main"]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
PYTHON_VERSION: "3.11"
UV_VERSION: "0.4.4"
jobs:
# ── 1. Lint & Format ────────────────────────────────────────────────────────
lint:
name: Lint & Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: ${{ env.UV_VERSION }}
- name: Set up Python
run: uv python install ${{ env.PYTHON_VERSION }}
- name: Create venv
working-directory: producers
run: uv venv
- name: Install dev dependencies
working-directory: producers
run: uv pip install ruff
- name: Ruff lint
working-directory: producers
run: uv run ruff check .
- name: Ruff format check
working-directory: producers
run: uv run ruff format --check .
# ── 2. Unit Tests ───────────────────────────────────────────────────────────
test:
name: Unit Tests (Python ${{ matrix.python-version }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: ${{ env.UV_VERSION }}
- name: Set up Python ${{ matrix.python-version }}
run: uv python install ${{ matrix.python-version }}
- name: Create venv
working-directory: producers
run: uv venv
- name: Install dependencies
working-directory: producers
run: |
uv pip install \
faker>=24.11.0 \
pydantic>=2.7.1 \
geopy>=2.4.1 \
pytest>=8.2.0 \
pytest-mock>=3.14.0
- name: Run tests
working-directory: producers
run: uv run python -m pytest tests/ -v --tb=short --color=yes
- name: Upload test results
if: always()
uses: actions/upload-artifact@v4
with:
name: test-results-py${{ matrix.python-version }}
path: producers/.pytest_cache/
# ── 3. Docker Build ─────────────────────────────────────────────────────────
docker-build:
name: Docker Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build producer image
uses: docker/build-push-action@v5
with:
context: ./producers
push: false
tags: fraud-detection-producer:ci
cache-from: type=gha
cache-to: type=gha,mode=max
# ── 4. dbt Compilation ──────────────────────────────────────────────────────
dbt-compile:
name: dbt Compilation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: ${{ env.UV_VERSION }}
- name: Set up Python
run: uv python install ${{ env.PYTHON_VERSION }}
- name: Create venv
run: uv venv
- name: Install dbt-risingwave
run: uv pip install dbt-risingwave==1.9.7
- name: Compile dbt models
working-directory: fraud_detection
run: |
# Dry run compilation without connecting to RisingWave
uv run dbt parse --profiles-dir .
env:
RISINGWAVE_HOST: localhost
RISINGWAVE_PORT: "4566"
RISINGWAVE_USER: root
RISINGWAVE_PASSWORD: ""
RISINGWAVE_DB: dev
# ── 5. Compose Config Validation ────────────────────────────────────────────
compose-validate:
name: Compose Config Validation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Copy .env
run: cp .env.example .env
- name: Validate docker-compose config
run: docker compose config --quiet
# ── 6. End-to-End Integration Test ─────────────────────────────────────────
# DISABLED: Uncomment to re-enable
# e2e:
# name: End-to-End Integration Test
# runs-on: ubuntu-latest
# if: github.event_name == 'pull_request' || github.ref == 'refs/heads/main'
# needs: [lint, test, docker-build]
# timeout-minutes: 20
#
# env:
# # Longer warm-up than the fastest tumble windows so every signal has a
# # real chance to fire at least once before assertions start polling.
# WARMUP_SECONDS: "180"
# # Each assertion polls up to this many seconds waiting for the target
# # row count — prevents flakiness from RisingWave MV refresh cadence.
# ASSERT_TIMEOUT: "180"
#
# steps:
# - uses: actions/checkout@v4
#
# - name: Copy .env
# run: cp .env.example .env
#
# - name: Start services
# run: docker compose up -d --build
#
# - name: Wait for RisingWave to be ready
# run: |
# echo "Waiting for RisingWave SQL endpoint..."
# timeout 180 bash -c '
# until docker run --rm --network fraud-detection-streaming_fraud-net \
# -e PGPASSWORD="" postgres:15-alpine \
# psql -h risingwave -p 4566 -U root -d dev -t -c "SELECT 1;" \
# >/dev/null 2>&1; do
# sleep 3
# done
# '
# echo "RisingWave SQL ready."
#
# - name: Wait for risingwave-init to complete (SQL DDL applied)
# run: |
# echo "Waiting for DDL initialisation..."
# timeout 180 bash -c '
# until docker inspect -f "{{.State.Status}}" fraud-rw-init 2>/dev/null \
# | grep -qE "^(exited|dead)$"; do
# sleep 3
# done
# '
# rc=$(docker inspect -f '{{.State.ExitCode}}' fraud-rw-init)
# echo "risingwave-init exit code: $rc"
# [ "$rc" = "0" ] || (docker logs fraud-rw-init && exit 1)
#
# - name: Wait for producer to start publishing
# run: |
# echo "Waiting for producer to publish KYC seed..."
# timeout 120 bash -c '
# until docker compose logs producer 2>&1 | grep -q "KYC seed complete"; do
# sleep 3
# done
# '
# echo "Producer is publishing events."
#
# - name: Wait for pipeline warm-up
# run: |
# echo "Warm-up: ${WARMUP_SECONDS}s for tumbling windows to produce rows…"
# sleep "$WARMUP_SECONDS"
#
# - name: Assert Redpanda topics have messages
# run: |
# set -e
# for topic in transactions login_events alert_events kyc_profile_events; do
# # Read 1 record from the start with a 10s wall-clock cap.
# got=$(timeout 10 docker compose exec -T redpanda rpk topic consume "$topic" \
# --brokers redpanda:29092 -n 1 -o start -f '%o\n' 2>/dev/null \
# | head -n 1 || true)
# if [ -n "$got" ]; then
# echo " ${topic}: has messages (offset=${got}) ✓"
# else
# echo "ERROR: topic ${topic} produced no records within 10s"
# exit 1
# fi
# done
#
# - name: Assert staging views populate (retry)
# run: |
# PSQL="docker run --rm --network fraud-detection-streaming_fraud-net \
# -e PGPASSWORD= postgres:15-alpine \
# psql -h risingwave -p 4566 -U root -d dev -t -A -c"
# assert_nonzero() {
# local view=$1 deadline=$(( $(date +%s) + ASSERT_TIMEOUT ))
# while [ "$(date +%s)" -lt "$deadline" ]; do
# rows=$($PSQL "SELECT COUNT(*) FROM ${view};" 2>/dev/null | tr -d ' \n' || true)
# if [ -n "$rows" ] && [ "$rows" -gt 0 ] 2>/dev/null; then
# echo " ${view}: ${rows} rows ✓"
# return 0
# fi
# sleep 5
# done
# echo "ERROR: ${view} still empty after ${ASSERT_TIMEOUT}s"
# return 1
# }
# for view in stg_transactions stg_login_events stg_alert_events stg_kyc_profiles; do
# assert_nonzero "$view" || exit 1
# done
#
# - name: Report fraud signal view populations
# run: |
# PSQL="docker run --rm --network fraud-detection-streaming_fraud-net \
# -e PGPASSWORD= postgres:15-alpine \
# psql -h risingwave -p 4566 -U root -d dev -t -A -c"
# for view in mv_velocity_alerts mv_cnp_spike mv_login_failure_storm mv_structuring_detection mv_device_anomalies mv_correlated_alert_burst; do
# rows=$($PSQL "SELECT COUNT(*) FROM ${view};" 2>/dev/null | tr -d ' \n' || echo "ERR")
# echo " ${view}: ${rows}"
# done
#
# - name: Assert risk score view populates (retry)
# run: |
# PSQL="docker run --rm --network fraud-detection-streaming_fraud-net \
# -e PGPASSWORD= postgres:15-alpine \
# psql -h risingwave -p 4566 -U root -d dev -t -A -c"
# deadline=$(( $(date +%s) + ASSERT_TIMEOUT ))
# while [ "$(date +%s)" -lt "$deadline" ]; do
# rows=$($PSQL "SELECT COUNT(*) FROM mv_account_risk_score_realtime;" 2>/dev/null | tr -d ' \n' || true)
# if [ -n "$rows" ] && [ "$rows" -gt 0 ] 2>/dev/null; then
# echo "mv_account_risk_score_realtime: ${rows} accounts ✓"
# exit 0
# fi
# sleep 5
# done
# echo "ERROR: mv_account_risk_score_realtime never populated"
# exit 1
#
# - name: Assert fraud cases exist (retry)
# run: |
# PSQL="docker run --rm --network fraud-detection-streaming_fraud-net \
# -e PGPASSWORD= postgres:15-alpine \
# psql -h risingwave -p 4566 -U root -d dev -t -A -c"
# deadline=$(( $(date +%s) + ASSERT_TIMEOUT ))
# while [ "$(date +%s)" -lt "$deadline" ]; do
# cases=$($PSQL "SELECT COUNT(*) FROM mv_open_fraud_cases;" 2>/dev/null | tr -d ' \n' || true)
# if [ -n "$cases" ] && [ "$cases" -gt 0 ] 2>/dev/null; then
# echo "mv_open_fraud_cases: ${cases} ✓"
# exit 0
# fi
# sleep 5
# done
# echo "ERROR: no open cases — fraud injection may not be reaching the detector"
# exit 1
#
# - name: Assert Grafana is reachable
# run: |
# deadline=$(( $(date +%s) + 60 ))
# while [ "$(date +%s)" -lt "$deadline" ]; do
# if curl -sf http://localhost:3000/api/health >/dev/null; then
# echo "Grafana is healthy ✓"
# exit 0
# fi
# sleep 3
# done
# echo "ERROR: Grafana /api/health unreachable"
# exit 1
#
# - name: Assert Grafana datasource queries RisingWave
# run: |
# code=$(curl -s -o /tmp/gf.json -w '%{http_code}' -u admin:admin \
# -X POST 'http://localhost:3000/api/ds/query' \
# -H 'Content-Type: application/json' \
# -d '{"queries":[{"refId":"A","datasource":{"type":"grafana-postgresql-datasource","uid":"risingwave"},"rawSql":"SELECT COUNT(*) AS n FROM mv_fraud_kpis_1min","format":"table"}]}')
# echo "HTTP ${code}"
# cat /tmp/gf.json
# echo ""
# [ "$code" = "200" ] || (echo "Grafana datasource query returned HTTP ${code}" && exit 1)
# # Per-query status must be 200 (not a datasource-side error)
# if grep -Eq '"status"[[:space:]]*:[[:space:]]*[45][0-9][0-9]' /tmp/gf.json; then
# echo "Grafana reported a query error"
# exit 1
# fi
# echo "Grafana → RisingWave OK ✓"
#
# - name: Print KPI snapshot
# if: always()
# run: |
# PSQL="docker run --rm --network fraud-detection-streaming_fraud-net \
# -e PGPASSWORD= postgres:15-alpine \
# psql -h risingwave -p 4566 -U root -d dev -c"
# echo "=== Fraud KPIs (last 3 minutes) ==="
# $PSQL "SELECT window_start, total_transactions, flagged_transactions, fraud_rate_pct FROM mv_fraud_kpis_1min ORDER BY window_start DESC LIMIT 3;" || true
# echo "=== Top Critical Risk Accounts ==="
# $PSQL "SELECT account_id, risk_score, risk_tier, contributing_signals FROM mv_account_risk_score_realtime WHERE risk_tier IN ('critical','high') ORDER BY risk_score DESC LIMIT 5;" || true
# echo "=== Open Fraud Cases ==="
# $PSQL "SELECT customer_id, account_id, ROUND(risk_score::numeric, 3) AS risk_score, risk_tier, recommended_action FROM mv_open_fraud_cases ORDER BY risk_score DESC LIMIT 10;" || true
#
# - name: Dump diagnostics on failure
# if: failure()
# run: |
# echo "=== docker compose ps ==="
# docker compose ps || true
# echo "=== producer logs (tail 200) ==="
# docker compose logs --tail=200 producer || true
# echo "=== risingwave-init logs ==="
# docker logs fraud-rw-init || true
# echo "=== risingwave logs (tail 100) ==="
# docker compose logs --tail=100 risingwave || true
# echo "=== redpanda logs (tail 50) ==="
# docker compose logs --tail=50 redpanda || true
# echo "=== grafana logs (tail 50) ==="
# docker compose logs --tail=50 grafana || true
#
# - name: Tear down
# if: always()
# run: docker compose down -v