|
| 1 | +/* eslint-disable react-refresh/only-export-components */ |
| 2 | +import { ComponentProps } from "react"; |
| 3 | +import { cva, cx } from "cva"; |
| 4 | + |
| 5 | +import { someIterable } from "../utils"; |
| 6 | + |
| 7 | +import { GridCard } from "./Card"; |
| 8 | +import MetricsChart from "./MetricsChart"; |
| 9 | + |
| 10 | +interface ChartPoint { |
| 11 | + date: number; |
| 12 | + metric: number | null; |
| 13 | +} |
| 14 | + |
| 15 | +interface MetricProps<T, K extends keyof T> { |
| 16 | + title: string; |
| 17 | + description: string; |
| 18 | + stream?: Map<number, T>; |
| 19 | + metric?: K; |
| 20 | + data?: ChartPoint[]; |
| 21 | + gate?: Map<number, unknown>; |
| 22 | + supported?: boolean; |
| 23 | + map?: (p: { date: number; metric: number | null }) => ChartPoint; |
| 24 | + domain?: [number, number]; |
| 25 | + unit: string; |
| 26 | + heightClassName?: string; |
| 27 | + referenceValue?: number; |
| 28 | + badge?: ComponentProps<typeof MetricHeader>["badge"]; |
| 29 | + badgeTheme?: ComponentProps<typeof MetricHeader>["badgeTheme"]; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Creates a chart array from a metrics map and a metric name. |
| 34 | + * |
| 35 | + * @param metrics - Expected to be ordered from oldest to newest. |
| 36 | + * @param metricName - Name of the metric to create a chart array for. |
| 37 | + */ |
| 38 | +export function createChartArray<T, K extends keyof T>( |
| 39 | + metrics: Map<number, T>, |
| 40 | + metricName: K, |
| 41 | +) { |
| 42 | + const result: { date: number; metric: number | null }[] = []; |
| 43 | + const iter = metrics.entries(); |
| 44 | + let next = iter.next() as IteratorResult<[number, T]>; |
| 45 | + const nowSeconds = Math.floor(Date.now() / 1000); |
| 46 | + |
| 47 | + // We want 120 data points, in the chart. |
| 48 | + const firstDate = Math.min(next.value?.[0] ?? nowSeconds, nowSeconds - 120); |
| 49 | + |
| 50 | + for (let t = firstDate; t < nowSeconds; t++) { |
| 51 | + while (!next.done && next.value[0] < t) next = iter.next(); |
| 52 | + const has = !next.done && next.value[0] === t; |
| 53 | + |
| 54 | + let metric = null; |
| 55 | + if (has) metric = next.value[1][metricName] as number; |
| 56 | + result.push({ date: t, metric }); |
| 57 | + |
| 58 | + if (has) next = iter.next(); |
| 59 | + } |
| 60 | + |
| 61 | + return result; |
| 62 | +} |
| 63 | + |
| 64 | +function computeReferenceValue(points: ChartPoint[]): number | undefined { |
| 65 | + const values = points |
| 66 | + .filter(p => p.metric != null && Number.isFinite(p.metric)) |
| 67 | + .map(p => Number(p.metric)); |
| 68 | + |
| 69 | + if (values.length === 0) return undefined; |
| 70 | + |
| 71 | + const sum = values.reduce((acc, v) => acc + v, 0); |
| 72 | + const mean = sum / values.length; |
| 73 | + return Math.round(mean); |
| 74 | +} |
| 75 | + |
| 76 | +const theme = { |
| 77 | + light: |
| 78 | + "bg-white text-black border border-slate-800/20 dark:border dark:border-slate-700 dark:bg-slate-800 dark:text-slate-300", |
| 79 | + danger: "bg-red-500 dark:border-red-700 dark:bg-red-800 dark:text-red-50", |
| 80 | + primary: "bg-blue-500 dark:border-blue-700 dark:bg-blue-800 dark:text-blue-50", |
| 81 | +}; |
| 82 | + |
| 83 | +interface SettingsItemProps { |
| 84 | + readonly title: string; |
| 85 | + readonly description: string | React.ReactNode; |
| 86 | + readonly badge?: string; |
| 87 | + readonly className?: string; |
| 88 | + readonly children?: React.ReactNode; |
| 89 | + readonly badgeTheme?: keyof typeof theme; |
| 90 | +} |
| 91 | + |
| 92 | +export function MetricHeader(props: SettingsItemProps) { |
| 93 | + const { title, description, badge } = props; |
| 94 | + const badgeVariants = cva({ variants: { theme: theme } }); |
| 95 | + |
| 96 | + return ( |
| 97 | + <div className="space-y-0.5"> |
| 98 | + <div className="flex items-center gap-x-2"> |
| 99 | + <div className="flex w-full items-center justify-between text-base font-semibold text-black dark:text-white"> |
| 100 | + {title} |
| 101 | + {badge && ( |
| 102 | + <span |
| 103 | + className={cx( |
| 104 | + "ml-2 rounded-sm px-2 py-1 font-mono text-[10px] leading-none font-medium", |
| 105 | + badgeVariants({ theme: props.badgeTheme ?? "light" }), |
| 106 | + )} |
| 107 | + > |
| 108 | + {badge} |
| 109 | + </span> |
| 110 | + )} |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + <div className="text-sm text-slate-700 dark:text-slate-300">{description}</div> |
| 114 | + </div> |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +export function Metric<T, K extends keyof T>({ |
| 119 | + title, |
| 120 | + description, |
| 121 | + stream, |
| 122 | + metric, |
| 123 | + data, |
| 124 | + gate, |
| 125 | + supported, |
| 126 | + map, |
| 127 | + domain = [0, 600], |
| 128 | + unit = "", |
| 129 | + heightClassName = "h-[127px]", |
| 130 | + badge, |
| 131 | + badgeTheme, |
| 132 | +}: MetricProps<T, K>) { |
| 133 | + const ready = gate ? gate.size > 0 : stream ? stream.size > 0 : true; |
| 134 | + const supportedFinal = |
| 135 | + supported ?? |
| 136 | + (stream && metric ? someIterable(stream, ([, s]) => s[metric] !== undefined) : true); |
| 137 | + |
| 138 | + // Either we let the consumer provide their own chartArray, or we create one from the stream and metric. |
| 139 | + const raw = data ?? ((stream && metric && createChartArray(stream, metric)) || []); |
| 140 | + |
| 141 | + // If the consumer provides a map function, we apply it to the raw data. |
| 142 | + const dataFinal: ChartPoint[] = map ? raw.map(map) : raw; |
| 143 | + |
| 144 | + // Compute the average value of the metric. |
| 145 | + const referenceValue = computeReferenceValue(dataFinal); |
| 146 | + |
| 147 | + return ( |
| 148 | + <div className="space-y-2"> |
| 149 | + <MetricHeader |
| 150 | + title={title} |
| 151 | + description={description} |
| 152 | + badge={badge} |
| 153 | + badgeTheme={badgeTheme} |
| 154 | + /> |
| 155 | + |
| 156 | + <GridCard> |
| 157 | + <div |
| 158 | + className={`flex ${heightClassName} w-full items-center justify-center text-sm text-slate-500`} |
| 159 | + > |
| 160 | + {!ready ? ( |
| 161 | + <div className="flex flex-col items-center space-y-1"> |
| 162 | + <p className="text-slate-700">Waiting for data...</p> |
| 163 | + </div> |
| 164 | + ) : supportedFinal ? ( |
| 165 | + <MetricsChart |
| 166 | + data={dataFinal} |
| 167 | + domain={domain} |
| 168 | + unit={unit} |
| 169 | + referenceValue={referenceValue} |
| 170 | + /> |
| 171 | + ) : ( |
| 172 | + <div className="flex flex-col items-center space-y-1"> |
| 173 | + <p className="text-black">Metric not supported</p> |
| 174 | + </div> |
| 175 | + )} |
| 176 | + </div> |
| 177 | + </GridCard> |
| 178 | + </div> |
| 179 | + ); |
| 180 | +} |
0 commit comments