Files
fnx_web/svelte/src/admin_panel/HostMetricsGraph.svelte

87 lines
2.0 KiB
Svelte
Raw Normal View History

2026-01-20 01:02:17 +01:00
<script lang="ts">
import Chart from "util/Chart.svelte";
2026-01-20 14:19:44 +01:00
import { host_colour, host_label } from "./HostMetricsLib";
2026-01-20 01:02:17 +01:00
let {
metric = "",
2026-01-21 23:48:34 +01:00
data_type = "",
timestamps = [],
metrics = {},
2026-01-20 14:19:44 +01:00
aggregate = false,
2026-01-20 01:02:17 +01:00
}: {
metric: string;
2026-01-21 23:48:34 +01:00
data_type: string;
timestamps: string[];
metrics: {[key: string]: number[]};
aggregate: boolean;
2026-01-20 01:02:17 +01:00
} = $props();
let chart: Chart = $state()
2026-01-20 14:19:44 +01:00
2026-01-21 23:48:34 +01:00
// Make load_graph reactive
$effect(() => {update_chart(timestamps, metrics, aggregate)})
const update_chart = async (timestamps: string[], metrics: {[key: string]: number[]}, aggregate: boolean) => {
chart.data().labels = [...timestamps];
// Truncate the datasets array in case we have more datasets cached than
// there are in the response
chart.data().datasets.length = Object.keys(metrics).length
let i = 0
if (aggregate === true) {
i = 1
chart.data().datasets[0] = {
label: "aggregate",
data: create_aggregate_dataset(metrics),
borderWidth: 1,
pointRadius: 0,
borderColor: "#ffffff",
backgroundColor: "#ffffff",
2026-01-20 14:19:44 +01:00
}
2026-01-21 23:48:34 +01:00
}
2026-01-20 14:19:44 +01:00
2026-01-21 23:48:34 +01:00
for (const host of Object.keys(metrics).sort()) {
if (chart.data().datasets[i] === undefined) {
chart.data().datasets[i] = {
label: "",
data: [],
2026-01-20 14:19:44 +01:00
borderWidth: 1,
pointRadius: 0,
}
}
2026-01-21 23:48:34 +01:00
chart.data().datasets[i].label = await host_label(host)
chart.data().datasets[i].borderColor = host_colour(host)
chart.data().datasets[i].backgroundColor = host_colour(host)
chart.data().datasets[i].data = [...metrics[host]]
i++
2026-01-20 14:19:44 +01:00
}
2026-01-21 23:48:34 +01:00
chart.update()
2026-01-20 14:19:44 +01:00
}
2026-01-21 23:48:34 +01:00
const create_aggregate_dataset = (hosts: {[key:string]: number[]}): number[] => {
2026-01-20 14:19:44 +01:00
let data: number[] = []
2026-01-21 23:48:34 +01:00
for (const host of Object.keys(hosts)) {
for (let idx = 0; idx < hosts[host].length; idx++) {
2026-01-20 14:19:44 +01:00
if (data[idx]===undefined) {
data[idx] = 0
}
2026-01-21 23:48:34 +01:00
data[idx] += hosts[host][idx]
2026-01-20 14:19:44 +01:00
}
}
return data
2026-01-20 01:02:17 +01:00
}
</script>
<div>
<div class="title">{metric}</div>
<Chart bind:this={chart} data_type={data_type} legend={false} ticks={false} animations={false} />
</div>
<style>
.title {
font-size: 1.2em;
}
</style>