Source code
src/showcase/adapters/live.js
This file is included in this documentation build. It is displayed as code and is not executed.
/**
* Simulate coherent showcase telemetry and utilization colors without changing saved records.
*
* @author Laurent Declercq l.declercq@agon-innovation.ch
* @version 20260923
*/
import { clampPercent } from '@agon/atrium/services/format';
/**
* Derive bounded live CPU and memory readings from a stable resource identifier and time.
*
* @param {object} resource - Detached fictional resource record.
* @param {number} [time=Date.now()] - Snapshot time in milliseconds.
* @returns {object} Detached resource with coherent telemetry, preserving inactive records.
*/
export function liveResource(resource, time = Date.now()) {
if (resource.status !== 'active') {
return { ...resource };
}
const seed = Array.from(resource.id).reduce(
/**
* Give each resource a stable phase independent of sorting or pagination.
*
* @param {number} total - Accumulated character values.
* @param {string} character - Identifier character.
* @returns {number} Stable numeric seed.
*/
(total, character) => total + character.charCodeAt(0),
0
);
const tick = Math.floor(time / 5000);
const bases = [ 52, 81, 93, 66, 77, 45 ];
return {
...resource,
usage: Math.round(clampPercent(bases[seed % bases.length] + 7 * Math.sin(tick * 0.65 + seed))),
memory: Math.round(clampPercent(bases[(seed + 2) % bases.length] + 6 * Math.sin(tick * 0.45 + seed + 1)))
};
}
/**
* Produce a rolling Requests/min series whose last sample equals the displayed live value.
*
* @param {object} metric - Detached metric definition.
* @param {number} [time=Date.now()] - Snapshot time in milliseconds.
* @returns {object} Updated request metric or the unchanged definition of another metric.
*/
export function liveMetric(metric, time = Date.now()) {
if (metric.id !== 'requests') {
return metric;
}
const tick = Math.floor(time / 5000);
const series = Array.from(
{ length: metric.series.length },
/**
* Sample a smooth fictional workload at consecutive five-second intervals.
*
* @param {unknown} unused - Unused array entry.
* @param {number} index - Position in the rolling history.
* @returns {number} Requests per minute at the selected instant.
*/
(unused, index) => {
const sample = tick - metric.series.length + 1 + index;
return Math.round(1268 + 210 * Math.sin(sample * 0.6) + 85 * Math.sin(sample * 0.23));
}
);
const value = series.at(-1);
const change = (value - series[0]) / series[0] * 100;
return {
...metric,
value,
series,
percent: Math.round(value / 3000 * 100),
trend: `${change >= 0 ? '↗' : '↘'} ${Math.abs(change).toFixed(1)}%`
};
}
/**
* Apply the showcase utilization thresholds to a safely bounded progress bar.
*
* @param {number} value - Utilization percentage.
* @param {string} [normal='var(--accent)'] - Trusted normal-state color from consumer markup.
* @returns {string} Width and color declarations with orange at 80 and red at 90 percent.
*/
export function usageStyle(value, normal = 'var(--accent)') {
const percent = clampPercent(value);
const color = percent >= 90 ? 'var(--red)' : percent >= 80 ? 'var(--showcase-warning)' : normal;
return `width: ${percent}%; background: ${color}`;
}
/**
* Own one visibility-aware live demonstration timer until its page is removed.
*
* @param {Function} update - Synchronous callback that updates existing observations.
* @returns {Function} Cleanup function that releases the timer.
*/
export function watchLiveUpdates(update) {
const timer = setInterval(
/**
* Avoid updating demonstration telemetry in a hidden browser tab.
*
* @returns {void} Updates visible page observations.
*/
() => {
if (document.visibilityState === 'visible') {
update();
}
},
5000
);
/**
* Release the interval owned by the current route.
*
* @returns {void} Stops further live updates.
*/
return function stop() {
clearInterval(timer);
};
}