mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-21 01:23:40 +00:00
perf(metrics): send all seven queries in one D1 batch
Avoid seven sequential network round trips and read totals, breakdowns and recent failures in one consistent transaction. Reuse the failed-send statement and row mapping while retaining group scoping.
This commit is contained in:
1 parent
244b5c6ffe
commit
6cdcedde92
4 files changed
+86
-56
No files matched your search
+20
-12
@@ -25,7 +25,7 @@ export interface SendRecord {
|
||||
const COLUMNS =
|
||||
"id, ts, route_id, group_id, event, repo, target, ok, error, status, message_id, delivery_id, platform, actor, action, duration_ms, error_code, attempts, detail";
|
||||
|
||||
interface LogRow {
|
||||
export interface LogRow {
|
||||
id: number;
|
||||
ts: number;
|
||||
route_id: string;
|
||||
@@ -47,7 +47,7 @@ interface LogRow {
|
||||
detail: string | null;
|
||||
}
|
||||
|
||||
function toRecord(r: LogRow): SendRecord {
|
||||
export function toRecord(r: LogRow): SendRecord {
|
||||
return {
|
||||
id: r.id,
|
||||
ts: r.ts,
|
||||
@@ -147,22 +147,30 @@ export async function getSendLogByDelivery(
|
||||
}
|
||||
}
|
||||
|
||||
/** The latest failed sends, optionally of one group (rows map with `toRecord`). */
|
||||
export function failedSendLogStatement(
|
||||
db: D1Database,
|
||||
limit = 20,
|
||||
groupId?: string,
|
||||
): 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);
|
||||
}
|
||||
|
||||
export async function getFailedSendLog(
|
||||
db: D1Database,
|
||||
limit = 20,
|
||||
groupId?: string,
|
||||
): Promise<SendRecord[]> {
|
||||
try {
|
||||
const stmt = 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 { results } = await stmt.all<LogRow>();
|
||||
const { results } = await failedSendLogStatement(db, limit, groupId).all<LogRow>();
|
||||
return results.map(toRecord);
|
||||
} catch (err) {
|
||||
log.warn({ err }, "Failed to load failed send log");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { log } from "../lib/log";
|
||||
import { getFailedSendLog, type SendRecord } from "../lib/send-log";
|
||||
import { failedSendLogStatement, toRecord, type LogRow, type SendRecord } from "../lib/send-log";
|
||||
|
||||
export interface DeliveryMetrics {
|
||||
total: number;
|
||||
@@ -64,65 +64,77 @@ export async function getDeliveryMetrics(
|
||||
groupId ? stmt.bind(groupId) : stmt;
|
||||
|
||||
try {
|
||||
const totals = await bindGroup(
|
||||
db.prepare(`SELECT COUNT(*) AS total, COALESCE(SUM(ok), 0) AS ok FROM send_logs${where}`),
|
||||
).all<TotalsRow>();
|
||||
const total = Number(totals.results[0]?.total) || 0;
|
||||
const ok = Number(totals.results[0]?.ok) || 0;
|
||||
// One round trip for all seven 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([
|
||||
bindGroup(
|
||||
db.prepare(`SELECT COUNT(*) AS total, COALESCE(SUM(ok), 0) AS ok FROM send_logs${where}`),
|
||||
),
|
||||
bindGroup(
|
||||
db.prepare(
|
||||
`SELECT platform AS key, COUNT(*) AS total, COALESCE(SUM(ok), 0) AS ok
|
||||
FROM send_logs${where} GROUP BY platform`,
|
||||
),
|
||||
),
|
||||
bindGroup(
|
||||
db.prepare(
|
||||
`SELECT event AS key, COUNT(*) AS total, COALESCE(SUM(ok), 0) AS ok
|
||||
FROM send_logs${where} GROUP BY event`,
|
||||
),
|
||||
),
|
||||
bindGroup(
|
||||
db.prepare(
|
||||
`SELECT status, COUNT(*) AS count FROM send_logs
|
||||
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 = <T>(i: number): T[] => (results[i]?.results ?? []) as T[];
|
||||
const totals = rows<TotalsRow>(0);
|
||||
const byPlatform = rows<BreakdownRow>(1);
|
||||
const byEvent = rows<BreakdownRow>(2);
|
||||
const byStatus = rows<StatusRow>(3);
|
||||
const duration = rows<AvgRow>(4);
|
||||
const attempts = rows<AttemptsRow>(5);
|
||||
const recentFailures = rows<LogRow>(6).map(toRecord);
|
||||
|
||||
const byPlatform = await bindGroup(
|
||||
db.prepare(
|
||||
`SELECT platform AS key, COUNT(*) AS total, COALESCE(SUM(ok), 0) AS ok
|
||||
FROM send_logs${where} GROUP BY platform`,
|
||||
),
|
||||
).all<BreakdownRow>();
|
||||
const byEvent = await bindGroup(
|
||||
db.prepare(
|
||||
`SELECT event AS key, COUNT(*) AS total, COALESCE(SUM(ok), 0) AS ok
|
||||
FROM send_logs${where} GROUP BY event`,
|
||||
),
|
||||
).all<BreakdownRow>();
|
||||
const byStatus = await bindGroup(
|
||||
db.prepare(
|
||||
`SELECT status, COUNT(*) AS count FROM send_logs
|
||||
WHERE status IS NOT NULL${and} GROUP BY status`,
|
||||
),
|
||||
).all<StatusRow>();
|
||||
const duration = await bindGroup(
|
||||
db.prepare(
|
||||
`SELECT AVG(duration_ms) AS avg FROM send_logs WHERE duration_ms IS NOT NULL${and}`,
|
||||
),
|
||||
).all<AvgRow>();
|
||||
const attempts = await bindGroup(
|
||||
db.prepare(
|
||||
`SELECT COALESCE(SUM(attempts), 0) AS total, AVG(attempts) AS avg
|
||||
FROM send_logs WHERE attempts IS NOT NULL${and}`,
|
||||
),
|
||||
).all<AttemptsRow>();
|
||||
const recentFailures = await getFailedSendLog(db, 20, groupId);
|
||||
const total = Number(totals[0]?.total) || 0;
|
||||
const ok = Number(totals[0]?.ok) || 0;
|
||||
|
||||
return {
|
||||
total,
|
||||
ok,
|
||||
failed: total - ok,
|
||||
failureRate: total > 0 ? (total - ok) / total : 0,
|
||||
byPlatform: byPlatform.results.map((r) => {
|
||||
byPlatform: byPlatform.map((r) => {
|
||||
const t = Number(r.total) || 0;
|
||||
const o = Number(r.ok) || 0;
|
||||
return { platform: r.key ?? "unknown", total: t, ok: o, failed: t - o };
|
||||
}),
|
||||
byEvent: byEvent.results.map((r) => {
|
||||
byEvent: byEvent.map((r) => {
|
||||
const t = Number(r.total) || 0;
|
||||
const o = Number(r.ok) || 0;
|
||||
return { event: r.key ?? "unknown", total: t, ok: o, failed: t - o };
|
||||
}),
|
||||
byStatus: byStatus.results.map((r) => ({
|
||||
byStatus: byStatus.map((r) => ({
|
||||
status: String(r.status),
|
||||
count: Number(r.count) || 0,
|
||||
})),
|
||||
avgDurationMs: Number(duration.results[0]?.avg) || 0,
|
||||
totalAttempts: Number(attempts.results[0]?.total) || 0,
|
||||
avgAttempts: Number(attempts.results[0]?.avg) || 0,
|
||||
avgDurationMs: Number(duration[0]?.avg) || 0,
|
||||
totalAttempts: Number(attempts[0]?.total) || 0,
|
||||
avgAttempts: Number(attempts[0]?.avg) || 0,
|
||||
recentFailures,
|
||||
};
|
||||
} catch (err) {
|
||||
|
||||
Reference in new issue
Block a user