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.
This commit is contained in:
翅膀 committed 2026-10-23 23:22:10 +08:00
1 parent 76380ff1c0
commit b71cd7b6bb
7 files changed
+351 -245

No files matched your search

+50 -17
View File
@@ -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<string, unknown>;
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<void> {
try {
await db
const insert = (groupSql: string, ...groupArgs: Array<string | null>): 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<vo
entry.action,
entry.targetType ?? null,
entry.targetId ?? null,
entry.groupId ?? null,
...groupArgs,
entry.detail ? JSON.stringify(entry.detail) : null,
entry.ip ?? null,
)
.run();
);
try {
try {
if (entry.groupInstance) {
// The id given is tried first (a primary-key lookup); the groups are
// scanned only when it no longer holds that instance (renamed).
await insert(
"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') = ?))",
entry.groupId ?? "",
entry.groupInstance,
entry.groupInstance,
).run();
} else if (entry.groupId) {
await insert("(SELECT id FROM d1_groups WHERE id = ?)", entry.groupId).run();
} else {
await insert("?", null).run();
}
} catch (err) {
// Groups not in D1 (an unmigrated database): file it as given.
if (!isMissingTable(err) || !entry.groupId) throw err;
await insert("?", entry.groupId).run();
}
} catch (err) {
log.warn({ err, action: entry.action }, "Failed to record audit entry");
}
@@ -73,23 +107,22 @@ export async function recordAudit(db: D1Database, entry: AuditEntry): Promise<vo
export async function getAuditLog(
db: D1Database,
opts: { groupId?: string; limit?: number } = {},
opts: { groupId?: GroupScope; limit?: number } = {},
): Promise<AuditEntry[]> {
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<AuditRow>();
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<AuditRow>();
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 [];
}
+65 -30
View File
@@ -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<void> {
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<unknown> =>
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;
}
}
}
}
+54 -15
View File
@@ -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<vo
}
}
export async function getSendLog(db: D1Database, limit = 50): Promise<SendRecord[]> {
/**
* 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<SendRecord[]> {
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<LogRow>();
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<SendRe
const row = results[0];
return row ? toRecord(row) : null;
} 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, id }, "Failed to load send log entry");
return null;
}
@@ -142,37 +178,40 @@ export async function getSendLogByDelivery(
.all<LogRow>();
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<SendRecord[]> {
try {
const { results } = await failedSendLogStatement(db, limit, groupId).all<LogRow>();
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 [];
}