forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeedHistoryChart.tsx
More file actions
203 lines (186 loc) · 5.67 KB
/
Copy pathSpeedHistoryChart.tsx
File metadata and controls
203 lines (186 loc) · 5.67 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
// src/components/charts/SpeedHistoryChart.tsx
"use client"
import type { EChartsOption } from "echarts"
import { useMemo } from "react"
import { hexToRgba } from "@/lib/color-utils"
import type { FleetSnapshot } from "@/lib/fleet"
import { formatSpeed } from "@/lib/formatters"
import { ChartECharts } from "./lib/ChartECharts"
import { ChartEmptyState } from "./lib/ChartEmptyState"
import { buildAxisPointer, buildTimeXAxis } from "./lib/chart-helpers"
import {
CHART_THEME,
chartAxisLabel,
chartDot,
chartGrid,
chartLegend,
chartTooltip,
chartTooltipHeader,
escHtml,
formatChartTimestamp,
} from "./lib/theme"
interface SpeedHistoryChartProps {
snapshots: FleetSnapshot[]
height?: number
}
const CYAN = CHART_THEME.upload
const AMBER = CHART_THEME.download
function parseSpeedBytes(val: string | null): number {
if (!val) return 0
// Stored as bigint string; convert safely without losing precision for speeds
// Speeds are typically well within Number.MAX_SAFE_INTEGER range
const n = Number(BigInt(val))
return Number.isFinite(n) ? n : 0
}
function buildOption(snapshots: FleetSnapshot[]): EChartsOption {
const sorted = [...snapshots].sort(
(a, b) => new Date(a.polledAt).getTime() - new Date(b.polledAt).getTime()
)
const uploadData: [number, number][] = []
const downloadData: [number, number][] = []
for (const snap of sorted) {
const ts = new Date(snap.polledAt).getTime()
const up = parseSpeedBytes(snap.uploadSpeedBytes)
const down = parseSpeedBytes(snap.downloadSpeedBytes)
uploadData.push([ts, up])
downloadData.push([ts, down])
}
return {
backgroundColor: "transparent",
legend: chartLegend(),
tooltip: chartTooltip("axis", {
axisPointer: buildAxisPointer(),
formatter: (params: unknown) => {
const items = params as Array<{
seriesName: string
value: [number, number]
color: string
axisIndex: number
}>
if (!items?.length) return ""
const ts = items[0].value[0]
const dateLabel = formatChartTimestamp(ts)
const rows = items
.map((item) => {
const dot = chartDot(item.color)
const speedLabel = formatSpeed(item.value[1])
return `${dot}<span style="color:${CHART_THEME.textSecondary};">${escHtml(item.seriesName)}:</span> <span style="color:${CHART_THEME.textPrimary};font-weight:600;">${speedLabel}</span>`
})
.join("<br/>")
return `${chartTooltipHeader(dateLabel)}${rows}`
},
}),
grid: chartGrid({ right: 64, left: 72 }),
xAxis: {
...buildTimeXAxis(),
splitLine: { show: false },
},
yAxis: [
{
type: "value",
name: "Upload",
nameTextStyle: {
color: CYAN,
fontFamily: CHART_THEME.fontMono,
fontSize: CHART_THEME.fontSizeCompact,
},
axisLine: { show: false },
axisTick: { show: false },
axisLabel: chartAxisLabel({
color: CYAN,
formatter: (val: number) => formatSpeed(val),
}),
splitLine: {
lineStyle: { color: CHART_THEME.gridLine, width: 1 },
},
},
{
type: "value",
name: "Download",
nameTextStyle: {
color: AMBER,
fontFamily: CHART_THEME.fontMono,
fontSize: CHART_THEME.fontSizeCompact,
},
position: "right",
axisLine: { show: false },
axisTick: { show: false },
axisLabel: chartAxisLabel({
color: AMBER,
formatter: (val: number) => formatSpeed(val),
}),
splitLine: { show: false },
},
],
series: [
{
name: "Upload",
type: "line",
yAxisIndex: 0,
data: uploadData,
smooth: true,
symbol: "none",
sampling: "lttb",
itemStyle: { color: CYAN },
lineStyle: { color: CYAN, width: 2, shadowColor: CYAN, shadowBlur: 8 },
emphasis: {
focus: "series" as const,
lineStyle: { shadowBlur: 16, shadowColor: CYAN },
},
areaStyle: {
color: {
type: "linear",
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{ offset: 0, color: CHART_THEME.accentGlow },
{ offset: 1, color: hexToRgba(CHART_THEME.upload, 0) },
],
},
},
},
{
name: "Download",
type: "line",
yAxisIndex: 1,
data: downloadData,
smooth: true,
symbol: "none",
sampling: "lttb",
itemStyle: { color: AMBER },
lineStyle: { color: AMBER, width: 2, shadowColor: AMBER, shadowBlur: 8 },
emphasis: {
focus: "series" as const,
lineStyle: { shadowBlur: 16, shadowColor: AMBER },
},
areaStyle: {
color: {
type: "linear",
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{ offset: 0, color: CHART_THEME.warnGlow },
{ offset: 1, color: hexToRgba(CHART_THEME.download, 0) },
],
},
},
},
],
}
}
function SpeedHistoryChart({ snapshots, height = 360 }: SpeedHistoryChartProps) {
const option = useMemo(() => buildOption(snapshots), [snapshots])
const hasSpeedData = snapshots.some(
(s) => s.uploadSpeedBytes !== null || s.downloadSpeedBytes !== null
)
if (!hasSpeedData) {
return <ChartEmptyState height={height} message="No speed history data available yet." />
}
return <ChartECharts option={option} style={{ height, width: "100%" }} />
}
export type { SpeedHistoryChartProps }
export { SpeedHistoryChart }