feat(observability): delivery metrics, admin metrics/delivery endpoints, admin API route wiring

This commit is contained in:
RhenCloud 2026-08-15 17:03:02 +08:00
parent 2470bd786d
commit 1e9ba3c5dd
No known key found for this signature in database
GPG key ID: A574A617378C4E0B
17 changed files with 682 additions and 7 deletions

View file

@ -20,7 +20,8 @@ import {
clientIp,
type GroupAccess,
} from "./auth";
import { getSendLog, getSendLogById } from "../lib/send-log";
import { getSendLog, getSendLogById, getSendLogByDelivery } from "../lib/send-log";
import { getDeliveryMetrics } from "../observability/metrics";
import { getAuditLog, recordAudit } from "../lib/audit";
import {
createInvite,
@ -1012,3 +1013,37 @@ export async function adminAudit(event: H3Event): Promise<Record<string, unknown
: entries.filter((e) => e.groupId != null && auth.scope.groupIds.has(e.groupId));
return { audit: visible };
}
/** GET /admin/api/metrics */
export async function adminApiMetrics(event: H3Event): Promise<Record<string, unknown>> {
const auth = await requireAnyAccess(event);
const env = cfEnv(event);
const metrics = await getDeliveryMetrics(env.DB);
if (auth.scope.isSuper) return { metrics };
return {
metrics: {
...metrics,
recentFailures: metrics.recentFailures.filter(
(f) => f.groupId != null && auth.scope.groupIds.has(f.groupId),
),
},
};
}
/** GET /admin/api/delivery/:deliveryId */
export async function adminApiDelivery(
event: H3Event,
deliveryId: string,
): Promise<Record<string, unknown>> {
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 };
}