Skip to content

fix(api): count alerts from organization_alerts in /api/runs/stats - #144

Merged
Goldokpa merged 1 commit into
Climate-Vision:mainfrom
Ayomipo18:fix/runs-stats-alert-counts
Nov 25, 2026
Merged

Goldokpa merged 1 commit into
Climate-Vision:mainfrom
Ayomipo18:fix/runs-stats-alert-counts

Conversation

@Ayomipo18

Copy link
Copy Markdown
Contributor

Summary

GET /api/runs/stats returned a 500 on every database, because it counted unacknowledged alerts in the legacy alerts table, which has no acknowledged column. Alerts are created, listed and acknowledged in organization_alerts, and nothing writes to the legacy table, so both alert counts now come from organization_alerts.

Related Issue

Closes #143

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Refactor / code cleanup
  • CI / build / tooling change

Key Changes

  • get_run_stats counts alerts.total and alerts.unacknowledged from organization_alerts. The response shape is unchanged.
  • New tests/test_run_stats.py. The endpoint had no tests. These run against a temporary database, so the counts are exact and the shared outputs/ database is left alone:
    • an empty database returns all zeros,
    • with 3 alerts, 1 of them acknowledged, the counts are total: 3, unacknowledged: 2,
    • the run counts by status, by analysis type and over the last 7 days, including a run older than 7 days that must not count as recent.

Testing

  • Unit tests pass locally (pytest tests/): 234 passed, 1 skipped, on both Python 3.9 / FastAPI 0.128 and Python 3.11 / FastAPI 0.165. The one failure, test_onnx_inference.py::test_run_inference_reports_backend_and_latency, fails the same way on main locally (no ONNX runtime).
  • Manual API test (curl / OpenAPI docs): covered by the new tests through TestClient.
  • Frontend smoke test (npm run dev): no frontend change.
  • New tests added for this change. All 3 fail on main with sqlite3.OperationalError: no such column: acknowledged and pass with the fix.

flake8 src/ --select=E9,F63,F7,F82 reports 0, and the new test file is black-clean.

With #142: this touches the same lines that #142 moves to routes/runs.py, so whichever merges second needs a small rebase. I'll do it on #142 if this one goes in first.

Checklist

GET /api/runs/stats counted unacknowledged alerts with
"SELECT COUNT(*) FROM alerts WHERE acknowledged = 0", but the legacy alerts
table has no acknowledged column, so the endpoint failed with
"sqlite3.OperationalError: no such column: acknowledged" and a 500 on every
database, empty or not.

Alerts are created, listed and acknowledged in organization_alerts, and
nothing writes to the legacy table, so both alert counts now come from
organization_alerts.

The new tests run against a temporary database: the stats of an empty
database, the alert counts with one of three alerts acknowledged, and the
run counts by status, by analysis type and over the last 7 days. All three
fail without the fix.
@Ayomipo18
Ayomipo18 requested a review from Goldokpa as a code owner November 24, 2026 21:12
@Goldokpa

Copy link
Copy Markdown
Member

Thanks @Ayomipo18, good catch. The endpoint has been returning a 500 since it was added, and nothing covered it. Counting from organization_alerts is right, since nothing writes to the legacy table anymore, and the tests on a temporary database pin the counts down properly. Merging.

@Goldokpa
Goldokpa merged commit 16a9201 into Climate-Vision:main Nov 25, 2026
4 checks passed
Ayomipo18 added a commit to Ayomipo18/ClimateVision that referenced this pull request Nov 25, 2026
Brings in Climate-Vision#144. Its fix to get_run_stats, which now counts alerts from
organization_alerts, is applied in routes/runs.py, where the endpoint
moved to; main.py keeps the refactored version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GET /api/runs/stats returns 500: no such column: acknowledged

2 participants