@linxiraos/pi-stats 1.4.11 → 1.4.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/CHANGELOG.md +7 -4
  2. package/THIRD-PARTY-NOTICES.txt +4 -5
  3. package/dist/client/index.css +1 -1
  4. package/dist/client/index.js +102 -99
  5. package/dist/types/client/api.d.ts +4 -1
  6. package/dist/types/client/app/nav.d.ts +1 -1
  7. package/dist/types/client/charts/Chart.d.ts +16 -8
  8. package/dist/types/client/charts/DotWhisker.d.ts +50 -0
  9. package/dist/types/client/charts/Sparkline.d.ts +1 -1
  10. package/dist/types/client/charts/Waterfall.d.ts +26 -0
  11. package/dist/types/client/charts/index.d.ts +2 -0
  12. package/dist/types/client/charts/types.d.ts +8 -2
  13. package/dist/types/client/i18n/messages/en.d.ts +2 -0
  14. package/dist/types/client/i18n/messages/nav.d.ts +2 -0
  15. package/dist/types/client/i18n/messages/zh-CN.d.ts +1 -1
  16. package/dist/types/client/routes/SpotlightRoute.d.ts +7 -0
  17. package/dist/types/client/routes/index.d.ts +1 -0
  18. package/dist/types/client/spotlight/Compounding.d.ts +8 -0
  19. package/dist/types/client/spotlight/Coverage.d.ts +6 -0
  20. package/dist/types/client/spotlight/Economics.d.ts +10 -0
  21. package/dist/types/client/spotlight/FetchBack.d.ts +9 -0
  22. package/dist/types/client/spotlight/Hero.d.ts +12 -0
  23. package/dist/types/client/spotlight/Overhead.d.ts +6 -0
  24. package/dist/types/client/spotlight/Ranking.d.ts +6 -0
  25. package/dist/types/client/spotlight/ResultsFeed.d.ts +6 -0
  26. package/dist/types/client/spotlight/Savings.d.ts +10 -0
  27. package/dist/types/client/spotlight/ShareTrend.d.ts +8 -0
  28. package/dist/types/client/spotlight/Steps.d.ts +6 -0
  29. package/dist/types/client/spotlight/TimeCost.d.ts +6 -0
  30. package/dist/types/client/spotlight/chips.d.ts +16 -0
  31. package/dist/types/client/spotlight/meta.d.ts +64 -0
  32. package/dist/types/db.d.ts +26 -0
  33. package/dist/types/index.d.ts +2 -1
  34. package/dist/types/parser.d.ts +4 -0
  35. package/dist/types/rollup.d.ts +22 -1
  36. package/dist/types/shared-types.d.ts +419 -0
  37. package/dist/types/spotlight-aggregator.d.ts +189 -0
  38. package/dist/types/spotlight-bootstrap.d.ts +32 -0
  39. package/dist/types/spotlight-ingest.d.ts +258 -0
  40. package/dist/types/spotlight-measures.d.ts +51 -0
  41. package/package.json +5 -5
  42. package/src/aggregator.ts +3 -1
  43. package/src/client/App.tsx +3 -0
  44. package/src/client/api.ts +21 -0
  45. package/src/client/app/nav.ts +4 -1
  46. package/src/client/charts/Chart.tsx +105 -22
  47. package/src/client/charts/DotWhisker.tsx +149 -0
  48. package/src/client/charts/Sparkline.tsx +3 -2
  49. package/src/client/charts/Waterfall.tsx +86 -0
  50. package/src/client/charts/index.ts +2 -0
  51. package/src/client/charts/types.ts +9 -2
  52. package/src/client/i18n/messages/nav.ts +2 -0
  53. package/src/client/react-css.d.ts +1 -1
  54. package/src/client/routes/SpotlightRoute.tsx +139 -0
  55. package/src/client/routes/index.ts +1 -0
  56. package/src/client/spotlight/Compounding.tsx +222 -0
  57. package/src/client/spotlight/Coverage.tsx +96 -0
  58. package/src/client/spotlight/Economics.tsx +167 -0
  59. package/src/client/spotlight/FetchBack.tsx +222 -0
  60. package/src/client/spotlight/Hero.tsx +177 -0
  61. package/src/client/spotlight/Overhead.tsx +135 -0
  62. package/src/client/spotlight/Ranking.tsx +248 -0
  63. package/src/client/spotlight/ResultsFeed.tsx +354 -0
  64. package/src/client/spotlight/Savings.tsx +355 -0
  65. package/src/client/spotlight/ShareTrend.tsx +103 -0
  66. package/src/client/spotlight/Steps.tsx +225 -0
  67. package/src/client/spotlight/TimeCost.tsx +64 -0
  68. package/src/client/spotlight/chips.tsx +63 -0
  69. package/src/client/spotlight/meta.ts +206 -0
  70. package/src/client/spotlight/spotlight.css +1387 -0
  71. package/src/client/styles.css +289 -1
  72. package/src/db.ts +486 -8
  73. package/src/index.ts +10 -1
  74. package/src/live.ts +21 -9
  75. package/src/parser.ts +36 -7
  76. package/src/rollup.ts +203 -10
  77. package/src/server.ts +42 -0
  78. package/src/shared-types.ts +453 -0
  79. package/src/spotlight-aggregator.ts +1738 -0
  80. package/src/spotlight-bootstrap.ts +110 -0
  81. package/src/spotlight-ingest.ts +981 -0
  82. package/src/spotlight-measures.ts +667 -0
