Add minimal dashboard page served at /
Test and publish / test (push) Successful in 15s
Test and publish / publish (push) Successful in 3m10s

Single embedded HTML page that reads the daily summary and activities
endpoints with a user-supplied API key: goals, totals by kind, top labels
and a timeline for the selected day.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-14 21:02:11 +01:00
co-authored by Claude Opus 5.5
parent f4ce04875c
commit 06800edd91
4 changed files with 183 additions and 0 deletions
+2
View File
@@ -14,6 +14,7 @@ import (
"ccp/internal/ingest"
"ccp/internal/store"
"ccp/internal/summary"
"ccp/internal/web"
)
const (
@@ -36,6 +37,7 @@ func New(s *store.Store, loc *time.Location, log *slog.Logger) http.Handler {
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
})
mux.HandleFunc("GET /readyz", srv.ready)
mux.HandleFunc("GET /{$}", web.Handler)
api := http.NewServeMux()
api.HandleFunc("POST /desktop/events", srv.ingestEvents(ingest.Desktop))
+164
View File
@@ -0,0 +1,164 @@
<!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); }
#login { margin-top:40px; display:flex; gap:8px; }
#login input { flex:1; }
</style>
</head>
<body>
<main>
<header>
<h1>Activity Tracker</h1>
<div class="nav" id="nav" hidden>
<button id="prev" aria-label="Previous day">‹</button>
<input type="date" id="date">
<button id="next" aria-label="Next day">›</button>
<button id="logout" title="Forget API key">Sign out</button>
</div>
</header>
<form id="login" hidden>
<input type="password" id="key" placeholder="API key (trk_…)" autocomplete="off" required>
<button>Open</button>
</form>
<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);
const KEY = "tracker_api_key";
const store = {
get() { try { return localStorage.getItem(KEY) || ""; } catch { return ""; } },
set(v) { try { v ? localStorage.setItem(KEY, v) : localStorage.removeItem(KEY); } catch {} },
};
let apiKey = store.get();
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 => ({ "&":"&amp;", "<":"&lt;", ">":"&gt;", '"':"&quot;" }[c]));
const today = () => new Date().toLocaleDateString("en-CA"); // YYYY-MM-DD in local time
async function api(path) {
const r = await fetch("api/v1/" + path, { headers: { Authorization: "Bearer " + apiKey } });
const body = await r.json().catch(() => ({}));
if (r.status === 401) { signOut(); throw new Error(body.error || "unauthorized"); }
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();
}
function signOut() {
apiKey = ""; store.set("");
show();
}
function show() {
$("login").hidden = !!apiKey;
$("nav").hidden = !apiKey;
$("content").hidden = true;
$("status").textContent = "";
if (apiKey) load();
}
$("login").onsubmit = e => {
e.preventDefault();
apiKey = $("key").value.trim(); store.set(apiKey); $("key").value = "";
show();
};
$("logout").onclick = signOut;
$("prev").onclick = () => shift(-1);
$("next").onclick = () => shift(1);
$("date").onchange = load;
$("date").value = today();
show();
</script>
</body>
</html>
+16
View File
@@ -0,0 +1,16 @@
// Package web serves the dashboard, a single static page that reads the
// /api/v1 query endpoints with an API key the viewer enters.
package web
import (
_ "embed"
"net/http"
)
//go:embed index.html
var index []byte
func Handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(index)
}