Skip to content

Commit 8c2311c

Browse files
author
kipruto45
committed
ci: add weekly monitoring workflow with anomaly log artifacts
1 parent 7587a09 commit 8c2311c

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
name: Scheduled Monitoring
2+
3+
on:
4+
schedule:
5+
- cron: "0 5 * * 1"
6+
workflow_dispatch:
7+
8+
jobs:
9+
monitoring-checks:
10+
runs-on: ubuntu-latest
11+
12+
services:
13+
postgres:
14+
image: postgres:16
15+
env:
16+
POSTGRES_DB: sql_data_engineering
17+
POSTGRES_USER: postgres
18+
POSTGRES_PASSWORD: postgres
19+
ports:
20+
- 5432:5432
21+
options: >-
22+
--health-cmd "pg_isready -U postgres -d sql_data_engineering"
23+
--health-interval 10s
24+
--health-timeout 5s
25+
--health-retries 10
26+
27+
steps:
28+
- name: Checkout repository
29+
uses: actions/checkout@v4
30+
31+
- name: Install PostgreSQL client
32+
run: |
33+
sudo apt-get update
34+
sudo apt-get install -y postgresql-client
35+
36+
- name: Wait for PostgreSQL readiness
37+
env:
38+
PGPASSWORD: postgres
39+
run: |
40+
for i in {1..30}; do
41+
if pg_isready -h 127.0.0.1 -p 5432 -U postgres -d sql_data_engineering; then
42+
exit 0
43+
fi
44+
sleep 2
45+
done
46+
echo "PostgreSQL service did not become ready in time."
47+
exit 1
48+
49+
- name: Prepare environment
50+
run: |
51+
cat > .env <<'EOF'
52+
DB_HOST=127.0.0.1
53+
DB_PORT=5432
54+
DB_NAME=sql_data_engineering
55+
DB_USER=postgres
56+
DB_PASSWORD=postgres
57+
DATABASE_URL=postgresql://postgres:postgres@127.0.0.1:5432/sql_data_engineering
58+
EOF
59+
60+
- name: Run ETL baseline
61+
env:
62+
DATABASE_URL: postgresql://postgres:postgres@127.0.0.1:5432/sql_data_engineering
63+
run: |
64+
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f database/schema.sql
65+
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f database/tables.sql
66+
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f database/constraints.sql
67+
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f database/indexes.sql
68+
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f etl/extract.sql
69+
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f etl/transform.sql
70+
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f etl/load.sql
71+
72+
- name: Run monitoring checks
73+
id: monitoring
74+
env:
75+
DATABASE_URL: postgresql://postgres:postgres@127.0.0.1:5432/sql_data_engineering
76+
run: |
77+
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f monitoring/row_count_checks.sql | tee monitoring_row_count.log
78+
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f monitoring/anomaly_detection.sql | tee monitoring_anomaly.log
79+
80+
ANOMALY_COUNT="$(psql "$DATABASE_URL" -tA <<'SQL'
81+
WITH daily_revenue AS (
82+
SELECT
83+
o.order_date AS day,
84+
SUM(CASE WHEN o.status = 'PAID' AND t.status = 'SUCCESS' THEN t.amount ELSE 0 END) AS revenue
85+
FROM oltp.orders o
86+
LEFT JOIN oltp.transactions t ON t.order_id = o.order_id
87+
GROUP BY o.order_date
88+
), stats AS (
89+
SELECT
90+
day,
91+
revenue,
92+
AVG(revenue) OVER () AS avg_revenue,
93+
STDDEV_SAMP(revenue) OVER () AS std_revenue
94+
FROM daily_revenue
95+
)
96+
SELECT COUNT(*)
97+
FROM stats
98+
WHERE ABS((revenue - avg_revenue) / NULLIF(std_revenue, 0)) >= 2.0;
99+
SQL
100+
)"
101+
ANOMALY_COUNT="$(echo "$ANOMALY_COUNT" | tr -d '[:space:]')"
102+
103+
echo "anomaly_count=${ANOMALY_COUNT}" >> "$GITHUB_OUTPUT"
104+
echo "Anomaly count: ${ANOMALY_COUNT}"
105+
106+
if [ "${ANOMALY_COUNT}" -gt 0 ]; then
107+
echo "::warning::Detected ${ANOMALY_COUNT} potential revenue anomaly row(s). Review monitoring_anomaly.log artifact."
108+
fi
109+
110+
- name: Upload monitoring logs
111+
uses: actions/upload-artifact@v4
112+
with:
113+
name: monitoring-logs
114+
path: |
115+
monitoring_row_count.log
116+
monitoring_anomaly.log

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ sql-data-engineering-project/
2828

2929
- GitHub Actions workflow: `.github/workflows/ci.yml`
3030
- Runs the full pipeline (`./run_all.sh`) on pushes and pull requests to `main`.
31+
- Scheduled monitoring workflow: `.github/workflows/monitoring_schedule.yml`
32+
- Runs weekly and on manual trigger; publishes monitoring logs as workflow artifacts.
3133

3234
## Quick Start
3335

0 commit comments

Comments
 (0)