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

131 lines
3.4 KiB
Svelte
Raw Normal View History

2026-01-20 01:02:17 +01:00
<script lang="ts">
import { onMount } from "svelte";
import Chart from "util/Chart.svelte";
2026-01-20 14:19:44 +01:00
import { host_colour, host_label } from "./HostMetricsLib";
import { get_host_metrics, type HostMetrics } from "lib/AdminAPI";
import { formatDate } from "util/Formatting";
2026-01-20 01:02:17 +01:00
let {
metric = "",
window = 0, // Size of the data window in minutes
interval = 0, // Interval of the datapoints in minutes
2026-01-20 14:19:44 +01:00
data_type = "number",
aggregate = false,
2026-01-20 01:02:17 +01:00
}: {
metric: string;
window: number;
interval: number;
data_type?: string;
2026-01-20 14:19:44 +01:00
aggregate?: boolean;
2026-01-20 01:02:17 +01:00
} = $props();
2026-01-20 14:19:44 +01:00
// Make load_graph reactive
$effect(() => {load_graph(metric, window, interval, aggregate)})
2026-01-20 01:02:17 +01:00
let chart: Chart = $state()
let chartTimeout: NodeJS.Timeout = null
2026-01-20 14:19:44 +01:00
const load_graph = async (_metric: string, _window: number, _interval: number, _aggregate: boolean) => {
2026-01-20 01:02:17 +01:00
if (chartTimeout !== null) { clearTimeout(chartTimeout) }
2026-01-20 14:19:44 +01:00
chartTimeout = setTimeout(() => { load_graph(metric, window, interval, aggregate) }, 10000)
2026-01-20 01:02:17 +01:00
let today = new Date()
let start = new Date()
2026-01-20 14:19:44 +01:00
start.setMinutes(start.getMinutes() - _window)
try {
const metrics = await get_host_metrics(start, today, _metric, _interval)
// Format the dates
metrics.timestamps.forEach((val: string, idx: number) => {
metrics.timestamps[idx] = formatDate(val, true, true, true)
2026-01-20 01:02:17 +01:00
});
2026-01-20 14:19:44 +01:00
chart.data().labels = metrics.timestamps;
// If the dataset uses the duration type, we need to convert the values
// to milliseconds
if (data_type === "duration") {
for (const host of Object.keys(metrics.host_amounts)) {
for (let i = 0; i < metrics.host_amounts[host].length; i++) {
// Go durations are expressed on nanoseconds, divide by 1
// million to convert to milliseconds
metrics.host_amounts[host][i] /= 1000000
}
}
}
// Truncate the datasets array in case we have more datasets cached than
// there are in the response
chart.data().datasets.length = Object.keys(metrics.host_amounts).length
2026-01-20 01:02:17 +01:00
let i = 0
2026-01-20 14:19:44 +01:00
if (_aggregate) {
i = 1
chart.data().datasets[0] = {
label: "aggregate",
data: create_aggregate_dataset(metrics),
borderWidth: 1,
pointRadius: 0,
borderColor: "#ffffff",
backgroundColor: "#ffffff",
}
}
for (const host of Object.keys(metrics.host_amounts).sort()) {
2026-01-20 01:02:17 +01:00
if (chart.data().datasets[i] === undefined) {
chart.data().datasets[i] = {
2026-01-20 14:19:44 +01:00
label: "",
2026-01-20 01:02:17 +01:00
data: [],
borderWidth: 1,
pointRadius: 0,
}
}
2026-01-20 14:19:44 +01:00
chart.data().datasets[i].label = await host_label(host)
2026-01-20 01:02:17 +01:00
chart.data().datasets[i].borderColor = host_colour(host)
chart.data().datasets[i].backgroundColor = host_colour(host)
2026-01-20 14:19:44 +01:00
chart.data().datasets[i].data = metrics.host_amounts[host]
2026-01-20 01:02:17 +01:00
i++
}
chart.update()
2026-01-20 14:19:44 +01:00
} catch (error) {
alert(error)
}
}
const create_aggregate_dataset = (metrics: HostMetrics): number[] => {
let data: number[] = []
for (const host of Object.keys(metrics.host_amounts)) {
for (let idx = 0; idx < metrics.host_amounts[host].length; idx++) {
if (data[idx]===undefined) {
data[idx] = 0
}
data[idx] += metrics.host_amounts[host][idx]
}
}
return data
2026-01-20 01:02:17 +01:00
}
onMount(() => {
2026-01-20 14:19:44 +01:00
load_graph(metric, window, interval, aggregate);
2026-01-20 01:02:17 +01:00
return () => {
if (chartTimeout !== null) {
clearTimeout(chartTimeout)
}
}
})
</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>