Add Prometheus-backed home server status
This commit is contained in:
+113
@@ -1939,3 +1939,116 @@ def hardcore_leaderboard_points_combined():
|
|||||||
return jsonify(rows[:100])
|
return jsonify(rows[:100])
|
||||||
|
|
||||||
# --- end Hardcore stats aggregator -------------------------------------------
|
# --- end Hardcore stats aggregator -------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/server-status")
|
||||||
|
def public_server_status():
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import urllib.parse
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
prometheus_url = os.environ.get("PROMETHEUS_URL", "http://5.0.0.20:9090").rstrip("/")
|
||||||
|
|
||||||
|
def prom_value(query):
|
||||||
|
url = prometheus_url + "/api/v1/query?" + urllib.parse.urlencode({"query": query})
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(url, timeout=2.5) as resp:
|
||||||
|
data = json.loads(resp.read().decode("utf-8"))
|
||||||
|
except Exception:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if data.get("status") != "success":
|
||||||
|
return 0
|
||||||
|
|
||||||
|
total = 0.0
|
||||||
|
for result in data.get("data", {}).get("result", []):
|
||||||
|
value = result.get("value", [None, "0"])[1]
|
||||||
|
try:
|
||||||
|
total += float(value)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
return int(total)
|
||||||
|
|
||||||
|
def q(metric, labels):
|
||||||
|
label_text = ",".join(f'{k}="{v}"' for k, v in labels.items())
|
||||||
|
return f'sum({metric}{{{label_text}}}) or vector(0)'
|
||||||
|
|
||||||
|
def newserv(region, service, ship, version):
|
||||||
|
return prom_value(q("pso_newserv_clients_connected", {
|
||||||
|
"region": region,
|
||||||
|
"service": service,
|
||||||
|
"ship": ship,
|
||||||
|
"version": version,
|
||||||
|
}))
|
||||||
|
|
||||||
|
def adhoc(region, game):
|
||||||
|
return prom_value(q("psppeeps_adhoc_connected_clients_by_product", {
|
||||||
|
"region": region,
|
||||||
|
"service": f"{region}-psppeeps-adhoc",
|
||||||
|
"ship": "psp",
|
||||||
|
"game": game,
|
||||||
|
}))
|
||||||
|
|
||||||
|
us = {
|
||||||
|
"alis_v2": newserv("us", "us-newserv-live", "live", "v2"),
|
||||||
|
"alis_v3": newserv("us", "us-newserv-live", "live", "v3"),
|
||||||
|
"alis_bb": newserv("us", "us-newserv-live", "live", "v4"),
|
||||||
|
"abion_hcbb": newserv("us", "us-newserv-hardcore", "hardcore", "v4"),
|
||||||
|
"adhoc_psp1": adhoc("us", "psp1"),
|
||||||
|
"adhoc_psp2i": adhoc("us", "psp2i"),
|
||||||
|
}
|
||||||
|
|
||||||
|
eu = {
|
||||||
|
"palma_v2": newserv("eu", "eu-newserv-live", "live", "v2"),
|
||||||
|
"palma_v3": newserv("eu", "eu-newserv-live", "live", "v3"),
|
||||||
|
"palma_bb": newserv("eu", "eu-newserv-live", "live", "v4"),
|
||||||
|
"aiedo_hcbb": newserv("eu", "eu-newserv-hardcore", "hardcore", "v4"),
|
||||||
|
"adhoc_psp1": adhoc("eu", "psp1"),
|
||||||
|
"adhoc_psp2i": adhoc("eu", "psp2i"),
|
||||||
|
}
|
||||||
|
|
||||||
|
us_total = sum(us.values())
|
||||||
|
eu_total = sum(eu.values())
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
"servers": [
|
||||||
|
{
|
||||||
|
"label": "US Server",
|
||||||
|
"players": us_total,
|
||||||
|
"ships": [
|
||||||
|
{"label": "Alis", "rows": [
|
||||||
|
{"label": "V2", "players": us["alis_v2"]},
|
||||||
|
{"label": "V3", "players": us["alis_v3"]},
|
||||||
|
{"label": "BB", "players": us["alis_bb"]},
|
||||||
|
]},
|
||||||
|
{"label": "Abion", "rows": [
|
||||||
|
{"label": "HC/BB", "players": us["abion_hcbb"]},
|
||||||
|
]},
|
||||||
|
{"label": "AdHoc-US", "rows": [
|
||||||
|
{"label": "PSP1", "players": us["adhoc_psp1"]},
|
||||||
|
{"label": "PSP2i", "players": us["adhoc_psp2i"]},
|
||||||
|
]},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "EU Server",
|
||||||
|
"players": eu_total,
|
||||||
|
"ships": [
|
||||||
|
{"label": "Palma", "rows": [
|
||||||
|
{"label": "V2", "players": eu["palma_v2"]},
|
||||||
|
{"label": "V3", "players": eu["palma_v3"]},
|
||||||
|
{"label": "BB", "players": eu["palma_bb"]},
|
||||||
|
]},
|
||||||
|
{"label": "Aiedo", "rows": [
|
||||||
|
{"label": "HC/BB", "players": eu["aiedo_hcbb"]},
|
||||||
|
]},
|
||||||
|
{"label": "AdHoc-EU", "rows": [
|
||||||
|
{"label": "PSP1", "players": eu["adhoc_psp1"]},
|
||||||
|
{"label": "PSP2i", "players": eu["adhoc_psp2i"]},
|
||||||
|
]},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|||||||
+23
-2
@@ -10,6 +10,7 @@
|
|||||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
|
||||||
<link rel="stylesheet" href="style.css?v=home-hardcore-preline-20260610-1">
|
<link rel="stylesheet" href="style.css?v=home-hardcore-preline-20260610-1">
|
||||||
<script src="app.js?v=account-status-label-20260609" defer></script>
|
<script src="app.js?v=account-status-label-20260609" defer></script>
|
||||||
|
<script src="server-status.js?v=prometheus-status-1" defer></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="site-shell">
|
<div class="site-shell">
|
||||||
@@ -53,9 +54,29 @@
|
|||||||
<h1 id="server-status-heading" class="section-title">Server Status</h1>
|
<h1 id="server-status-heading" class="section-title">Server Status</h1>
|
||||||
|
|
||||||
<div class="status-list" role="list" aria-label="Current server player counts">
|
<div class="status-list" role="list" aria-label="Current server player counts">
|
||||||
<div class="status-row status-parent" role="listitem"><span>US Server</span><span></span></div>
|
<div class="status-row status-parent status-server" role="listitem"><span>US Server</span><span>0 Players</span></div>
|
||||||
|
<div class="status-row status-parent status-ship" role="listitem"><span>Alis</span><span></span></div>
|
||||||
|
<div class="status-row status-child" role="listitem"><span>V2</span><span>0 Players</span></div>
|
||||||
|
<div class="status-row status-child" role="listitem"><span>V3</span><span>0 Players</span></div>
|
||||||
|
<div class="status-row status-child" role="listitem"><span>BB</span><span>0 Players</span></div>
|
||||||
|
<div class="status-row status-parent status-ship" role="listitem"><span>Abion</span><span></span></div>
|
||||||
|
<div class="status-row status-child" role="listitem"><span>HC/BB</span><span>0 Players</span></div>
|
||||||
|
<div class="status-row status-parent status-ship" role="listitem"><span>AdHoc-US</span><span></span></div>
|
||||||
|
<div class="status-row status-child" role="listitem"><span>PSP1</span><span>0 Players</span></div>
|
||||||
|
<div class="status-row status-child" role="listitem"><span>PSP2i</span><span>0 Players</span></div>
|
||||||
|
|
||||||
<div class="status-row status-parent" role="listitem"><span>Alis</span><span>0 Players</span></div>
|
<div class="status-row status-parent status-server" role="listitem"><span>EU Server</span><span>0 Players</span></div>
|
||||||
|
<div class="status-row status-parent status-ship" role="listitem"><span>Palma</span><span></span></div>
|
||||||
|
<div class="status-row status-child" role="listitem"><span>V2</span><span>0 Players</span></div>
|
||||||
|
<div class="status-row status-child" role="listitem"><span>V3</span><span>0 Players</span></div>
|
||||||
|
<div class="status-row status-child" role="listitem"><span>BB</span><span>0 Players</span></div>
|
||||||
|
<div class="status-row status-parent status-ship" role="listitem"><span>Aiedo</span><span></span></div>
|
||||||
|
<div class="status-row status-child" role="listitem"><span>HC/BB</span><span>0 Players</span></div>
|
||||||
|
<div class="status-row status-parent status-ship" role="listitem"><span>AdHoc-EU</span><span></span></div>
|
||||||
|
<div class="status-row status-child" role="listitem"><span>PSP1</span><span>0 Players</span></div>
|
||||||
|
<div class="status-row status-child" role="listitem"><span>PSP2i</span><span>0 Players</span></div>
|
||||||
|
</div>
|
||||||
|
<div class="status-row status-parent" role="listitem"><span>Alis</span><span>0 Players</span></div>
|
||||||
<div class="status-row status-child" role="listitem"><span>V2</span><span>0 Players</span></div>
|
<div class="status-row status-child" role="listitem"><span>V2</span><span>0 Players</span></div>
|
||||||
<div class="status-row status-child" role="listitem"><span>V3</span><span>0 Players</span></div>
|
<div class="status-row status-child" role="listitem"><span>V3</span><span>0 Players</span></div>
|
||||||
<div class="status-row status-child" role="listitem"><span>BB</span><span>0 Players</span></div>
|
<div class="status-row status-child" role="listitem"><span>BB</span><span>0 Players</span></div>
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
(() => {
|
||||||
|
const list = document.querySelector(".status-list");
|
||||||
|
if (!list) return;
|
||||||
|
|
||||||
|
const esc = (value) => String(value ?? "").replace(/[&<>"']/g, (ch) => ({
|
||||||
|
"&": "&",
|
||||||
|
"<": "<",
|
||||||
|
">": ">",
|
||||||
|
"\"": """,
|
||||||
|
"'": "'",
|
||||||
|
}[ch]));
|
||||||
|
|
||||||
|
const players = (value) => {
|
||||||
|
const n = Number(value || 0);
|
||||||
|
return `${n.toLocaleString()} ${n === 1 ? "Player" : "Players"}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const render = (data) => {
|
||||||
|
const servers = Array.isArray(data?.servers) ? data.servers : [];
|
||||||
|
if (!servers.length) return;
|
||||||
|
|
||||||
|
list.innerHTML = servers.map((server) => `
|
||||||
|
<div class="status-row status-parent status-server" role="listitem">
|
||||||
|
<span>${esc(server.label)}</span><span>${players(server.players)}</span>
|
||||||
|
</div>
|
||||||
|
${(server.ships || []).map((ship) => `
|
||||||
|
<div class="status-row status-parent status-ship" role="listitem">
|
||||||
|
<span>${esc(ship.label)}</span><span></span>
|
||||||
|
</div>
|
||||||
|
${(ship.rows || []).map((row) => `
|
||||||
|
<div class="status-row status-child" role="listitem">
|
||||||
|
<span>${esc(row.label)}</span><span>${players(row.players)}</span>
|
||||||
|
</div>
|
||||||
|
`).join("")}
|
||||||
|
`).join("")}
|
||||||
|
`).join("");
|
||||||
|
};
|
||||||
|
|
||||||
|
fetch("/api/server-status", { cache: "no-store" })
|
||||||
|
.then((res) => res.ok ? res.json() : null)
|
||||||
|
.then((data) => data && render(data))
|
||||||
|
.catch(() => {});
|
||||||
|
})();
|
||||||
Reference in New Issue
Block a user