fix(observability): a second small chart for everything but main work (T3-7)

Main work is often 97% of the tokens, so summaries, the auto-approver and
session titles were hairlines. Under the main chart, the same days without
main work on their own scale, and a line saying a bar shows each part's
tokens when pointed at.
This commit is contained in:
Devika Verma
2026-11-05 16:43:26 +05:30
parent 07f394db99
commit dfc2d40a51
6 changed files with 73 additions and 19 deletions
+43 -19
View File
@@ -23,6 +23,7 @@ import {
comparable,
hhmm,
localDays,
overheadByDay,
previousDays,
purposeByDay,
SERIES,
@@ -168,6 +169,7 @@ export function UsageBody({ data, period, t, onOpenTurn }: { data: UsageData; pe
const canCompare = comparable(data.total.kept_from, prevStart);
const pct = canCompare && prev ? changePct(all?.total ?? 0, prev.total) : null;
const chart = purposeByDay(days, data.dayPurpose.groups);
const overhead = overheadByDay(chart);
const names = data.total.names || data.coworkers.names || {};
const turns = all?.turns ?? 0;
const checked = all?.checked ?? 0;
@@ -194,30 +196,22 @@ export function UsageBody({ data, period, t, onOpenTurn }: { data: UsageData; pe
<div className="p3-two">
<div className={CARD + " p3-card"}>
<b className="p3-h">{t("p3.usage.per_day")}</b>
<div className="p3-bars" data-testid="usage-bars">
{chart.rows.map((r) => (
<div key={r.label} className="p3-bar" title={`${r.label}: ${formatTokens(r.total)}`}>
{SERIES.map((s) =>
r.values[s] ? (
<i key={s} className={`p3-s-${s}`} style={{ height: `${Math.max(1, (r.values[s] / chart.max) * 100)}%` }}
title={`${t(`p3.series.${s}`)}: ${r.values[s].toLocaleString()}`} />
) : null,
)}
</div>
))}
</div>
{days.length > 1 && (
<div className="p3-xl">
{days.map(([label, start]) => (
<span key={label}>{days.length <= 7 ? new Date(start).toLocaleDateString(undefined, { weekday: "short", day: "2-digit" }) : new Date(start).getDate()}</span>
))}
</div>
)}
<Bars rows={chart.rows} max={chart.max} series={SERIES} t={t} testid="usage-bars" />
<DayLabels days={days} />
<div className="p3-legend" data-testid="usage-legend">
{SERIES.filter((s) => chart.legend[s] > 0).map((s) => (
<span key={s}><i className={`p3-s-${s}`} />{t(`p3.series.${s}`)} {formatTokens(chart.legend[s])}</span>
))}
</div>
{overhead && (
<div className="mt-3" data-testid="usage-overhead">
<b className="p3-h">{t("p3.usage.overhead")}</b>
<div className="text-meta text-faint">{t("p3.usage.overhead_sub", { tokens: formatTokens(overhead.max) })}</div>
<Bars rows={overhead.rows} max={overhead.max} series={overhead.series} t={t} small testid="usage-overhead-bars" />
<DayLabels days={days} />
</div>
)}
<div className="text-meta text-faint mt-2">{t("p3.usage.point_hint")}</div>
</div>
<div className={CARD + " p3-card"}>
<b className="p3-h">{t("p3.usage.by_coworker")}</b>
@@ -294,6 +288,36 @@ export function UsageBody({ data, period, t, onOpenTurn }: { data: UsageData; pe
);
}
type ChartRow = { label: string; values: Record<string, number>; total: number };
function Bars({ rows, max, series, t, small, testid }: { rows: ChartRow[]; max: number; series: readonly string[]; t: T; small?: boolean; testid: string }) {
return (
<div className={"p3-bars" + (small ? " small" : "")} data-testid={testid}>
{rows.map((r) => (
<div key={r.label} className="p3-bar" title={`${r.label}: ${formatTokens(r.total)}`}>
{series.map((s) =>
r.values[s] ? (
<i key={s} className={`p3-s-${s}`} style={{ height: `${Math.max(1, (r.values[s] / max) * 100)}%` }}
title={`${t(`p3.series.${s}`)}: ${r.values[s].toLocaleString()}`} />
) : null,
)}
</div>
))}
</div>
);
}
function DayLabels({ days }: { days: [string, number, number][] }) {
if (days.length <= 1) return null;
return (
<div className="p3-xl">
{days.map(([label, start]) => (
<span key={label}>{days.length <= 7 ? new Date(start).toLocaleDateString(undefined, { weekday: "short", day: "2-digit" }) : new Date(start).getDate()}</span>
))}
</div>
);
}
function Tile({ label, value, sub, testid }: { label: string; value: string; sub: string; testid?: string }) {
return (
<div className="p3-kpi" data-testid={testid}>
+3
View File
@@ -3935,6 +3935,9 @@
"not_checked_other": "{{count}} not checked yet",
"median_sub": "waiting for you not counted",
"per_day": "Tokens per day, by purpose",
"overhead": "Everything but main work",
"overhead_sub": "On its own scale: the tallest day is {{tokens}} tokens.",
"point_hint": "Point at a bar to see each part's tokens.",
"by_coworker": "By coworker",
"coworker": "Coworker",
"automation": "(automation)",
+3
View File
@@ -3876,6 +3876,9 @@
"not_checked_other": "{{count}} 个尚未检查",
"median_sub": "不计等你的时间",
"per_day": "每天的令牌,按用途",
"overhead": "主要工作以外的全部",
"overhead_sub": "单独的刻度:最高的一天为 {{tokens}} 令牌。",
"point_hint": "指向柱形可查看每一部分的令牌数。",
"by_coworker": "按同事",
"coworker": "同事",
"automation": "(自动化)",
+13
View File
@@ -7,6 +7,7 @@ import {
footerParts,
healthTone,
localDays,
overheadByDay,
previousDays,
purposeByDay,
seriesOf,
@@ -69,6 +70,18 @@ describe("tokens per day, by purpose", () => {
expect(out.rows[1].total).toBe(7);
expect(out.legend).toEqual({ main: 120, summary: 0, auto_approver: 7, title: 3, other: 0 });
});
it("the second chart leaves main work out and has its own scale (T3-7)", () => {
const days: [string, number, number][] = [["d1", 0, 10], ["d2", 10, 20]];
const g = (day: string, purpose: string, total: number) => ({ key: { day, purpose }, total }) as any;
const chart = purposeByDay(days, [g("d1", "main", 17_000_000), g("d1", "summary", 36_000), g("d2", "auto_approver", 457_000), g("d2", "title", 7_700)]);
const o = overheadByDay(chart)!;
expect(o.series).toEqual(["summary", "auto_approver", "title", "other"]);
expect(o.rows.map((r) => r.total)).toEqual([36_000, 464_700]);
expect(o.max).toBe(464_700); // not the 17M of main work
expect(o.rows[0].values.main).toBe(0);
// only main work: no second chart
expect(overheadByDay(purposeByDay(days, [g("d1", "main", 5)]))).toBeNull();
});
});
describe("the Auto-approve strip", () => {
+10
View File
@@ -105,6 +105,16 @@ export function purposeByDay(days: DayBoundary[], groups: RollupGroup[]) {
return { rows, legend, max: Math.max(1, ...rows.map((r) => r.total)) };
}
/** T3-7: the same days without main work, on their own scale, so summaries, the
* auto-approver and titles get a readable size (main work is often 95% or more). Null when
* the period has none of them. */
export function overheadByDay(chart: ReturnType<typeof purposeByDay>) {
const series = SERIES.filter((s) => s !== "main");
const rows = chart.rows.map((r) => ({ ...r, values: { ...r.values, main: 0 }, total: r.total - r.values.main }));
const max = Math.max(0, ...rows.map((r) => r.total));
return max > 0 ? { rows, max, series } : null;
}
// -- the Auto-approve strip (§7.3, M3-R5) -----------------------------------------------------
/** Under 10 checks a part is a count ("allowed 1"), from 10 a share ("allowed 72%"): "allowed
* 100%" of one check says nothing. */
+1
View File
@@ -2620,6 +2620,7 @@ html[data-theme="dark"] { --bar-model:#4c8dff; --bar-tool:#3fb8ad; --bar-wait:#3
.p3-card { padding:12px 14px; min-width:0; }
.p3-h { font-weight:500; font-size:var(--fs-ui); color:var(--ink); }
.p3-bars { display:flex; align-items:flex-end; gap:3px; height:120px; margin-top:10px; border-bottom:1px solid var(--line); }
.p3-bars.small { height:56px; margin-top:6px; } /* T3-7 */
.p3-bar { flex:1; height:100%; display:flex; flex-direction:column-reverse; min-width:2px; }
.p3-bar i { display:block; width:100%; }
.p3-bar i:last-child { border-radius:3px 3px 0 0; }