| import { useMemo, useState } from "react"; |
| import type { ActivityEvent } from "../types"; |
| import { |
| buildOverviewTurns, |
| type Cat, |
| type OverviewTurn, |
| } from "../activityOverviewModel"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| const CAT_ORDER: Cat[] = ["reasoning", "generating", "tool", "idle"]; |
| |
| |
| |
| |
| const ACTIVE_CATS: Cat[] = ["reasoning", "generating", "tool"]; |
| const CAT_META: Record<Cat, { label: string; color: string }> = { |
| reasoning: { label: "Reasoning", color: "#a78bfa" }, |
| generating: { label: "Generating", color: "#4edca3" }, |
| tool: { label: "Tool / waiting", color: "#4ea3dc" }, |
| idle: { label: "Idle", color: "#3a3a46" }, |
| }; |
|
|
| const RES: { label: string; s: number }[] = [ |
| { label: "5s", s: 5 }, |
| { label: "1m", s: 60 }, |
| { label: "10m", s: 600 }, |
| { label: "1h", s: 3600 }, |
| { label: "12h", s: 43200 }, |
| { label: "24h", s: 86400 }, |
| ]; |
| |
| |
| |
| |
| const MAX_BUCKETS = 10000; |
| |
| |
| const BAR_PX = 4; |
| |
| |
| |
| const PER_EVENT_SECS = 1; |
| |
| |
| |
| const ZERO_EVENT_COMP_SECS = 0.25; |
| |
| |
| |
| const GAP_THRESHOLD = 1800; |
| const GAP_COMPRESSED = 120; |
|
|
| interface Bucket { |
| |
| |
| |
| realStart: number; |
| realEnd: number; |
| cats: Record<Cat, number>; |
| taskSecs: Record<string, number>; |
| total: number; |
| gap: number; |
| isGap: boolean; |
| topTask: string; |
| } |
|
|
| |
| |
| |
| |
| interface Seg { |
| kind: "active" | "idle" | "gap"; |
| realStart: number; realLen: number; |
| compStart: number; compLen: number; |
| turn: OverviewTurn; |
| |
| |
| |
| cat?: Cat; |
| } |
|
|
| export function ActivityOverview({ |
| events, |
| endTime, |
| startTime, |
| deskEndTime, |
| taskContent, |
| liveEvents = [], |
| }: { |
| events: ActivityEvent[]; |
| endTime?: number; |
| startTime?: string; |
| deskEndTime?: string; |
| taskContent?: string | null; |
| liveEvents?: ActivityEvent[]; |
| }) { |
| const turns = useMemo<OverviewTurn[]>( |
| () => buildOverviewTurns(events, { endTime, startTime, deskEndTime, taskContent, liveEvents }), |
| [events, endTime, startTime, deskEndTime, taskContent, liveEvents], |
| ); |
|
|
| const t0 = turns.length ? turns[0].start : 0; |
| const t1 = turns.length ? turns[turns.length - 1].end : 0; |
| const realSpan = t1 - t0; |
|
|
| |
| |
| |
| |
| const timeline = useMemo(() => { |
| const segs: Seg[] = []; |
| let comp = 0; |
| const pushIdleTail = (turn: OverviewTurn, from: number) => { |
| const idleDur = turn.end - from; |
| if (idleDur <= 0) return; |
| const isGap = idleDur > GAP_THRESHOLD; |
| const compLen = isGap ? GAP_COMPRESSED : idleDur; |
| segs.push({ kind: isGap ? "gap" : "idle", realStart: from, realLen: idleDur, compStart: comp, compLen, turn }); |
| comp += compLen; |
| }; |
| for (const turn of turns) { |
| const dur = turn.end - turn.start; |
| if (dur <= 0) continue; |
| if (turn.timed && turn.times.length === turn.seq.length) { |
| |
| |
| |
| |
| |
| |
| let prev = turn.start; |
| turn.seq.forEach((cat, i) => { |
| const t = turn.times[i]; |
| const len = t - prev; |
| if (len > 0) { |
| segs.push({ kind: "active", cat, realStart: prev, realLen: len, compStart: comp, compLen: len, turn }); |
| comp += len; |
| } else { |
| |
| |
| |
| |
| |
| |
| segs.push({ kind: "active", cat, realStart: prev, realLen: 0, compStart: comp, compLen: ZERO_EVENT_COMP_SECS, turn }); |
| comp += ZERO_EVENT_COMP_SECS; |
| } |
| prev = t; |
| }); |
| pushIdleTail(turn, prev); |
| } else { |
| |
| |
| |
| const activeDur = Math.min(dur, turn.seq.length * PER_EVENT_SECS); |
| if (activeDur > 0) { |
| segs.push({ kind: "active", realStart: turn.start, realLen: activeDur, compStart: comp, compLen: activeDur, turn }); |
| comp += activeDur; |
| } |
| pushIdleTail(turn, turn.start + activeDur); |
| } |
| } |
| return { segs, compTotal: comp }; |
| }, [turns]); |
|
|
| const { segs, compTotal } = timeline; |
|
|
| const [res, setRes] = useState(5); |
| const resolution = res; |
| const [hover, setHover] = useState<number | null>(null); |
|
|
| |
| const compToReal = (c: number): number => { |
| if (!segs.length) return t0; |
| for (const s of segs) { |
| if (c <= s.compStart + s.compLen) { |
| const frac = s.compLen > 0 ? (c - s.compStart) / s.compLen : 0; |
| return s.realStart + frac * s.realLen; |
| } |
| } |
| const last = segs[segs.length - 1]; |
| return last.realStart + last.realLen; |
| }; |
|
|
| const buckets = useMemo<Bucket[]>(() => { |
| if (compTotal <= 0) return []; |
| const n = Math.min(Math.ceil(compTotal / resolution), MAX_BUCKETS); |
| const out: Bucket[] = Array.from({ length: n }, (_, k) => ({ |
| realStart: compToReal(k * resolution), |
| realEnd: compToReal((k + 1) * resolution), |
| cats: { reasoning: 0, generating: 0, tool: 0, idle: 0 }, |
| taskSecs: {}, |
| total: 0, |
| gap: 0, |
| isGap: false, |
| topTask: "", |
| })); |
| |
| |
| |
| const place = (cat: Cat | "gap", task: string, from: number, to: number) => { |
| if (to <= from) return; |
| const kStart = Math.max(0, Math.floor(from / resolution)); |
| const kEnd = Math.min(n - 1, Math.floor(to / resolution)); |
| for (let k = kStart; k <= kEnd; k++) { |
| const bs = k * resolution; |
| const ov = Math.min(to, bs + resolution) - Math.max(from, bs); |
| if (ov <= 0) continue; |
| if (cat === "gap") { |
| out[k].gap += ov; |
| } else { |
| out[k].cats[cat] += ov; |
| out[k].total += ov; |
| out[k].taskSecs[task] = (out[k].taskSecs[task] || 0) + ov; |
| } |
| } |
| }; |
| for (const s of segs) { |
| if (s.kind === "active") { |
| if (s.cat) { |
| |
| place(s.cat, s.turn.task, s.compStart, s.compStart + s.compLen); |
| } else { |
| |
| const seq = s.turn.seq; |
| const slot = s.compLen / seq.length; |
| seq.forEach((cat, i) => place(cat, s.turn.task, s.compStart + i * slot, s.compStart + (i + 1) * slot)); |
| } |
| } else if (s.kind === "idle") { |
| place("idle", s.turn.task, s.compStart, s.compStart + s.compLen); |
| } else { |
| place("gap", s.turn.task, s.compStart, s.compStart + s.compLen); |
| } |
| } |
| for (const b of out) { |
| b.isGap = b.gap > b.total; |
| if (b.isGap) { b.topTask = "⋯ paused"; continue; } |
| let best = "", bestS = -1; |
| for (const [tk, s] of Object.entries(b.taskSecs)) if (s > bestS) { bestS = s; best = tk; } |
| b.topTask = best; |
| } |
| return out; |
| }, [segs, compTotal, resolution]); |
|
|
| |
| const bands = useMemo(() => { |
| const out: { task: string; span: number }[] = []; |
| for (const b of buckets) { |
| const last = out[out.length - 1]; |
| if (last && last.task === b.topTask) last.span += 1; |
| else out.push({ task: b.topTask || "—", span: 1 }); |
| } |
| return out; |
| }, [buckets]); |
|
|
| |
| |
| |
| const barEls = useMemo(() => buckets.map((b, i) => ( |
| b.isGap ? ( |
| |
| <div |
| key={i} |
| className="ov-bar" |
| onMouseEnter={() => setHover(i)} |
| title="paused (downtime collapsed)" |
| style={{ |
| flex: `0 0 ${BAR_PX * 2}px`, minWidth: BAR_PX * 2, height: "100%", boxSizing: "border-box", |
| background: "repeating-linear-gradient(135deg, transparent 0 2px, rgba(255,255,255,0.06) 2px 4px)", |
| borderRight: "1px solid var(--bg2)", |
| }} |
| /> |
| ) : ( |
| <div |
| key={i} |
| className="ov-bar" |
| onMouseEnter={() => setHover(i)} |
| style={{ |
| flex: `1 0 ${BAR_PX}px`, minWidth: BAR_PX, height: "100%", boxSizing: "border-box", |
| borderRight: "1px solid var(--bg2)", |
| display: "flex", flexDirection: "column", justifyContent: "flex-end", |
| }} |
| > |
| {CAT_ORDER.map((c) => { |
| const frac = b.total > 0 ? b.cats[c] / b.total : 0; |
| if (frac <= 0) return null; |
| return <div key={c} style={{ height: `${frac * 100}%`, background: CAT_META[c].color }} />; |
| })} |
| </div> |
| ) |
| )), [buckets]); |
|
|
| const bandEls = useMemo(() => bands.map((b, i) => ( |
| <div key={i} title={b.task} style={{ |
| flex: `${b.span} 0 ${b.span * BAR_PX}px`, minWidth: 0, boxSizing: "border-box", |
| padding: "2px 4px", borderRadius: 3, background: "rgba(255,255,255,0.05)", color: "var(--text)", |
| fontSize: 9.5, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis", |
| borderLeft: "2px solid var(--accent2)", |
| }}>{b.task}</div> |
| )), [bands]); |
|
|
| if (compTotal <= 0 || !buckets.length) { |
| return <div style={{ padding: 24, fontSize: 12, color: "var(--text-dim)" }}> |
| Not enough timed activity to chart yet. |
| </div>; |
| } |
|
|
| const totals: Record<Cat, number> = { reasoning: 0, generating: 0, tool: 0, idle: 0 }; |
| for (const b of buckets) for (const c of CAT_ORDER) totals[c] += b.cats[c]; |
| |
| const grand = ACTIVE_CATS.reduce((s, c) => s + totals[c], 0) || 1; |
|
|
| const fmt = (sec: number) => { |
| const d = new Date(sec * 1000); |
| if (resolution >= 86400) { |
| return d.toLocaleDateString([], { month: "short", day: "numeric" }); |
| } |
| if (resolution >= 60) { |
| return d.toLocaleString([], { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit" }); |
| } |
| return d.toLocaleString([], { hour: "2-digit", minute: "2-digit", second: "2-digit" }); |
| }; |
| const spanFmt = (s: number) => s >= 86400 ? `${(s / 86400).toFixed(1)} d` : s >= 3600 ? `${(s / 3600).toFixed(1)} h` : s >= 60 ? `${Math.max(1, Math.round(s / 60))} min` : `${Math.max(1, Math.round(s))}s`; |
| const spanLabel = spanFmt(realSpan); |
|
|
| const H = 150; |
| const hb = hover != null ? buckets[hover] : null; |
|
|
| return ( |
| <div style={{ padding: "10px 14px", fontSize: 11 }}> |
| {/* Resolution selector + legend */} |
| <div style={{ display: "flex", alignItems: "center", gap: 10, flexWrap: "wrap", marginBottom: 8 }}> |
| <span style={{ color: "var(--text-dim)" }}>Resolution</span> |
| <div style={{ display: "flex", gap: 4 }}> |
| {RES.map((r) => { |
| const tooFine = compTotal / r.s > MAX_BUCKETS; |
| const active = r.s === resolution; |
| return ( |
| <button |
| key={r.label} |
| disabled={tooFine} |
| onClick={() => setRes(r.s)} |
| style={{ |
| fontSize: 10, padding: "2px 8px", borderRadius: 5, |
| cursor: tooFine ? "not-allowed" : "pointer", opacity: tooFine ? 0.35 : 1, |
| background: active ? "var(--accent2)" : "transparent", |
| color: active ? "#fff" : "var(--text-dim)", |
| border: `1px solid ${active ? "var(--accent2)" : "var(--card-border)"}`, |
| }} |
| >{r.label}</button> |
| ); |
| })} |
| </div> |
| <div style={{ marginLeft: "auto", display: "flex", gap: 10 }}> |
| {CAT_ORDER.map((c) => ( |
| <span key={c} style={{ display: "flex", alignItems: "center", gap: 4, color: "var(--text-dim)" }}> |
| <span style={{ width: 9, height: 9, borderRadius: 2, background: CAT_META[c].color }} /> |
| {CAT_META[c].label}{c === "idle" ? "" : ` ${Math.round((totals[c] / grand) * 100)}%`} |
| </span> |
| ))} |
| </div> |
| </div> |
| |
| {/* Scrollable chart — task bands + stacked bars share one scroll area so |
| they stay column-aligned. Bars grow to fill the panel when few; once |
| there are more than fit, they keep BAR_PX and the strip scrolls. */} |
| <style>{`.ov-bar:hover{outline:1px solid var(--text);outline-offset:-1px;}`}</style> |
| <div style={{ overflowX: "auto", overflowY: "hidden" }} onMouseLeave={() => setHover(null)}> |
| <div style={{ minWidth: "100%", display: "flex", flexDirection: "column" }}> |
| <div style={{ display: "flex", marginBottom: 3 }}>{bandEls}</div> |
| <div style={{ display: "flex", height: H, alignItems: "flex-end" }}>{barEls}</div> |
| </div> |
| </div> |
| |
| {/* Time axis */} |
| <div style={{ display: "flex", justifyContent: "space-between", color: "var(--text-dim)", marginTop: 4, fontSize: 9.5 }}> |
| <span>{fmt(buckets[0].realStart)}</span> |
| <span>{spanLabel} span · {buckets.length} × {RES.find((r) => r.s === resolution)?.label}</span> |
| <span>{fmt(buckets[buckets.length - 1].realEnd)}</span> |
| </div> |
| |
| {/* Hover readout */} |
| <div style={{ |
| marginTop: 8, padding: "6px 8px", borderRadius: 5, minHeight: 34, |
| background: "var(--bg)", border: "1px solid var(--card-border)", color: "var(--text-dim)", |
| }}> |
| {hb ? ( |
| <span> |
| <strong style={{ color: "var(--text)" }}>{fmt(hb.realStart)} → {fmt(hb.realEnd)}</strong> |
| {hb.isGap ? ( |
| <>{" · "}paused — {spanFmt(hb.realEnd - hb.realStart)} of downtime collapsed</> |
| ) : ( |
| <> |
| {" · "}{hb.topTask || "—"}{" · "} |
| {ACTIVE_CATS.map((c) => { |
| const act = hb.cats.reasoning + hb.cats.generating + hb.cats.tool; |
| return `${CAT_META[c].label.split(" ")[0]} ${act > 0 ? Math.round((hb.cats[c] / act) * 100) : 0}%`; |
| }).join(" / ")} |
| </> |
| )} |
| </span> |
| ) : ( |
| <span>Per-turn estimate (Hermes batches event times). Hover a bar for its range, task, and breakdown.</span> |
| )} |
| </div> |
| </div> |
| ); |
| } |
|
|