feat(events): add pagination, filters, sort, and page size

Extend GET /api/runs with offset/total, date and trigger filters,
and configurable column sort. Rebuild Events page with URL-driven state
matching the KV page pagination pattern.

Co-authored-by: Nasyarobby Putra <nasyarobby@gmail.com>
This commit is contained in:
Cursor Agent
2026-08-20 23:28:35 +00:00
co-authored by nsrb
parent 14a08c700c
commit 20654b0682
6 changed files with 343 additions and 79 deletions
+28
View File
@@ -0,0 +1,28 @@
import * as store from "../../store.js";
/**
* @param {Record<string, string | undefined>} q
*/
export function parseRunQueryParams(q) {
const limit = q.limit != null ? Number(q.limit) : undefined;
const offset = q.offset != null ? Number(q.offset) : undefined;
return {
owner: q.owner || undefined,
workflow: q.workflow || undefined,
status: q.status || undefined,
trigger_type: q.trigger || undefined,
after: q.after || undefined,
before: q.before || undefined,
limit: Number.isFinite(limit) ? limit : undefined,
offset: Number.isFinite(offset) ? offset : undefined,
sort: q.sort || undefined,
order: q.order || undefined,
};
}
/**
* @param {Record<string, string | undefined>} q
*/
export async function queryRunsFromRequest(q) {
return store.queryRuns(parseRunQueryParams(q));
}
+2 -10
View File
@@ -1,20 +1,12 @@
import * as store from "../../store.js";
import { queryRunsFromRequest } from "./run-query.js";
/**
* @param {import("fastify").FastifyInstance} fastify
*/
export default async function runsPlugin(fastify) {
fastify.get("/runs", async (req) => {
const q = /** @type {Record<string, string | undefined>} */ (req.query ?? {});
const limit = q.limit ? Number(q.limit) : undefined;
const runs = await store.listRuns({
owner: q.owner,
workflow: q.workflow,
status: q.status,
limit: Number.isFinite(limit) ? limit : undefined,
before: q.before,
});
return { runs };
return queryRunsFromRequest(/** @type {Record<string, string | undefined>} */ (req.query ?? {}));
});
fastify.get("/consecutive-failures", async (req) => {
+3 -10
View File
@@ -14,6 +14,7 @@ import usersPlugin from "./src/api/users.js";
import scriptsPluginFactory from "./src/api/scripts.js";
import workflowsPluginFactory from "./src/api/workflows.js";
import runsPlugin from "./src/api/runs.js";
import { queryRunsFromRequest } from "./src/api/run-query.js";
import dashboardPluginFactory from "./src/api/dashboard.js";
import secretsPlugin from "./src/api/secrets.js";
import kvPlugin from "./src/api/kv.js";
@@ -198,16 +199,8 @@ export async function startApp(opts = {}) {
"/admin/runs",
{ onRequest: [server.authenticate] },
async (req, reply) => {
const q = /** @type {Record<string, string | undefined>} */ (req.query);
const limit = q.limit ? Number(q.limit) : undefined;
const runs = await store.listRuns({
owner: q.owner,
workflow: q.workflow,
status: q.status,
limit: Number.isFinite(limit) ? limit : undefined,
before: q.before,
});
return reply.send({ runs });
const q = /** @type {Record<string, string | undefined>} */ (req.query ?? {});
return reply.send(await queryRunsFromRequest(q));
},
);
+101 -14
View File
@@ -222,19 +222,29 @@ export async function insertLogs(rows) {
);
}
/** @type {Record<string, string>} */
const RUN_SORT_COLUMNS = {
status: "status",
workflow: "workflow_name",
revision: "workflow_revision",
trigger: "trigger_type",
started_at: "started_at",
duration: "duration_ms",
};
/**
* @param {import("knex").Knex.QueryBuilder} q
* @param {{
* owner?: string,
* workflow?: string,
* status?: string | string[],
* limit?: number,
* trigger_type?: string,
* after?: string,
* before?: string,
* }} [filters]
* }} filters
*/
export async function listRuns(filters = {}) {
const limit = Math.min(Math.max(filters.limit ?? 50, 1), 200);
let q = db("workflow_runs").select("*").orderBy("started_at", "desc");
if (filters.owner) q = q.where("owner", filters.owner);
function applyRunFilters(q, filters) {
if (filters.owner) q.where("owner", filters.owner);
if (filters.workflow) {
const key = String(filters.workflow);
if (key.includes("*")) {
@@ -243,25 +253,102 @@ export async function listRuns(filters = {}) {
.replaceAll("%", "\\%")
.replaceAll("_", "\\_")
.replaceAll("*", "%");
q = q.whereRaw("workflow LIKE ? ESCAPE '\\'", [pattern]);
q.whereRaw("workflow LIKE ? ESCAPE '\\'", [pattern]);
} else {
q = q.where("workflow", key);
q.where("workflow", key);
}
}
if (filters.status) {
if (Array.isArray(filters.status)) {
q = q.whereIn("status", filters.status);
q.whereIn("status", filters.status);
} else {
q = q.where("status", filters.status);
q.where("status", filters.status);
}
}
if (filters.before) q = q.where("started_at", "<", filters.before);
const rows = await q.limit(limit);
return rows.map((row) => ({
if (filters.trigger_type) q.where("trigger_type", filters.trigger_type);
if (filters.after) q.where("started_at", ">=", filters.after);
if (filters.before) q.where("started_at", "<", filters.before);
return q;
}
/**
* @param {import("knex").Knex.QueryBuilder} q
* @param {string | undefined} sort
* @param {string | undefined} order
*/
function applyRunSort(q, sort, order) {
const column = RUN_SORT_COLUMNS[sort ?? ""] ?? "started_at";
const direction = order === "asc" ? "asc" : "desc";
q.orderBy(column, direction);
if (column !== "started_at") q.orderBy("started_at", "desc");
return q;
}
/**
* @param {Record<string, unknown>} row
*/
function mapRunRow(row) {
return {
...row,
input: deserialize(row.input),
output: deserialize(row.output),
}));
};
}
/**
* @param {{
* owner?: string,
* workflow?: string,
* status?: string | string[],
* trigger_type?: string,
* after?: string,
* before?: string,
* limit?: number,
* offset?: number,
* sort?: string,
* order?: string,
* }} [filters]
*/
export async function queryRuns(filters = {}) {
const limit = Math.min(Math.max(filters.limit ?? 50, 1), 200);
const offset = Math.max(Number(filters.offset) || 0, 0);
let q = db("workflow_runs");
q = applyRunFilters(q, filters);
const countRow = await q.clone().count({ count: "*" }).first();
const total = Number(countRow?.count ?? 0);
let rowsQ = q.clone().select("*");
rowsQ = applyRunSort(rowsQ, filters.sort, filters.order);
const rows = await rowsQ.limit(limit).offset(offset);
return {
runs: rows.map(mapRunRow),
total,
limit,
offset,
};
}
/**
* @param {{
* owner?: string,
* workflow?: string,
* status?: string | string[],
* trigger_type?: string,
* after?: string,
* before?: string,
* limit?: number,
* }} [filters]
*/
export async function listRuns(filters = {}) {
const limit = Math.min(Math.max(filters.limit ?? 50, 1), 200);
let q = db("workflow_runs").select("*");
q = applyRunFilters(q, filters);
q = applyRunSort(q, "started_at", "desc");
const rows = await q.limit(limit);
return rows.map(mapRunRow);
}
/**