feat(ui): update ui style

This commit is contained in:
RhenCloud 2026-08-16 21:25:57 +08:00
parent ca6dfd3f64
commit 47d7c9105b
No known key found for this signature in database
GPG key ID: A574A617378C4E0B
35 changed files with 1577 additions and 591 deletions

View file

@ -2,7 +2,7 @@ import type { Env, Config, Route } from "./types";
import { log } from "./lib/log";
import { migrateRoutes, validateRoutes } from "./config/schema";
const CONFIG_CACHE_TTL = 60_000;
const CONFIG_CACHE_TTL = 300_000;
const ROUTES_KEY = "config:routes";
let configCache: { config: Config; expiresAt: number } | null = null;

View file

@ -4,7 +4,7 @@ export interface MessageTracker {
delete(eventId: string, targetId: string): Promise<void>;
}
const MESSAGE_KEY_TTL_SECONDS = 604800;
const MESSAGE_KEY_TTL_SECONDS = 86400;
export function kvMessageTracker(kv: KVNamespace): MessageTracker {
const key = (eventId: string, targetId: string): string => `msg:${eventId}:${targetId}`;

View file

@ -147,12 +147,20 @@ export async function getSendLogByDelivery(
}
}
export async function getFailedSendLog(db: D1Database, limit = 20): Promise<SendRecord[]> {
export async function getFailedSendLog(
db: D1Database,
limit = 20,
groupId?: string,
): Promise<SendRecord[]> {
try {
const { results } = await db
.prepare(`SELECT ${COLUMNS} FROM send_logs WHERE ok = 0 ORDER BY ts DESC LIMIT ?`)
.bind(limit)
.all<LogRow>();
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>();
return results.map(toRecord);
} catch (err) {
log.warn({ err }, "Failed to load failed send log");

View file

@ -54,42 +54,47 @@ const EMPTY: DeliveryMetrics = {
recentFailures: [],
};
export async function getDeliveryMetrics(db: D1Database): Promise<DeliveryMetrics> {
export async function getDeliveryMetrics(db: D1Database, groupId?: string): Promise<DeliveryMetrics> {
const where = groupId ? " WHERE group_id = ?" : "";
const and = groupId ? " AND group_id = ?" : "";
const bindGroup = (stmt: D1PreparedStatement): D1PreparedStatement =>
groupId ? stmt.bind(groupId) : stmt;
try {
const totals = await db
.prepare(`SELECT COUNT(*) AS total, COALESCE(SUM(ok), 0) AS ok FROM send_logs`)
.all<TotalsRow>();
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;
const byPlatform = await db
.prepare(
const byPlatform = await bindGroup(
db.prepare(
`SELECT platform AS key, COUNT(*) AS total, COALESCE(SUM(ok), 0) AS ok
FROM send_logs GROUP BY platform`,
)
.all<BreakdownRow>();
const byEvent = await db
.prepare(
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 GROUP BY event`,
)
.all<BreakdownRow>();
const byStatus = await db
.prepare(
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 GROUP BY status`,
)
.all<StatusRow>();
const duration = await db
.prepare(`SELECT AVG(duration_ms) AS avg FROM send_logs WHERE duration_ms IS NOT NULL`)
.all<AvgRow>();
const attempts = await db
.prepare(
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`,
)
.all<AttemptsRow>();
const recentFailures = await getFailedSendLog(db, 20);
FROM send_logs WHERE attempts IS NOT NULL${and}`,
),
).all<AttemptsRow>();
const recentFailures = await getFailedSendLog(db, 20, groupId);
return {
total,

View file

@ -31,8 +31,8 @@ export const DELIVERY_QUEUE = "webhooker-delivery";
export const DELIVERY_DLQ = "webhooker-delivery-dlq";
const MAX_QUEUE_MESSAGE_BYTES = 100_000;
const PAYLOAD_KV_TTL_SECONDS = 60 * 60 * 24;
const STATE_KV_TTL_SECONDS = 60 * 60 * 24;
const PAYLOAD_KV_TTL_SECONDS = 3600;
const STATE_KV_TTL_SECONDS = 3600;
const RETRYABLE_ERROR_CODES = new Set(["DISCORD_5XX", "TELEGRAM_5XX", "NETWORK", "RETRIES"]);

View file

@ -1018,7 +1018,12 @@ export async function adminAudit(event: H3Event): Promise<Record<string, unknown
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);
const query = getQuery(event);
const groupId = String(query["groupId"] ?? "") || undefined;
if (groupId && !auth.scope.isSuper && !auth.scope.groupIds.has(groupId)) {
return respondError(event, 403, "Forbidden");
}
const metrics = await getDeliveryMetrics(env.DB, groupId);
if (auth.scope.isSuper) return { metrics };
return {
metrics: {

View file

@ -4,6 +4,8 @@ import { log } from "../lib/log";
import { migrateGroups, validateGroups } from "../config/schema";
const GROUPS_KEY = "config:groups";
const GROUPS_CACHE_TTL = 300_000;
let groupsCache: { groups: Group[]; expiresAt: number } | null = null;
/**
* Normalizes a group's member list. Groups stored with the legacy `adminIds`
@ -38,9 +40,14 @@ export function normalizeGroupMembers(group: Group): GroupMember[] {
}
export async function loadGroups(kv: KVNamespace): Promise<Group[]> {
if (groupsCache && Date.now() < groupsCache.expiresAt) {
return groupsCache.groups;
}
try {
const stored = await kv.get<Group[]>(GROUPS_KEY, "json");
if (Array.isArray(stored)) return validateGroups(migrateGroups(stored));
const groups = Array.isArray(stored) ? validateGroups(migrateGroups(stored)) : [];
groupsCache = { groups, expiresAt: Date.now() + GROUPS_CACHE_TTL };
return groups;
} catch (err) {
log.warn({ err }, "Failed to load groups from KV");
}
@ -49,6 +56,11 @@ export async function loadGroups(kv: KVNamespace): Promise<Group[]> {
export async function saveGroups(kv: KVNamespace, groups: Group[]): Promise<void> {
await kv.put(GROUPS_KEY, JSON.stringify(groups));
groupsCache = null;
}
export function invalidateGroupsCache(): void {
groupsCache = null;
}
/** Case-insensitive match of a GitHub userId or login against a list of ids/logins. */

View file

@ -10,7 +10,7 @@ export interface Invite {
note?: string;
}
const INVITE_TTL = 7 * 24 * 3600;
const INVITE_TTL = 24 * 3600;
function generateInviteToken(): string {
const bytes = new Uint8Array(32);

View file

@ -1,7 +1,7 @@
import type { Env } from "../types";
const SESSION_COOKIE = "wh_admin_session";
const SESSION_TTL = 7 * 24 * 3600;
const SESSION_TTL = 24 * 3600;
export interface AdminSession {
userId: string;

View file

@ -126,7 +126,7 @@ export async function processWebhook(
// retries of the same delivery never dispatch twice.
const store = kvIdempotencyStore(env.KV);
const key = deliveryKey(provider.id, tenantId, event.deliveryId);
if (!(await store.claim(key, 300))) {
if (!(await store.claim(key, 120))) {
return { status: 200, body: { ok: true, duplicate: true, requestId } };
}
}