Skip to content

Commit 02d96fd

Browse files
committed
Add counts and schema to next queue endpoint
Refactor queue next route to build a base query/where clause and return richer metadata: fetch the next record, the filtered count, total count, and the queue table schema. Response payload now nests the record alongside filtered_count, total_count, schema and a message, and sets meta status/title accordingly. Also remove unused static asset app/static/python-logo.svg.
1 parent 63a9088 commit 02d96fd

2 files changed

Lines changed: 41 additions & 288 deletions

File tree

app/api/queue/routes/next.py

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def get_next_queue(
1818
cursor = conn.cursor()
1919

2020
# Build query with optional filters
21-
query = "SELECT * FROM queue"
21+
base_query = "SELECT * FROM queue"
2222
filters = []
2323
params = []
2424
if collection:
@@ -27,34 +27,52 @@ def get_next_queue(
2727
if group:
2828
filters.append('"group" = %s')
2929
params.append(group)
30-
if filters:
31-
query += " WHERE " + " AND ".join(filters)
32-
query += " ORDER BY updated DESC LIMIT 1;"
30+
where_clause = (" WHERE " + " AND ".join(filters)) if filters else ""
3331

32+
# 1. Get the next record
33+
query = base_query + where_clause + " ORDER BY updated DESC LIMIT 1;"
3434
cursor.execute(query, params)
3535
row = cursor.fetchone()
3636
columns = [desc[0] for desc in cursor.description] if cursor.description else []
37+
record = dict(zip(columns, row)) if row and columns else None
38+
39+
# 2. Get count of records matching filters
40+
count_query = "SELECT COUNT(*) FROM queue" + where_clause + ";"
41+
cursor.execute(count_query, params)
42+
filtered_row = cursor.fetchone()
43+
filtered_count = filtered_row[0] if filtered_row else 0
44+
45+
# 3. Get total count
46+
cursor.execute("SELECT COUNT(*) FROM queue;")
47+
total_row = cursor.fetchone()
48+
total_count = total_row[0] if total_row else 0
49+
50+
# 4. Get table schema
51+
cursor.execute("SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_name = 'queue';")
52+
schema = [
53+
{"name": row[0], "type": row[1], "nullable": row[2]} for row in cursor.fetchall()
54+
]
55+
3756
conn.close()
3857

39-
if row and columns:
40-
record = dict(zip(columns, row))
41-
# Build a dynamic title with filters
42-
filters = []
43-
if collection:
44-
filters.append(f"collection='{collection}'")
45-
if group:
46-
filters.append(f"group='{group}'")
47-
filter_str = f" (filtered by {', '.join(filters)})" if filters else ""
48-
title = f"Next queue record found{filter_str}"
49-
return {
50-
"meta": make_meta("success", title),
51-
"data": record
52-
}
53-
else:
54-
return {
55-
"meta": make_meta("info", "No queue record to show"),
56-
"data": None,
57-
"message": "Nothing to show for the given filters."
58+
# Build a dynamic title with filters
59+
filter_labels = []
60+
if collection:
61+
filter_labels.append(f"collection='{collection}'")
62+
if group:
63+
filter_labels.append(f"group='{group}'")
64+
filter_str = f" (filtered by {', '.join(filter_labels)})" if filter_labels else ""
65+
title = f"Next queue record found{filter_str}" if record else "No queue record to show"
66+
67+
return {
68+
"meta": make_meta("success" if record else "info", title),
69+
"data": {
70+
"record": record,
71+
"filtered_count": filtered_count,
72+
"total_count": total_count,
73+
"schema": schema,
74+
"message": None if record else "Nothing to show for the given filters."
5875
}
76+
}
5977
except Exception as e:
6078
raise HTTPException(status_code=500, detail=str(e))

app/static/python-logo.svg

Lines changed: 0 additions & 265 deletions
This file was deleted.

0 commit comments

Comments
 (0)