feat(telemetry): rollups, alerts, scheduler, health checks, admin server fns

- pure rollup accumulators with tests; daily upserts keyed date/metric/dim
- alert checks with db-backed cooldown and push to admin players
- in-process scheduler started from health probes
- health endpoint checks pb + supertokens, stale-while-revalidate, always 200
- sse connection counter moved to emitter
- telemetry feature server fns: events/errors/groups/rollups/alerts/status
This commit is contained in:
2026-08-25 21:38:08 -07:00
parent e2863c1f6c
commit 06fe1f4ed7
12 changed files with 1009 additions and 11 deletions
+11 -6
View File
@@ -1,9 +1,14 @@
import { createFileRoute } from "@tanstack/react-router";
import { serverEvents, EVENT_TYPES, type ServerEvent } from "@/lib/events/emitter";
import {
serverEvents,
EVENT_TYPES,
incrementConnections,
decrementConnections,
type ServerEvent,
} from "@/lib/events/emitter";
import { logger } from "@/lib/logger";
import { superTokensRequestMiddleware } from "@/utils/supertokens";
let activeConnections = 0;
const encoder = new TextEncoder();
export const Route = createFileRoute("/api/events/$")({
@@ -11,9 +16,9 @@ export const Route = createFileRoute("/api/events/$")({
middleware: [superTokensRequestMiddleware],
handlers: {
GET: ({ request }) => {
activeConnections++;
const active = incrementConnections();
const connectionId = `conn_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
logger.info(`ServerEvents | New connection ${connectionId}. Active: ${activeConnections}`);
logger.info(`ServerEvents | New connection ${connectionId}. Active: ${active}`);
let cleanedUp = false;
let cleanup = () => {};
@@ -41,12 +46,12 @@ export const Route = createFileRoute("/api/events/$")({
cleanup = () => {
if (cleanedUp) return;
cleanedUp = true;
activeConnections--;
const remaining = decrementConnections();
for (const type of EVENT_TYPES) {
serverEvents.off(type, handleEvent);
}
clearInterval(pingInterval);
logger.info(`ServerEvents | Connection ${connectionId} cleanup completed. Active: ${activeConnections}`);
logger.info(`ServerEvents | Connection ${connectionId} cleanup completed. Active: ${remaining}`);
};
request.signal?.addEventListener("abort", cleanup);