🚧 Add better page browsing
This commit is contained in:
+18
@@ -1563,6 +1563,11 @@ def create_app(
|
||||
limit: int = Query(100, gt=0, description="Page size"),
|
||||
offset: int = Query(0, ge=0, description="Page offset"),
|
||||
) -> dict[str, Any]:
|
||||
"""One page of messages. The response carries a filter-aware
|
||||
"total" (the count over the SAME filters as the returned items)
|
||||
next to limit/offset, so pagers can show "page X of Y" and support
|
||||
jumping to the last page without a second request. GET
|
||||
/messages/count stays the unfiltered total for the dashboard."""
|
||||
if from_time is not None and to_time is not None and from_time > to_time:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
@@ -1684,6 +1689,18 @@ def create_app(
|
||||
conn = psycopg.connect(config.dsn)
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
# Total matching the CURRENT filters, for the pager's
|
||||
# page count / jump-to-last. Counting plain mqtt_messages
|
||||
# (aliased m.) with the same WHERE is exact: the LEFT
|
||||
# JOINs of the page query never remove rows. Same
|
||||
# connection => same snapshot, so total and items can
|
||||
# never disagree.
|
||||
cur.execute(
|
||||
sql.SQL("SELECT count(*) FROM mqtt_messages m") + where,
|
||||
params,
|
||||
)
|
||||
row = cur.fetchone()
|
||||
total = row[0] if row is not None else 0
|
||||
cur.execute(query, (*params, limit, offset))
|
||||
rows = cur.fetchall()
|
||||
finally:
|
||||
@@ -1692,6 +1709,7 @@ def create_app(
|
||||
raise _db_unavailable(exc) from exc
|
||||
|
||||
return {
|
||||
"total": total,
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
"from": from_time.isoformat() if from_time is not None else None,
|
||||
|
||||
Reference in New Issue
Block a user