2026-11-17 14:08:13 +00:00
const RATES = { poll : 60 , fewest : 2 , most : 9 };
2026-11-14 21:33:22 +00:00
const RATE_PERIODS = [
[ "minute" , "min" , "per minute" ],
[ "hour" , "h" , "per hour" ],
[ "day" , "d" , "per day" ],
[ "week" , "wk" , "per week" ],
[ "month" , "mo" , "per month of 30 days" ],
[ "year" , "yr" , "per year of 365 days" ],
2026-11-17 14:08:13 +00:00
[ "total" , "all" , "in the whole window" ],
2026-11-14 21:33:22 +00:00
];
const RATE_METRICS = [
[ "tasks" , "bi-check2-square" , "Tasks done" , "count" ],
[ "commits" , "bi-git" , "Commits" , "count" ],
[ "krs" , "bi-bullseye" , "Key results met" , "count" ],
[ "tokens" , "bi-coin" , "Tokens" , "count" ],
[ "cpu" , "bi-cpu" , "CPU" , "percent" ],
[ "ram" , "bi-memory" , "RAM" , "percent" ],
[ "vram" , "bi-gpu-card" , "VRAM" , "vram" ],
[ "swap" , "bi-arrow-left-right" , "Swap" , "percent" ],
[ "disk" , "bi-hdd" , "Disk" , "disk" ],
];
const BYTE_UNITS = [ "B" , "KiB" , "MiB" , "GiB" , "TiB" , "PiB" ];
2026-11-17 14:08:13 +00:00
const rateView = { data : null , dom : null , ticket : 0 , at : - Infinity };
2026-11-14 21:33:22 +00:00
const periodOf = ( id ) => RATE_PERIODS . find (([ name ]) => name === id ) || RATE_PERIODS [ 1 ];
const metricOf = ( id ) => RATE_METRICS . find (([ name ]) => name === id );
function bytesText ( value ) {
const power = value > 0 ? Math . min ( Math . floor ( Math . log2 ( value ) / 10 ), BYTE_UNITS . length - 1 ) : 0 ;
return ` ${ power ? fixed ( value / 2 ** ( 10 * power )) : Math . round ( value ) } ${ BYTE_UNITS [ power ] } ` ;
}
function countText ( value ) {
if ( value >= 1000 ) {
return compactCount ( value );
}
if ( value >= 10 || Number . isInteger ( value )) {
return String ( Math . round ( value ));
}
return value < 0.1 ? String ( Number ( value . toPrecision ( 1 ))) : fixed ( value );
}
/**
* @param entry - one metric of one period from /api/rates, or null before the first answer.
* @param kind - the metric's kind: count, percent, vram or disk.
* @param short - the period's short name a count is per, "" for none.
2026-11-17 14:08:13 +00:00
* @returns the cell text: ≈ before an estimated count, n/a without a record or sample in the window.
2026-11-14 21:33:22 +00:00
*/
function rateText ( entry , kind , short ) {
if ( ! entry ) {
return "…" ;
}
if ( entry . value === null ) {
return "n/a" ;
}
if ( kind === "count" ) {
return ` ${ entry . approx ? "≈ " : "" }${ countText ( entry . value ) }${ short ? `/ ${ short } ` : "" } ` ;
}
return kind === "vram" && entry . used !== null ? ` ${ percentOf ( entry . value ) } · ${ bytesText ( entry . used ) } ` : percentOf ( entry . value );
}
function rateHint ( entry , metric , period ) {
const [ id , , label , kind ] = metric ;
const [, , per ] = period ;
if ( ! entry ) {
return ` ${ label } ${ per } : loading` ;
}
if ( entry . value === null ) {
2026-11-17 14:08:13 +00:00
return ` ${ label } ${ per } : n/a, ${ kind === "count" ? "no record of this kind reaches into the window" : "no sample in the window" } ` ;
2026-11-14 21:33:22 +00:00
}
2026-11-17 14:08:13 +00:00
const length = rateView . data . to - rateView . data . from ;
const covered = entry . covered !== null && entry . covered < length - 1 ;
const scaled = ! entry . approx ? "" : covered ? `; ≈ its records cover only the last ${ spanName ( entry . covered ) } of the window, so the rate divides by that part` : "; ≈ the window is shorter than the hour tokens are counted in" ;
2026-11-14 21:33:22 +00:00
const where = id === "disk" ? ` on ${ entry . mount } , ${ bytesText ( entry . used ) } of ${ bytesText ( entry . total ) } ` : "" ;
const mean = kind === "count" ? "" : " on average" ;
return ` ${ label } ${ per } : ${ rateText ( entry , kind , "" ) }${ mean }${ where }${ scaled } ` ;
}
function ratesBar () {
const host = document . getElementById ( "rates" );
const chip = h ( "summary" , { class : "rates-chip" });
2026-11-17 14:08:13 +00:00
const units = h ( "div" , { class : "segmented rates-units" , role : "group" , "aria-label" : "Rate unit" },
RATE_PERIODS . map (([ id , short , per ]) => h ( "button" , { type : "button" , "data-period" : id , title : per , on : { click : () => chooseRates ({... settings . rates , period : id })}}, short )));
2026-11-14 21:33:22 +00:00
const body = h ( "tbody" , {});
const head = h ( "tr" , {}, h ( "th" , { scope : "col" }, "Metric" ), h ( "th" , { scope : "col" , title : "Shown in the chip" }, "Chip" ), RATE_PERIODS . map (([ id , short , per ]) => h ( "th" , { scope : "col" , "data-period" : id , title : per }, short )));
const box = h ( "div" , { class : "menu-box end rates-box" , role : "group" , "aria-label" : "Rates per period" },
2026-11-17 14:08:13 +00:00
h ( "h3" , {}, "Rates per period" ), ... timeSection (), h ( "h3" , {}, "Unit" ), units ,
2026-11-14 21:33:22 +00:00
h ( "div" , { class : "rates-scroll" }, h ( "table" , { class : "rates-table" }, h ( "thead" , {}, head ), body )),
2026-11-17 14:08:13 +00:00
h ( "p" , { class : "muted" }, `Every column rates the chosen window: a count is its sum per unit, all the sum itself; a gauge averages over the window. ≈ means the records of a kind begin inside the window, so the rate divides by the part they cover, or the window is shorter than the hour tokens are counted in. n/a means no record or sample in the window. The chip shows ${ RATES . fewest } to ${ RATES . most } metrics.` ));
2026-11-14 21:33:22 +00:00
const menu = h ( "details" , { class : "menu rates-menu" }, chip , box );
2026-11-17 14:08:13 +00:00
host . replaceChildren ( menu );
rateView . dom = { chip , units , body , menu };
window . addEventListener ( "resize" , () => fitChip ( chip ));
2026-11-14 21:33:22 +00:00
}
function rateCell ( metric , period , chosen ) {
const entry = rateView . data ? rateView . data . rates [ period [ 0 ]][ metric [ 0 ]] : null ;
const short = period [ 0 ] === "total" ? "" : period [ 1 ];
return h ( "td" , { "data-period" : period [ 0 ], "data-approx" : String ( Boolean ( entry && entry . approx )), "aria-current" : period [ 0 ] === chosen . period ? "true" : null , title : rateHint ( entry , metric , period )},
rateText ( entry , metric [ 3 ], metric [ 3 ] === "count" ? short : "" ));
}
function rateRow ( metric , chosen ) {
const [ id , name , label ] = metric ;
const shown = chosen . metrics . includes ( id );
const locked = shown ? chosen . metrics . length <= RATES . fewest : chosen . metrics . length >= RATES . most ;
const pick = h ( "input" , { type : "checkbox" , checked : shown , disabled : locked , "aria-label" : ` ${ label } in the chip` , on : { change : () => chooseRates ({... chosen , metrics : shown ? chosen . metrics . filter (( other ) => other !== id ) : [... chosen . metrics , id ]})}});
return h ( "tr" , { "data-metric" : id }, h ( "th" , { scope : "row" }, icon ( name ), ` ${ label } ` ), h ( "td" , {}, pick ), RATE_PERIODS . map (( period ) => rateCell ( metric , period , chosen )));
}
2026-11-17 14:08:13 +00:00
function fitChip ( chip ) {
const shown = [... chip . querySelectorAll ( ".rate" )];
const more = chip . querySelector ( ".rate-more" );
if ( ! more ) {
return ;
}
shown . forEach (( node ) => {
node . hidden = false ;
});
more . hidden = true ;
for ( let kept = shown . length ; kept > 1 && chip . scrollWidth > chip . clientWidth ; kept -= 1 ) {
shown [ kept - 1 ]. hidden = true ;
more . hidden = false ;
more . textContent = `+ ${ shown . length - kept + 1 } ` ;
}
}
2026-11-14 21:33:22 +00:00
function drawRates () {
const dom = rateView . dom ;
if ( ! dom ) {
return ;
}
const chosen = settings . rates ;
const period = periodOf ( chosen . period );
const short = period [ 0 ] === "total" ? "" : period [ 1 ];
const entries = chosen . metrics . map (( id ) => [ metricOf ( id ), rateView . data ? rateView . data . rates [ period [ 0 ]][ id ] : null ]);
2026-11-17 14:08:13 +00:00
for ( const button of dom . units . children ) {
button . setAttribute ( "aria-pressed" , String ( button . dataset . period === period [ 0 ]));
}
dom . chip . replaceChildren ( h ( "span" , { class : "rate-window" }, ` ${ windowName () } · ${ short ? `/ ${ short } ` : "all" } ` ),
... entries . map (([ metric , entry ]) => h ( "span" , { class : "rate" , "data-metric" : metric [ 0 ], "data-approx" : String ( Boolean ( entry && entry . approx )), title : rateHint ( entry , metric , period )},
icon ( metric [ 1 ]), h ( "span" , { class : "rate-value" }, rateText ( entry , metric [ 3 ], metric [ 3 ] === "count" ? short : "" )))),
h ( "span" , { class : "rate-more" , hidden : true }), caret ());
dom . chip . setAttribute ( "aria-label" , `Rates ${ period [ 2 ] } over ${ windowName () } : ${ entries . map (([ metric , entry ]) => ` ${ metric [ 2 ] } ${ rateText ( entry , metric [ 3 ], "" ) } ` ). join ( ", " ) } ` );
dom . chip . title = "Rates per period: time range, replay, unit and every metric" ;
fitChip ( dom . chip );
2026-11-14 21:33:22 +00:00
const focused = document . activeElement instanceof Element ? document . activeElement . closest ( "#rates tr[data-metric]" ) : null ;
dom . body . replaceChildren (... RATE_METRICS . map (( metric ) => rateRow ( metric , chosen )));
const again = focused ? dom . body . querySelector ( `tr[data-metric=" ${ focused . dataset . metric } "] input` ) : null ;
if ( again ) {
again . focus ();
}
}
async function chooseRates ( next ) {
if ( ! ( await saveSettings ({... settings , rates : next }))) {
drawRates ();
}
}
async function fetchRates () {
2026-11-17 14:08:13 +00:00
rateView . ticket += 1 ;
const ticket = rateView . ticket ;
rateView . at = nowSeconds ();
2026-11-14 21:33:22 +00:00
try {
2026-11-17 14:08:13 +00:00
const response = await fetch ( `/api/rates? ${ windowQuery () } ` , { cache : "no-store" });
if ( response . ok && ticket === rateView . ticket ) {
2026-11-14 21:33:22 +00:00
rateView . data = await response . json ();
}
} catch {
ui . offline = true ;
}
drawRates ();
}
function pollRates () {
2026-11-17 14:08:13 +00:00
if ( ! document . hidden && nowSeconds () - rateView . at >= RATES . poll ) {
2026-11-14 21:33:22 +00:00
fetchRates ();
}
}