Learn
08 · Weekly KPI
08: Build-Along - Weekly SQL KPI Report + Anomaly Summary + Email
Goal: build a workflow your team could actually run every week, not a toy syntax demo.
You will implement one practical automation end to end:
- gather KPI rows from SQL,
- compute anomaly flags,
- generate a human-readable digest,
- send the digest by email.
Why This Is A Good Grapheme Use Case
Most teams already do this manually:
- run a SQL report,
- scan for outliers,
- summarize for ops/product,
- send a weekly update.
Grapheme turns that into one explicit, reviewable workflow source.
Business Requirements
Your workflow should:
- produce a weekly report line per day,
- flag anomalies using clear thresholds,
- include a summary line with anomaly count and averages,
- send one email payload that is readable by humans,
- remain deterministic for local practice runs.
Final Workflow File
Reference implementation:
examples/realworld/weekly-sql-kpi-report-alert.gr
Step 1: Build A Deterministic SQL Dataset
Use an inline SQL CTE (values (...)) so the tutorial works without external DB setup.
Why:
- every learner gets the same output,
- anomaly behavior is reproducible,
- no hidden infrastructure dependency.
Step 2: Encode Anomaly Rules In Query Logic
In the SQL layer, flag each row when KPI conditions cross thresholds.
Current rule used in the example:
- anomaly when
churn_rate > 0.050ORmrr < 11000
Why this matters:
- explicit rules are auditable,
- policy and business thresholds stay easy to review.
Step 3: Generate Operator-Readable Lines
Use SQL printf(...) to pre-format per-day report lines and one summary line.
Then in Grapheme:
- extract
rows, - map
line, - join lines into one email body.
This keeps payload shaping explicit in workflow source.
Step 4: Send Email Digest
Send one digest email using smtp.send_mail with:
tosubjectbody
Workflow source already includes this final step.
Run It
CLI path:
GRAPHEME_ALLOWED_SQL_CONNECTIONS=sqlite::memory: \
GRAPHEME_ALLOWED_SMTP_DOMAINS=example.com \
grapheme run examples/realworld/weekly-sql-kpi-report-alert.gr --json
Workspace path:
GRAPHEME_ALLOWED_SQL_CONNECTIONS=sqlite::memory: \
GRAPHEME_ALLOWED_SMTP_DOMAINS=example.com \
cargo run -- run examples/realworld/weekly-sql-kpi-report-alert.gr --json
Expected Output Signals
Look for:
"outcome": "succeeded"- SMTP result object with acceptance state
- email body lines ending with a
SUMMARY | ...row
Failure Drills (Required)
Run these intentionally to learn operations, not just happy-path demos.
- Remove SQL allow-list env var.
- Expect policy denial or connection rejection.
- Remove SMTP allow-list env var.
- Expect mail send denial/failure.
- Tighten anomaly threshold in SQL.
- Verify anomaly_count rises in summary line.
- Loosen anomaly threshold.
- Verify anomaly_count falls.
What To Customize For Real Teams
- Replace inline CTE with your real KPI table query.
- Externalize threshold values (churn/mrr targets) as workflow inputs.
- Add recipients for product, finance, and on-call channels.
- Add a second branch for paging/escalation when anomaly_count exceeds a threshold.
Production Readiness Checklist
Before adopting in production-like operations:
- Confirm output shape contract in JSON mode.
- Verify anomaly thresholds with domain owners.
- Run both success and failure drills.
- Document policy env requirements beside run command.
- Add this workflow to weekly telemetry review notes.
This is where Grapheme moves from language capability to operational leverage.