@@ -1,7 +1,8 @@
1
1
  /**
2
- * Core SVG chart: categorical x slots, stacked or grouped bars, stacked areas
3
- * and lines, an optional right axis, a hover cursor and a floating tooltip.
4
- * Marks are crisp and unsmoothed — square bars, straight segments.
2
+ * Core SVG chart: categorical x slots, stacked or grouped bars, stacked areas,
3
+ * lines and dots, an optional right axis, a hover cursor and a floating tooltip.
4
+ * Marks are crisp and unsmoothed — square bars, straight segments. Negative
5
+ * values extend the axis below zero (bars hang down from the baseline).
5
6
  *
6
7
  * `TimeChart` wraps this for bucketed time series; use `Chart` directly for
7
8
  * categorical axes (hour of day, model names).
@@ -9,7 +10,7 @@
9
10
 
10
11
  import { Fragment, type ReactNode, useId, useMemo, useState } from "react";
11
12
  import { formatCompact } from "../data/formatters";
12
- import type { ChartKind, ChartSeries, ReferenceLine } from "./types";
13
+ import type { ChartKind, ChartSeries, ReferenceLine, SlotMarker } from "./types";
13
14
  import { useWidth } from "./useWidth";
14
15
 
15
16
  export interface ChartProps {
@@ -39,6 +40,8 @@ export interface ChartProps {
39
40
  /** Show a total row in the tooltip. Default: stacked with 2+ series. */
40
41
  showTotal?: boolean;
41
42
  references?: readonly ReferenceLine[];
43
+ /** Vertical guides at x slots, labelled along the top edge. */
44
+ markers?: readonly SlotMarker[];
42
45
  onSelect?: (index: number) => void;
43
46
  /** Shown over empty axes when every visible value is zero. */
44
47
  emptyLabel?: ReactNode;
