Compare commits

..

2 Commits

Author SHA1 Message Date
1cd07427f0 Request host metrics all at once 2026-01-21 23:48:34 +01:00
6b7d6d81bb Add aggregate statistics 2026-01-20 14:19:44 +01:00
4 changed files with 192 additions and 81 deletions

View File

@@ -1,13 +1,18 @@
<script lang="ts">
import { onMount } from "svelte";
import { onDestroy, onMount } from "svelte";
import HostMetricsGraph from "./HostMetricsGraph.svelte";
import { load_host_names } from "./HostMetricsLib";
import { get_host_metrics, load_host_names, type HostMetrics } from "./HostMetricsLib";
import Expandable from "util/Expandable.svelte";
import ToggleButton from "layout/ToggleButton.svelte";
import { formatDate } from "util/Formatting";
import { loading_finish, loading_start } from "lib/Loading";
const groups: {
title: string,
graphs: {
metric: string,
agg_base?: string,
agg_divisor?: string,
data_type: string,
}[],
}[] = [
@@ -16,6 +21,7 @@ const groups: {
graphs: [
{metric: "api_request", data_type: "number"},
{metric: "api_request_duration", data_type: "duration"},
{metric: "api_request_duration_avg", agg_base: "api_request_duration", agg_divisor: "api_request", data_type: "duration"},
{metric: "api_error", data_type: "number"},
{metric: "api_panic", data_type: "number"},
],
@@ -30,7 +36,9 @@ const groups: {
graphs: [
{metric: "database_query", data_type: "number"},
{metric: "database_query_duration", data_type: "duration"},
{metric: "database_query_duration_avg", agg_base: "database_query_duration", agg_divisor: "database_query", data_type: "duration"},
{metric: "database_query_rows", data_type: "number"},
{metric: "database_query_rows_avg", agg_base: "database_query_rows", agg_divisor: "database_query", data_type: "number"},
{metric: "database_query_retries", data_type: "number"},
{metric: "database_query_error", data_type: "number"},
],
@@ -55,6 +63,8 @@ const groups: {
{metric: "pixelstore_neighbour_read_size", data_type: "bytes"},
{metric: "pixelstore_reed_solomon_read", data_type: "number"},
{metric: "pixelstore_reed_solomon_read_size", data_type: "bytes"},
{metric: "pixelstore_read_retry_success", data_type: "number"},
{metric: "pixelstore_read_retry_error", data_type: "number"},
],
}, {
title: "Pixelstore shards",
@@ -66,22 +76,109 @@ const groups: {
{metric: "pixelstore_shard_move", data_type: "number"},
{metric: "pixelstore_shard_move_size", data_type: "bytes"},
],
}, {
title: "Pixelstore API",
graphs: [
{metric: "pixelstore_api_error_400", data_type: "number"},
{metric: "pixelstore_api_error_500", data_type: "number"},
{metric: "pixelstore_api_put_file", data_type: "number"},
{metric: "pixelstore_api_put_file_size", data_type: "bytes"},
{metric: "pixelstore_api_get_file", data_type: "number"},
{metric: "pixelstore_api_file_exists", data_type: "number"},
{metric: "pixelstore_api_status", data_type: "number"},
],
},
]
let dataWindow: number = $state(60)
let dataInterval: number = $state(1)
let showAggregate: boolean = $state(false)
let metrics: HostMetrics = $state({timestamps: [], metrics: {}})
let metrics_timeout: NodeJS.Timeout = null
const load_metrics = async (window: number, interval: number) => {
if (metrics_timeout !== null) { clearTimeout(metrics_timeout) }
metrics_timeout = setTimeout(() => { load_metrics(dataWindow, dataInterval) }, 10000)
let today = new Date()
let start = new Date()
start.setMinutes(start.getMinutes() - window)
let metrics_list: string[] = []
for (const group of groups) {
for (const graph of group.graphs) {
if (graph.metric !== undefined) {
metrics_list.push(graph.metric)
}
}
}
loading_start()
try {
metrics = await get_host_metrics(start, today, metrics_list, interval)
// Format the dates
metrics.timestamps.forEach((val: string, idx: number) => {
metrics.timestamps[idx] = formatDate(val, true, true, true)
});
} catch (error) {
alert(error)
} finally {
loading_finish()
}
// If the dataset uses the duration type, we need to convert the values
// to milliseconds
for (const group of groups) {
for (const graph of group.graphs) {
if (graph.data_type === "duration" && metrics.metrics[graph.metric] !== undefined) {
for (const host of Object.keys(metrics.metrics[graph.metric])) {
for (let i = 0; i < metrics.metrics[graph.metric][host].length; i++) {
// Go durations are expressed on nanoseconds, divide by
// 1 million to convert to milliseconds
metrics.metrics[graph.metric][host][i] /= 1000000
}
}
}
// If the graph is an aggregate, we'll need to create a new dataset
// with the child datasets as base. Here we create a new dataset by
// dividing one dataset by another
if (graph.agg_base !== undefined && graph.agg_divisor !== undefined) {
for (const host of Object.keys(metrics.metrics[graph.agg_base])) {
metrics.metrics[graph.metric][host] = []
for (let i = 0; i < metrics.metrics[graph.agg_base][host].length; i++) {
if (metrics.metrics[graph.agg_divisor][host][i] > 0) {
metrics.metrics[graph.metric][host].push(
metrics.metrics[graph.agg_base][host][i] / metrics.metrics[graph.agg_divisor][host][i]
)
} else {
metrics.metrics[graph.metric][host].push(0)
}
}
}
}
}
}
}
const setWindow = (window: number, interval: number) => {
dataWindow = window
dataInterval = interval
load_metrics(dataWindow, dataInterval);
}
let loaded = $state(false)
onMount(async () => {
await load_host_names()
await load_metrics(dataWindow, dataInterval);
loaded = true
})
onDestroy(() => {
if (metrics_timeout !== null) {
clearTimeout(metrics_timeout)
}
})
</script>
{#if loaded}
@@ -96,6 +193,8 @@ onMount(async () => {
<button onclick={() => setWindow(525600, 1440)}>Year 1d</button>
<button onclick={() => setWindow(1051200, 1440)}>Two Years 1d</button>
<button onclick={() => setWindow(2628000, 1440)}>Five Years 1d</button>
<br/>
<ToggleButton bind:on={showAggregate}>Aggregate</ToggleButton>
</div>
{#each groups as group (group.title)}
@@ -107,10 +206,11 @@ onMount(async () => {
<div class="grid">
{#each group.graphs as graph (graph.metric)}
<HostMetricsGraph
window={dataWindow}
interval={dataInterval}
metric={graph.metric}
data_type={graph.data_type}
timestamps={metrics.timestamps}
metrics={metrics.metrics[graph.metric]}
aggregate={showAggregate}
/>
{/each}
</div>

View File

@@ -1,92 +1,77 @@
<script lang="ts">
import { onMount } from "svelte";
import Chart from "util/Chart.svelte";
import { get_endpoint } from "lib/PixeldrainAPI";
import { host_colour, host_label } from "./HostMetricsLib";
let {
metric = "",
window = 0, // Size of the data window in minutes
interval = 0, // Interval of the datapoints in minutes
data_type = "number"
data_type = "",
timestamps = [],
metrics = {},
aggregate = false,
}: {
metric: string;
window: number;
interval: number;
data_type?: string;
data_type: string;
timestamps: string[];
metrics: {[key: string]: number[]};
aggregate: boolean;
} = $props();
let chart: Chart = $state()
let chartTimeout: NodeJS.Timeout = null
const loadGraph = () => {
if (chartTimeout !== null) { clearTimeout(chartTimeout) }
chartTimeout = setTimeout(() => { loadGraph() }, 10000)
// Make load_graph reactive
$effect(() => {update_chart(timestamps, metrics, aggregate)})
let today = new Date()
let start = new Date()
start.setMinutes(start.getMinutes() - window)
const update_chart = async (timestamps: string[], metrics: {[key: string]: number[]}, aggregate: boolean) => {
chart.data().labels = [...timestamps];
fetch(
get_endpoint() + "/admin/host_metrics" +
"?start=" + start.toISOString() +
"&end=" + today.toISOString() +
"&metric="+ metric +
"&interval=" + interval
).then(resp => {
if (!resp.ok) { return Promise.reject("Error: " + resp.status); }
return resp.json();
}).then(async resp => {
resp.timestamps.forEach((val: string, idx: number) => {
let date = new Date(val);
let dateStr: string = date.getFullYear().toString();
dateStr += "-" + ("00" + (date.getMonth() + 1)).slice(-2);
dateStr += "-" + ("00" + date.getDate()).slice(-2);
dateStr += " " + ("00" + date.getHours()).slice(-2);
dateStr += ":" + ("00" + date.getMinutes()).slice(-2);
resp.timestamps[idx] = " " + dateStr + " "; // Poor man's padding
});
chart.data().labels = resp.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
for (const host of Object.keys(resp.host_amounts).sort()) {
if (aggregate === true) {
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).sort()) {
if (chart.data().datasets[i] === undefined) {
chart.data().datasets[i] = {
label: await host_label(host),
label: "",
data: [],
borderWidth: 1,
pointRadius: 0,
}
}
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)
if (data_type === "duration") {
for (let i = 0; i < resp.host_amounts[host].length; i++) {
// Go durations are expressed on nanoseconds, divide by 1
// million to convert to milliseconds
resp.host_amounts[host][i] /= 1000000
}
}
chart.data().datasets[i].data = resp.host_amounts[host]
chart.data().datasets[i].data = [...metrics[host]]
i++
}
chart.update()
})
}
onMount(() => {
loadGraph();
return () => {
if (chartTimeout !== null) {
clearTimeout(chartTimeout)
const create_aggregate_dataset = (hosts: {[key:string]: number[]}): number[] => {
let data: number[] = []
for (const host of Object.keys(hosts)) {
for (let idx = 0; idx < hosts[host].length; idx++) {
if (data[idx]===undefined) {
data[idx] = 0
}
data[idx] += hosts[host][idx]
}
}
})
return data
}
</script>
<div>

View File

@@ -1,8 +1,26 @@
import { loading_finish, loading_start } from "lib/Loading";
import { get_endpoint } from "lib/PixeldrainAPI";
import { check_response, get_endpoint } from "lib/PixeldrainAPI";
import hsl2rgb from "pure-color/convert/hsl2rgb";
import rgb2hex from "pure-color/convert/rgb2hex";
export type HostMetrics = {
timestamps: string[]
// First key is the requested metric, second key is the host ID
metrics: { [key: string]: { [key: string]: number[] } }
}
export const get_host_metrics = async (start: Date, end: Date, metrics: string[], interval: number): Promise<HostMetrics> => {
return await check_response(
await fetch(
get_endpoint() + "/admin/host_metrics" +
"?start=" + start.toISOString() +
"&end=" + end.toISOString() +
"&metrics=" + metrics.join(",") +
"&interval=" + interval
)
) as HostMetrics
};
let host_colours: { [key: string]: string } = {}
export const host_colour = (id: string): string => {
let host_count: number = Object.keys(host_colours).length
@@ -12,11 +30,18 @@ export const host_colour = (id: string): string => {
return host_colours[id]
}
const colour_interval = 360 / (host_count + 1)
// Divide the colour wheel by the number of hosts we need to colour. We cap
// the steps at 24 hue, which allows for 15 colours
const colour_interval = Math.max(360 / (host_count + 1), 24)
var i = 0
for (const host of Object.keys(host_colours).sort()) {
host_colours[host] = rgb2hex(hsl2rgb([i * colour_interval, 100, 70]))
const hue = (i * colour_interval) % 360
// Lightness decreases with 10 for each cycle of the colour wheel
const lightness = 80 - (Math.floor((i * colour_interval) / 360) * 10)
host_colours[host] = rgb2hex(hsl2rgb([hue, 100, lightness]))
i++
}

View File

@@ -15,6 +15,7 @@ export type User = {
subscription: Subscription,
storage_space_used: number,
filesystem_storage_used: number,
filesystem_node_count: number,
is_admin: boolean,
balance_micro_eur: number,
hotlinking_enabled: boolean,
@@ -138,7 +139,7 @@ export const logout_user = async (redirect_path: string) => {
{ method: "DELETE" },
))
document.cookie = "pd_auth_key=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
// document.cookie = "pd_auth_key=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
window.location.pathname = redirect_path
}