Files
skills/orchestrator/templates/limits.js
T

86 lines
3.7 KiB
JavaScript
Raw Normal View History

const Limits = (() => {
"use strict";
const METRICS = ["cpu", "ram", "swap", "disk", "psi"];
const LEVELS = ["lower", "upper", "stop", "mayday"];
const TARGETED = ["cpu", "ram"];
const NAMES = {cpu: "CPU", ram: "RAM", swap: "Swap", disk: "Disk", psi: "PSI"};
const LEVEL_NAMES = {lower: "lower", upper: "upper", stop: "STOP", mayday: "MAYDAY"};
const ORDER = "lower < upper < STOP < MAYDAY";
const percent = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= 100;
const plain = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value);
const exactly = (value, keys) => plain(value) && Object.keys(value).length === keys.length && keys.every((key) => Object.hasOwn(value, key));
/**
* @param limits - a limits object as the page would save it.
* @returns why it is no limits object, the same rules as kvb-org-organize limits.py problem(): exactly METRICS, each
* with exactly LEVELS, every value a percent from 0 to 100, lower < upper < STOP < MAYDAY; null when it is one.
*/
function problem(limits) {
if (!exactly(limits, METRICS)) {
return `the limits are no object of exactly ${METRICS.join(", ")}`;
}
for (const metric of METRICS) {
const levels = limits[metric];
if (!exactly(levels, LEVELS)) {
return `${NAMES[metric]} holds not exactly ${LEVELS.join(", ")}`;
}
const values = LEVELS.map((level) => levels[level]);
if (!values.every(percent)) {
return `${NAMES[metric]} holds a value that is no percent from 0 to 100`;
}
if (!values.every((value, index) => index === 0 || values[index - 1] < value)) {
return `${NAMES[metric]} breaks ${ORDER}: ${values.join(" / ")} %`;
}
}
return null;
}
/**
* @param next - valid limits the page would save.
* @param held - the limits in force, null while the queue config applies.
* @returns every value of next above the one in force as {metric, level, from, to}, from null while none is in
* force: a higher lower or upper admits more load, a higher STOP or MAYDAY raises an alarm threshold, so each needs
* the passkey; an empty list means every value stays or tightens.
*/
function loosened(next, held) {
return METRICS.flatMap((metric) => LEVELS.map((level) => ({metric, level, from: held ? held[metric][level] : null, to: next[metric][level]})))
.filter((change) => change.from === null || change.to > change.from);
}
/**
* @param limits - valid limits.
* @param metric - one of METRICS.
* @param level - one of LEVELS.
* @param value - the new value.
* @returns a copy of limits with only that value changed.
*/
function withValue(limits, metric, level, value) {
return {...limits, [metric]: {...limits[metric], [level]: value}};
}
/**
* @param limits - the limits in force, or null.
* @param profiles - {name: limits} of the hub.
* @returns the name of the profile holding exactly these values, null for none.
*/
function profileOf(limits, profiles) {
if (!limits) {
return null;
}
const found = Object.entries(profiles).find(([, values]) => METRICS.every((metric) => LEVELS.every((level) => values[metric][level] === limits[metric][level])));
return found ? found[0] : null;
}
/**
* @param change - one entry of loosened().
* @returns it as words, like `CPU upper 90 → 95 %`.
*/
function said(change) {
return `${NAMES[change.metric]} ${LEVEL_NAMES[change.level]} ${change.from === null ? "unset" : change.from} → ${change.to} %`;
}
return {METRICS, LEVELS, TARGETED, NAMES, LEVEL_NAMES, ORDER, problem, loosened, withValue, profileOf, said};
})();
if (typeof module === "object" && module.exports) {
module.exports = Limits;
}