Spaces:
Runtime error
Runtime error
File size: 44,639 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 | "use client";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useTranslations } from "next-intl";
import Link from "next/link";
import Card from "@/shared/components/Card";
import ProviderIcon from "@/shared/components/ProviderIcon";
import ModelCooldownsCard from "./components/ModelCooldownsCard";
import { useProviderNodeMap, resolveProviderName } from "@/lib/display/useProviderNodeMap";
type KnownBreakerState = "CLOSED" | "OPEN" | "HALF_OPEN" | "DEGRADED";
type BreakerState = KnownBreakerState | (string & {});
type ProviderBreaker = {
provider: string;
state: BreakerState;
failureCount: number;
lastFailure: string | null;
retryAfterMs: number;
};
type LockoutEntry = {
reason?: string;
until?: number | string | null;
remainingMs?: number;
model?: string;
accountId?: string;
};
type SessionTop = {
sessionId: string;
requestCount: number;
connectionId?: string | null;
ageMs: number;
idleMs: number;
createdAt?: string;
lastActiveAt?: string;
};
type QuotaMonitor = {
sessionId?: string;
accountId?: string;
provider?: string;
window?: string;
status?: "ok" | "alerting" | "exhausted" | "error" | string;
remainingPercent?: number;
};
type HealthPayload = {
timestamp?: string;
providerBreakers?: ProviderBreaker[];
lockouts?: Record<string, LockoutEntry>;
quotaMonitor?: {
active?: number;
alerting?: number;
exhausted?: number;
errors?: number;
monitors?: QuotaMonitor[];
};
sessions?: {
activeCount?: number;
stickyBoundCount?: number;
byApiKey?: Record<string, number>;
top?: SessionTop[];
};
};
type Connection = {
id: string;
provider: string;
name?: string;
displayName?: string;
email?: string;
authType?: string;
rateLimitedUntil?: string | null;
testStatus?: string;
lastError?: string;
lastErrorType?: string;
errorCode?: string | number;
backoffLevel?: number;
};
type FeedEventKind =
| "circuit-opened"
| "circuit-degraded"
| "circuit-recovered"
| "circuit-closed"
| "cooldown-added"
| "cooldown-cleared"
| "lockout-added"
| "lockout-cleared"
| "session-new"
| "quota-alert"
| "quota-exhausted"
| "quota-recovered";
type FeedEvent = {
id: string;
ts: number;
kind: FeedEventKind;
title: string;
detail: string;
};
type FeedFilter = "all" | "circuits" | "cooldowns" | "lockouts" | "sessions" | "quotas";
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Constants & helpers
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const REFRESH_INTERVAL_MS = 5000;
const FEED_MAX_EVENTS = 50;
const EMPTY_PROVIDER_BREAKERS: ProviderBreaker[] = [];
type BreakerTone = { dot: string; bg: string; ring: string; label: string; icon: string };
const BREAKER_TONE: Record<string, BreakerTone> = {
CLOSED: {
dot: "#22c55e",
bg: "rgba(34,197,94,0.10)",
ring: "rgba(34,197,94,0.30)",
label: "OK",
icon: "check_circle",
},
HALF_OPEN: {
dot: "#eab308",
bg: "rgba(234,179,8,0.10)",
ring: "rgba(234,179,8,0.30)",
label: "RECOV",
icon: "sync",
},
DEGRADED: {
dot: "#f97316",
bg: "rgba(249,115,22,0.10)",
ring: "rgba(249,115,22,0.30)",
label: "DEG",
icon: "warning",
},
OPEN: {
dot: "#ef4444",
bg: "rgba(239,68,68,0.10)",
ring: "rgba(239,68,68,0.30)",
label: "OPEN",
icon: "block",
},
};
const FALLBACK_BREAKER_TONE: BreakerTone = {
dot: "#64748b",
bg: "rgba(100,116,139,0.10)",
ring: "rgba(100,116,139,0.30)",
label: "UNK",
icon: "help",
};
const FEED_KIND_META: Record<FeedEventKind, { icon: string; color: string; group: FeedFilter }> = {
"circuit-opened": { icon: "block", color: "#ef4444", group: "circuits" },
"circuit-degraded": { icon: "warning", color: "#f97316", group: "circuits" },
"circuit-recovered": { icon: "sync", color: "#eab308", group: "circuits" },
"circuit-closed": { icon: "check_circle", color: "#22c55e", group: "circuits" },
"cooldown-added": { icon: "ac_unit", color: "#3b82f6", group: "cooldowns" },
"cooldown-cleared": { icon: "lock_open", color: "#22c55e", group: "cooldowns" },
"lockout-added": { icon: "lock", color: "#f97316", group: "lockouts" },
"lockout-cleared": { icon: "lock_open", color: "#22c55e", group: "lockouts" },
"session-new": { icon: "fingerprint", color: "#06b6d4", group: "sessions" },
"quota-alert": { icon: "warning", color: "#eab308", group: "quotas" },
"quota-exhausted": { icon: "error", color: "#ef4444", group: "quotas" },
"quota-recovered": { icon: "check_circle", color: "#22c55e", group: "quotas" },
};
function fmtMs(ms: number | undefined | null): string {
if (!ms || ms <= 0) return "0s";
if (ms < 1000) return `${ms}ms`;
const s = Math.ceil(ms / 1000);
if (s < 60) return `${s}s`;
const m = Math.floor(s / 60);
const rs = s % 60;
if (m < 60) return rs > 0 ? `${m}m ${rs}s` : `${m}m`;
const h = Math.floor(m / 60);
const rm = m % 60;
return `${h}h ${rm}m`;
}
function fmtClock(ts: number): string {
return new Date(ts).toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
});
}
function shortId(value: string | null | undefined, max = 12): string {
if (!value) return "β";
return value.length > max ? `${value.slice(0, max)}β¦` : value;
}
function untilMs(value: number | string | null | undefined): number {
if (typeof value === "number") return value - Date.now();
if (typeof value === "string") {
const ts = new Date(value).getTime();
if (Number.isFinite(ts)) return ts - Date.now();
}
return 0;
}
function normalizeBreakerState(state: string | null | undefined): string {
return String(state || "")
.trim()
.toUpperCase();
}
function getBreakerTone(normalizedState: string): BreakerTone {
return BREAKER_TONE[normalizedState] || FALLBACK_BREAKER_TONE;
}
function pushFeed(prev: FeedEvent[], events: FeedEvent[]): FeedEvent[] {
if (events.length === 0) return prev;
const merged = [...events, ...prev];
return merged.slice(0, FEED_MAX_EVENTS);
}
// Compute new feed events by diffing previous vs next snapshot.
// Conservative: only emit transitions (added/cleared/state-change), never
// emit for items that simply persist between polls.
function diffSnapshots(
prev: { health: HealthPayload | null; conns: Connection[] },
next: { health: HealthPayload | null; conns: Connection[] },
nowTs: number
): FeedEvent[] {
const out: FeedEvent[] = [];
// Circuit breakers
const prevBreakers = new Map((prev.health?.providerBreakers ?? []).map((b) => [b.provider, b]));
const nextBreakers = new Map((next.health?.providerBreakers ?? []).map((b) => [b.provider, b]));
for (const [provider, nextB] of nextBreakers) {
const prevB = prevBreakers.get(provider);
if (!prevB) continue;
const prevState = normalizeBreakerState(prevB.state);
const nextState = normalizeBreakerState(nextB.state);
if (prevState === nextState) continue;
if (nextState === "OPEN") {
out.push({
id: `cb-open-${provider}-${nowTs}`,
ts: nowTs,
kind: "circuit-opened",
title: `${provider} circuit OPEN`,
detail: `threshold hit Β· retry in ${fmtMs(nextB.retryAfterMs)}`,
});
} else if (nextState === "HALF_OPEN") {
out.push({
id: `cb-half-${provider}-${nowTs}`,
ts: nowTs,
kind: "circuit-recovered",
title: `${provider} HALF_OPEN`,
detail: `probing recovery`,
});
} else if (nextState === "DEGRADED") {
out.push({
id: `cb-deg-${provider}-${nowTs}`,
ts: nowTs,
kind: "circuit-degraded",
title: `${provider} DEGRADED`,
detail: `${nextB.failureCount} failures Β· degraded but serving`,
});
} else if (nextState === "CLOSED" && prevState !== "CLOSED") {
out.push({
id: `cb-close-${provider}-${nowTs}`,
ts: nowTs,
kind: "circuit-closed",
title: `${provider} circuit CLOSED`,
detail: `recovered to healthy`,
});
}
}
// Cooldowns (per-connection rateLimitedUntil)
const prevCooldowns = new Map(
prev.conns
.filter((c) => c.rateLimitedUntil && untilMs(c.rateLimitedUntil) > 0)
.map((c) => [c.id, c])
);
const nextCooldowns = new Map(
next.conns
.filter((c) => c.rateLimitedUntil && untilMs(c.rateLimitedUntil) > 0)
.map((c) => [c.id, c])
);
for (const [id, conn] of nextCooldowns) {
if (!prevCooldowns.has(id)) {
const label = conn.name || conn.email || conn.displayName || shortId(id);
out.push({
id: `cd-add-${id}-${nowTs}`,
ts: nowTs,
kind: "cooldown-added",
title: `${conn.provider}/${label} cooling`,
detail: `${fmtMs(untilMs(conn.rateLimitedUntil))} Β· ${conn.lastError || "unavailable"}`,
});
}
}
for (const [id, conn] of prevCooldowns) {
if (!nextCooldowns.has(id)) {
const label = conn.name || conn.email || conn.displayName || shortId(id);
out.push({
id: `cd-clr-${id}-${nowTs}`,
ts: nowTs,
kind: "cooldown-cleared",
title: `${conn.provider}/${label} resumed`,
detail: `cooldown cleared`,
});
}
}
// Lockouts
const prevLockKeys = new Set(Object.keys(prev.health?.lockouts ?? {}));
const nextLockKeys = new Set(Object.keys(next.health?.lockouts ?? {}));
for (const key of nextLockKeys) {
if (!prevLockKeys.has(key)) {
const entry = next.health?.lockouts?.[key];
out.push({
id: `lk-add-${key}-${nowTs}`,
ts: nowTs,
kind: "lockout-added",
title: `${key} locked`,
detail: entry?.reason ? `reason: ${entry.reason}` : "rate-limit lockout",
});
}
}
for (const key of prevLockKeys) {
if (!nextLockKeys.has(key)) {
out.push({
id: `lk-clr-${key}-${nowTs}`,
ts: nowTs,
kind: "lockout-cleared",
title: `${key} unlocked`,
detail: `lockout expired`,
});
}
}
// Sessions (additions only β sessions are short-lived, deletions are noise)
const prevSessions = new Set((prev.health?.sessions?.top ?? []).map((s) => s.sessionId));
for (const s of next.health?.sessions?.top ?? []) {
if (prevSessions.has(s.sessionId)) continue;
out.push({
id: `ss-new-${s.sessionId}-${nowTs}`,
ts: nowTs,
kind: "session-new",
title: `new session ${shortId(s.sessionId, 8)}`,
detail: s.connectionId ? `bound to ${shortId(s.connectionId, 10)}` : "no binding yet",
});
}
// Quota monitors (alerting / exhausted transitions)
const prevQuota = new Map(
(prev.health?.quotaMonitor?.monitors ?? []).map((m) => [
`${m.accountId ?? ""}:${m.provider ?? ""}:${m.window ?? ""}`,
m,
])
);
for (const m of next.health?.quotaMonitor?.monitors ?? []) {
const key = `${m.accountId ?? ""}:${m.provider ?? ""}:${m.window ?? ""}`;
const prevM = prevQuota.get(key);
const prevStatus = prevM?.status;
if (m.status === prevStatus) continue;
if (m.status === "exhausted") {
out.push({
id: `qe-${key}-${nowTs}`,
ts: nowTs,
kind: "quota-exhausted",
title: `${m.accountId ?? "?"} EXHAUSTED`,
detail: `${m.window ?? ""}${m.provider ? ` Β· ${m.provider}` : ""}`,
});
} else if (m.status === "alerting") {
out.push({
id: `qa-${key}-${nowTs}`,
ts: nowTs,
kind: "quota-alert",
title: `${m.accountId ?? "?"} ALERTING`,
detail: `${m.window ?? ""}${
typeof m.remainingPercent === "number" ? ` Β· ${Math.round(m.remainingPercent)}% left` : ""
}`,
});
} else if (prevStatus === "exhausted" || prevStatus === "alerting") {
out.push({
id: `qr-${key}-${nowTs}`,
ts: nowTs,
kind: "quota-recovered",
title: `${m.accountId ?? "?"} recovered`,
detail: `${m.window ?? ""} back to OK`,
});
}
}
return out;
}
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Component
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export default function RuntimePageClient() {
const t = useTranslations("runtime");
const nodeMap = useProviderNodeMap();
const [health, setHealth] = useState<HealthPayload | null>(null);
const [connections, setConnections] = useState<Connection[]>([]);
const [loading, setLoading] = useState(true);
const [paused, setPaused] = useState(false);
const [lastUpdated, setLastUpdated] = useState<number | null>(null);
const [feed, setFeed] = useState<FeedEvent[]>([]);
const [feedFilter, setFeedFilter] = useState<FeedFilter>("all");
const prevSnapshotRef = useRef<{ health: HealthPayload | null; conns: Connection[] }>({
health: null,
conns: [],
});
const initialLoadRef = useRef(true);
const fetchAll = useCallback(async () => {
try {
const [healthRes, connsRes] = await Promise.all([
fetch("/api/monitoring/health"),
fetch("/api/providers/client"),
]);
const healthData: HealthPayload | null = healthRes.ok ? await healthRes.json() : null;
const connsData = connsRes.ok ? await connsRes.json() : null;
const conns: Connection[] = Array.isArray(connsData?.connections)
? connsData.connections
: [];
const nowTs = Date.now();
const prev = prevSnapshotRef.current;
// Skip diff on first load β everything is "new" but we don't want a
// burst of fake events on mount.
if (!initialLoadRef.current) {
const events = diffSnapshots(prev, { health: healthData, conns }, nowTs);
if (events.length > 0) setFeed((cur) => pushFeed(cur, events));
}
initialLoadRef.current = false;
prevSnapshotRef.current = { health: healthData, conns };
setHealth(healthData);
setConnections(conns);
setLastUpdated(nowTs);
} catch (err) {
console.error("[Runtime] fetch failed", err);
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
fetchAll();
if (paused) return;
const id = setInterval(fetchAll, REFRESH_INTERVAL_MS);
return () => clearInterval(id);
}, [fetchAll, paused]);
// ββ Derived data ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const cooldowns = useMemo(() => {
return connections.filter((c) => c.rateLimitedUntil && untilMs(c.rateLimitedUntil) > 0);
}, [connections]);
const breakers = health?.providerBreakers ?? EMPTY_PROVIDER_BREAKERS;
const lockoutEntries = useMemo<Array<[string, LockoutEntry]>>(
() => Object.entries(health?.lockouts ?? {}),
[health]
);
const counts = useMemo(() => {
let openCircuits = 0;
let halfCircuits = 0;
let degradedCircuits = 0;
let unknownCircuits = 0;
for (const breaker of breakers) {
const state = normalizeBreakerState(breaker.state);
if (state === "OPEN") {
openCircuits++;
} else if (state === "HALF_OPEN") {
halfCircuits++;
} else if (state === "DEGRADED") {
degradedCircuits++;
} else if (state !== "CLOSED") {
unknownCircuits++;
}
}
const totalBreakers = breakers.length;
const affectedCircuits = openCircuits + halfCircuits + degradedCircuits + unknownCircuits;
const sessions = health?.sessions?.activeCount ?? 0;
const lockouts = lockoutEntries.length;
const quota = health?.quotaMonitor;
const quotaAlertingTotal =
(quota?.alerting ?? 0) + (quota?.exhausted ?? 0) + (quota?.errors ?? 0);
return {
sessions,
stickyBound: health?.sessions?.stickyBoundCount ?? 0,
openCircuits,
halfCircuits,
degradedCircuits,
unknownCircuits,
affectedCircuits,
totalBreakers,
cooldowns: cooldowns.length,
lockouts,
quotaAlerting: quotaAlertingTotal,
quotaExhausted: quota?.exhausted ?? 0,
};
}, [breakers, cooldowns, health, lockoutEntries]);
const filteredFeed = useMemo(() => {
if (feedFilter === "all") return feed;
return feed.filter((ev) => FEED_KIND_META[ev.kind].group === feedFilter);
}, [feed, feedFilter]);
const overallHealthy = counts.totalBreakers - counts.affectedCircuits;
const overallPercent =
counts.totalBreakers > 0 ? Math.round((overallHealthy / counts.totalBreakers) * 100) : 100;
// ββ Render ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
return (
<div className="flex flex-col gap-4">
{/* Header */}
<div className="flex items-start justify-between flex-wrap gap-3">
<div>
<h1 className="text-xl font-bold text-text-main flex items-center gap-2">
<span className="material-symbols-outlined text-[24px] text-primary">bolt</span>
{t("title")}
</h1>
<p className="text-sm text-text-muted mt-0.5">{t("description")}</p>
</div>
<div className="flex items-center gap-2">
<span className="text-[11px] text-text-muted tabular-nums">
{lastUpdated ? `β» ${fmtClock(lastUpdated)}` : "β"}
</span>
<button
type="button"
onClick={() => setPaused((p) => !p)}
className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-bg-subtle border border-border text-text-main text-[12px] cursor-pointer hover:bg-black/[0.04] dark:hover:bg-white/[0.04]"
>
<span className="material-symbols-outlined text-[16px]">
{paused ? "play_arrow" : "pause"}
</span>
{paused ? t("resume") : t("pause")}
</button>
<button
type="button"
onClick={() => fetchAll()}
disabled={loading}
className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-bg-subtle border border-border text-text-main text-[12px] cursor-pointer hover:bg-black/[0.04] dark:hover:bg-white/[0.04] disabled:opacity-50"
title={t("refreshNow")}
>
<span
className={`material-symbols-outlined text-[16px] ${loading ? "animate-spin" : ""}`}
>
refresh
</span>
</button>
</div>
</div>
{/* Row 1 β KPIs */}
<div className="grid grid-cols-2 lg:grid-cols-4 gap-3">
<KpiCard
icon="fingerprint"
label={t("kpiSessions")}
value={counts.sessions}
hint={t("hintStickyBound", { count: counts.stickyBound })}
tone="#06b6d4"
onClick={() => setFeedFilter("sessions")}
active={feedFilter === "sessions"}
/>
<KpiCard
icon="bolt"
label={t("kpiCircuits")}
value={`${counts.openCircuits} / ${counts.totalBreakers}`}
hint={
counts.halfCircuits + counts.degradedCircuits + counts.unknownCircuits > 0
? t("hintRecovering", {
count: counts.halfCircuits + counts.degradedCircuits + counts.unknownCircuits,
})
: counts.openCircuits === 0
? t("hintAllHealthy")
: t("hintOpen")
}
tone={
counts.openCircuits > 0
? "#ef4444"
: counts.halfCircuits + counts.degradedCircuits + counts.unknownCircuits > 0
? "#eab308"
: "#22c55e"
}
onClick={() => setFeedFilter("circuits")}
active={feedFilter === "circuits"}
/>
<KpiCard
icon="ac_unit"
label={t("kpiCooldowns")}
value={counts.cooldowns}
hint={t("hintConnsCooling")}
tone={counts.cooldowns > 0 ? "#3b82f6" : "#22c55e"}
onClick={() => setFeedFilter("cooldowns")}
active={feedFilter === "cooldowns"}
/>
<KpiCard
icon="lock"
label={t("kpiLockouts")}
value={counts.lockouts}
hint={t("hintModelsBlocked")}
tone={counts.lockouts > 0 ? "#f97316" : "#22c55e"}
onClick={() => setFeedFilter("lockouts")}
active={feedFilter === "lockouts"}
/>
</div>
<ModelCooldownsCard />
{/* Row 2 β Resilience layers (left, 2/3) + Live Feed (right, 1/3) */}
<div className="grid grid-cols-1 xl:grid-cols-[2fr_1fr] gap-3">
<Card padding="md">
<SectionHeader
icon="shield"
title={t("resilienceTitle")}
subtitle={t("resilienceSubtitle")}
trailing={
<div className="flex items-center gap-3 text-[11px]">
<span className="text-green-500">β {overallHealthy}</span>
<span className="text-amber-500">
β {counts.halfCircuits + counts.degradedCircuits + counts.unknownCircuits}
</span>
<span className="text-red-500">β {counts.openCircuits}</span>
</div>
}
/>
<div className="mb-4 mt-2">
<div className="h-2 rounded-sm bg-black/[0.06] dark:bg-white/[0.06] overflow-hidden">
<div
className="h-full rounded-sm bg-gradient-to-r from-green-500 via-amber-500 to-red-500/40"
style={{ width: `${overallPercent}%` }}
/>
</div>
<div className="mt-1 text-[11px] text-text-muted tabular-nums">
{t("providersHealthy", { percent: overallPercent })}
</div>
</div>
{/* Layer 1 */}
<LayerSection
id={1}
title={t("layer1Title")}
description={t("layer1Desc")}
badge={t("badgeAffectedOf", {
affected: counts.affectedCircuits,
total: counts.totalBreakers,
})}
badgeTone={
counts.openCircuits > 0
? "red"
: counts.halfCircuits + counts.degradedCircuits + counts.unknownCircuits > 0
? "amber"
: "green"
}
>
{breakers.length === 0 ? (
<EmptyHint text={t("emptyCircuits")} />
) : (
<div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 gap-2">
{breakers.map((b) => {
const state = normalizeBreakerState(b.state);
const tone = getBreakerTone(state);
return (
<div
key={b.provider}
className="rounded-md border px-2.5 py-2 flex flex-col gap-0.5"
style={{ borderColor: tone.ring, background: tone.bg }}
title={`${resolveProviderName(b.provider, nodeMap)} Β· ${state || "UNKNOWN"} Β· failures ${b.failureCount}`}
>
<div className="flex items-center gap-1.5 text-[11px] font-semibold text-text-main">
<ProviderIcon providerId={b.provider} size={14} />
<span className="truncate flex-1">
{resolveProviderName(b.provider, nodeMap)}
</span>
<span style={{ color: tone.dot }} className="text-[10px] font-bold">
{tone.label}
</span>
</div>
<div className="text-[10px] text-text-muted tabular-nums">
{state === "OPEN"
? `retry ${fmtMs(b.retryAfterMs)}`
: `${b.failureCount} failures`}
</div>
</div>
);
})}
</div>
)}
</LayerSection>
{/* Layer 2 */}
<LayerSection
id={2}
title={t("layer2Title")}
description={t("layer2Desc")}
badge={t("badgeCooling", { count: counts.cooldowns })}
badgeTone={counts.cooldowns > 0 ? "blue" : "green"}
>
{cooldowns.length === 0 ? (
<EmptyHint text={t("emptyCooldowns")} />
) : (
<div className="flex flex-col divide-y divide-border/60">
{cooldowns.slice(0, 8).map((c) => {
const remaining = untilMs(c.rateLimitedUntil);
const label = c.name || c.email || c.displayName || shortId(c.id);
return (
<div
key={c.id}
className="py-2 grid items-center gap-2"
style={{
gridTemplateColumns: "minmax(0,1.5fr) 70px minmax(0,1fr) 60px 60px",
}}
>
<div className="flex items-center gap-2 min-w-0">
<ProviderIcon providerId={c.provider} size={18} />
<div className="min-w-0">
<div className="text-[12px] text-text-main truncate font-medium">
{resolveProviderName(c.provider, nodeMap)}/{label}
</div>
{c.lastErrorType && (
<div className="text-[10px] text-text-muted truncate">
{c.lastErrorType}
</div>
)}
</div>
</div>
<div className="text-[11px] tabular-nums text-blue-400">
{fmtMs(remaining)}
</div>
<div className="text-[10px] text-text-muted truncate">
{c.lastError || "β"}
</div>
<div className="text-[10px] text-text-muted tabular-nums">
L{c.backoffLevel ?? 0}
</div>
<div className="text-[10px] text-text-muted tabular-nums text-right">
{c.errorCode ?? ""}
</div>
</div>
);
})}
{cooldowns.length > 8 && (
<div className="pt-2 text-[10px] text-text-muted">
{t("moreCooldowns", { count: cooldowns.length - 8 })}
</div>
)}
</div>
)}
</LayerSection>
{/* Layer 3 */}
<LayerSection
id={3}
title={t("layer3Title")}
description={t("layer3Desc")}
badge={t("badgeLocked", { count: lockoutEntries.length })}
badgeTone={lockoutEntries.length > 0 ? "orange" : "green"}
>
{lockoutEntries.length === 0 ? (
<EmptyHint text={t("emptyLockouts")} />
) : (
<div className="flex flex-col divide-y divide-border/60">
{lockoutEntries.slice(0, 8).map(([key, lk]) => {
const remaining =
typeof lk.remainingMs === "number" ? lk.remainingMs : untilMs(lk.until);
return (
<div
key={key}
className="py-2 grid items-center gap-2"
style={{ gridTemplateColumns: "minmax(0,1.5fr) minmax(0,1fr) 70px" }}
>
<div className="flex items-center gap-2 min-w-0">
<span className="material-symbols-outlined text-[14px] text-orange-400 shrink-0">
lock
</span>
<span className="text-[12px] text-text-main truncate font-medium">
{key}
</span>
</div>
<div className="text-[10px] text-text-muted truncate">
{lk.reason || "rate limit"}
</div>
<div className="text-[11px] tabular-nums text-orange-400 text-right">
{remaining > 0 ? fmtMs(remaining) : "β"}
</div>
</div>
);
})}
{lockoutEntries.length > 8 && (
<div className="pt-2 text-[10px] text-text-muted">
{t("moreLockouts", { count: lockoutEntries.length - 8 })}
</div>
)}
</div>
)}
</LayerSection>
</Card>
{/* Live Feed */}
<Card padding="md">
<SectionHeader
icon="rss_feed"
title={t("feedTitle")}
subtitle={t("feedSubtitle", { count: filteredFeed.length })}
trailing={
<div className="flex items-center gap-1.5">
<select
value={feedFilter}
onChange={(e) => setFeedFilter(e.target.value as FeedFilter)}
className="text-[10px] bg-bg-subtle border border-border rounded px-1.5 py-1 cursor-pointer text-text-main"
>
<option value="all">{t("feedFilterAll")}</option>
<option value="circuits">{t("feedFilterCircuits")}</option>
<option value="cooldowns">{t("feedFilterCooldowns")}</option>
<option value="lockouts">{t("feedFilterLockouts")}</option>
<option value="sessions">{t("feedFilterSessions")}</option>
<option value="quotas">{t("feedFilterQuotas")}</option>
</select>
<button
type="button"
onClick={() => setFeed([])}
className="text-[10px] px-1.5 py-1 rounded border border-border text-text-muted hover:text-text-main hover:bg-black/[0.04] dark:hover:bg-white/[0.04] cursor-pointer"
title={t("feedClear")}
>
{t("feedClear")}
</button>
</div>
}
/>
<div className="mt-2 flex flex-col gap-1.5 max-h-[640px] overflow-auto pr-1">
{filteredFeed.length === 0 ? (
<div className="text-center py-12 text-text-muted">
<span className="material-symbols-outlined text-[40px] opacity-30 block mb-2">
hourglass_empty
</span>
<p className="text-xs">
{feed.length === 0 ? t("feedEmptyWaiting") : t("feedEmptyFiltered")}
</p>
</div>
) : (
filteredFeed.map((ev) => {
const meta = FEED_KIND_META[ev.kind];
return (
<div
key={ev.id}
className="flex items-start gap-2 px-2 py-1.5 rounded hover:bg-black/[0.03] dark:hover:bg-white/[0.03]"
>
<span
className="material-symbols-outlined text-[14px] mt-0.5"
style={{ color: meta.color }}
>
{meta.icon}
</span>
<div className="flex-1 min-w-0">
<div className="flex items-center justify-between gap-2">
<div className="text-[11px] font-medium text-text-main truncate">
{ev.title}
</div>
<span className="text-[9px] text-text-muted tabular-nums shrink-0">
{fmtClock(ev.ts)}
</span>
</div>
<div className="text-[10px] text-text-muted truncate">{ev.detail}</div>
</div>
</div>
);
})
)}
</div>
</Card>
</div>
{/* Row 3 β Sessions table + Quota Monitors */}
<div className="grid grid-cols-1 xl:grid-cols-[3fr_2fr] gap-3">
<Card padding="md">
<SectionHeader
icon="fingerprint"
title={t("sessionsTitle")}
subtitle={t("sessionsSubtitle")}
trailing={
<span className="text-[11px] text-text-muted">
{t("sessionsActive", { count: counts.sessions })}
</span>
}
/>
{(health?.sessions?.top ?? []).length === 0 ? (
<div className="text-center py-8 text-text-muted">
<span className="material-symbols-outlined text-[40px] opacity-30 block mb-2">
fingerprint
</span>
<p className="text-sm">{t("sessionsEmptyTitle")}</p>
<p className="text-xs mt-1">{t("sessionsEmptyHint")}</p>
</div>
) : (
<div className="overflow-x-auto mt-2">
<table className="w-full text-sm">
<thead>
<tr className="border-b border-border/40">
<th className="text-left py-2 px-2 text-[10px] font-semibold text-text-muted uppercase tracking-wider">
{t("tblSession")}
</th>
<th className="text-right py-2 px-2 text-[10px] font-semibold text-text-muted uppercase tracking-wider">
{t("tblAge")}
</th>
<th className="text-right py-2 px-2 text-[10px] font-semibold text-text-muted uppercase tracking-wider">
{t("tblIdle")}
</th>
<th className="text-right py-2 px-2 text-[10px] font-semibold text-text-muted uppercase tracking-wider">
{t("tblReqs")}
</th>
<th className="text-left py-2 px-2 text-[10px] font-semibold text-text-muted uppercase tracking-wider">
{t("tblBoundTo")}
</th>
</tr>
</thead>
<tbody>
{(health?.sessions?.top ?? []).map((s) => (
<tr key={s.sessionId} className="border-b border-border/10 hover:bg-surface/20">
<td className="py-2 px-2">
<span className="font-mono text-[11px] text-text-muted">
{shortId(s.sessionId, 14)}
</span>
</td>
<td className="py-2 px-2 text-right text-[11px] text-text-muted tabular-nums">
{fmtMs(s.ageMs)}
</td>
<td className="py-2 px-2 text-right text-[11px] text-text-muted tabular-nums">
{fmtMs(s.idleMs)}
</td>
<td className="py-2 px-2 text-right">
<span className="text-[11px] font-semibold tabular-nums">
{s.requestCount}
</span>
</td>
<td className="py-2 px-2">
{s.connectionId ? (
<span className="font-mono text-[10px] text-cyan-400">
{shortId(s.connectionId, 12)}
</span>
) : (
<span className="text-text-muted/40">β</span>
)}
</td>
</tr>
))}
</tbody>
</table>
{Object.keys(health?.sessions?.byApiKey ?? {}).length > 0 && (
<div className="mt-2 pt-2 border-t border-border/40 text-[10px] text-text-muted">
{t("topApiKeys")}:{" "}
{Object.entries(health?.sessions?.byApiKey ?? {})
.sort((a, b) => b[1] - a[1])
.slice(0, 3)
.map(([k, n]) => `${shortId(k, 8)}(${n})`)
.join(" Β· ")}
</div>
)}
</div>
)}
</Card>
<Card padding="md">
<SectionHeader
icon="radar"
title={t("quotaMonitorsTitle")}
subtitle={t("quotaMonitorsSubtitle")}
trailing={
<Link href="/dashboard/quota" className="text-[11px] text-primary hover:underline">
{t("openQuota")} β
</Link>
}
/>
{(() => {
const monitors = health?.quotaMonitor?.monitors ?? [];
const exhausted = monitors.filter((m) => m.status === "exhausted");
const alerting = monitors.filter((m) => m.status === "alerting");
const errors = monitors.filter((m) => m.status === "error");
const total = exhausted.length + alerting.length + errors.length;
if (total === 0) {
return (
<div className="text-center py-8 text-text-muted">
<span className="material-symbols-outlined text-[40px] opacity-30 block mb-2">
radar
</span>
<p className="text-sm">{t("allQuotasHealthy")}</p>
</div>
);
}
return (
<div className="mt-2 flex flex-col gap-3">
{exhausted.length > 0 && (
<QuotaGroup tone="red" label={t("statusExhausted")} items={exhausted} />
)}
{alerting.length > 0 && (
<QuotaGroup tone="amber" label={t("statusAlerting")} items={alerting} />
)}
{errors.length > 0 && (
<QuotaGroup tone="orange" label={t("statusError")} items={errors} />
)}
</div>
);
})()}
</Card>
</div>
</div>
);
}
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Sub-components
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function KpiCard({
icon,
label,
value,
hint,
tone,
onClick,
active,
}: {
icon: string;
label: string;
value: string | number;
hint: string;
tone: string;
onClick: () => void;
active: boolean;
}) {
return (
<button
type="button"
onClick={onClick}
className="text-left rounded-xl border px-4 py-3 transition-colors cursor-pointer bg-surface"
style={{
borderColor: active ? tone : "var(--color-border)",
boxShadow: active ? `0 0 0 2px ${tone}22` : undefined,
}}
>
<div className="flex items-center justify-between">
<span className="text-[11px] uppercase tracking-wider font-semibold text-text-muted">
{label}
</span>
<span className="material-symbols-outlined text-[16px]" style={{ color: tone }}>
{icon}
</span>
</div>
<div className="mt-1 text-2xl font-bold tabular-nums" style={{ color: tone }}>
{value}
</div>
<div className="text-[10px] text-text-muted truncate">{hint}</div>
</button>
);
}
function SectionHeader({
icon,
title,
subtitle,
trailing,
}: {
icon: string;
title: string;
subtitle?: string;
trailing?: React.ReactNode;
}) {
return (
<div className="flex items-start justify-between gap-3">
<div className="flex items-start gap-2">
<span className="material-symbols-outlined text-[20px] text-primary mt-0.5">{icon}</span>
<div>
<h2 className="text-base font-semibold text-text-main leading-tight">{title}</h2>
{subtitle && <p className="text-[11px] text-text-muted mt-0.5">{subtitle}</p>}
</div>
</div>
{trailing && <div className="shrink-0">{trailing}</div>}
</div>
);
}
function LayerSection({
id,
title,
description,
badge,
badgeTone,
children,
}: {
id: 1 | 2 | 3;
title: string;
description: string;
badge: string;
badgeTone: "red" | "amber" | "green" | "blue" | "orange";
children: React.ReactNode;
}) {
const toneMap = {
red: { bg: "rgba(239,68,68,0.10)", text: "#ef4444" },
amber: { bg: "rgba(234,179,8,0.10)", text: "#eab308" },
green: { bg: "rgba(34,197,94,0.10)", text: "#22c55e" },
blue: { bg: "rgba(59,130,246,0.10)", text: "#3b82f6" },
orange: { bg: "rgba(249,115,22,0.10)", text: "#f97316" },
} as const;
const tone = toneMap[badgeTone];
return (
<div className="mt-4 border-t border-border/40 pt-3 first-of-type:border-t-0 first-of-type:pt-0">
<div className="flex items-center justify-between gap-2 mb-2">
<div className="flex items-center gap-2">
<span className="text-[10px] uppercase tracking-wider font-bold text-text-muted">
Layer {id}
</span>
<h3 className="text-[13px] font-semibold text-text-main">{title}</h3>
</div>
<span
className="text-[10px] font-semibold px-2 py-0.5 rounded-full tabular-nums"
style={{ background: tone.bg, color: tone.text }}
>
{badge}
</span>
</div>
<p className="text-[11px] text-text-muted mb-2">{description}</p>
{children}
</div>
);
}
function EmptyHint({ text }: { text: string }) {
return (
<div className="text-[11px] text-text-muted italic py-3 text-center bg-bg-subtle/40 rounded-md">
{text}
</div>
);
}
function QuotaGroup({
tone,
label,
items,
}: {
tone: "red" | "amber" | "orange";
label: string;
items: QuotaMonitor[];
}) {
const t = useTranslations("runtime");
const toneMap = {
red: { text: "#ef4444", bg: "rgba(239,68,68,0.08)", border: "rgba(239,68,68,0.20)" },
amber: { text: "#eab308", bg: "rgba(234,179,8,0.08)", border: "rgba(234,179,8,0.20)" },
orange: { text: "#f97316", bg: "rgba(249,115,22,0.08)", border: "rgba(249,115,22,0.20)" },
} as const;
const tc = toneMap[tone];
return (
<div>
<div
className="text-[10px] font-bold uppercase tracking-wider mb-1.5"
style={{ color: tc.text }}
>
{label}
</div>
<div className="flex flex-col gap-1">
{items.slice(0, 6).map((m, i) => (
<div
key={`${m.accountId ?? ""}:${m.window ?? ""}:${i}`}
className="rounded-md border px-2.5 py-1.5 flex items-center justify-between gap-2"
style={{ background: tc.bg, borderColor: tc.border }}
>
<div className="min-w-0">
<div className="text-[11px] font-medium text-text-main truncate">
{m.accountId ?? "β"}
{m.provider ? ` / ${resolveProviderName(m.provider, nodeMap)}` : ""}
</div>
<div className="text-[10px] text-text-muted">{m.window ?? ""}</div>
</div>
{typeof m.remainingPercent === "number" && (
<span className="text-[11px] font-bold tabular-nums" style={{ color: tc.text }}>
{Math.round(m.remainingPercent)}%
</span>
)}
</div>
))}
{items.length > 6 && (
<div className="text-[10px] text-text-muted text-center pt-1">
{t("moreSuffix", { count: items.length - 6 })}
</div>
)}
</div>
</div>
);
}
|