garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

.. /web/src/lib/meters.ts

raw

/*
 * A node's disk and load as meters: how full, and whether that is a problem.
 * The thresholds are the ones `midas check` and the node's own warning use, so
 * the page goes red when the agents are told.
 */

import {bytes} from '@/lib/format'
import type {Reading} from '@/lib/types'

export type Level = 'ok' | 'warn' | 'bad'

export interface Meter {
    /** 0…1, or null when the node does not report what to measure against (a midas older than 2026-10-20). */
    fill: number | null
    level: Level
    text: string
}

/** Below this much free on /work a node warns its holder (`node.rs`'s LOW_DISK). */
const LOW_DISK = 10e9

export function diskMeter(r: Reading | null | undefined): Meter | null {
    if (r?.disk_free == null) return null
    const fill = r.disk_total ? Math.min(1, Math.max(0, 1 - r.disk_free / r.disk_total)) : null
    const level: Level = r.disk_free < LOW_DISK ? 'bad' : fill != null && fill > 0.85 ? 'warn' : 'ok'
    return {fill, level, text: `${bytes(r.disk_free)} free`}
}

/** Load against the CPUs the node may use: past 1 per CPU, builds queue for a core. */
export function loadMeter(r: Reading | null | undefined): Meter | null {
    if (r?.load == null) return null
    const per = r.cpus ? r.load / r.cpus : null
    const level: Level = per == null ? 'ok' : per > 1 ? 'bad' : per > 0.75 ? 'warn' : 'ok'
    const text = `load ${r.load.toFixed(1)}${r.cpus ? ` of ${r.cpus}` : ''}`
    return {fill: per == null ? null : Math.min(1, per), level, text}
}