From b71cd7b6bb2ceb96a79356251d0be0a30fc74157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=85=E8=86=80?= Date: Fri, 23 Oct 2026 23:22:10 +0800 Subject: [PATCH] fix(logs): scope queries before limiting results Filter authorized groups in SQL before LIMIT and aggregate only visible rows. Preserve current group attribution for late writes, surface read failures and combine related totals in one scan. --- server/lib/lib/audit.ts | 67 ++++++++++---- server/lib/lib/send-log-batch.ts | 95 +++++++++++++------- server/lib/lib/send-log.ts | 69 +++++++++++---- server/lib/observability/metrics.ts | 65 +++++++------- server/lib/web/admin.ts | 64 +++++++------- tests/audit.test.ts | 104 +++++++--------------- tests/delivery-metrics.test.ts | 132 ++++++++++++++++++---------- 7 files changed, 351 insertions(+), 245 deletions(-) diff --git a/server/lib/lib/audit.ts b/server/lib/lib/audit.ts index 8268f80..ef3c959 100644 --- a/server/lib/lib/audit.ts +++ b/server/lib/lib/audit.ts @@ -1,4 +1,6 @@ +import { groupScopeClause, type GroupScope } from "./send-log"; import { log } from "./log"; +import { isMissingTable } from "../storage/d1"; export interface AuditEntry { id?: number; @@ -10,6 +12,11 @@ export interface AuditEntry { targetType?: string; targetId?: string; groupId?: string; + /** + * Immutable instance of the group (write-only): the entry is filed under + * whatever id that instance has when it is written. + */ + groupInstance?: string; /** Free-form metadata. Never include secrets or message bodies. */ detail?: Record; ip?: string; @@ -46,13 +53,20 @@ function toEntry(r: AuditRow): AuditEntry { }; } -/** Best-effort write; failures never break the business flow. */ +/** + * Best-effort write; failures never break the business flow. + * + * The group is resolved when the row is written, not when the action was + * decided: an entry landing after its group was renamed is filed under the + * new id (with the instance known), after a delete or a rename under none — + * never under an id another group may take over later. + */ export async function recordAudit(db: D1Database, entry: AuditEntry): Promise { - try { - await db + const insert = (groupSql: string, ...groupArgs: Array): D1PreparedStatement => + db .prepare( `INSERT INTO audit_logs (ts, actor_id, actor_login, action, target_type, target_id, group_id, detail, ip) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, + VALUES (?, ?, ?, ?, ?, ?, ${groupSql}, ?, ?)`, ) .bind( entry.ts, @@ -61,11 +75,31 @@ export async function recordAudit(db: D1Database, entry: AuditEntry): Promise { const limit = Math.min(Math.max(opts.limit ?? 50, 1), 200); try { - if (opts.groupId) { - const { results } = await db - .prepare(`SELECT ${COLUMNS} FROM audit_logs WHERE group_id = ? ORDER BY ts DESC LIMIT ?`) - .bind(opts.groupId, limit) - .all(); - return results.map(toEntry); - } + const clause = groupScopeClause(opts.groupId); const { results } = await db - .prepare(`SELECT ${COLUMNS} FROM audit_logs ORDER BY ts DESC LIMIT ?`) - .bind(limit) + .prepare( + `SELECT ${COLUMNS} FROM audit_logs${clause ? ` WHERE ${clause.sql}` : ""} ORDER BY ts DESC LIMIT ?`, + ) + .bind(...(clause?.args ?? []), limit) .all(); return results.map(toEntry); } catch (err) { + // Only a table not created yet means "nothing yet"; any other failure + // must reach the caller, not pass for an empty history. + if (!isMissingTable(err)) throw err; log.warn({ err }, "Failed to load audit log"); return []; } diff --git a/server/lib/lib/send-log-batch.ts b/server/lib/lib/send-log-batch.ts index 9cc344d..80d6077 100644 --- a/server/lib/lib/send-log-batch.ts +++ b/server/lib/lib/send-log-batch.ts @@ -1,38 +1,73 @@ import type { SendRecord } from "./send-log"; import { log } from "./log"; +import { isMissingTable } from "../storage/d1"; + +/** + * Best-effort write of a dispatch's send logs, in one D1 transaction. A + * failed write is tried once more before it is given up (and logged). + */ +/** + * The group a log row belongs to now, found by its instance: the id it had + * at dispatch is tried first (one primary-key lookup — the usual case); only + * when it was renamed or deleted meanwhile are the groups scanned. + */ +const GROUP_BY_INSTANCE = + "COALESCE((SELECT id FROM d1_groups WHERE id = ? AND json_valid(data) AND json_extract(data, '$.instance') = ?), (SELECT id FROM (SELECT id, CASE WHEN json_valid(data) THEN data ELSE '{}' END AS data FROM d1_groups) WHERE json_extract(data, '$.instance') = ?))"; export async function recordSendBatch(db: D1Database, records: SendRecord[]): Promise { if (records.length === 0) return; - try { - const stmts = records.map((r) => - db - .prepare( - `INSERT INTO send_logs (ts, route_id, group_id, event, repo, target, ok, error, status, message_id, delivery_id, platform, actor, action, duration_ms, error_code, attempts, detail) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - ) - .bind( - r.ts, - r.routeId, - r.groupId ?? null, - r.event, - r.repo ?? null, - r.target, - r.ok ? 1 : 0, - r.error ?? null, - r.status ?? null, - r.messageId ?? null, - r.deliveryId ?? null, - r.platform ?? null, - r.actor ?? null, - r.action ?? null, - r.durationMs ?? null, - r.errorCode ?? null, - r.attempts ?? null, - r.detail ? JSON.stringify(r.detail) : null, - ), + // With the group's instance known, the row is filed under whatever id + // that instance has at insert time: a delivery finishing after its group + // was renamed lands under the new id, after a delete under none — never + // under an id another group may own by then. Without groups in D1 (an + // unmigrated database) rows are filed under the id as given. + const write = (byInstance: boolean): Promise => + db.batch( + records.map((r) => { + const resolve = byInstance && r.groupInstance != null; + return db + .prepare( + `INSERT INTO send_logs (ts, route_id, group_id, event, repo, target, ok, error, status, message_id, delivery_id, platform, actor, action, duration_ms, error_code, attempts, detail) + VALUES (?, ?, ${resolve ? GROUP_BY_INSTANCE : "?"}, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, + ) + .bind( + r.ts, + r.routeId, + ...(resolve + ? [r.groupId ?? "", r.groupInstance, r.groupInstance] + : [r.groupId ?? null]), + r.event, + r.repo ?? null, + r.target, + r.ok ? 1 : 0, + r.error ?? null, + r.status ?? null, + r.messageId ?? null, + r.deliveryId ?? null, + r.platform ?? null, + r.actor ?? null, + r.action ?? null, + r.durationMs ?? null, + r.errorCode ?? null, + r.attempts ?? null, + r.detail ? JSON.stringify(r.detail) : null, + ); + }), ); - await db.batch(stmts); - } catch (err) { - log.warn({ err, count: records.length }, "Failed to record send log batch"); + let byInstance = true; + for (let attempt = 1; ; attempt++) { + try { + await write(byInstance); + return; + } catch (err) { + if (byInstance && isMissingTable(err) && /d1_groups/.test(String(err))) { + byInstance = false; + continue; + } + if (attempt >= 2 || isMissingTable(err)) { + log.warn({ err, count: records.length }, "Failed to record send log batch"); + return; + } + } } } diff --git a/server/lib/lib/send-log.ts b/server/lib/lib/send-log.ts index 001bbaf..f14e212 100644 --- a/server/lib/lib/send-log.ts +++ b/server/lib/lib/send-log.ts @@ -1,10 +1,13 @@ import { log } from "./log"; +import { isMissingTable } from "../storage/d1"; export interface SendRecord { id?: number; ts: number; routeId: string; groupId?: string; + /** Immutable instance of the group (write-only: resolves `group_id` at insert time). */ + groupInstance?: string; event: string; repo?: string; target: string; @@ -104,14 +107,44 @@ export async function recordSend(db: D1Database, record: SendRecord): Promise { +/** + * Rows visible to a reader: every group (undefined), one group (a string) or + * a set of groups (an array — empty matches nothing). The condition is applied + * in SQL, before ordering and LIMIT, so another group's rows can neither leak + * into nor crowd out the reader's own. + */ +export type GroupScope = string | string[] | undefined; + +export function groupScopeClause(scope: GroupScope): { sql: string; args: string[] } | null { + if (scope === undefined) return null; + const ids = typeof scope === "string" ? [scope] : scope; + if (ids.length === 0) return { sql: "0", args: [] }; + if (ids.length === 1) return { sql: "group_id = ?", args: ids }; + // One JSON parameter, however many groups: D1 binds at most 100 per statement. + return { + sql: "group_id IN (SELECT value FROM json_each(?))", + args: [JSON.stringify(ids)], + }; +} + +export async function getSendLog( + db: D1Database, + limit = 50, + scope?: GroupScope, +): Promise { try { + const clause = groupScopeClause(scope); const { results } = await db - .prepare(`SELECT ${COLUMNS} FROM send_logs ORDER BY ts DESC LIMIT ?`) - .bind(limit) + .prepare( + `SELECT ${COLUMNS} FROM send_logs${clause ? ` WHERE ${clause.sql}` : ""} ORDER BY ts DESC LIMIT ?`, + ) + .bind(...(clause?.args ?? []), limit) .all(); return results.map(toRecord); } catch (err) { + // Only a table not created yet means "nothing yet"; any other failure + // must reach the caller, not pass for an empty history. + if (!isMissingTable(err)) throw err; log.warn({ err }, "Failed to load send log"); return []; } @@ -126,6 +159,9 @@ export async function getSendLogById(db: D1Database, id: number): Promise(); return results.map(toRecord); } catch (err) { + // Only a table not created yet means "nothing yet"; any other failure + // must reach the caller, not pass for an empty history. + if (!isMissingTable(err)) throw err; log.warn({ err, deliveryId }, "Failed to load send log by delivery"); return []; } } -/** The latest failed sends, optionally of one group (rows map with `toRecord`). */ +/** The latest failed sends within a group scope (rows map with `toRecord`). */ export function failedSendLogStatement( db: D1Database, limit = 20, - groupId?: string, + scope?: GroupScope, ): D1PreparedStatement { - return groupId - ? db - .prepare( - `SELECT ${COLUMNS} FROM send_logs WHERE ok = 0 AND group_id = ? ORDER BY ts DESC LIMIT ?`, - ) - .bind(groupId, limit) - : db - .prepare(`SELECT ${COLUMNS} FROM send_logs WHERE ok = 0 ORDER BY ts DESC LIMIT ?`) - .bind(limit); + const clause = groupScopeClause(scope); + return db + .prepare( + `SELECT ${COLUMNS} FROM send_logs WHERE ok = 0${clause ? ` AND ${clause.sql}` : ""} ORDER BY ts DESC LIMIT ?`, + ) + .bind(...(clause?.args ?? []), limit); } export async function getFailedSendLog( db: D1Database, limit = 20, - groupId?: string, + groupId?: GroupScope, ): Promise { try { const { results } = await failedSendLogStatement(db, limit, groupId).all(); return results.map(toRecord); } catch (err) { + // Only a table not created yet means "nothing yet"; any other failure + // must reach the caller, not pass for an empty history. + if (!isMissingTable(err)) throw err; log.warn({ err }, "Failed to load failed send log"); return []; } diff --git a/server/lib/observability/metrics.ts b/server/lib/observability/metrics.ts index fe75d16..36dfe85 100644 --- a/server/lib/observability/metrics.ts +++ b/server/lib/observability/metrics.ts @@ -1,5 +1,13 @@ +import { isMissingTable } from "../storage/d1"; import { log } from "../lib/log"; -import { failedSendLogStatement, toRecord, type LogRow, type SendRecord } from "../lib/send-log"; +import { + failedSendLogStatement, + groupScopeClause, + toRecord, + type GroupScope, + type LogRow, + type SendRecord, +} from "../lib/send-log"; export interface DeliveryMetrics { total: number; @@ -18,6 +26,9 @@ export interface DeliveryMetrics { interface TotalsRow { total: number; ok: number; + avg: number | null; + attempts_total: number; + attempts_avg: number | null; } interface BreakdownRow { @@ -31,15 +42,6 @@ interface StatusRow { count: number; } -interface AvgRow { - avg: number | null; -} - -interface AttemptsRow { - total: number; - avg: number | null; -} - const EMPTY: DeliveryMetrics = { total: 0, ok: 0, @@ -56,19 +58,26 @@ const EMPTY: DeliveryMetrics = { export async function getDeliveryMetrics( db: D1Database, - groupId?: string, + groupId?: GroupScope, ): Promise { - const where = groupId ? " WHERE group_id = ?" : ""; - const and = groupId ? " AND group_id = ?" : ""; + const clause = groupScopeClause(groupId); + const where = clause ? ` WHERE ${clause.sql}` : ""; + const and = clause ? ` AND ${clause.sql}` : ""; const bindGroup = (stmt: D1PreparedStatement): D1PreparedStatement => - groupId ? stmt.bind(groupId) : stmt; + clause && clause.args.length > 0 ? stmt.bind(...clause.args) : stmt; try { - // One round trip for all seven queries (each D1 call is a network hop), and + // One round trip for all five queries (each D1 call is a network hop), and // a batch runs as one transaction, so the totals and breakdowns agree. const results = await db.batch([ + // Totals, average duration and attempts in one scan (AVG and SUM skip + // NULLs, as the separate filtered queries did). bindGroup( - db.prepare(`SELECT COUNT(*) AS total, COALESCE(SUM(ok), 0) AS ok FROM send_logs${where}`), + db.prepare( + `SELECT COUNT(*) AS total, COALESCE(SUM(ok), 0) AS ok, AVG(duration_ms) AS avg, + COALESCE(SUM(attempts), 0) AS attempts_total, AVG(attempts) AS attempts_avg + FROM send_logs${where}`, + ), ), bindGroup( db.prepare( @@ -88,17 +97,6 @@ export async function getDeliveryMetrics( WHERE status IS NOT NULL${and} GROUP BY status`, ), ), - bindGroup( - db.prepare( - `SELECT AVG(duration_ms) AS avg FROM send_logs WHERE duration_ms IS NOT NULL${and}`, - ), - ), - bindGroup( - db.prepare( - `SELECT COALESCE(SUM(attempts), 0) AS total, AVG(attempts) AS avg - FROM send_logs WHERE attempts IS NOT NULL${and}`, - ), - ), failedSendLogStatement(db, 20, groupId), ]); const rows = (i: number): T[] => (results[i]?.results ?? []) as T[]; @@ -106,9 +104,7 @@ export async function getDeliveryMetrics( const byPlatform = rows(1); const byEvent = rows(2); const byStatus = rows(3); - const duration = rows(4); - const attempts = rows(5); - const recentFailures = rows(6).map(toRecord); + const recentFailures = rows(4).map(toRecord); const total = Number(totals[0]?.total) || 0; const ok = Number(totals[0]?.ok) || 0; @@ -132,12 +128,15 @@ export async function getDeliveryMetrics( status: String(r.status), count: Number(r.count) || 0, })), - avgDurationMs: Number(duration[0]?.avg) || 0, - totalAttempts: Number(attempts[0]?.total) || 0, - avgAttempts: Number(attempts[0]?.avg) || 0, + avgDurationMs: Number(totals[0]?.avg) || 0, + totalAttempts: Number(totals[0]?.attempts_total) || 0, + avgAttempts: Number(totals[0]?.attempts_avg) || 0, recentFailures, }; } catch (err) { + // Only a table not created yet means "nothing yet"; any other failure + // must reach the caller, not pass for an empty history. + if (!isMissingTable(err)) throw err; log.warn({ err }, "Failed to compute delivery metrics"); return EMPTY; } diff --git a/server/lib/web/admin.ts b/server/lib/web/admin.ts index 0c466f0..1f52809 100644 --- a/server/lib/web/admin.ts +++ b/server/lib/web/admin.ts @@ -22,7 +22,7 @@ import { clientIp, type GroupAccess, } from "./auth"; -import { getSendLog, getSendLogById, getSendLogByDelivery } from "../lib/send-log"; +import { getSendLog, getSendLogById, getSendLogByDelivery, type GroupScope } from "../lib/send-log"; import { getDeliveryMetrics } from "../observability/metrics"; import { getAuditLog, recordAudit } from "../lib/audit"; import { @@ -759,19 +759,25 @@ export async function adminApiRoutesPut(event: H3Event): Promise> { const auth = await requireAnyAccess(event); const env = cfEnv(event); const query = getQuery(event); - const limit = Math.min(Math.max(Number(String(query["limit"] ?? "50")), 1), 100); + const limit = Math.min(Math.max(Number(String(query["limit"] ?? "50")) || 50, 1), 100); const filterGroupId = String(query["groupId"] ?? "") || undefined; - const allLogs = await getSendLog(env.DB, 200); - const allowed = auth.scope.isSuper - ? allLogs - : allLogs.filter((l) => l.groupId != null && auth.scope.groupIds.has(l.groupId)); - const logs = filterGroupId ? allowed.filter((l) => l.groupId === filterGroupId) : allowed; - return { logs: logs.slice(0, limit) }; + if (filterGroupId && !auth.scope.isSuper && !auth.scope.groupIds.has(filterGroupId)) { + return respondError(event, 403, "Forbidden"); + } + const logs = await getSendLog(env.DB, limit, visibleScope(event, filterGroupId)); + return { logs }; } /** GET /admin/api/logs/:id */ @@ -1080,16 +1086,13 @@ export async function adminAudit(event: H3Event): Promise e.groupId != null && auth.scope.groupIds.has(e.groupId)); - return { audit: visible }; + const audit = await getAuditLog(env.DB, { groupId: visibleScope(event, groupId), limit }); + return { audit }; } /** GET /admin/api/metrics */ @@ -1101,16 +1104,10 @@ export async function adminApiMetrics(event: H3Event): Promise f.groupId != null && auth.scope.groupIds.has(f.groupId), - ), - }, - }; + // Every aggregate (not only the failure list) is computed over the groups + // the reader may see; nobody but a super admin gets global totals. + const metrics = await getDeliveryMetrics(env.DB, visibleScope(event, groupId)); + return { metrics }; } /** GET /admin/api/delivery/:deliveryId */ @@ -1121,14 +1118,17 @@ export async function adminApiDelivery( const auth = await requireAnyAccess(event); const env = cfEnv(event); const rows = await getSendLogByDelivery(env.DB, deliveryId); - if (rows.length === 0) return respondError(event, 404, "Delivery not found"); - if ( - !auth.scope.isSuper && - rows.some((r) => r.groupId == null || !auth.scope.groupIds.has(r.groupId)) - ) { - return respondError(event, 403, "Forbidden"); - } - return { deliveryId, attempts: rows }; + // One delivery can fan out to several groups: show the reader their part. + // A delivery id is chosen by its sender, so two senders (two groups' + // endpoints) may use the same one: `?groupId=` narrows it to one group. + const only = getQuery(event)["groupId"]; + const attempts = ( + auth.scope.isSuper + ? rows + : rows.filter((r) => r.groupId != null && auth.scope.groupIds.has(r.groupId)) + ).filter((r) => typeof only !== "string" || !only || r.groupId === only); + if (attempts.length === 0) return respondError(event, 404, "Delivery not found"); + return { deliveryId, attempts }; } function validateFragments( diff --git a/tests/audit.test.ts b/tests/audit.test.ts index c116c97..04a0ed7 100644 --- a/tests/audit.test.ts +++ b/tests/audit.test.ts @@ -1,79 +1,33 @@ import { describe, it, expect, beforeEach } from "bun:test"; import { recordAudit, getAuditLog, pruneAuditLogs } from "../server/lib/lib/audit"; - -function createMockD1(): D1Database { - let rows: Array> = []; - let id = 1; - - function queryAll(sql: string, args: unknown[]): Array> { - if (sql.startsWith("INSERT")) return []; - let filtered = rows; - if (sql.includes("group_id = ?")) { - const gid = args[0] as string; - filtered = rows.filter((r) => r.group_id === gid); - } - const limit = args[args.length - 1] as number; - return [...filtered] - .sort((a, b) => (b.ts as number) - (a.ts as number)) - .slice(0, limit) - .map((r) => ({ - id: r.id, - ts: r.ts ?? 0, - actor_id: r.actor_id ?? null, - actor_login: r.actor_login ?? null, - action: r.action ?? "", - target_type: r.target_type ?? null, - target_id: r.target_id ?? null, - group_id: r.group_id ?? null, - detail: r.detail ?? null, - ip: r.ip ?? null, - })); - } - - const db = { - prepare: (sql: string): { bind: (args: unknown[]) => unknown } => ({ - bind: (...args: unknown[]) => ({ - run: async (): Promise<{ meta: { changes: number }; results?: unknown[] }> => { - if (sql.startsWith("INSERT")) { - const row: Record = { id: id++ }; - for (const [key, value] of [ - ["ts", args[0]], - ["actor_id", args[1]], - ["actor_login", args[2]], - ["action", args[3]], - ["target_type", args[4]], - ["target_id", args[5]], - ["group_id", args[6]], - ["detail", args[7]], - ["ip", args[8]], - ] as const) { - if (value != null) row[key] = value; - } - rows.push(row); - return { meta: { changes: 1 } }; - } - if (sql.startsWith("DELETE")) { - const cutoff = args[0] as number; - const before = rows.length; - rows = rows.filter((r) => (r.ts as number) >= cutoff); - return { meta: { changes: before - rows.length } }; - } - return { meta: { changes: 0 }, results: queryAll(sql, args) }; - }, - all: async (): Promise<{ results: Array> }> => ({ - results: queryAll(sql, args), - }), - }), - }), - } as unknown as D1Database; - return db; -} +import { createSqliteD1 } from "./d1-sqlite"; describe("audit log", () => { let db: D1Database; - beforeEach(() => { - db = createMockD1(); + beforeEach(async () => { + db = createSqliteD1().db; + for (const id of ["g1", "g2", "g3"]) { + await db + .prepare( + "INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) VALUES (?, ?, ?, 1, 0, 0)", + ) + .bind(id, id, JSON.stringify({ id, name: id, instance: `inst-${id}` })) + .run(); + } + }); + + it("files a late entry under the group's current id, never a deleted one", async () => { + // The group was renamed (same instance) before the entry was written. + await db.prepare("UPDATE d1_groups SET id = 'g9' WHERE id = 'g1'").run(); + await recordAudit(db, { ts: 1, action: "renamed", groupId: "g1", groupInstance: "inst-g1" }); + // An entry for a group deleted meanwhile belongs to no group. + await db.prepare("DELETE FROM d1_groups WHERE id = 'g2'").run(); + await recordAudit(db, { ts: 2, action: "deleted", groupId: "g2" }); + expect((await getAuditLog(db, { groupId: "g9" })).map((e) => e.action)).toEqual(["renamed"]); + expect(await getAuditLog(db, { groupId: "g1" })).toEqual([]); + expect(await getAuditLog(db, { groupId: "g2" })).toEqual([]); + expect((await getAuditLog(db)).map((e) => e.groupId ?? null)).toEqual([null, "g9"]); }); it("records and loads entries newest-first", async () => { @@ -109,6 +63,16 @@ describe("audit log", () => { expect(entries[0]!.action).toBe("y"); }); + it("filters by a set of groups before applying the limit", async () => { + await recordAudit(db, { ts: 1, actorLogin: "a", action: "mine", groupId: "g1" }); + for (let i = 0; i < 5; i++) { + await recordAudit(db, { ts: 10 + i, actorLogin: "b", action: "other", groupId: "g2" }); + } + const entries = await getAuditLog(db, { groupId: ["g1", "g3"], limit: 2 }); + expect(entries.map((e) => e.action)).toEqual(["mine"]); + expect(await getAuditLog(db, { groupId: [] })).toEqual([]); + }); + it("prunes entries older than the retention window", async () => { const now = Date.now(); await recordAudit(db, { ts: now - 100 * 86400_000, actorLogin: "a", action: "old" }); diff --git a/tests/delivery-metrics.test.ts b/tests/delivery-metrics.test.ts index 86ff0b1..37cd503 100644 --- a/tests/delivery-metrics.test.ts +++ b/tests/delivery-metrics.test.ts @@ -5,7 +5,8 @@ import { getFailedSendLog, type SendRecord, } from "../server/lib/lib/send-log"; -import { adminApiMetrics, adminApiDelivery } from "../server/lib/web/admin"; +import { adminApiMetrics, adminApiDelivery, adminApiLogs } from "../server/lib/web/admin"; +import { createSqliteD1 } from "./d1-sqlite"; import { createAdminSession, adminCookie } from "../server/lib/web/session"; import { invalidateGroupsCache } from "../server/lib/web/groups"; import { makeEvent, responseStatus } from "./helpers"; @@ -95,6 +96,8 @@ const logRow = (overrides: Record = {}): Record { it("aggregates send_logs into metrics", async () => { const db = createMetricsDB({ + // Totals, average duration and attempts come from one scan. + attempts_total: [{ total: 10, ok: 8, avg: 120.5, attempts_total: 12, attempts_avg: 1.2 }], "platform AS key": [ { key: "discord", total: 7, ok: 6 }, { key: "telegram", total: 3, ok: 2 }, @@ -104,10 +107,7 @@ describe("delivery metrics", () => { { status: "200", count: 8 }, { status: "500", count: 2 }, ], - "AVG(duration_ms)": [{ avg: 120.5 }], - "SUM(attempts)": [{ total: 12, avg: 1.2 }], "ok = 0": [logRow({ ok: 0, status: 500, error_code: "DISCORD_5XX" })], - "COUNT(*) AS total": [{ total: 10, ok: 8 }], }); const m = await getDeliveryMetrics(db); // Every query goes out in a single batch (one D1 round trip). @@ -166,70 +166,106 @@ describe("delivery metrics", () => { }); }); +describe("delivery metrics (SQLite)", () => { + it("averages duration and attempts over the rows that have them, in one scan", async () => { + const { db, sqlite } = createSqliteD1(); + const insert = sqlite.prepare( + "INSERT INTO send_logs (ts, route_id, group_id, event, target, ok, duration_ms, attempts) VALUES (?, 'r', 'g', 'push', 't', ?, ?, ?)", + ); + insert.run(1, 1, 100, 1); + insert.run(2, 0, 300, 3); + insert.run(3, 1, null, null); + const m = await getDeliveryMetrics(db); + expect(m.total).toBe(3); + expect(m.ok).toBe(2); + expect(m.avgDurationMs).toBe(200); + expect(m.totalAttempts).toBe(4); + expect(m.avgAttempts).toBe(2); + }); +}); + describe("admin metrics/delivery handlers", () => { beforeEach(() => invalidateGroupsCache()); - it("returns global metrics for super admins", async () => { + + /** A real (SQLite) D1 with two groups and send logs in both. */ + function seeded(): { env: Env; kv: KVNamespace } { + const { db, sqlite } = createSqliteD1(); const kv = createMockKV(); - const db = createMetricsDB({ "COUNT(*) AS total": [{ total: 5, ok: 4 }] }); - const env = createEnv({ KV: kv, DB: db, ADMIN_USER_IDS: "1001" }); - const sid = await createAdminSession(kv, "1001", "alice"); - const event = makeEvent("/api/metrics", { headers: { cookie: adminCookie(sid) }, env }); + const insert = sqlite.prepare( + "INSERT INTO send_logs (ts, route_id, group_id, event, target, ok, delivery_id, error_code) VALUES (?, 'r', ?, 'push', 't', ?, ?, ?)", + ); + insert.run(1, "mine", 1, "d1", null); + insert.run(2, "mine", 0, "d1", "NETWORK"); + insert.run(3, "theirs", 0, "d1", "NETWORK"); + insert.run(4, "theirs", 1, "d2", null); + insert.run(5, "theirs", 1, "d3", null); + const groups = [ + { id: "mine", name: "Mine", adminIds: [], members: [{ login: "bob", role: "owner" }] }, + { id: "theirs", name: "Theirs", adminIds: [], members: [{ login: "eve", role: "owner" }] }, + ]; + for (const g of groups) { + sqlite + .prepare( + "INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) VALUES (?, ?, ?, 1, 0, 0)", + ) + .run(g.id, g.name, JSON.stringify(g)); + } + return { env: createEnv({ KV: kv, DB: db, ADMIN_USER_IDS: "1001" }), kv }; + } + + async function as(kv: KVNamespace, userId: string, login: string): Promise { + return adminCookie(await createAdminSession(kv, userId, login)); + } + + it("returns global metrics for super admins", async () => { + const { env, kv } = seeded(); + const cookie = await as(kv, "1001", "alice"); + const event = makeEvent("/api/metrics", { headers: { cookie }, env }); const res = (await adminApiMetrics(event)) as { metrics: { total: number } }; expect(responseStatus(event)).toBe(200); expect(res.metrics.total).toBe(5); }); - it("filters recentFailures for non-super users", async () => { - const kv = createMockKV(); - await kv.put( - "config:groups", - JSON.stringify([ - { id: "mine", name: "Mine", adminIds: [], members: [{ login: "bob", role: "owner" }] }, - ]), - ); - const db = createMetricsDB({ - "ok = 0": [logRow({ ok: 0, group_id: "mine" }), logRow({ ok: 0, group_id: "theirs" })], - "COUNT(*) AS total": [{ total: 1, ok: 0 }], - }); - const env = createEnv({ KV: kv, DB: db }); - const sid = await createAdminSession(kv, "2002", "bob"); - const event = makeEvent("/api/metrics", { headers: { cookie: adminCookie(sid) }, env }); + it("scopes every aggregate to the reader's groups", async () => { + const { env, kv } = seeded(); + const cookie = await as(kv, "2002", "bob"); + const event = makeEvent("/api/metrics", { headers: { cookie }, env }); const res = (await adminApiMetrics(event)) as { - metrics: { recentFailures: Array<{ groupId?: string }> }; + metrics: { total: number; failed: number; recentFailures: Array<{ groupId?: string }> }; }; - expect(res.metrics.recentFailures).toHaveLength(1); - expect(res.metrics.recentFailures[0]!.groupId).toBe("mine"); + expect(res.metrics.total).toBe(2); + expect(res.metrics.failed).toBe(1); + expect(res.metrics.recentFailures.map((f) => f.groupId)).toEqual(["mine"]); }); it("returns 404 for an unknown delivery", async () => { - const kv = createMockKV(); - const env = createEnv({ - KV: kv, - DB: createMetricsDB({ delivery_id: [] }), - ADMIN_USER_IDS: "1001", - }); - const sid = await createAdminSession(kv, "1001", "alice"); - const event = makeEvent("/api/delivery/nope", { headers: { cookie: adminCookie(sid) }, env }); + const { env, kv } = seeded(); + const cookie = await as(kv, "1001", "alice"); + const event = makeEvent("/api/delivery/nope", { headers: { cookie }, env }); const res = (await adminApiDelivery(event, "nope")) as { error?: string }; expect(responseStatus(event)).toBe(404); expect(res.error).toBe("Delivery not found"); }); - it("returns correlated attempts for a delivery", async () => { - const kv = createMockKV(); - const db = createMetricsDB({ - delivery_id: [logRow({ id: 1, ok: 1, delivery_id: "d1" })], - }); - const env = createEnv({ KV: kv, DB: db, ADMIN_USER_IDS: "1001" }); - const sid = await createAdminSession(kv, "1001", "alice"); - const event = makeEvent("/api/delivery/d1", { headers: { cookie: adminCookie(sid) }, env }); + it("shows a reader only their groups' attempts of a shared delivery", async () => { + const { env, kv } = seeded(); + const cookie = await as(kv, "2002", "bob"); + const event = makeEvent("/api/delivery/d1", { headers: { cookie }, env }); const res = (await adminApiDelivery(event, "d1")) as { - deliveryId?: string; - attempts?: Array<{ deliveryId?: string }>; + attempts?: Array<{ groupId?: string }>; }; expect(responseStatus(event)).toBe(200); - expect(res.deliveryId).toBe("d1"); - expect(res.attempts).toHaveLength(1); - expect(res.attempts![0]!.deliveryId).toBe("d1"); + expect(res.attempts!.map((a) => a.groupId)).toEqual(["mine", "mine"]); + const other = makeEvent("/api/delivery/d2", { headers: { cookie }, env }); + await adminApiDelivery(other, "d2"); + expect(responseStatus(other)).toBe(404); + }); + + it("lists logs of the reader's groups even when others are newer", async () => { + const { env, kv } = seeded(); + const cookie = await as(kv, "2002", "bob"); + const event = makeEvent("/api/logs?limit=2", { headers: { cookie }, env }); + const res = (await adminApiLogs(event)) as { logs: Array<{ groupId?: string }> }; + expect(res.logs.map((l) => l.groupId)).toEqual(["mine", "mine"]); }); });