|
| 1 | +/** Self-contained HTML dashboard for /admin/state (no build step, no deps). */ |
| 2 | +export const ADMIN_DASHBOARD_HTML = `<!DOCTYPE html> |
| 3 | +<html lang="vi"> |
| 4 | +<head> |
| 5 | +<meta charset="utf-8"> |
| 6 | +<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 7 | +<title>zcode-proxy — pool dashboard</title> |
| 8 | +<style> |
| 9 | + :root { color-scheme: dark; } |
| 10 | + body { font-family: ui-monospace, Menlo, Consolas, monospace; background: #0d1117; color: #e6edf3; margin: 20px; } |
| 11 | + h1 { font-size: 18px; } h2 { font-size: 14px; color: #8b949e; margin-top: 24px; } |
| 12 | + .meta { color: #8b949e; font-size: 12px; } |
| 13 | + table { border-collapse: collapse; width: 100%; margin-top: 8px; font-size: 13px; } |
| 14 | + th, td { border: 1px solid #30363d; padding: 6px 10px; text-align: right; } |
| 15 | + th:first-child, td:first-child { text-align: left; } |
| 16 | + th { background: #161b22; color: #8b949e; } |
| 17 | + .ok { color: #3fb950; } .warn { color: #d29922; } .bad { color: #f85149; } |
| 18 | + .refresh { font-size: 12px; color: #8b949e; margin-top: 4px; } |
| 19 | + select { background: #161b22; color: #e6edf3; border: 1px solid #30363d; padding: 4px; font-family: inherit; } |
| 20 | + #transitions { font-size: 12px; max-height: 260px; overflow-y: auto; } |
| 21 | + #transitions div { padding: 2px 0; border-bottom: 1px solid #21262d; } |
| 22 | +</style> |
| 23 | +</head> |
| 24 | +<body> |
| 25 | +<h1>zcode-proxy — pool dashboard</h1> |
| 26 | +<div class="meta">Đếm trong RAM — reset khi restart proxy. Tokens = usage từ response (nếu có).</div> |
| 27 | +<div class="refresh">Auto-refresh: |
| 28 | + <select id="interval" onchange="setInterval_()"> |
| 29 | + <option value="5">5s</option> |
| 30 | + <option value="10" selected>10s</option> |
| 31 | + <option value="30">30s</option> |
| 32 | + <option value="0">off</option> |
| 33 | + </select> |
| 34 | + <span id="updated"></span> |
| 35 | +</div> |
| 36 | +
|
| 37 | +<h2>Tài khoản — tổng theo acc</h2> |
| 38 | +<table id="accounts"></table> |
| 39 | +
|
| 40 | +<h2>Phân phối theo model (per acc)</h2> |
| 41 | +<table id="models"></table> |
| 42 | +
|
| 43 | +<h2>Tốc độ trừ quota (poll history — gần nhất first)</h2> |
| 44 | +<table id="polls"></table> |
| 45 | +
|
| 46 | +<h2>Chuyển trạng thái (50 dòng gần nhất)</h2> |
| 47 | +<div id="transitions"></div> |
| 48 | +
|
| 49 | +<script> |
| 50 | +let timer = null; |
| 51 | +function setInterval_() { |
| 52 | + const v = Number(document.getElementById("interval").value); |
| 53 | + if (timer) clearInterval(timer); |
| 54 | + if (v > 0) { timer = setInterval(load, v * 1000); load(); } |
| 55 | +} |
| 56 | +async function load() { |
| 57 | + try { |
| 58 | + const resp = await fetch("/admin/state" + (urlKey ? "?key=" + encodeURIComponent(urlKey) : "")); |
| 59 | + if (resp.status === 401) { document.body.innerHTML = "<h1>401 — cần proxy API key</h1><div>Cấu hình auth trong browser không hỗ trợ header tùy chỉnh — dùng curl hoặc thêm ?key=</div>"; return; } |
| 60 | + const d = await resp.json(); |
| 61 | + render(d); |
| 62 | + document.getElementById("updated").textContent = " · cập nhật " + new Date().toLocaleTimeString(); |
| 63 | + } catch (e) { |
| 64 | + document.getElementById("updated").textContent = " · lỗi: " + e.message; |
| 65 | + } |
| 66 | +} |
| 67 | +function fmt(n) { return n === undefined ? "—" : Number(n).toLocaleString("vi-VN"); } |
| 68 | +function esc(s) { const d = document.createElement("div"); d.textContent = String(s); return d.innerHTML; } |
| 69 | +
|
| 70 | +function render(d) { |
| 71 | + // Accounts table |
| 72 | + let h = "<tr><th>acc</th><th>status</th><th>requests</th><th>success</th><th>1005</th><th>429</th><th>401</th><th>tokens</th></tr>"; |
| 73 | + for (const a of d.accounts) { |
| 74 | + const t = a.totals; |
| 75 | + const cls = t.quota1005 > 0 ? "warn" : "ok"; |
| 76 | + h += "<tr><td>" + esc(a.label || a.id.slice(0, 8)) + "</td><td class='" + cls + "'>" + esc(a.status) + "</td><td>" + fmt(t.requests) + "</td><td>" + fmt(t.success) + "</td><td>" + fmt(t.quota1005) + "</td><td>" + fmt(t.rate429) + "</td><td>" + fmt(t.auth401) + "</td><td>" + fmt(t.tokensUsed) + "</td></tr>"; |
| 77 | + } |
| 78 | + h += "<tr><td><b>busyGlobal</b></td><td colspan='7'>" + fmt(d.busyGlobal) + " (429 từ chính proxy — queue/cap)</td></tr>"; |
| 79 | + document.getElementById("accounts").innerHTML = h; |
| 80 | +
|
| 81 | + // Per-model distribution + share % |
| 82 | + h = "<tr><th>acc</th><th>model</th><th>requests</th><th>% phân phối</th><th>success</th><th>1005</th><th>429</th><th>Retry-After cuối</th><th>tokens</th></tr>"; |
| 83 | + const totalsByModel = {}; |
| 84 | + for (const a of d.accounts) for (const [k, c] of Object.entries(a.models)) totalsByModel[k] = (totalsByModel[k] ?? 0) + c.requests; |
| 85 | + for (const a of d.accounts) { |
| 86 | + for (const [k, c] of Object.entries(a.models)) { |
| 87 | + const share = totalsByModel[k] > 0 ? Math.round(c.requests / totalsByModel[k] * 100) : 0; |
| 88 | + const shareCls = share >= 40 && share <= 60 ? "ok" : share >= 25 ? "warn" : "bad"; |
| 89 | + h += "<tr><td>" + esc(a.label || a.id.slice(0, 8)) + "</td><td>" + esc(k) + "</td><td>" + fmt(c.requests) + "</td><td class='" + shareCls + "'>" + share + "%</td><td>" + fmt(c.success) + "</td><td>" + fmt(c.quota1005) + "</td><td>" + fmt(c.rate429) + "</td><td>" + fmt(c.lastRetryAfterSec) + "s</td><td>" + fmt(c.tokensUsed) + "</td></tr>"; |
| 90 | + } |
| 91 | + } |
| 92 | + document.getElementById("models").innerHTML = h || "<tr><td>chưa có data</td></tr>"; |
| 93 | +
|
| 94 | + // Poll history: drain rate per bucket (latest vs oldest in window) |
| 95 | + h = "<tr><th>acc</th><th>bucket</th><th>remaining (mới nhất)</th><th>used (mới nhất)</th><th>điểm</th><th>tốc độ trừ</th></tr>"; |
| 96 | + for (const [acc, samples] of Object.entries(d.pollHistory)) { |
| 97 | + const byModel = {}; |
| 98 | + for (const s of samples) (byModel[s.model] ??= []).push(s); |
| 99 | + for (const [model, list] of Object.entries(byModel)) { |
| 100 | + if (list.length < 2) { h += "<tr><td>" + esc(acc.slice(0, 8)) + "</td><td>" + esc(model) + "</td><td>" + fmt(list[0].remainingUnits) + "</td><td>" + fmt(list[0].usedUnits) + "</td><td>" + list.length + "</td><td>— (cần ≥2 điểm)</td></tr>"; continue; } |
| 101 | + const newest = list[list.length - 1], oldest = list[0]; |
| 102 | + const dtH = (newest.at - oldest.at) / 3600000; |
| 103 | + const dUsed = (newest.usedUnits ?? 0) - (oldest.usedUnits ?? 0); |
| 104 | + const rate = dtH > 0 ? Math.round(dUsed / dtH).toLocaleString("vi-VN") + "/h" : "—"; |
| 105 | + h += "<tr><td>" + esc(acc.slice(0, 8)) + "</td><td>" + esc(model) + "</td><td>" + fmt(newest.remainingUnits) + "</td><td>" + fmt(newest.usedUnits) + "</td><td>" + list.length + "</td><td>" + rate + "</td></tr>"; |
| 106 | + } |
| 107 | + } |
| 108 | + document.getElementById("polls").innerHTML = h || "<tr><td>chưa có data</td></tr>"; |
| 109 | +
|
| 110 | + // Transitions |
| 111 | + let t = ""; |
| 112 | + for (const tr of [...d.transitions].reverse()) { |
| 113 | + const cls = tr.to === "healthy" ? "ok" : tr.to === "exhausted" || tr.to === "expired" ? "bad" : "warn"; |
| 114 | + t += "<div><span class='meta'>" + new Date(tr.at).toLocaleTimeString() + "</span> " + esc(tr.accountId.slice(0, 8)) + " <span class='" + cls + "'>" + esc(tr.from) + " → " + esc(tr.to) + "</span> <span class='meta'>" + esc(tr.reason) + "</span></div>"; |
| 115 | + } |
| 116 | + document.getElementById("transitions").innerHTML = t || "<div>chưa có</div>"; |
| 117 | +} |
| 118 | +
|
| 119 | +// Optional key via query param (?key=...) for browser access without header support: |
| 120 | +const urlKey = new URLSearchParams(location.search).get("key"); |
| 121 | +setInterval_(); |
| 122 | +load(); |
| 123 | +</script> |
| 124 | +</body> |
| 125 | +</html>`; |
0 commit comments