Use peewee instead of rw sql for the CTE query.

This commit is contained in:
Greg 2026-05-11 16:46:43 -07:00
parent 328a26b169
commit 39fba9b0a7

View File

@ -392,27 +392,66 @@ def events_explore(
if not allowed_cameras: if not allowed_cameras:
return JSONResponse(content=[]) return JSONResponse(content=[])
explore_columns = (
Event.id,
Event.camera,
Event.label,
Event.sub_label,
Event.zones,
Event.start_time,
Event.end_time,
Event.has_clip,
Event.has_snapshot,
Event.plus_id,
Event.retain_indefinitely,
Event.top_score,
Event.false_positive,
Event.box,
Event.data,
)
# Single query: per-label COUNT and top-N ranking by start_time computed # Single query: per-label COUNT and top-N ranking by start_time computed
# via window functions in a CTE, then filtered to rn <= limit. Replaces # via window functions in a CTE, then filtered to rn <= limit. Replaces
# the previous loop that issued 2 queries per distinct label. # the previous loop that issued 2 queries per distinct label.
camera_placeholders = ",".join(["?"] * len(allowed_cameras)) event_count = fn.COUNT(Event.id).over(partition_by=[Event.label]).alias("event_count")
sql = f""" rn = fn.ROW_NUMBER().over(
WITH ranked AS ( partition_by=[Event.label], order_by=[Event.start_time.desc()]
SELECT ).alias("rn")
id, camera, label, sub_label, zones, start_time, end_time,
has_clip, has_snapshot, plus_id, retain_indefinitely, base_query = (
top_score, false_positive, box, data, Event.select(
COUNT(*) OVER (PARTITION BY label) AS event_count, *explore_columns,
ROW_NUMBER() OVER ( event_count,
PARTITION BY label ORDER BY start_time DESC rn,
) AS rn
FROM event
WHERE camera IN ({camera_placeholders})
) )
SELECT * FROM ranked .where(Event.camera << allowed_cameras)
WHERE rn <= ? )
ORDER BY event_count DESC, start_time DESC ranked = base_query.cte("ranked")
""" query = (
Event.select(
ranked.c.id,
ranked.c.camera,
ranked.c.label,
ranked.c.sub_label,
ranked.c.zones,
ranked.c.start_time,
ranked.c.end_time,
ranked.c.has_clip,
ranked.c.has_snapshot,
ranked.c.plus_id,
ranked.c.retain_indefinitely,
ranked.c.top_score,
ranked.c.false_positive,
ranked.c.box,
ranked.c.data,
ranked.c.event_count,
)
.from_(ranked)
.with_cte(ranked)
.where(ranked.c.rn <= limit)
.order_by(ranked.c.event_count.desc(), ranked.c.start_time.desc())
.objects()
)
allowed_data_keys = { allowed_data_keys = {
"type", "type",
@ -450,7 +489,7 @@ def events_explore(
}, },
"event_count": event.event_count, "event_count": event.event_count,
} }
for event in Event.raw(sql, *allowed_cameras, limit) for event in query
] ]
return JSONResponse(content=processed_events) return JSONResponse(content=processed_events)