Add a power history to each game server
Each change a read of a Fragnet account sees, and each power signal sent from here with who sent it, is written down (180 days). A server's details show the share of the last 30 days it was on, starts and stops, a seven-day bar and the latest changes, with who asked for them. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9126200295
commit
a2b43bcd5d
14 files changed
+9773
-4
No files matched your search
@@ -1524,6 +1524,12 @@ export interface FragnetServerInfo {
|
||||
firstSeenAt: string;
|
||||
/** The newest backup that worked, and whether a newer one failed; null when the backups could not be listed. */
|
||||
newestBackup: { at: string | null; failedSince: boolean } | null;
|
||||
/** How the server has been on and off, as this app saw it; null before it has been read. */
|
||||
power: {
|
||||
stats: { trackedSince: string; onPercent: number | null; spanMs: number; starts: number; stops: number; lastOnAt: string | null; lastOffAt: string | null; on: boolean } | null;
|
||||
timeline: { from: string; to: string; segments: { from: string; to: string; on: boolean }[] } | null;
|
||||
recent: { at: string; kind: "on" | "off" | "signal"; detail: string | null; by: string | null }[];
|
||||
} | null;
|
||||
}
|
||||
|
||||
/** One Fragnet game panel account (an integration of type fragnet), as its page shows it. */
|
||||
|
||||
@@ -10,6 +10,8 @@ import { formatBytes } from "./settings/StorageSettings";
|
||||
import { confirmDialog } from "../utils/dialogs";
|
||||
import { readableError } from "../utils/errors";
|
||||
import { toast } from "../utils/toast";
|
||||
import { barPosition, eventText, onHeadline } from "../utils/gamePower";
|
||||
import { formatDateTime } from "../utils/date";
|
||||
|
||||
type Server = FragnetServerInfo;
|
||||
|
||||
@@ -81,6 +83,70 @@ const SIGNAL_TEXT: Record<FragnetSignal, { verb: string; done: string; confirm:
|
||||
kill: { verb: "Kill", done: "kill", confirm: "Kill {name}? It is stopped at once, without saving — unsaved progress is lost. Use it only when Stop does not work.", danger: true },
|
||||
};
|
||||
|
||||
/** How the server has been on and off over the last days, as this app saw it: the share of the time it was on, a bar for the last week, and the latest changes and who asked for them. */
|
||||
function PowerHistory({ power }: { power: Server["power"] }) {
|
||||
const stats = power?.stats ?? null;
|
||||
const timeline = power?.timeline ?? null;
|
||||
const headline = stats ? onHeadline(stats) : null;
|
||||
return (
|
||||
<div className="col-12">
|
||||
<h5>Power history</h5>
|
||||
{!stats && <div className="text-secondary small">Nothing yet — this app keeps it from the first time it reads a server, so it fills in as the days go by.</div>}
|
||||
{stats && (
|
||||
<div className="row g-3">
|
||||
<div className="col-lg-8">
|
||||
{headline ? <div className="fw-bold">{headline}</div> : <div className="text-secondary small">Too little is known to say how much of the time it is on (it was first read less than an hour ago).</div>}
|
||||
<div className="small text-secondary mb-2">
|
||||
Started {stats.starts} time{stats.starts === 1 ? "" : "s"}, stopped {stats.stops} in that time
|
||||
{stats.lastOnAt && (
|
||||
<>
|
||||
{" "}
|
||||
· last turned on <TimeAgo value={stats.lastOnAt} />
|
||||
</>
|
||||
)}
|
||||
{stats.lastOffAt && (
|
||||
<>
|
||||
{" "}
|
||||
· last turned off <TimeAgo value={stats.lastOffAt} />
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
{timeline && timeline.segments.length > 0 && (
|
||||
<>
|
||||
<div className="position-relative rounded overflow-hidden bg-secondary-lt" style={{ height: 16 }} role="img" aria-label="The last seven days: green where the server was on, grey where it was off">
|
||||
{timeline.segments.map((seg, i) => {
|
||||
const [left, width] = barPosition(timeline, seg);
|
||||
return <div key={i} className={seg.on ? "bg-green" : "bg-secondary"} style={{ position: "absolute", top: 0, bottom: 0, left: `${left}%`, width: `${width}%`, opacity: seg.on ? 1 : 0.35 }} title={`${seg.on ? "On" : "Off"} from ${formatDateTime(new Date(seg.from))} to ${formatDateTime(new Date(seg.to))}`} />;
|
||||
})}
|
||||
</div>
|
||||
<div className="d-flex justify-content-between small text-secondary mt-1">
|
||||
<span>{formatDateTime(new Date(timeline.from))}</span>
|
||||
<span>now</span>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
<div className="small text-secondary mt-1">Seen by reading the account every five minutes, so each time is to the nearest five. Known since {formatDateTime(new Date(stats.trackedSince))}.</div>
|
||||
</div>
|
||||
<div className="col-lg-4">
|
||||
<div className="small text-secondary mb-1">Latest</div>
|
||||
{power && power.recent.length === 0 && <div className="small text-secondary">Nothing has changed since it was first read.</div>}
|
||||
<ul className="list-unstyled small mb-0">
|
||||
{power?.recent.slice(0, 8).map((e, i) => (
|
||||
<li key={i}>
|
||||
<span className={e.kind === "on" ? "text-green" : e.kind === "off" ? "text-secondary" : "text-blue"} aria-hidden="true">
|
||||
{e.kind === "on" ? "●" : e.kind === "off" ? "○" : "▸"}
|
||||
</span>{" "}
|
||||
{eventText(e)} <span className="text-secondary">· <TimeAgo value={e.at} /></span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** Whether a backup of this server is being made now (one the panel has begun and not finished, from the last two hours). */
|
||||
function backupInProgress(s: Server): boolean {
|
||||
return (s.backups ?? []).some((b) => b.completedAt === null && Date.now() - Date.parse(b.createdAt ?? "") < 2 * 60 * 60_000);
|
||||
@@ -186,6 +252,7 @@ function Details({ s, panelUrl, canAct, onSignal, onBackup, busy }: { s: Server;
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
<PowerHistory power={s.power} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/** How a game server's power history is told on the page: a headline, the words for an event, and where each stretch of the timeline sits in its bar. */
|
||||
|
||||
const HOUR = 3_600_000;
|
||||
const DAY = 24 * HOUR;
|
||||
|
||||
export interface PowerStatsInfo {
|
||||
trackedSince: string;
|
||||
onPercent: number | null;
|
||||
spanMs: number;
|
||||
starts: number;
|
||||
stops: number;
|
||||
lastOnAt: string | null;
|
||||
lastOffAt: string | null;
|
||||
on: boolean;
|
||||
}
|
||||
|
||||
export interface PowerEventInfo {
|
||||
at: string;
|
||||
kind: "on" | "off" | "signal";
|
||||
detail: string | null;
|
||||
by: string | null;
|
||||
}
|
||||
|
||||
const plural = (n: number, one: string) => `${n} ${one}${n === 1 ? "" : "s"}`;
|
||||
|
||||
/** "6 days", "5 hours", "40 minutes". */
|
||||
export function spanText(ms: number): string {
|
||||
if (ms >= 2 * DAY) return plural(Math.round(ms / DAY), "day");
|
||||
if (ms >= 2 * HOUR) return plural(Math.round(ms / HOUR), "hour");
|
||||
return plural(Math.max(1, Math.round(ms / 60_000)), "minute");
|
||||
}
|
||||
|
||||
/** "On 94.3% of the last 30 days", or of "the 6 days it has been watched" when that is all that is known; null while under an hour is known. */
|
||||
export function onHeadline(s: Pick<PowerStatsInfo, "onPercent" | "spanMs">): string | null {
|
||||
if (s.onPercent === null) return null;
|
||||
const pct = Number.isInteger(s.onPercent) ? String(s.onPercent) : s.onPercent.toFixed(1);
|
||||
return s.spanMs >= 29.5 * DAY ? `On ${pct}% of the last 30 days` : `On ${pct}% of the ${spanText(s.spanMs)} it has been watched`;
|
||||
}
|
||||
|
||||
/** The words for one event: what happened, and who asked for it when it was done from here. */
|
||||
export function eventText(e: Pick<PowerEventInfo, "kind" | "detail" | "by">): string {
|
||||
if (e.kind === "signal") return `${e.by ?? "Someone"} sent ${e.detail ?? "a power signal"}`;
|
||||
const what = e.kind === "on" ? "Turned on" : "Turned off";
|
||||
return e.by ? `${what} — ${e.detail ?? "asked"} by ${e.by}` : what;
|
||||
}
|
||||
|
||||
/** Where a stretch sits in a bar covering the timeline, in percent: [left, width]. The part before anything was known is empty. */
|
||||
export function barPosition(timeline: { from: string; to: string }, seg: { from: string; to: string }): [number, number] {
|
||||
const start = Date.parse(timeline.from);
|
||||
const total = Date.parse(timeline.to) - start;
|
||||
if (!(total > 0)) return [0, 0];
|
||||
const left = ((Date.parse(seg.from) - start) / total) * 100;
|
||||
const width = ((Date.parse(seg.to) - Date.parse(seg.from)) / total) * 100;
|
||||
return [Math.max(0, Math.round(left * 100) / 100), Math.max(0, Math.round(width * 100) / 100)];
|
||||
}
|
||||
Reference in new issue
Block a user