Files

462 lines
29 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>App usage</title>
<style>
[hidden] { display: none !important; }
html, body { margin: 0; background: hsl(var(--background)); }
.mono { font-family: var(--aia-mono); font-variant-numeric: tabular-nums; }
.panels { gap: 16px; }
.gpill { padding-left: 3px; }
.gpill.dim { opacity: .45; }
.av { display: inline-flex; align-items: center; justify-content: center; width: 16px; height: 16px; border-radius: 999px;
font: 600 7px/1 var(--aia-font); color: #fff; flex-shrink: 0; }
.stack { display: inline-flex; align-items: center; flex-shrink: 0; }
.stack .av { margin-left: -4px; box-shadow: 0 0 0 1px hsl(var(--card)); } .stack .av:first-child { margin-left: 0; }
.stack .more { margin-left: 4px; font-size: 10px; }
.panel { padding: 16px; }
.ph { display: flex; align-items: flex-start; gap: 8px; margin-bottom: 12px; }
.ph .t { flex: 1; min-width: 0; }
.finding { margin: 0 0 12px; line-height: 1.6; }
.finding b { font: 600 12px var(--aia-mono); font-variant-numeric: tabular-nums; }
.rows { display: flex; flex-direction: column; gap: 6px; }
.rr { display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 2px 8px; font-size: 12px; }
@media (min-width: 640px) { .rr { grid-template-columns: 12rem 1fr auto auto; } }
.rr .lab { display: flex; align-items: center; gap: 6px; min-width: 0; }
.rr .lab span.txt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rr .bar { order: 9; grid-column: span 3; min-width: 0; }
@media (min-width: 640px) { .rr .bar { order: 0; grid-column: auto; } }
.rr .bar div { height: 7px; border-radius: 0 4px 4px 0; }
.rr .n { width: 48px; text-align: right; font-size: 11px; }
.sw { width: 8px; height: 8px; border-radius: 2px; flex-shrink: 0; }
.kind { font-size: 9px; padding: 0 3px; border-radius: 3px; border: 1px solid hsl(var(--border)); flex-shrink: 0; }
.legend { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 10px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.more-btn { margin-top: 8px; }
.area { margin-bottom: 10px; } .area .h { display: flex; align-items: center; gap: 6px; font-size: 11px; font-weight: 500; margin-bottom: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips .aia-badge { font-weight: 500; }
.chips .aia-badge .k { opacity: .65; font-weight: 400; }
.share { display: flex; gap: 2px; width: 100%; height: 20px; }
.share.thin { height: 12px; }
.share div { min-width: 2px; border-radius: 2px; flex-basis: 0; }
.share div:first-child { border-top-left-radius: 4px; border-bottom-left-radius: 4px; }
.share div:last-child { border-top-right-radius: 4px; border-bottom-right-radius: 4px; }
.sbh { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 4px; font-size: 11px; }
.sbh .l { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gap12 > * + * { margin-top: 12px; } .gap16 > * + * { margin-top: 16px; }
.sub { display: flex; align-items: center; gap: 8px; padding-left: 12px; margin-top: 4px; font-size: 11px; }
.tw { overflow-x: auto; }
.aia-table td.aia-num { font-family: var(--aia-mono); font-size: 12px; color: hsl(var(--muted-foreground)); }
.aia-table th.aia-num { text-align: right; }
details > summary { cursor: pointer; list-style: none; font-size: 13px; font-weight: 600; }
details > summary::-webkit-details-marker { display: none; }
details > summary .c { display: inline-block; transition: transform .15s; margin-right: 4px; }
details[open] > summary .c { transform: rotate(90deg); }
.foot { text-align: center; margin: 16px 0 0; }
.stale { margin: 0 0 12px; }
.fade { opacity: .6; transition: opacity .15s; }
</style>
</head>
<body>
<div class="aia-page" id="wrap">
<div class="aia-toolbar">
<div class="aia-seg" id="who"></div>
<span class="aia-spacer"></span>
<div class="aia-seg" id="range"></div>
</div>
<div class="aia-row" id="guests" style="margin:-4px 0 12px" hidden></div>
<p class="stale aia-small aia-muted" id="stale" hidden></p>
<div id="msg" hidden></div>
<div id="panels" class="panels aia-stack" hidden></div>
<p class="foot aia-small aia-muted">Kept on this machine: raw events 90 days, daily counts for good. No prompt, path or text is recorded —
a feature id, who, which device, which day.</p>
</div>
<script>
(function () {
"use strict";
var api = window.aiAgentPlugin || null;
var AREAS = ["Conversations", "Composer", "Projects", "Lab", "Dashboards", "Settings", "App"];
var AREA_COLOR = { Conversations: "#2a78d6", Composer: "#eb6834", Projects: "#1baf7a", Lab: "#6250d6",
Dashboards: "#eda100", Settings: "#94a3b8", App: "#e87ba4" };
var DEVICES = ["phone", "tablet", "desktop", "script"];
var DEVICE_LABEL = { phone: "Phone", tablet: "Tablet", desktop: "Desktop", script: "Script" };
var DEVICE_COLOR = { phone: "#2a78d6", tablet: "#1baf7a", desktop: "#eb6834", script: "#94a3b8" };
var ORIGIN_LABEL = { app: "The app", script: "A script (CLI, mail)", cron: "Cron job", scheduled: "Send later",
child: "Another conversation", mcp: "An AI app (MCP)", handoff: "Handoff", fork: "Fork", newProject: "New project",
unknown: "Terminal / before tracking" };
var ORIGIN_COLOR = { app: "#2a78d6", script: "#eb6834", cron: "#1baf7a", scheduled: "#eda100", child: "#6250d6",
mcp: "#e87ba4", handoff: "#008300", fork: "#e34948", newProject: "rgba(42,120,214,.6)", unknown: "#94a3b8" };
var WHO = [["all", "Everyone"], ["owner", "You"], ["guests", "Guests"]];
var RANGES = ["7d", "30d", "90d"];
var RANGE_DAYS = { "7d": 7, "30d": 30, "90d": 90 };
var state = { range: "30d", who: "all" };
try { var sv = JSON.parse(localStorage.getItem("aia-app-usage") || "{}"); if (RANGES.indexOf(sv.range) >= 0) state.range = sv.range; } catch (e) {}
var data = null, seq = 0, tables = {}, showAll = false, handle = null;
var RANGE_WORDS = { "7d": "Last 7 days", "30d": "Last 30 days", "90d": "Last 90 days" };
// reads: aiAgentPlugin.query (cached answer at once, then the fresh one); an older host only has action()
function read(name, payload, cb) {
if (api && api.query) return api.query(name, payload, cb);
api.action(name, payload).then(
function (d) { cb(d, { fromCache: false, fetching: false, at: Date.now() }); },
function (e) { cb(null, { error: String((e && e.message) || e), fromCache: false, fetching: false, at: Date.now() }); });
return { stop: function () {}, refresh: function () { return Promise.resolve(); } };
}
function setHeader(h) { if (api && api.setHeader) api.setHeader(h); }
function $(id) { return document.getElementById(id); }
function el(tag, attrs, kids) {
var n = document.createElement(tag);
for (var k in attrs || {}) {
if (attrs[k] == null) continue;
if (k === "text") n.textContent = attrs[k];
else if (k === "style") n.setAttribute("style", attrs[k]);
else if (k === "on") for (var ev in attrs.on) n.addEventListener(ev, attrs.on[ev]);
else n.setAttribute(k, attrs[k]);
}
(kids || []).forEach(function (c) { if (c != null && c !== false) n.appendChild(typeof c === "string" || typeof c === "number" ? document.createTextNode(String(c)) : c); });
return n;
}
function B(x) { return el("b", { text: String(x) }); }
var pct = function (x) { return Math.round(x * 100) + "%"; };
var count = function (n) { return Number(n || 0).toLocaleString("en-US"); };
var queued = false;
function resize() {
if (!api || typeof api.resize !== "function" || queued) return;
queued = true;
requestAnimationFrame(function () { queued = false; api.resize(Math.ceil(document.documentElement.scrollHeight) + 4); });
}
if (window.ResizeObserver) new ResizeObserver(resize).observe(document.body);
// ── people ────────────────────────────────────────────────────────────────
function hue(s) { var h = 0; for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) >>> 0; return h % 360; }
function nameOf(uid) { var p = ((data && data.people) || []).find(function (x) { return x.id === uid; }); return p ? p.name : uid; }
function avatar(uid) {
var nm = nameOf(uid) || uid;
var ini = nm.split(/\s+/).map(function (w) { return w[0] || ""; }).join("").slice(0, 2).toUpperCase();
return el("span", { class: "av", title: nm, style: "background:hsl(" + hue(uid) + " 55% 45%)", text: ini });
}
function stack(uids, max) {
max = max || 4;
if (!uids.length) return el("span");
var s = el("span", { class: "stack", title: uids.map(nameOf).join(", ") });
uids.slice(0, max).forEach(function (u) { s.appendChild(avatar(u)); });
if (uids.length > max) s.appendChild(el("span", { class: "more aia-muted", text: "+" + (uids.length - max) }));
return s;
}
// ── analysis (was usageAnalysis.ts) ───────────────────────────────────────
function scopeOf(who) { return who === "owner" ? "owner" : (who === "guests" || who.indexOf("guest:") === 0) ? "guests" : "all"; }
function registry() { var m = {}; ((data && data.registry) || []).forEach(function (f) { m[f.id] = f; }); return m; }
function featureRows(reg) {
return data.features.filter(function (f) { return f.kind === "view" || f.kind === "event"; }).map(function (f) {
var r = reg[f.name];
return { id: f.name, label: r ? r.label : f.name, area: (r && r.area) || "App", kind: f.kind, n: f.n,
users: Object.keys(f.byUser || {}), byDevice: Object.assign({}, f.byDevice || {}), lastDay: f.lastDay || null };
}).sort(function (a, b) { return b.n - a.n || a.label.localeCompare(b.label); });
}
function apiRows() {
return data.features.filter(function (f) { return f.kind === "api"; }).map(function (f) {
return { route: f.name, n: f.n, users: Object.keys(f.byUser || {}), byDevice: f.byDevice || {} };
}).sort(function (a, b) { return b.n - a.n; });
}
function reachable(reg, who) {
var guests = scopeOf(who) === "guests";
return Object.keys(reg).map(function (k) { return reg[k]; }).filter(function (f) { return !(guests && f.ownerOnly); });
}
function unused(reg, who) {
var used = {}; data.features.forEach(function (f) { used[f.name] = 1; });
return reachable(reg, who).filter(function (f) { return !used[f.id]; }).sort(function (a, b) {
return AREAS.indexOf(a.area) - AREAS.indexOf(b.area) || a.label.localeCompare(b.label); });
}
function mainDevice(bd) {
var tot = 0, best = null, bn = -1;
Object.keys(bd).forEach(function (d) { tot += bd[d] || 0; if ((bd[d] || 0) > bn) { bn = bd[d] || 0; best = d; } });
return tot ? { device: best, share: bn / tot } : null;
}
function topShare(rows, k) {
var tot = rows.reduce(function (a, r) { return a + r.n; }, 0);
return tot ? rows.slice(0, k).reduce(function (a, r) { return a + r.n; }, 0) / tot : 0;
}
function trackedDays(since) {
if (!since) return null;
var t = Date.parse(since + "T00:00:00");
return isNaN(t) ? null : Math.max(1, Math.floor((Date.now() - t) / 86400000) + 1);
}
function adoption(reg) {
var guestIds = {}; data.people.forEach(function (p) { if (p.guest) guestIds[p.id] = 1; });
var nGuests = Object.keys(guestIds).length;
var byName = {}; data.features.forEach(function (f) { byName[f.name] = f; });
return (data.whatsNew || []).map(function (entry) {
var day = entry.publishedAt ? entry.publishedAt.slice(0, 10) : null;
var feats = Object.keys(reg).map(function (k) { return reg[k]; }).filter(function (f) { return f.whatsNew === entry.id; }).map(function (f) {
var first = (byName[f.id] && byName[f.id].firstUse) || {}, ad = [], bef = [];
Object.keys(first).forEach(function (uid) { if (!guestIds[uid]) return; if (day && first[uid] < day) bef.push(uid); else ad.push(uid); });
return { id: f.id, label: f.label, adopters: ad, before: bef };
});
return { entry: entry, guests: nGuests, features: feats };
});
}
// ── viz ───────────────────────────────────────────────────────────────────
function panel(id, title, question, finding, chart, table) {
var on = !!tables[id];
var head = el("div", { class: "ph" }, [el("div", { class: "t" }, [el("h2", { class: "aia-title", text: title }), el("p", { class: "aia-subtitle", text: question })])]);
if (table) head.appendChild(el("button", { class: "aia-btn aia-btn-ghost aia-btn-sm", type: "button", title: on ? "Show the chart" : "Show the numbers as a table",
text: on ? "Chart" : "Table", on: { click: function () { tables[id] = !on; render(); } } }));
return el("section", { class: "aia-card panel" }, [head, el("p", { class: "aia-note finding" }, finding), on && table ? table : chart]);
}
function dataTable(head, rows) {
var t = el("table", { class: "aia-table" }, [el("thead", null, [el("tr", null, head.map(function (h, i) { return el("th", { class: i ? "aia-num" : null, text: h }); }))])]);
var tb = el("tbody");
rows.forEach(function (r) { tb.appendChild(el("tr", null, r.map(function (c, i) { return el("td", { class: i ? "aia-num" : null }, [c]); }))); });
t.appendChild(tb);
return el("div", { class: "tw" }, [t]);
}
function legend(items) {
return el("div", { class: "legend aia-muted" }, items.map(function (i) { return el("span", null, [el("span", { class: "sw", style: "background:" + i[1] }), i[0]]); }));
}
function shareBar(segs, thin) {
var tot = segs.reduce(function (a, s) { return a + s.value; }, 0) || 1;
return el("div", { class: "share" + (thin ? " thin" : "") }, segs.filter(function (s) { return s.value > 0; }).map(function (s) {
return el("div", { title: s.label + " · " + (s.value / tot * 100).toFixed(1) + "%", style: "flex-grow:" + s.value + ";background:" + s.color });
}));
}
function rankRow(label, color, kind, title, share, uids, n) {
var lab = el("div", { class: "lab" }, [el("span", { class: "sw", style: "background:" + color }),
kind ? el("span", { class: "kind aia-muted", text: kind === "view" ? "page" : "action" }) : null,
el("span", { class: "txt", title: title || label, text: label })]);
var w = Math.max(share * 100, share > 0 ? 1.5 : 0);
return el("div", { class: "rr" }, [lab, el("div", { class: "bar" }, [el("div", { style: "width:" + w + "%;background:" + color })]),
stack(uids, 3), el("span", { class: "n mono aia-muted", text: count(n) })]);
}
function deviceSegs(bd) { return DEVICES.map(function (d) { return { label: DEVICE_LABEL[d], color: DEVICE_COLOR[d], value: bd[d] || 0 }; }); }
// ── panels ────────────────────────────────────────────────────────────────
function mostUsed(rows, nReach, range) {
var max = rows.length ? rows[0].n : 1;
var shown = showAll ? rows : rows.slice(0, 15);
var f = !rows.length ? ["Nothing counted in " + range + " yet — page views and feature actions arrive as people use the app."]
: [B(rows.length), " of " + nReach + " features were used in " + range + "; the top " + Math.min(5, rows.length) + " (" +
rows.slice(0, 5).map(function (r) { return r.label; }).join(", ") + ") make ", B(pct(topShare(rows, 5))), " of all use."];
var chart = el("div", null, [el("div", { class: "rows" }, shown.map(function (r) {
return rankRow(r.label, AREA_COLOR[r.area], r.kind, r.id, r.n / max, r.users, r.n); }))]);
if (rows.length > 15) chart.appendChild(el("button", { class: "aia-btn aia-btn-sm more-btn", type: "button", text: showAll ? "Show the top 15" : "Show all " + rows.length,
on: { click: function () { showAll = !showAll; render(); } } }));
if (rows.length) chart.appendChild(legend(AREAS.map(function (a) { return [a, AREA_COLOR[a]]; })));
var table = dataTable(["Feature", "Area", "Kind", "Uses", "People", "Mostly on", "Last day"], rows.map(function (r) {
var m = mainDevice(r.byDevice);
return [r.label, r.area, r.kind === "view" ? "page" : "action", count(r.n), r.users.length, m ? DEVICE_LABEL[m.device] + " " + pct(m.share) : "—", r.lastDay || "—"];
}));
return panel("most", "Which features get used?", "Pages and actions by uses, " + range, f, chart, table);
}
function unusedPanel(list, nReach, range, young) {
var f = list.length ? [B(list.length), " of " + nReach + " features weren’t used in " + range + " — candidates to simplify, hide behind a setting, or drop."]
: ["Every feature in the registry was used at least once in " + range + "."];
if (young) f.push(" Counting only since " + young.since + " (" + young.days + " d): give it the full " + range + " before pruning anything.");
var chart = el("div");
AREAS.forEach(function (a) {
var items = list.filter(function (x) { return x.area === a; });
if (!items.length) return;
chart.appendChild(el("div", { class: "area" }, [el("div", { class: "h" }, [el("span", { class: "sw", style: "background:" + AREA_COLOR[a] }), a,
el("span", { class: "aia-muted", style: "font-weight:400", text: "· " + items.length })]),
el("div", { class: "chips" }, items.map(function (x) { return el("span", { class: "aia-badge", title: x.id }, [
el("span", { class: "k", text: x.kind === "view" ? "page" : "action" }), x.label]); }))]));
});
var table = dataTable(["Feature", "Area", "Kind", "Id"], list.map(function (x) { return [x.label, x.area, x.kind === "view" ? "page" : "action", x.id]; }));
return panel("unused", "What goes unused?", "Registry features with no use in " + range, f, chart, table);
}
function devicesPanel(rows) {
var dv = data.devices || {};
var total = DEVICES.reduce(function (a, d) { return a + (dv[d] || 0); }, 0);
var share = function (d) { return total ? (dv[d] || 0) / total : 0; };
var solid = rows.filter(function (r) { return r.n >= 5; });
var by = function (d) { return solid.slice().sort(function (a, b) { return (b.byDevice[d] || 0) / b.n - (a.byDevice[d] || 0) / a.n; })[0]; };
var mp = by("phone"), md = by("desktop");
var f;
if (!total) f = ["No device reported yet."];
else {
f = [B(pct(share("phone"))), " of use is on a phone, ", B(pct(share("desktop"))), " on a desktop"];
if (share("tablet") > 0) f.push(", " + pct(share("tablet")) + " on a tablet");
f.push(".");
var said = false;
if (mp && (mp.byDevice.phone || 0) / mp.n > 0.5) { f.push(" " + mp.label + " is the most phone-heavy (" + pct((mp.byDevice.phone || 0) / mp.n) + ")"); said = true; }
if (md && md !== mp && (md.byDevice.desktop || 0) / md.n > 0.5) { f.push((said ? "; " : " ") + md.label + " the most desktop-heavy (" + pct((md.byDevice.desktop || 0) / md.n) + ")"); said = true; }
if (said) f.push(".");
}
var chart = el("div");
if (total) {
chart.className = "gap16";
chart.appendChild(el("div", null, [el("div", { class: "sbh" }, [el("span", { class: "l", text: "All use" }), el("span", { class: "mono aia-muted", text: count(total) })]), shareBar(deviceSegs(dv))]));
var bars = el("div", { class: "gap12" });
rows.slice(0, 8).forEach(function (r) {
bars.appendChild(el("div", null, [el("div", { class: "sbh" }, [el("span", { class: "l", text: r.label }), el("span", { class: "mono aia-muted", text: count(r.n) })]), shareBar(deviceSegs(r.byDevice))]));
});
chart.appendChild(el("div", null, [bars, legend(DEVICES.filter(function (d) { return dv[d]; }).map(function (d) { return [DEVICE_LABEL[d], DEVICE_COLOR[d]]; }))]));
}
var table = dataTable(["Feature", "Phone", "Tablet", "Desktop", "Mostly"], rows.map(function (r) {
var m = mainDevice(r.byDevice);
return [r.label, r.byDevice.phone || 0, r.byDevice.tablet || 0, r.byDevice.desktop || 0, m ? DEVICE_LABEL[m.device] : "—"];
}));
return panel("devices", "Phone or desktop?", "Where pages and actions were used", f, chart, table);
}
function adoptionPanel(items) {
var latest = items[0], f;
if (!latest) f = ["No What’s new entry is published yet."];
else {
var linked = latest.features;
f = ["“" + latest.entry.title + "” was seen by ", B(latest.entry.seen), " of " + latest.guests + " guest" + (latest.guests === 1 ? "" : "s")];
if (linked.length) f.push("; ", B(linked.reduce(function (a, x) { return a + x.adopters.length; }, 0)),
" started using " + linked.map(function (x) { return x.label; }).join(", ") + " after it went out.");
else f.push(". No feature points at it yet — add whatsNew: \"" + latest.entry.id + "\" to its entry in lib/telemetry/features.ts to follow its adoption.");
}
var chart = el("div", { class: "gap12" });
items.forEach(function (a) {
var box = el("div", null, [el("div", { class: "sbh" }, [el("span", { class: "l", text: a.entry.title }),
el("span", { class: "mono aia-muted", text: (a.entry.publishedAt || "").slice(0, 10) + " · seen " + a.entry.seen + "/" + a.guests })]),
shareBar([{ label: "Seen", color: "hsl(var(--primary, 24 95% 53%))", value: Math.min(a.entry.seen, a.guests || a.entry.seen) },
{ label: "Not yet", color: "hsl(var(--muted-foreground, 215 16% 47%) / .25)", value: Math.max(0, a.guests - a.entry.seen) }], true)]);
a.features.forEach(function (x) {
box.appendChild(el("div", { class: "sub aia-muted" }, ["›", el("span", { text: x.label }), el("span", { class: "mono", text: x.adopters.length + " took it up" }),
stack(x.adopters), x.before.length ? el("span", { class: "mono", text: "· " + x.before.length + " already used it" }) : null]));
});
chart.appendChild(box);
});
if (items.length) chart.appendChild(legend([["Seen", "hsl(var(--primary, 24 95% 53%))"], ["Not yet", "hsl(var(--muted-foreground, 215 16% 47%) / .25)"]]));
var trows = [];
items.forEach(function (a) {
(a.features.length ? a.features : [null]).forEach(function (x) {
trows.push([a.entry.title, (a.entry.publishedAt || "—").slice(0, 10), a.entry.seen, a.guests, x ? x.label : "—", x ? x.adopters.length : "—", x ? x.before.length : "—"]);
});
});
return panel("adoption", "Did guests take up what What’s new announced?", "Each entry: who saw it, who then used the feature", f, chart,
dataTable(["Entry", "Published", "Seen", "Guests", "Feature", "Took it up", "Used before"], trows));
}
function originsPanel(range) {
var runs = data.runs || [];
var total = runs.reduce(function (a, r) { return a + r.n; }, 0);
var rows = runs.slice().sort(function (a, b) { return b.n - a.n; }).map(function (r) {
return { origin: r.origin, label: ORIGIN_LABEL[r.origin] || r.origin, color: ORIGIN_COLOR[r.origin] || ORIGIN_COLOR.unknown,
n: r.n, share: total ? r.n / total : 0, users: Object.keys(r.byUser || {}) };
});
var unk = rows.find(function (r) { return r.origin === "unknown"; });
var f;
if (!total) f = ["No conversation started in " + range + "."];
else {
f = ["Of ", B(count(total)), " conversations in " + range + ", ", B(pct(rows[0].share)), " started from " + rows[0].label.toLowerCase()];
if (rows[1]) f.push(", ", B(pct(rows[1].share)), " from " + rows[1].label.toLowerCase());
if (rows[2]) f.push(", " + pct(rows[2].share) + " from " + rows[2].label.toLowerCase());
f.push(".");
if (unk && unk.share > 0.25) f.push(" “Terminal / before tracking” is the ones started outside the hub, or before /api/spawn stamped an origin.");
}
var max = rows.length ? rows[0].n : 1;
var chart = el("div", { class: "gap12" });
if (total) {
chart.appendChild(shareBar(rows.map(function (r) { return { label: r.label, color: r.color, value: r.n }; })));
chart.appendChild(el("div", { class: "rows" }, rows.map(function (r) { return rankRow(r.label, r.color, null, null, r.n / max, r.users, r.n); })));
}
return panel("origins", "Where do conversations start?", "Every conversation started in " + range + ", by origin", f, chart,
dataTable(["Started from", "Conversations", "Share", "People"], rows.map(function (r) { return [r.label, count(r.n), pct(r.share), r.users.length]; })));
}
function apiPanel(rows) {
if (!rows.length) return null;
var tot = rows.reduce(function (a, r) { return a + r.n; }, 0);
return el("details", { class: "aia-card panel" }, [el("summary", null, [el("span", { class: "c aia-muted", text: "›" }), "Server-side (API routes)",
el("span", { class: "aia-muted", style: "font-weight:400;font-size:11px", text: " · " + rows.length + " routes, " + count(tot) + " calls" })]),
el("p", { class: "aia-small aia-muted", text: "What the backend counted per route and person — the CLI, the mail trigger and other scripts show as “Script”." }),
dataTable(["Route", "Calls", "People", "Phone", "Desktop", "Script"], rows.map(function (r) {
return [r.route, count(r.n), r.users.length, r.byDevice.phone || 0, r.byDevice.desktop || 0, r.byDevice.script || 0]; }))]);
}
// ── chrome ────────────────────────────────────────────────────────────────
function seg(box, items, cur, pick) {
box.textContent = "";
items.forEach(function (it) {
box.appendChild(el("button", { type: "button", class: it[0] === cur ? "aia-active" : null, "aria-pressed": it[0] === cur ? "true" : "false",
text: it[1], on: { click: function () { if (it[0] !== cur) pick(it[0]); } } }));
});
}
function setWho(w) { state.who = w; load(); }
function setRange(r) { state.range = r; try { localStorage.setItem("aia-app-usage", JSON.stringify({ range: r })); } catch (e) {} load(); }
function whoWords() {
if (state.who === "all") return "everyone";
if (state.who === "owner") return "you";
if (state.who === "guests") return "guests";
return nameOf(state.who.slice(6));
}
function header(rows) {
var line = RANGE_WORDS[state.range] + " · " + whoWords();
if (rows) line += " · " + count(rows.reduce(function (a, r) { return a + r.n; }, 0)) + " uses of " + rows.length + " features";
setHeader({ subtitle: line });
}
function message(kind, text, strong) {
var m = $("msg"); m.textContent = ""; m.className = kind === "error" ? "aia-error" : "aia-empty";
if (strong) m.appendChild(el("strong", { text: strong }));
m.appendChild(document.createTextNode(text));
m.hidden = false;
}
function render() {
var segWho = scopeOf(state.who) === "guests" ? "guests" : state.who;
seg($("who"), WHO, segWho, setWho);
seg($("range"), RANGES.map(function (r) { return [r, r]; }), state.range, setRange);
var g = $("guests"); g.textContent = "";
if (data && segWho === "guests") {
data.people.filter(function (p) { return p.guest; }).forEach(function (p) {
var on = state.who === "guest:" + p.id;
g.appendChild(el("button", { type: "button", class: "aia-chip gpill" + (on ? " aia-active" : (state.who.indexOf("guest:") === 0 ? " dim" : "")),
title: on ? "Show every guest" : "Show only " + p.name + "'s use", on: { click: function () { setWho(on ? "guests" : "guest:" + p.id); } } },
[avatar(p.id), el("span", { text: p.name })]));
});
}
g.hidden = !g.firstChild;
if (!data) { header(null); resize(); return; }
var reg = registry();
var rows = featureRows(reg);
var nReach = reachable(reg, state.who).length;
header(rows);
var days = trackedDays(data.trackedSince);
var young = data.trackedSince && days != null && days < RANGE_DAYS[state.range] ? { since: data.trackedSince, days: days } : null;
var p = $("panels"); p.textContent = "";
[mostUsed(rows, nReach, state.range), unusedPanel(unused(reg, state.who), nReach, state.range, young), devicesPanel(rows),
adoptionPanel(adoption(reg)), originsPanel(state.range), apiPanel(apiRows())].forEach(function (n) { if (n) p.appendChild(n); });
$("msg").hidden = true; p.hidden = false;
resize();
}
// a range / who change: drop the old read, start the new one (the old answer stays, faded, until the new one paints)
function load() {
var my = ++seq;
if (handle) { handle.stop(); handle = null; }
render();
$("panels").classList.add("fade");
if (!api) { message("empty", "Open this page from the ai-agent."); resize(); return; }
handle = read("usage", { range: state.range, who: state.who }, function (d, meta) {
if (my !== seq) return;
meta = meta || {};
$("panels").classList.toggle("fade", !!meta.fetching);
if (d) {
data = d;
var st = $("stale");
st.hidden = !meta.error;
st.textContent = meta.error ? "Couldn’t refresh: " + meta.error + " — showing the last answer." : "";
render();
} else if (meta.error) {
data = null;
$("panels").hidden = true; $("stale").hidden = true;
render();
message("error", meta.error);
resize();
}
});
}
setInterval(function () { if (!document.hidden && data && handle) handle.refresh(); }, 120000);
load();
})();
</script>
</body>
</html>