Files
homarr/packages/widgets/src/unifi-os/throughput-chart.tsx
T
zacharyandClaude Opus 5 ff24a5b009 feat(widgets): 5s refresh, download first, chart from the first reading
Trois demandes utilisateur, plus une adaptation rendue nécessaire par la
première.

- Ordre inversé : descendant avant montant.
- Rafraîchissement porté de 60 s à 5 s. Il fallait le faire sur les DEUX
  couches locales : l'intervalle client seul aurait relu la même entrée
  de cache serveur douze fois. Le cache cloud reste à 5 min, ses
  métriques n'évoluant que par tranches de 5 min.
- Le graphe est rendu dès la première mesure : vide plutôt qu'absent,
  ce qui évite un saut de mise en page et n'affirme rien de faux.

Adaptation : la console ne rafraîchit ses statistiques que toutes les
30 s (mesuré — `lastHeartbeatAt` avance par pas de 30 s exacts, valeurs
identiques entre deux battements). Interroger toutes les 5 s renvoie
donc six fois la même mesure. Sans filtrage, le graphe deviendrait un
escalier et 60 points ne couvriraient plus que 5 minutes. Une lecture
identique à la précédente n'est donc pas enregistrée.

Seuil de péremption locale recalibré de 150 s à 30 s : l'ancien était
dimensionné pour 60 s + 60 s et ne signalait plus rien d'utile.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 22:21:43 +02:00

120 lines
3.5 KiB
TypeScript

"use client";
import { AreaChart } from "@mantine/charts";
import { Group, Paper, Stack, Text } from "@mantine/core";
import type { ThroughputPoint } from "./throughput-history";
/**
* Series colours.
*
* Not copied from the `systemResources` network chart, which pairs `orange.5`
* with `yellow.5`: that pair scores a normal-vision ΔE of 12.1, below the
* legibility floor of 15, so even readers with full colour vision struggle to
* tell the two lines apart. This pair was validated instead, and passes every
* check — lightness band, chroma, colour-vision-deficiency separation,
* normal-vision separation and surface contrast — against BOTH the light and
* the dark chart surface, so one pair serves both themes.
*/
const DOWN_COLOR = "blue.7";
const UP_COLOR = "orange.8";
const CHART_HEIGHT = 44;
/**
* A dot carries the series identity so the figure beside it can stay in muted
* ink: colour is never the only thing distinguishing up from down — the arrow
* glyph and the label do it too.
*/
const SeriesKey = ({ color, label }: { color: string; label: string }) => (
<Group gap={4} wrap="nowrap">
<Text c={color} size="xs" lh={1} aria-hidden>
●
</Text>
<Text c="dimmed" size="xs" ff="monospace" lh={1}>
{label}
</Text>
</Group>
);
/**
* Defined at module level rather than inline in the `content` prop: recharts
* clones this element and injects `payload`, so it keeps a stable identity
* across renders. The repo's other chart tooltips pass a closure instead and
* each carries a `no-unstable-nested-components` warning for it.
*/
const ThroughputTooltip = ({
payload,
formatRate,
upName,
downName,
}: {
payload?: { name?: string | number; value?: string | number }[];
formatRate: (bitsPerSecond: number) => string;
upName: string;
downName: string;
}) => {
if (!payload || payload.length === 0) return null;
return (
<Paper px="xs" py={4} shadow="md" withBorder>
<Stack gap={2}>
{payload.map((item) => (
<Text key={String(item.name)} size="xs" c="dimmed" ff="monospace">
{item.name === "up" ? `↑ ${upName}` : `↓ ${downName}`} {formatRate(Number(item.value))}
</Text>
))}
</Stack>
</Paper>
);
};
export interface ThroughputChartProps {
history: ThroughputPoint[];
/** Rendered beside the up marker, already formatted. */
upLabel: string;
/** Rendered beside the down marker, already formatted. */
downLabel: string;
/** Formats a raw bits-per-second value for the tooltip. */
formatRate: (bitsPerSecond: number) => string;
upName: string;
downName: string;
}
export const ThroughputChart = ({
history,
upLabel,
downLabel,
formatRate,
upName,
downName,
}: ThroughputChartProps) => (
<Stack gap={2}>
<Group gap="sm" wrap="nowrap" justify="flex-end">
<SeriesKey color={DOWN_COLOR} label={`↓ ${downLabel}`} />
<SeriesKey color={UP_COLOR} label={`↑ ${upLabel}`} />
</Group>
<AreaChart
h={CHART_HEIGHT}
data={history}
dataKey="index"
series={[
{ name: "down", label: downName, color: DOWN_COLOR },
{ name: "up", label: upName, color: UP_COLOR },
]}
curveType="monotone"
strokeWidth={2}
withDots={false}
withXAxis={false}
withYAxis={false}
gridAxis="none"
withLegend={false}
tooltipAnimationDuration={150}
tooltipProps={{
content: <ThroughputTooltip formatRate={formatRate} upName={upName} downName={downName} />,
}}
/>
</Stack>
);