perf: speed up code queue first paint
This commit is contained in:
@@ -457,29 +457,40 @@ async function queueSummaryForResponse(includeDevReady = true, tasks?: QueueTask
|
||||
async function queueSummaryForHealth(includeDevReady = true): Promise<JsonValue> {
|
||||
const summary = queueSummary(includeDevReady, ctx().tasks()) as Record<string, JsonValue>;
|
||||
if (!ctx().databaseReady()) return summary;
|
||||
const [totalRows, statusRows, queueStatusRows, unreadRows] = await Promise.all([
|
||||
ctx().sql<Array<{ total: string | number }>>`SELECT COUNT(*) AS total FROM unidesk_code_queue_tasks`,
|
||||
ctx().sql<Array<{ status: string; count: string | number }>>`
|
||||
SELECT status, COUNT(*) AS count
|
||||
FROM unidesk_code_queue_tasks
|
||||
GROUP BY status
|
||||
`,
|
||||
ctx().sql<Array<{ queue_id: string; status: string; count: string | number }>>`
|
||||
SELECT queue_id, status, COUNT(*) AS count
|
||||
FROM unidesk_code_queue_tasks
|
||||
GROUP BY queue_id, status
|
||||
`,
|
||||
ctx().sql<Array<{ queue_id: string; count: string | number }>>`
|
||||
SELECT queue_id, COUNT(*) AS count
|
||||
FROM unidesk_code_queue_tasks
|
||||
WHERE status IN ('succeeded', 'failed', 'canceled')
|
||||
AND read_at IS NULL
|
||||
GROUP BY queue_id
|
||||
`,
|
||||
]);
|
||||
const aggregateRows = await ctx().sql<QueueSummaryAggregateRow[]>`
|
||||
SELECT
|
||||
(SELECT COUNT(*) FROM unidesk_code_queue_tasks) AS total,
|
||||
COALESCE((
|
||||
SELECT jsonb_object_agg(status, count)
|
||||
FROM (
|
||||
SELECT status, COUNT(*) AS count
|
||||
FROM unidesk_code_queue_tasks
|
||||
GROUP BY status
|
||||
) AS status_counts
|
||||
), '{}'::jsonb) AS status_counts,
|
||||
COALESCE((
|
||||
SELECT jsonb_agg(jsonb_build_object('queueId', queue_id, 'status', status, 'count', count))
|
||||
FROM (
|
||||
SELECT queue_id, status, COUNT(*) AS count
|
||||
FROM unidesk_code_queue_tasks
|
||||
GROUP BY queue_id, status
|
||||
) AS queue_status_counts
|
||||
), '[]'::jsonb) AS queue_status_counts,
|
||||
COALESCE((
|
||||
SELECT jsonb_agg(jsonb_build_object('queueId', queue_id, 'count', count))
|
||||
FROM (
|
||||
SELECT queue_id, COUNT(*) AS count
|
||||
FROM unidesk_code_queue_tasks
|
||||
WHERE status IN ('succeeded', 'failed', 'canceled')
|
||||
AND read_at IS NULL
|
||||
GROUP BY queue_id
|
||||
) AS unread_counts
|
||||
), '[]'::jsonb) AS unread_counts
|
||||
`;
|
||||
const aggregate = aggregateRows[0] ?? { total: ctx().tasks().length, status_counts: {}, queue_status_counts: [], unread_counts: [] };
|
||||
const counts: Record<string, number> = {};
|
||||
for (const row of statusRows) counts[row.status] = Number(row.count);
|
||||
const unreadByQueue = new Map(unreadRows.map((row) => [ctx().safeQueueId(row.queue_id), Number(row.count)]));
|
||||
for (const [status, count] of Object.entries(aggregate.status_counts ?? {})) counts[status] = Number(count);
|
||||
const unreadByQueue = new Map((aggregate.unread_counts ?? []).map((row) => [ctx().safeQueueId(String(row.queueId ?? row.queue_id ?? "")), Number(row.count ?? 0)]));
|
||||
const summaries = new Map<string, {
|
||||
name: string;
|
||||
total: number;
|
||||
@@ -502,8 +513,8 @@ async function queueSummaryForHealth(includeDevReady = true): Promise<JsonValue>
|
||||
updatedAt: queue.updatedAt,
|
||||
});
|
||||
}
|
||||
for (const row of queueStatusRows) {
|
||||
const queueId = ctx().safeQueueId(row.queue_id);
|
||||
for (const row of aggregate.queue_status_counts ?? []) {
|
||||
const queueId = ctx().safeQueueId(String(row.queueId ?? row.queue_id ?? ""));
|
||||
let queue = summaries.get(queueId);
|
||||
if (queue === undefined) {
|
||||
queue = {
|
||||
@@ -518,8 +529,9 @@ async function queueSummaryForHealth(includeDevReady = true): Promise<JsonValue>
|
||||
};
|
||||
summaries.set(queueId, queue);
|
||||
}
|
||||
const count = Number(row.count);
|
||||
queue.counts[row.status] = count;
|
||||
const count = Number(row.count ?? 0);
|
||||
const status = String(row.status || "");
|
||||
queue.counts[status] = count;
|
||||
queue.total += count;
|
||||
}
|
||||
for (const [queueId, queue] of summaries) {
|
||||
@@ -540,7 +552,7 @@ async function queueSummaryForHealth(includeDevReady = true): Promise<JsonValue>
|
||||
createdAt: queue.createdAt,
|
||||
updatedAt: queue.updatedAt,
|
||||
})) as unknown as JsonValue[];
|
||||
summary.total = Number(totalRows[0]?.total ?? ctx().tasks().length);
|
||||
summary.total = Number(aggregate.total ?? ctx().tasks().length);
|
||||
summary.counts = counts as unknown as JsonValue;
|
||||
summary.unreadTerminal = Array.from(unreadByQueue.values()).reduce((total, count) => total + count, 0);
|
||||
summary.queues = queues;
|
||||
@@ -657,6 +669,12 @@ function taskPageRows(filteredTasks: QueueTask[], url: URL, limit: number): {
|
||||
|
||||
type TaskIdRow = { id: string; created_at?: Date | string };
|
||||
type CountRow = { count: string | number };
|
||||
type QueueSummaryAggregateRow = {
|
||||
total: string | number;
|
||||
status_counts: Record<string, string | number> | null;
|
||||
queue_status_counts: Array<{ queueId?: string; queue_id?: string; status?: string; count?: string | number }> | null;
|
||||
unread_counts: Array<{ queueId?: string; queue_id?: string; count?: string | number }> | null;
|
||||
};
|
||||
type DailyCountRow = { date: string; count: string | number };
|
||||
type DailyCompletedRow = {
|
||||
date: string;
|
||||
|
||||
Reference in New Issue
Block a user