mc-dashboard/lib/time.ts
hurkicorgi 9ccd4ca772 Per-player playtime tracking + sparkline hover tooltips
- New GET /api/players/playtime (authed): reads each vanilla stats file
  under /home/minecraft/server/world/stats and maps uuid→name via
  usercache.json. Returns playtime in ticks and hours, plus file mtime
  as "last seen". Memoized 60s via lib/cache#memoAsync.
- New PlaytimeLeaderboard card in the Players tab:
    - Rank, avatar (click → PlayerDrawer), relative last-seen, hours.
    - Top 10 by default with toggle to show all; combined hours badge.
- PlayerDrawer surfaces the player's total hours + last-seen using the
  same cached playtime query, plus fills in UUID if the player is not
  in ops/whitelist/banned.
- lib/time.ts: formatHours() — minutes under 1h, "H.Hh" under a day,
  "Dd Hh" above.
- Analytics sparklines gain hover/touch tooltips:
    - Timestamps threaded through from each MetricEntry.
    - SVG overlay captures onMouseMove/onTouchMove, computes nearest
      index by x, draws a dashed guide line + focus dot.
    - Floating tooltip shows HH:MM + formatted value with unit; edge
      clamping keeps it in-frame at the extremes.
    - Per-chart formatValue so RAM shows GB, CPU 0 decimals, Players
      integer, TPS 1 decimal. Keeps the peak-player marker intact.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 06:24:13 -06:00

36 lines
1.3 KiB
TypeScript

export function timeAgo(iso: string | number | Date): string {
const d = typeof iso === "string" || typeof iso === "number" ? new Date(iso) : iso;
const sec = Math.round((Date.now() - d.getTime()) / 1000);
if (!isFinite(sec) || sec < 0) return "";
if (sec < 45) return "just now";
const min = Math.round(sec / 60);
if (min < 60) return `${min}m ago`;
const hr = Math.round(min / 60);
if (hr < 24) return `${hr}h ago`;
const day = Math.round(hr / 24);
if (day < 30) return `${day}d ago`;
const mo = Math.round(day / 30);
if (mo < 12) return `${mo}mo ago`;
const yr = Math.round(mo / 12);
return `${yr}y ago`;
}
// Format a duration in hours as "H.Hh" under a day, otherwise "Dd Hh".
export function formatHours(hours: number): string {
if (!isFinite(hours) || hours <= 0) return "0h";
if (hours < 1) {
const m = Math.round(hours * 60);
return `${m}m`;
}
if (hours < 24) return `${hours.toFixed(1)}h`;
const days = Math.floor(hours / 24);
const rem = Math.round(hours - days * 24);
return rem ? `${days}d ${rem}h` : `${days}d`;
}
export function formatBytes(n: number): string {
if (!isFinite(n) || n <= 0) return "0 B";
const units = ["B", "KB", "MB", "GB", "TB"];
const i = Math.min(Math.floor(Math.log(n) / Math.log(1024)), units.length - 1);
return `${(n / Math.pow(1024, i)).toFixed(i === 0 ? 0 : 1)} ${units[i]}`;
}