forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRatioStabilityChart.tsx
More file actions
302 lines (266 loc) · 8.86 KB
/
Copy pathRatioStabilityChart.tsx
File metadata and controls
302 lines (266 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// src/components/charts/RatioStabilityChart.tsx
//
// Functions: computeEmaWithBand, buildRatioStabilityOption, RatioStabilityChart
"use client"
import type { EChartsOption } from "echarts"
import { hexToRgba } from "@/lib/color-utils"
import { formatRatioDisplay } from "@/lib/formatters"
import type { Snapshot } from "@/types/api"
import type { FleetChartProps, TrackerSnapshotSeries } from "@/types/charts"
import { ChartECharts } from "./lib/ChartECharts"
import { ChartEmptyState } from "./lib/ChartEmptyState"
import {
buildAxisPointer,
buildTimeXAxis,
fmtNum,
insideZoom,
yAxisAutoRange,
} from "./lib/chart-helpers"
import { LogScaleToggle } from "./lib/LogScaleToggle"
import {
CHART_THEME,
chartAxisLabel,
chartDot,
chartGrid,
chartLegend,
chartTooltip,
chartTooltipHeader,
escHtml,
formatChartTimestamp,
} from "./lib/theme"
import { useLogScale } from "./lib/useLogScale"
interface RatioStabilityChartProps extends FleetChartProps {
emaPeriod?: number
bandWindow?: number
}
interface EmaWithBandResult {
ema: [number, number][]
upper: [number, number][]
lower: [number, number][]
stdDevByTs: Map<number, number>
}
function computeEmaWithBand(
snapshots: Snapshot[],
emaPeriod: number,
bandWindow: number
): EmaWithBandResult {
const filtered = snapshots
.filter((s) => s.ratio !== null)
.sort((a, b) => new Date(a.polledAt).getTime() - new Date(b.polledAt).getTime())
if (filtered.length === 0) {
return { ema: [], upper: [], lower: [], stdDevByTs: new Map() }
}
const ratios = filtered.map((s) => s.ratio as number)
const tsMs = filtered.map((s) => new Date(s.polledAt).getTime())
const alpha = 2 / (emaPeriod + 1)
const emaPoints: [number, number][] = []
const upperPoints: [number, number][] = []
const lowerPoints: [number, number][] = []
const stdDevByTs = new Map<number, number>()
for (let t = 0; t < ratios.length; t++) {
const ts = tsMs[t]
// Compute EMA
const emaT = t === 0 ? ratios[0] : alpha * ratios[t] + (1 - alpha) * emaPoints[t - 1][1]
// Compute rolling std dev over last bandWindow raw ratio values
const windowStart = Math.max(0, t + 1 - bandWindow)
const window = ratios.slice(windowStart, t + 1)
const n = window.length
const mean = window.reduce((sum, v) => sum + v, 0) / n
const variance = window.reduce((sum, v) => sum + (v - mean) ** 2, 0) / n
const stdDev = Math.sqrt(variance)
const upperT = emaT + stdDev
const lowerT = Math.max(0, emaT - stdDev)
emaPoints.push([ts, emaT])
upperPoints.push([ts, upperT])
lowerPoints.push([ts, lowerT])
stdDevByTs.set(ts, stdDev)
}
return { ema: emaPoints, upper: upperPoints, lower: lowerPoints, stdDevByTs }
}
function buildRatioStabilityOption(
trackerData: TrackerSnapshotSeries[],
emaPeriod: number,
bandWindow: number,
useLog: boolean
): EChartsOption {
// Pre-compute EMA + band for each tracker
const trackerComputations = trackerData.map((tracker) => {
const result = computeEmaWithBand(tracker.snapshots, emaPeriod, bandWindow)
return { tracker, ...result }
})
// Build legend data (only EMA series)
const legendData = trackerData.map((t) => t.name)
// Build ECharts series array: lower base + diff band + EMA line per tracker
const series: EChartsOption["series"] = []
for (const { tracker, ema, upper, lower } of trackerComputations) {
const stackId = `band-${tracker.name}`
// Band diff data: upper - lower at each timestamp
const bandDiffData: [number, number][] = lower.map(([ts, lowerVal], i) => {
const upperVal = upper[i]?.[1] ?? lowerVal
return [ts, upperVal - lowerVal]
})
// Series 1: lower baseline (invisible, establishes the stack floor)
series.push({
name: `${tracker.name}-lower`,
type: "line",
sampling: "lttb",
stack: stackId,
data: lower,
symbol: "none",
smooth: true,
legendHoverLink: false,
lineStyle: { opacity: 0 },
areaStyle: { opacity: 0 },
tooltip: { show: false },
})
// Series 2: band fill (upper - lower), stacked on top of lower
series.push({
name: `${tracker.name}-band`,
type: "line",
sampling: "lttb",
stack: stackId,
data: bandDiffData,
symbol: "none",
smooth: true,
legendHoverLink: false,
lineStyle: { opacity: 0 },
areaStyle: {
color: hexToRgba(tracker.color, 0.12),
},
tooltip: { show: false },
})
// Series 3: EMA line (visible, in legend)
series.push({
name: tracker.name,
type: "line",
sampling: "lttb",
data: ema,
symbol: "none",
smooth: true,
lineStyle: {
color: tracker.color,
width: 2,
},
itemStyle: { color: tracker.color },
emphasis: {
focus: "series" as const,
lineStyle: { shadowBlur: 16, shadowColor: tracker.color },
},
})
}
return {
backgroundColor: "transparent",
grid: chartGrid({ right: 16, left: 64 }),
legend: chartLegend({ data: legendData }),
tooltip: chartTooltip("axis", {
axisPointer: buildAxisPointer(CHART_THEME.borderMid, 0.8, 1),
formatter: (params: unknown) => {
const items = params as Array<{
seriesName: string
value: [number, number] | null
color: string
seriesIndex: number
}>
if (!items || items.length === 0) return ""
// Extract ms timestamp from the first item's value tuple
const firstValue = items[0]?.value
if (!firstValue) return ""
const tsMs = firstValue[0]
const time = formatChartTimestamp(tsMs)
// Only show EMA series in tooltip (filter out lower/band series by name suffix)
const emaItems = items.filter(
(item) => !item.seriesName.endsWith("-lower") && !item.seriesName.endsWith("-band")
)
if (emaItems.length === 0) return ""
const rows = emaItems
.filter((item) => item.value !== null && item.value !== undefined)
.map((item) => {
const emaVal = (item.value as [number, number])[1]
// Look up std dev for this tracker + timestamp
const computation = trackerComputations.find((c) => c.tracker.name === item.seriesName)
const sigma = computation?.stdDevByTs.get(tsMs) ?? null
const sigmaStr =
sigma !== null
? ` <span style="color:${CHART_THEME.textTertiary};">[±${sigma.toFixed(2)} σ]</span>`
: ""
return (
chartDot(item.color) +
`<span style="color:${CHART_THEME.textSecondary};">${escHtml(item.seriesName)}:</span> ` +
`<span style="color:${CHART_THEME.textPrimary};font-weight:600;">${formatRatioDisplay(emaVal)}</span>` +
` <span style="color:${CHART_THEME.textTertiary};">(EMA)</span>` +
sigmaStr
)
})
.join("<br/>")
return chartTooltipHeader(time) + rows
},
}),
xAxis: buildTimeXAxis(),
yAxis: {
type: useLog ? "log" : "value",
name: useLog ? "Ratio (log)" : "Ratio",
scale: true,
...(useLog ? {} : yAxisAutoRange()),
nameTextStyle: {
color: CHART_THEME.textTertiary,
fontFamily: CHART_THEME.fontMono,
fontSize: CHART_THEME.fontSizeCompact,
},
axisLine: { show: false },
axisTick: { show: false },
axisLabel: chartAxisLabel({
formatter: (val: number) => fmtNum(val, 1),
}),
splitLine: {
lineStyle: {
color: CHART_THEME.gridLine,
width: 1,
},
},
},
dataZoom: insideZoom(Math.max(...trackerComputations.map((c) => c.ema.length), 0)),
series,
}
}
function RatioStabilityChart({
trackerData,
height = 360,
emaPeriod = 7,
bandWindow = 14,
}: RatioStabilityChartProps) {
const hasEnoughData = trackerData.some(
(t) => t.snapshots.filter((s) => s.ratio !== null).length >= 3
)
const allRatioValues: number[] = []
for (const tracker of trackerData) {
for (const snap of tracker.snapshots) {
if (snap.ratio !== null && snap.ratio > 0) allRatioValues.push(snap.ratio)
}
}
const logScale = useLogScale(allRatioValues)
if (!hasEnoughData) {
return <ChartEmptyState height={height} message="Not enough data for stability analysis" />
}
return (
<div className="flex flex-col gap-2">
<div className="flex justify-end">
<LogScaleToggle
effectiveLog={logScale.effectiveLog}
isAuto={logScale.isAuto}
onToggle={logScale.onToggle}
/>
</div>
<ChartECharts
option={buildRatioStabilityOption(
trackerData,
emaPeriod,
bandWindow,
logScale.effectiveLog
)}
style={{ height, width: "100%" }}
/>
</div>
)
}
export type { RatioStabilityChartProps }
export { computeEmaWithBand, RatioStabilityChart }