Like job-tracker, the tracker forwards the visitor's cookies to the site backend's /auth/validate-admin instead of asking for an API key. Signed in, the dashboard shows every device's data. The new /setup page has instructions for each collector and creates and revokes API keys. The userscript is served publicly at /tracker.user.js. Co-Authored-By: Claude Opus 5.5 <[email protected]>
135 lines
5.7 KiB
HTML
135 lines
5.7 KiB
HTML
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>Activity Tracker</title>
|
||
<style>
|
||
:root { --bg:#fafafa; --fg:#1a1a1a; --muted:#6b6b6b; --line:#e2e2e2; --bar:#4a7bd0; --ok:#2e8b57; --bad:#c0392b; }
|
||
@media (prefers-color-scheme: dark) {
|
||
:root { --bg:#151515; --fg:#e6e6e6; --muted:#999; --line:#2c2c2c; --bar:#6f9be6; --ok:#5cc28a; --bad:#e2685a; }
|
||
}
|
||
* { box-sizing: border-box; }
|
||
body { margin:0; background:var(--bg); color:var(--fg); font:15px/1.45 system-ui, sans-serif; }
|
||
main { max-width:760px; margin:0 auto; padding:24px 16px 48px; }
|
||
header { display:flex; flex-wrap:wrap; gap:8px; align-items:center; justify-content:space-between; }
|
||
h1 { font-size:20px; margin:0; }
|
||
h2 { font-size:13px; text-transform:uppercase; letter-spacing:.05em; color:var(--muted); margin:28px 0 8px; }
|
||
input, button { font:inherit; color:inherit; background:transparent; border:1px solid var(--line); border-radius:6px; padding:4px 8px; }
|
||
button { cursor:pointer; }
|
||
.nav { display:flex; gap:6px; align-items:center; }
|
||
.muted { color:var(--muted); }
|
||
.err { color:var(--bad); }
|
||
table { width:100%; border-collapse:collapse; }
|
||
td { padding:5px 0; border-bottom:1px solid var(--line); vertical-align:top; }
|
||
td.num { text-align:right; white-space:nowrap; font-variant-numeric:tabular-nums; padding-left:12px; }
|
||
td.label { overflow-wrap:anywhere; }
|
||
.bar { height:4px; background:var(--bar); border-radius:2px; margin-top:3px; }
|
||
.ok { color:var(--ok); } .bad { color:var(--bad); }
|
||
a { color:var(--bar); }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<main>
|
||
<header>
|
||
<h1>Activity Tracker</h1>
|
||
<div class="nav">
|
||
<button id="prev" aria-label="Previous day">‹</button>
|
||
<input type="date" id="date">
|
||
<button id="next" aria-label="Next day">›</button>
|
||
<a href="setup">Setup</a>
|
||
</div>
|
||
</header>
|
||
|
||
<p id="status" class="muted"></p>
|
||
<div id="content" hidden>
|
||
<h2>Goals</h2><table id="goals"></table>
|
||
<h2>By kind</h2><table id="kinds"></table>
|
||
<h2>Top labels</h2><table id="labels"></table>
|
||
<h2>Timeline</h2><table id="timeline"></table>
|
||
</div>
|
||
</main>
|
||
<script>
|
||
const $ = id => document.getElementById(id);
|
||
|
||
function dur(s) {
|
||
s = Math.round(s);
|
||
const h = Math.floor(s / 3600), m = Math.floor(s % 3600 / 60);
|
||
if (h) return `${h}h ${String(m).padStart(2, "0")}m`;
|
||
return m ? `${m}m` : `${s}s`;
|
||
}
|
||
const time = t => new Date(t).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
|
||
const esc = s => String(s ?? "").replace(/[&<>"]/g, c => ({ "&":"&", "<":"<", ">":">", '"':""" }[c]));
|
||
const today = () => new Date().toLocaleDateString("en-CA"); // YYYY-MM-DD in local time
|
||
|
||
async function api(path) {
|
||
// Authenticated by the main site's session cookies, which the browser sends.
|
||
const r = await fetch("api/v1/" + path);
|
||
const body = await r.json().catch(() => ({}));
|
||
if (r.status === 401) { location.reload(); throw new Error("session expired"); } // reload goes to the login page
|
||
if (!r.ok) throw new Error(body.error || r.statusText);
|
||
return body;
|
||
}
|
||
|
||
function rows(el, items, render, empty) {
|
||
el.innerHTML = items.length ? items.map(render).join("") : `<tr><td class="muted">${empty}</td></tr>`;
|
||
}
|
||
|
||
async function load() {
|
||
const date = $("date").value;
|
||
$("status").textContent = "Loading…";
|
||
try {
|
||
const sum = await api("summary/daily?date=" + date);
|
||
const q = new URLSearchParams({ from: sum.from, to: sum.to, limit: "500" });
|
||
const { activities } = await api("activities?" + q);
|
||
|
||
rows($("goals"), sum.goals, g => `<tr>
|
||
<td class="label">${esc(g.kind)}${g.label ? " · " + esc(g.label) : ""}
|
||
<span class="muted">${esc(g.op)} ${dur(g.target_s)}</span></td>
|
||
<td class="num">${dur(g.actual_s)}</td>
|
||
<td class="num ${g.met ? "ok" : "bad"}">${g.met ? "✓ met" : "✗ missed"}</td></tr>`,
|
||
"No goals. Add one with <code>tracker goal add</code>.");
|
||
|
||
const kinds = Object.entries(sum.by_kind).sort((a, b) => b[1] - a[1]);
|
||
const maxK = kinds[0]?.[1] || 1;
|
||
rows($("kinds"), kinds, ([k, s]) => `<tr>
|
||
<td class="label">${esc(k)}<div class="bar" style="width:${s / maxK * 100}%"></div></td>
|
||
<td class="num">${dur(s)}</td></tr>`, "Nothing recorded.");
|
||
|
||
const labels = [...sum.totals].sort((a, b) => b.seconds - a.seconds).slice(0, 15);
|
||
const maxL = labels[0]?.seconds || 1;
|
||
rows($("labels"), labels, t => `<tr>
|
||
<td class="label">${esc(t.label || "—")} <span class="muted">${esc(t.kind)}</span>
|
||
<div class="bar" style="width:${t.seconds / maxL * 100}%"></div></td>
|
||
<td class="num">${dur(t.seconds)}</td></tr>`, "Nothing recorded.");
|
||
|
||
const acts = activities.filter(a => a.duration_s >= 60)
|
||
.sort((a, b) => new Date(b.started_at) - new Date(a.started_at));
|
||
rows($("timeline"), acts, a => `<tr>
|
||
<td class="num muted" style="padding:5px 12px 5px 0">${time(a.started_at)}–${time(a.ended_at)}</td>
|
||
<td class="label">${esc(a.label || a.kind)} <span class="muted">${esc(a.kind)}${a.attrs?.title ? " · " + esc(a.attrs.title) : ""}</span></td>
|
||
<td class="num">${dur(a.duration_s)}</td></tr>`, "No activities of a minute or longer.");
|
||
|
||
$("content").hidden = false;
|
||
$("status").textContent = activities.length >= 500 ? "Timeline shows the first 500 activities." : "";
|
||
} catch (e) {
|
||
$("status").innerHTML = `<span class="err">${esc(e.message)}</span>`;
|
||
}
|
||
}
|
||
|
||
function shift(days) {
|
||
const d = new Date($("date").value + "T12:00:00");
|
||
d.setDate(d.getDate() + days);
|
||
$("date").value = d.toLocaleDateString("en-CA");
|
||
load();
|
||
}
|
||
|
||
$("prev").onclick = () => shift(-1);
|
||
$("next").onclick = () => shift(1);
|
||
$("date").onchange = load;
|
||
$("date").value = today();
|
||
load();
|
||
</script>
|
||
</body>
|
||
</html>
|