318 lines
10 KiB
HTML
318 lines
10 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>图层使用统计</title>
|
||
<style>
|
||
:root { color-scheme: light; }
|
||
* { box-sizing: border-box; }
|
||
html, body { height: 100%; overflow: hidden; }
|
||
body {
|
||
margin: 0;
|
||
font: 14px/1.45 "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
|
||
color: #1a1d21;
|
||
background: #eef1f4;
|
||
}
|
||
main {
|
||
height: 100%;
|
||
max-width: 1080px;
|
||
margin: 0 auto;
|
||
padding: 20px;
|
||
display: flex;
|
||
flex-direction: column;
|
||
min-height: 0;
|
||
}
|
||
h1 { margin: 0 0 16px; font-size: 20px; font-weight: 650; flex: none; }
|
||
.cards {
|
||
flex: 1;
|
||
min-height: 0;
|
||
display: grid;
|
||
grid-template-columns: 1fr 1fr;
|
||
gap: 16px;
|
||
}
|
||
section {
|
||
min-height: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
background: #fff;
|
||
border-radius: 12px;
|
||
padding: 18px 20px 12px;
|
||
box-shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 0 0 1px rgba(16, 24, 40, .04);
|
||
}
|
||
.list {
|
||
flex: 1;
|
||
min-height: 0;
|
||
overflow: auto;
|
||
overscroll-behavior: contain;
|
||
}
|
||
h2 {
|
||
margin: 0 0 12px;
|
||
font-size: 13px;
|
||
font-weight: 600;
|
||
color: #667085;
|
||
display: flex;
|
||
align-items: baseline;
|
||
gap: 8px;
|
||
flex: none;
|
||
}
|
||
.hero { display: flex; align-items: baseline; gap: 8px; margin: 0 0 12px; flex: none; }
|
||
.hero strong { font-size: 40px; line-height: 1; font-weight: 650; letter-spacing: -1px; }
|
||
.hero span { color: #667085; font-size: 13px; }
|
||
.empty { color: #98a2b3; margin: 10px 0 0; }
|
||
table { width: 100%; border-collapse: collapse; }
|
||
th { text-align: left; font-size: 12px; font-weight: 600; color: #98a2b3; padding: 0 0 8px; border-bottom: 1px solid #eef0f3; position: sticky; top: 0; background: #fff; }
|
||
td { padding: 10px 0; border-bottom: 1px solid #f4f5f7; vertical-align: middle; }
|
||
tr:last-child td { border-bottom: 0; }
|
||
td.name { font-weight: 600; }
|
||
td.num { width: 56px; font-variant-numeric: tabular-nums; }
|
||
.filters { display: flex; flex-wrap: wrap; align-items: end; gap: 8px 10px; margin-bottom: 10px; flex: none; }
|
||
label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: #667085; }
|
||
input[type="datetime-local"] {
|
||
font: inherit;
|
||
color: #1a1d21;
|
||
width: 188px;
|
||
padding: 6px 8px;
|
||
border: 1px solid #d0d5dd;
|
||
border-radius: 7px;
|
||
background: #fff;
|
||
}
|
||
input:focus { outline: 2px solid #1a1d21; outline-offset: 1px; }
|
||
.btns { display: flex; flex-wrap: wrap; gap: 6px; }
|
||
button {
|
||
font: inherit;
|
||
padding: 6px 10px;
|
||
border-radius: 7px;
|
||
border: 1px solid #d0d5dd;
|
||
background: #fff;
|
||
color: #1a1d21;
|
||
cursor: pointer;
|
||
}
|
||
button:hover { background: #f2f4f7; }
|
||
button.primary { background: #1a1d21; color: #fff; border-color: #1a1d21; }
|
||
button.primary:hover { background: #333; }
|
||
button:disabled { opacity: .45; cursor: default; }
|
||
.fail { color: #b42318; font-size: 12px; font-weight: 500; flex: none; min-height: 1em; margin: 0 0 6px; }
|
||
.range-head { flex: none; }
|
||
.meta { color: #98a2b3; font-size: 12px; margin: 0 0 6px; }
|
||
.sum { font-size: 15px; font-weight: 650; margin: 0 0 2px; }
|
||
@media (max-width: 860px) {
|
||
.cards { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<main>
|
||
<h1>图层使用统计</h1>
|
||
<div class="cards">
|
||
<section>
|
||
<h2>现在 <span id="fail" class="fail"></span></h2>
|
||
<p class="hero"><strong id="total">—</strong><span>总在线连接</span></p>
|
||
<div id="online-list" class="list"></div>
|
||
</section>
|
||
<section>
|
||
<h2>查历史</h2>
|
||
<div class="filters">
|
||
<label>从 <input id="start" type="datetime-local" step="1"></label>
|
||
<label>到 <input id="end" type="datetime-local" step="1"></label>
|
||
<div class="btns">
|
||
<button type="button" id="q1">1 小时</button>
|
||
<button type="button" id="q24">24 小时</button>
|
||
<button type="button" id="q72">3 天</button>
|
||
<button type="button" id="qgo" class="primary">查询</button>
|
||
</div>
|
||
</div>
|
||
<p id="range-err" class="fail"></p>
|
||
<div id="range-head" class="range-head"></div>
|
||
<div id="range-list" class="list"></div>
|
||
</section>
|
||
</div>
|
||
</main>
|
||
<script>
|
||
(function () {
|
||
var RETAIN = 72 * 60 * 60 * 1000;
|
||
var SLACK = 5 * 60 * 1000;
|
||
var startEl = document.getElementById("start");
|
||
var endEl = document.getElementById("end");
|
||
var errEl = document.getElementById("range-err");
|
||
var buttons = [document.getElementById("q1"), document.getElementById("q24"), document.getElementById("q72"), document.getElementById("qgo")];
|
||
var timer = 0;
|
||
var offset = 0;
|
||
var defaulted = false;
|
||
|
||
function serverNow() { return Date.now() + offset; }
|
||
function pad(n) { return String(n).padStart(2, "0"); }
|
||
function bj(ms) { return new Date(ms + 8 * 3600 * 1000); }
|
||
|
||
function toLocal(ms) {
|
||
var d = bj(ms);
|
||
return d.getUTCFullYear() + "-" + pad(d.getUTCMonth() + 1) + "-" + pad(d.getUTCDate())
|
||
+ "T" + pad(d.getUTCHours()) + ":" + pad(d.getUTCMinutes()) + ":" + pad(d.getUTCSeconds());
|
||
}
|
||
|
||
function toShow(ms) {
|
||
var d = bj(ms);
|
||
return d.getUTCFullYear() + "/" + pad(d.getUTCMonth() + 1) + "/" + pad(d.getUTCDate())
|
||
+ " " + pad(d.getUTCHours()) + ":" + pad(d.getUTCMinutes());
|
||
}
|
||
|
||
function parseLocal(s) {
|
||
var m = String(s).match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2}))?$/);
|
||
if (!m) return null;
|
||
return Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +(m[6] || 0)) - 8 * 3600 * 1000;
|
||
}
|
||
|
||
function toRFC(ms) { return toLocal(ms) + "+08:00"; }
|
||
|
||
function el(tag, text) {
|
||
var node = document.createElement(tag);
|
||
if (text != null) node.textContent = text;
|
||
return node;
|
||
}
|
||
|
||
function fillTable(parent, rows, online) {
|
||
parent.textContent = "";
|
||
if (!rows || rows.length === 0) {
|
||
parent.appendChild(el("p", online ? "现在没有人在用。" : "这段时间没有记录。"));
|
||
parent.lastChild.className = "empty";
|
||
return;
|
||
}
|
||
var table = el("table");
|
||
var hr = el("tr");
|
||
hr.appendChild(el("th", "图层"));
|
||
hr.appendChild(el("th", "连接"));
|
||
var thead = el("thead");
|
||
thead.appendChild(hr);
|
||
table.appendChild(thead);
|
||
var tb = el("tbody");
|
||
rows.forEach(function (row) {
|
||
var tr = el("tr");
|
||
var name = el("td", row.table);
|
||
name.className = "name";
|
||
var num = el("td", String(row.count));
|
||
num.className = "num";
|
||
tr.appendChild(name);
|
||
tr.appendChild(num);
|
||
tb.appendChild(tr);
|
||
});
|
||
table.appendChild(tb);
|
||
parent.appendChild(table);
|
||
}
|
||
|
||
function fillDefault() {
|
||
var end = serverNow();
|
||
startEl.value = toLocal(end - 60 * 60 * 1000);
|
||
endEl.value = toLocal(end);
|
||
var earliest = toLocal(end - RETAIN);
|
||
var latest = toLocal(end);
|
||
startEl.min = earliest;
|
||
startEl.max = latest;
|
||
endEl.min = earliest;
|
||
endEl.max = latest;
|
||
}
|
||
|
||
function validateRange() {
|
||
var s = parseLocal(startEl.value);
|
||
var e = parseLocal(endEl.value);
|
||
if (s == null || e == null) return "请选择开始和结束时间";
|
||
if (s >= e) return "开始时间要早于结束时间";
|
||
if (e - s > RETAIN) return "一次最多查 3 天";
|
||
var earliest = serverNow() - RETAIN;
|
||
if (s < earliest && earliest - s > SLACK) return "只能查最近 3 天";
|
||
return "";
|
||
}
|
||
|
||
function setBusy(busy) {
|
||
buttons.forEach(function (btn) { btn.disabled = busy; });
|
||
}
|
||
|
||
function renderRange(data) {
|
||
var head = document.getElementById("range-head");
|
||
head.textContent = "";
|
||
head.appendChild(el("p", "总连接 " + data.total));
|
||
head.lastChild.className = "sum";
|
||
head.appendChild(el("p", toShow(Date.parse(data.start)) + " – " + toShow(Date.parse(data.end))));
|
||
head.lastChild.className = "meta";
|
||
fillTable(document.getElementById("range-list"), data.tables, false);
|
||
}
|
||
|
||
function queryRange() {
|
||
var msg = validateRange();
|
||
if (msg) {
|
||
errEl.textContent = msg;
|
||
return;
|
||
}
|
||
errEl.textContent = "";
|
||
setBusy(true);
|
||
var url = "/api/range?start=" + encodeURIComponent(toRFC(parseLocal(startEl.value)))
|
||
+ "&end=" + encodeURIComponent(toRFC(parseLocal(endEl.value)));
|
||
fetch(url, { cache: "no-store" }).then(function (res) {
|
||
return res.json().then(function (data) {
|
||
if (!res.ok) {
|
||
errEl.textContent = data && data.message ? data.message : "查询失败";
|
||
return;
|
||
}
|
||
renderRange(data);
|
||
});
|
||
}).catch(function () {
|
||
errEl.textContent = "查询失败";
|
||
}).then(function () {
|
||
setBusy(false);
|
||
});
|
||
}
|
||
|
||
function quick(ms) {
|
||
var end = serverNow();
|
||
startEl.value = toLocal(end - ms);
|
||
endEl.value = toLocal(end);
|
||
queryRange();
|
||
}
|
||
|
||
function refreshOnline() {
|
||
if (document.hidden) return Promise.resolve();
|
||
return fetch("/api/online", { cache: "no-store" }).then(function (res) {
|
||
if (!res.ok) throw new Error("bad status");
|
||
return res.json();
|
||
}).then(function (data) {
|
||
offset = Date.parse(data.server_time) - Date.now();
|
||
if (!defaulted) {
|
||
fillDefault();
|
||
defaulted = true;
|
||
queryRange();
|
||
}
|
||
document.getElementById("total").textContent = String(data.total);
|
||
document.getElementById("fail").textContent = "";
|
||
fillTable(document.getElementById("online-list"), data.tables, true);
|
||
}).catch(function () {
|
||
document.getElementById("fail").textContent = "暂时连不上";
|
||
});
|
||
}
|
||
|
||
function schedule() {
|
||
clearTimeout(timer);
|
||
if (document.hidden) return;
|
||
timer = setTimeout(function () {
|
||
refreshOnline().then(schedule);
|
||
}, 30000);
|
||
}
|
||
|
||
function tick() {
|
||
return refreshOnline().then(schedule);
|
||
}
|
||
|
||
document.getElementById("q1").addEventListener("click", function () { quick(60 * 60 * 1000); });
|
||
document.getElementById("q24").addEventListener("click", function () { quick(24 * 60 * 60 * 1000); });
|
||
document.getElementById("q72").addEventListener("click", function () { quick(72 * 60 * 60 * 1000); });
|
||
document.getElementById("qgo").addEventListener("click", queryRange);
|
||
document.addEventListener("visibilitychange", function () {
|
||
clearTimeout(timer);
|
||
if (!document.hidden) tick();
|
||
});
|
||
|
||
fillDefault();
|
||
tick();
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html>
|