@@ -67,6 +70,7 @@ export function Chart({
67
70
  hidden,
68
71
  showTotal,
69
72
  references,
73
+ markers,
70
74
  onSelect,
71
75
  emptyLabel = "No data in this range",
72
76
  tooltipExtra,
@@ -80,31 +84,58 @@ export function Chart({
80
84
  const layout = useMemo(() => {
81
85
  const left = visible.filter(s => s.axis !== "right");
82
86
  const right = visible.filter(s => s.axis === "right");
83
- const stackable = (s: ChartSeries) => stacked && (s.kind ?? kind) !== "line";
87
+ const stackable = (s: ChartSeries) => {
88
+ const mark = s.kind ?? kind;
89
+ return stacked && (mark === "bars" || mark === "area");
90
+ };
84
91
 
85
92
  // Per-slot stack bases for stacked left series, in series order (first = bottom).
93
+ // Positive and negative values stack away from zero separately.
86
94
  const bases = new Map<string, number[]>();
87
95
  const tops = Array.from({ length: slots }, () => 0);
96
+ const bottoms = Array.from({ length: slots }, () => 0);
88
97
  let leftMax = 0;
98
+ let leftMin = 0;
89
99
  for (const s of left) {
90
100
  if (stackable(s)) {
91
- const base = tops.slice();
101
+ const base = Array.from({ length: slots }, () => 0);
102
+ for (let i = 0; i < slots; i++) {
103
+ const v = s.values[i] ?? 0;
104
+ if (v < 0) {
105
+ base[i] = bottoms[i];
106
+ bottoms[i] += v;
107
+ } else {
108
+ base[i] = tops[i];
109
+ tops[i] += v;
110
+ }
111
+ }
92
112
  bases.set(s.key, base);
93
- for (let i = 0; i < slots; i++) tops[i] += s.values[i] ?? 0;
94
113
  } else {
95
- for (let i = 0; i < slots; i++) leftMax = Math.max(leftMax, s.values[i] ?? 0);
114
+ for (let i = 0; i < slots; i++) {
115
+ leftMax = Math.max(leftMax, s.values[i] ?? 0);
116
+ leftMin = Math.min(leftMin, s.values[i] ?? 0);
117
+ }
96
118
  }
97
119
  }
98
- for (const top of tops) leftMax = Math.max(leftMax, top);
120
+ for (let i = 0; i < slots; i++) {
121
+ leftMax = Math.max(leftMax, tops[i]);
122
+ leftMin = Math.min(leftMin, bottoms[i]);
123
+ }
99
124
  let rightMax = 0;
100
- for (const s of right) for (let i = 0; i < slots; i++) rightMax = Math.max(rightMax, s.values[i] ?? 0);
125
+ let rightMin = 0;
126
+ for (const s of right) {
127
+ for (let i = 0; i < slots; i++) {
128
+ rightMax = Math.max(rightMax, s.values[i] ?? 0);
129
+ rightMin = Math.min(rightMin, s.values[i] ?? 0);
130
+ }
131
+ }
101
132
  for (const r of references ?? []) {
102
133
  if (r.axis === "right") rightMax = Math.max(rightMax, r.value);
103
134
  else leftMax = Math.max(leftMax, r.value);
104
135
  }
105
136
 
106
- const leftScale = niceScale(yMax ?? leftMax);
107
- const rightScale = right.length > 0 ? niceScale(yMaxRight ?? rightMax) : null;
137
+ const leftScale = niceScale(yMax ?? leftMax, leftMin);
138
+ const rightScale = right.length > 0 ? niceScale(yMaxRight ?? rightMax, rightMin) : null;
108
139
  const empty = visible.every(s => s.values.every(v => !v));
109
140
  return { left, right, bases, stackable, leftScale, rightScale, empty };
110
141
  }, [visible, slots, stacked, kind, yMax, yMaxRight, references]);
@@ -116,8 +147,10 @@ export function Chart({
116
147
  const plotW = Math.max(0, width - padLeft - padRight);
117
148
  const plotH = height - PAD_TOP - PAD_BOTTOM;
118
149
  const slotW = slots > 0 ? plotW / slots : 0;
119
- const yLeft = (v: number) => PAD_TOP + plotH - (v / layout.leftScale.max) * plotH;
120
- const yRight = (v: number) => PAD_TOP + plotH - (v / (layout.rightScale?.max ?? 1)) * plotH;
150
+ const project = (scale: AxisScale, v: number) =>
151
+ PAD_TOP + plotH - ((v - scale.min) / (scale.max - scale.min)) * plotH;
152
+ const yLeft = (v: number) => project(layout.leftScale, v);
153
+ const yRight = (v: number) => (layout.rightScale ? project(layout.rightScale, v) : yLeft(v));
121
154
  const cx = (i: number) => padLeft + (i + 0.5) * slotW;
122
155
 
123
156
  const barSeries = visible.filter(s => (s.kind ?? kind) === "bars");
@@ -212,7 +245,12 @@ export function Chart({
212
245
  return (
213
246
  <g key={s.key}>
214
247
  <path d={`${line}${floor}Z`} fill={fill} fillOpacity={s.pattern ? 1 : 0.2} />
215
- <path className="chart-line" d={line} stroke={s.color} />
248
+ <path
249
+ className="chart-line"
250
+ d={line}
251
+ stroke={s.color}
252
+ strokeDasharray={s.dashed ? "4 3" : undefined}
253
+ />
216
254
  </g>
217
255
  );
218
256
  })}
@@ -280,6 +318,38 @@ export function Chart({
280
318
  );
281
319
  })}
282
320
 
321
+ {visible
322
+ .filter(s => (s.kind ?? kind) === "dots")
323
+ .map(s => {
324
+ const y = s.axis === "right" ? yRight : yLeft;
325
+ return (
326
+ <g key={s.key} className="chart-dots" fill={s.color}>
327
+ {s.values.map((v, i) =>
328
+ v === null || v === undefined ? null : (
329
+ <circle key={i} cx={cx(i)} cy={y(v)} r={hover === i ? 3.5 : 2.5} />
330
+ ),
331
+ )}
332
+ </g>
333
+ );
334
+ })}
335
+
336
+ {markers?.map(m =>
337
+ m.index >= 0 && m.index < slots ? (
338
+ <g key={`marker:${m.index}`} className="chart-marker">
339
+ <line x1={cx(m.index)} x2={cx(m.index)} y1={PAD_TOP} y2={PAD_TOP + plotH} />
340
+ {m.label && (
341
+ <text
342
+ x={cx(m.index) + (cx(m.index) > padLeft + plotW / 2 ? -5 : 5)}
343
+ y={PAD_TOP + 9}
344
+ textAnchor={cx(m.index) > padLeft + plotW / 2 ? "end" : "start"}
345
+ >
346
+ {m.label}
347
+ </text>
348
+ )}
349
+ </g>
350
+ ) : null,
351
+ )}
352
+
283
353
  {references?.map(r => {
284
354
  const y = r.axis === "right" ? yRight(r.value) : yLeft(r.value);
285
355
  return (
@@ -351,16 +421,29 @@ export function Chart({
351
421
  );
352
422
  }
353
423
 
354
- /** Round an axis maximum up to 1/2/2.5/5 × 10ⁿ and return evenly spaced ticks from 0. */
355
- export function niceScale(max: number): { max: number; ticks: number[] } {
356
- if (!(max > 0)) return { max: 1, ticks: [0] };
357
- const rough = max / Y_TICKS;
424
+ export interface AxisScale {
425
+ min: number;
426
+ max: number;
427
+ ticks: number[];
428
+ }
429
+
430
+ /**
431
+ * Round an axis span out to 1/2/2.5/5 × 10ⁿ steps and return evenly spaced
432
+ * ticks. The span always includes zero; `min` (default 0) extends it below.
433
+ */
434
+ export function niceScale(max: number, min = 0): AxisScale {
435
+ const hi = Math.max(0, max);
436
+ const lo = Math.min(0, min);
437
+ if (!(hi - lo > 0)) return { min: 0, max: 1, ticks: [0] };
438
+ const rough = (hi - lo) / Y_TICKS;
358
439
  const magnitude = 10 ** Math.floor(Math.log10(rough));
359
440
  const step = [1, 2, 2.5, 5, 10].map(m => m * magnitude).find(s => s >= rough) ?? 10 * magnitude;
360
- const top = Math.ceil(max / step) * step;
441
+ const top = Math.ceil(hi / step) * step;
442
+ const bottom = Math.floor(lo / step) * step;
361
443
  const ticks: number[] = [];
362
- for (let t = 0; t <= top + step / 2; t += step) ticks.push(Number(t.toPrecision(12)));
363
- return { max: top, ticks };
444
+ for (let t = bottom; t <= top + step / 2; t += step) ticks.push(Number(t.toPrecision(12)));
445
+ // An all-negative axis still needs a span above its lowest tick.
446
+ return { min: bottom, max: top > bottom ? top : bottom + step, ticks };
364
447
  }
365
448
 
366
449
  function labelWidth(labels: readonly string[]): number {
@@ -0,0 +1,149 @@
1
+ import type { ReactNode } from "react";
2
+ import { niceScale } from "./Chart";
3
+
4
+ /** One estimate on a row: a point and its interval. */
5
+ export interface DotWhiskerMark {
6
+ key: string;
7
+ value: number | null;
8
+ lo: number | null;
9
+ hi: number | null;
10
+ color: string;
11
+ /** `dot` (default) filled; `ring` hollow (a counterfactual); `diamond` a reference level; `square` a variant. */
12
+ shape?: "dot" | "ring" | "diamond" | "square";
13
+ /** Vertical offset from the row's center in px, to keep several marks of one row apart. */
14
+ offset?: number;
15
+ /** Hover text naming the mark. */
16
+ title?: string;
17
+ }
18
+
19
+ export interface DotWhiskerRow {
20
+ key: string;
21
+ label: ReactNode;
22
+ marks: readonly DotWhiskerMark[];
23
+ /** Right-column figure; defaults to the first mark's value. */
24
+ figure?: ReactNode;
25
+ /** Small print under the figure; defaults to the first mark's interval. */
26
+ detail?: ReactNode;
27
+ }
28
+
29
+ export interface DotWhiskerGuide {
30
+ key: string;
31
+ value: number;
32
+ color: string;
33
+ }
34
+
35
+ /** A shaded span through every row (e.g. a reference's interval). */
36
+ export interface DotWhiskerBand {
37
+ lo: number;
38
+ hi: number;
39
+ color: string;
40
+ }
41
+
42
+ export interface DotWhiskerProps {
43
+ rows: readonly DotWhiskerRow[];
44
+ /** Vertical guides through every row (e.g. chance levels). */
45
+ guides?: readonly DotWhiskerGuide[];
46
+ band?: DotWhiskerBand;
47
+ format: (value: number) => string;
48
+ /** Axis tick formatter; defaults to `format`. */
49
+ formatTick?: (value: number) => string;
50
+ }
51
+
52
+ /**
53
+ * Point estimates with their intervals on one shared axis that always spans
54
+ * zero (and extends below it for signed values), one row per group. Layout is
55
+ * percentage-based, so it never measures the DOM.
56
+ */
57
+ export function DotWhisker({ rows, guides, band, format, formatTick }: DotWhiskerProps) {
58
+ let max = 0;
59
+ let min = 0;
60
+ const extend = (v: number | null) => {
61
+ if (v === null) return;
62
+ max = Math.max(max, v);
63
+ min = Math.min(min, v);
64
+ };
65
+ for (const row of rows) {
66
+ for (const mark of row.marks) {
67
+ extend(mark.value);
68
+ extend(mark.lo);
69
+ extend(mark.hi);
70
+ }
71
+ }
72
+ for (const guide of guides ?? []) extend(guide.value);
73
+ if (band) {
74
+ extend(band.lo);
75
+ extend(band.hi);
76
+ }
77
+ const scale = niceScale(max, min);
78
+ const span = scale.max - scale.min;
79
+ const at = (v: number) => ((Math.min(scale.max, Math.max(scale.min, v)) - scale.min) / span) * 100;
80
+ const pct = (v: number) => `${at(v)}%`;
81
+ const underlay = (
82
+ <>
83
+ {band && (
84
+ <span
85
+ className="dw-band"
86
+ style={{ left: pct(band.lo), width: `${at(band.hi) - at(band.lo)}%`, "--band": band.color }}
87
+ />
88
+ )}
89
+ {guides?.map(guide => (
90
+ <span key={guide.key} className="dw-guide" style={{ left: pct(guide.value), "--guide": guide.color }} />
91
+ ))}
92
+ </>
93
+ );
94
+
95
+ return (
96
+ <div className="dw">
97
+ {rows.map(row => {
98
+ const first = row.marks[0];
99
+ const interval =
100
+ first !== undefined && first.lo !== null && first.hi !== null
101
+ ? `${format(first.lo)}–${format(first.hi)}`
102
+ : "";
103
+ return (
104
+ <div key={row.key} className="dw-row">
105
+ <div className="dw-label">{row.label}</div>
106
+ <div className="dw-track">
107
+ {underlay}
108
+ {row.marks.map(mark => (
109
+ <span
110
+ key={mark.key}
111
+ className="dw-mark"
112
+ data-shape={mark.shape ?? "dot"}
113
+ style={{ "--dot": mark.color, "--dy": `${mark.offset ?? 0}px` }}
114
+ >
115
+ {mark.lo !== null && mark.hi !== null && (
116
+ <span
117
+ className="dw-whisker"
118
+ style={{ left: pct(mark.lo), width: `${at(mark.hi) - at(mark.lo)}%` }}
119
+ />
120
+ )}
121
+ {mark.value !== null && (
122
+ <span className="dw-dot" style={{ left: pct(mark.value) }} title={mark.title} />
123
+ )}
124
+ </span>
125
+ ))}
126
+ </div>
127
+ <div className="dw-value">
128
+ <span className="num">
129
+ {row.figure ?? (first?.value === null || first === undefined ? "–" : format(first.value))}
130
+ </span>
131
+ <span className="dw-detail">{row.detail ?? interval}</span>
132
+ </div>
133
+ </div>
134
+ );
135
+ })}
136
+ <div className="dw-row dw-axis" aria-hidden="true">
137
+ <div />
138
+ <div className="dw-track">
139
+ {scale.ticks.map(t => (
140
+ <span key={t} className="dw-tick" style={{ left: pct(t) }}>
141
+ {(formatTick ?? format)(t)}
142
+ </span>
143
+ ))}
144
+ </div>
145
+ <div />
146
+ </div>
147
+ </div>
148
+ );
149
+ }
@@ -10,18 +10,19 @@ export interface SparklineProps {
10
10
  area?: boolean;
11
11
  }
12
12
 
13
- /** Axis-free trend line for stat tiles and table cells. */
13
+ /** Axis-free trend line for stat tiles and table cells; negative values dip below the zero line. */
14
14
  export function Sparkline({ values, color = "var(--chart-primary)", height = 28, width, area = true }: SparklineProps) {
15
15
  const [ref, measured] = useWidth<HTMLDivElement>();
16
16
  const w = width ?? measured;
17
17
  const max = Math.max(0, ...values);
18
+ const min = Math.min(0, ...values);
18
19
  const n = values.length;
19
20
  let line = "";
20
21
  if (w > 0 && n > 0) {
21
22
  const step = n > 1 ? w / (n - 1) : 0;
22
23
  for (let i = 0; i < n; i++) {
23
24
  const x = n > 1 ? i * step : w / 2;
24
- const y = max > 0 ? height - 2 - (values[i] / max) * (height - 4) : height - 2;
25
+ const y = max > min ? height - 2 - ((values[i] - min) / (max - min)) * (height - 4) : height - 2;
25
26
  line += `${i === 0 ? "M" : "L"}${x.toFixed(1)},${y.toFixed(1)}`;
26
27
  }
27
28
  }
@@ -0,0 +1,86 @@
1
+ import type { ReactNode } from "react";
2
+
3
+ export interface WaterfallStep {
4
+ key: string;
5
+ label: ReactNode;
6
+ /** `total` bars start at zero and reset the running total; `delta` bars float from it. */
7
+ kind: "total" | "delta";
8
+ /** A total's level, or a delta's signed change. */
9
+ value: number;
10
+ /** Any CSS background (solid color or gradient). */
11
+ color: string;
12
+ /** Dim figure after the value (e.g. share of the first total). */
13
+ note?: ReactNode;
14
+ title?: string;
15
+ }
16
+
17
+ export interface WaterfallProps {
18
+ steps: readonly WaterfallStep[];
19
+ /** Formats magnitudes; deltas get their sign from the component. */
20
+ format: (value: number) => string;
21
+ }
22
+
23
+ interface PlacedStep extends WaterfallStep {
24
+ from: number;
25
+ to: number;
26
+ /** Running total before this step (the connector's position). */
27
+ before: number;
28
+ }
29
+
30
+ /**
31
+ * Horizontal bridge chart: a total, the changes that eat into it, and the
32
+ * total left at the end. Rows read top to bottom; dashed connectors carry the
33
+ * running total from one bar to the next. Pure layout (percentages), so it
34
+ * never measures the DOM and reflows with its column.
35
+ */
36
+ export function Waterfall({ steps, format }: WaterfallProps) {
37
+ let running = 0;
38
+ const placed: PlacedStep[] = steps.map(step => {
39
+ const before = running;
40
+ if (step.kind === "total") {
41
+ running = step.value;
42
+ return { ...step, from: 0, to: step.value, before };
43
+ }
44
+ running += step.value;
45
+ return { ...step, from: before, to: running, before };
46
+ });
47
+ let lo = 0;
48
+ let hi = 0;
49
+ for (const step of placed) {
50
+ lo = Math.min(lo, step.from, step.to);
51
+ hi = Math.max(hi, step.from, step.to);
52
+ }
53
+ const span = hi - lo || 1;
54
+ const pct = (v: number) => ((v - lo) / span) * 100;
55
+
56
+ return (
57
+ <ol className="waterfall">
58
+ {placed.map((step, i) => {
59
+ const left = pct(Math.min(step.from, step.to));
60
+ const width = Math.abs(pct(step.to) - pct(step.from));
61
+ const sign = step.kind === "delta" ? (step.value < 0 ? "−" : "+") : step.value < 0 ? "−" : "";
62
+ return (
63
+ <li key={step.key} className="waterfall-row" data-kind={step.kind} title={step.title}>
64
+ <span className="waterfall-label">{step.label}</span>
65
+ <span className="waterfall-track">
66
+ {lo < 0 && <span className="waterfall-zero" style={{ left: `${pct(0)}%` }} />}
67
+ {i > 0 && <span className="waterfall-link" style={{ left: `${pct(step.before)}%` }} />}
68
+ <span
69
+ className="waterfall-bar"
70
+ data-empty={step.value === 0}
71
+ style={{ left: `${left}%`, width: `${width}%`, "--bar": step.color }}
72
+ />
73
+ </span>
74
+ <span className="waterfall-value">
75
+ <span className="num">
76
+ {sign}
77
+ {format(Math.abs(step.value))}
78
+ </span>
79
+ {step.note !== undefined && <span className="waterfall-note">{step.note}</span>}
80
+ </span>
81
+ </li>
82
+ );
83
+ })}
84
+ </ol>
85
+ );
86
+ }
@@ -1,7 +1,9 @@
1
1
  export * from "./BarList";
2
2
  export * from "./Chart";
3
+ export * from "./DotWhisker";
3
4
  export * from "./Legend";
4
5
  export * from "./ShareBar";
5
6
  export * from "./Sparkline";
6
7
  export * from "./TimeChart";
7
8
  export * from "./types";
9
+ export * from "./Waterfall";
@@ -11,13 +11,20 @@ export interface ChartSeries {
11
11
  axis?: "left" | "right";
12
12
  /** Diagonal hatching instead of a solid fill (bars/areas). */
13
13
  pattern?: "hatch";
14
- /** Dashed stroke (lines). */
14
+ /** Dashed stroke (lines, area outlines). */
15
15
  dashed?: boolean;
16
16
  /** Leave this series out of the tooltip (e.g. a trend line that repeats a bar). */
17
17
  tooltip?: false;
18
18
  }
19
19
 
20
- export type ChartKind = "bars" | "area" | "line";
20
+ /** `dots` marks each value with a point and no connecting segment (raw samples under a trend line). */
21
+ export type ChartKind = "bars" | "area" | "line" | "dots";
22
+
23
+ /** Vertical guide at an x slot (e.g. an experiment start or a compaction). */
24
+ export interface SlotMarker {
25
+ index: number;
26
+ label?: string;
27
+ }
21
28
 
22
29
  /** Horizontal guide drawn across the plot (e.g. a 100% capacity line). */
23
30
  export interface ReferenceLine {
@@ -18,6 +18,7 @@ export const nav = {
18
18
  "nav.frustration": "Frustration",
19
19
  "nav.projects": "Projects",
20
20
  "nav.gain": "Gain",
21
+ "nav.spotlight": "Spotlight",
21
22
  "nav.dashboard": "zetacode stats",
22
23
  "nav.breadcrumb-stats": "stats",
23
24
  "nav.pages": "Pages",
@@ -38,6 +39,7 @@ export const nav = {
38
39
  "nav.frustration": "情绪压力",
39
40
  "nav.projects": "项目",
40
41
  "nav.gain": "收益",
42
+ "nav.spotlight": "亮点",
41
43
  "nav.dashboard": "zeta 统计",
42
44
  "nav.breadcrumb-stats": "统计",
43
45
  "nav.pages": "导航",
@@ -1,7 +1,7 @@
1
1
  import "react";
2
2
 
3
3
  declare module "react" {
4
- /** Custom properties (`--arm`, `--i` …) in `style` objects, which React passes through to the element. */
4
+ /** Custom properties (`--kind`, `--i` …) in `style` objects, which React passes through to the element. */
5
5
  interface CSSProperties {
6
6
  [property: `--${string}`]: string | number | undefined;
7
7
  }
@@ -0,0 +1,139 @@
1
+ import { ScanSearch } from "lucide-react";
2
+ import { useState } from "react";
3
+ import { getSpotlightDashboardStats } from "../api";
4
+ import { useQuery } from "../data/query";
5
+ import { rangeMeta } from "../data/range";
6
+ import { t } from "../i18n";
7
+ import { Compounding } from "../spotlight/Compounding";
8
+ import { Coverage } from "../spotlight/Coverage";
9
+ import { Economics } from "../spotlight/Economics";
10
+ import { FetchBack, FetchBackGaps } from "../spotlight/FetchBack";
11
+ import { SpotlightHero } from "../spotlight/Hero";
12
+ import { Overhead } from "../spotlight/Overhead";
13
+ import { Ranking } from "../spotlight/Ranking";
14
+ import { ResultsFeed } from "../spotlight/ResultsFeed";
15
+ import { SavingsOverTime, WhereSavingsWent } from "../spotlight/Savings";
16
+ import { ShareTrend } from "../spotlight/ShareTrend";
17
+ import { Steps } from "../spotlight/Steps";
18
+ import { TimeCost } from "../spotlight/TimeCost";
19
+ import type { SpotlightAgentScope, TimeRange } from "../types";
20
+ import { Card, ChartSkeleton, PageHeader, QueryView, Segmented } from "../ui";
21
+ import "../spotlight/spotlight.css";
22
+
23
+ export interface SpotlightRouteProps {
24
+ active: boolean;
25
+ range: TimeRange;
26
+ }
27
+
28
+ const AGENT_OPTIONS = [
29
+ { value: "all", label: "All", title: "Every experiment session, subagents and advisors included" },
30
+ { value: "main", label: "Main", title: "Sessions you drove: the main agent only" },
31
+ { value: "subagent", label: "Subagents", title: "Sessions the task tool spawned" },
32
+ ] as const;
33
+
34
+ /** Prose for the scope in empty states ("main-agent "). */
35
+ const AGENT_SCOPE_PROSE: Record<SpotlightAgentScope, string> = { all: "", main: "main-agent ", subagent: "subagent " };
36
+
37
+ /** The page reads as four questions, in order; each heading opens the cards that answer one. */
38
+ function SectionHeading({ id, title, children }: { id: string; title: string; children: string }) {
39
+ return (
40
+ <header className="spot-section rise" id={id}>
41
+ <h2 className="spot-section-title">{title}</h2>
42
+ <p className="spot-section-copy">{children}</p>
43
+ </header>
44
+ );
45
+ }
46
+
47
+ export function SpotlightRoute({ active, range }: SpotlightRouteProps) {
48
+ // Every experiment session shapes, subagents included; scoping keeps their very different workloads apart.
49
+ const [agent, setAgent] = useState<SpotlightAgentScope>("all");
50
+ const stats = useQuery(
51
+ ["spotlight", range, agent],
52
+ ({ signal }) => getSpotlightDashboardStats(range, agent, signal),
53
+ { enabled: active },
54
+ );
55
+ const meta = rangeMeta(range);
56
+ const experimentStart = stats.data?.experimentStart ?? null;
57
+
58
+ return (
59
+ <div className="page spot-page">
60
+ <PageHeader
61
+ title="Spotlight"
62
+ description={`What shaping tool output saved in ${t(meta.windowKey)} against what pi would have shown unshaped, after taking back what the agent fetched again because of a cut.`}
63
+ actions={<Segmented aria-label="Sessions" options={AGENT_OPTIONS} value={agent} onChange={setAgent} />}
64
+ />
65
+
66
+ <QueryView
67
+ query={stats}
68
+ skeleton={<ChartSkeleton height={220} />}
69
+ isEmpty={data => data.sessions === 0 && data.recent.length === 0}
70
+ empty={
71
+ <Card>
72
+ <div className="spot-empty">
73
+ <span className="spot-empty-icon" aria-hidden="true">
74
+ <ScanSearch size={20} />
75
+ </span>
76
+ <h2 className="spot-empty-title">
77
+ {experimentStart === null
78
+ ? "The Spotlight experiment hasn't run yet"
79
+ : `No ${AGENT_SCOPE_PROSE[agent]}experiment sessions in ${t(meta.windowKey)}`}
80
+ </h2>
81
+ <p className="spot-empty-hint">
82
+ {experimentStart === null ? (
83
+ <>
84
+ Turn it on with <code>spotlight.experiment.enabled</code> in your settings or{" "}
85
+ <code>PI_SPOTLIGHT_EXPERIMENT=1</code>. Every session then shapes tool output, a random
86
+ share of results is held out unshaped as the counterfactual, and this page tracks what the
87
+ cuts save as you work.
88
+ </>
89
+ ) : agent === "all" ? (
90
+ "Try a longer range, or start a session with the experiment enabled."
91
+ ) : (
92
+ "Try a longer range, or switch to All sessions."
93
+ )}
94
+ </p>
95
+ </div>
96
+ </Card>
97
+ }
98
+ >
99
+ {data => (
100
+ <>
101
+ <SpotlightHero stats={data} windowLabel={t(meta.windowKey)} stale={stats.stale} />
102
+
103
+ <SectionHeading id="spot-saved" title="What it saved">
104
+ Where the saving came from, what fetch-backs took back, and what later requests no longer carry.
105
+ </SectionHeading>
106
+ <div className="grid grid-main-side">
107
+ <SavingsOverTime stats={data} range={range} stale={stats.stale} />
108
+ <WhereSavingsWent stats={data} stale={stats.stale} />
109
+ </div>
110
+ <Compounding stats={data} active={active} stale={stats.stale} />
111
+ <ShareTrend stats={data} range={range} stale={stats.stale} />
112
+
113
+ <SectionHeading id="spot-cost" title="What it cost">
114
+ Agent time, Spotlight's latency and failures, and whether shaped sessions got worse.
115
+ </SectionHeading>
116
+ <TimeCost stats={data} stale={stats.stale} />
117
+ <Overhead stats={data} stale={stats.stale} />
118
+
119
+ <SectionHeading id="spot-work" title="Is Spotlight doing the work?">
120
+ Whether its heat finds the lines the agent needs, and whether its cuts come back less than cuts
121
+ made without it.
122
+ </SectionHeading>
123
+ <Ranking stats={data} stale={stats.stale} />
124
+ <FetchBack stats={data} stale={stats.stale} />
125
+ <FetchBackGaps stats={data} stale={stats.stale} />
126
+
127
+ <SectionHeading id="spot-tune" title="Where to tune">
128
+ Which steps, sizes and tools pay, and which tools Spotlight doesn't shape yet.
129
+ </SectionHeading>
130
+ <Steps stats={data} stale={stats.stale} />
131
+ <Economics stats={data} stale={stats.stale} />
132
+ <Coverage stats={data} stale={stats.stale} />
133
+ <ResultsFeed rows={data.recent} stale={stats.stale} />
134
+ </>
135
+ )}
136
+ </QueryView>
137
+ </div>
138
+ );
139
+ }
@@ -7,5 +7,6 @@ export * from "./OverviewRoute";
7
7
  export * from "./ProjectsRoute";
8
8
  export * from "./ProvidersRoute";
9
9
  export * from "./RequestsRoute";
10
+ export * from "./SpotlightRoute";
10
11
  export * from "./ToolsRoute";
11
12
  export * from "./TracesRoute";