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:
bobbanandClaude Sonnet 5.5 committed 2026-11-10 19:30:27 +01:00
1 parent 9126200295
commit a2b43bcd5d
14 files changed
+9773 -4

No files matched your search

+67
View File
@@ -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>
);
}