materialize event counts in metrics endpoint

The metrics endpoint stored a lazy peewee query on the shared Prometheus collector, and collect() ran it later. Overlapping scrapes run in separate threadpool threads, so two requests could execute and step the same query's sqlite cursor at once. The rows are now read with list() before they reach the collector.
This commit is contained in:
Josh Hawkins
2026-10-22 15:15:43 -05:00
parent 91e91c68ef
commit 463cf29386
+3 -1
View File
@@ -177,7 +177,9 @@ def metrics(request: Request):
# Retrieve the latest statistics and update the Prometheus metrics
stats = request.app.stats_emitter.get_latest_stats()
# query DB for count of events by camera, label
event_counts: list[dict[str, Any]] = (
# Materialize the rows; the collector is shared across request threads and a
# lazy query would step one sqlite cursor from two threads.
event_counts: list[dict[str, Any]] = list(
Event.select(Event.camera, Event.label, fn.Count())
.group_by(Event.camera, Event.label)
.dicts()