/** * Ring-buffer stats history for per-container charts. */ const DEFAULT_MAX_POINTS = Number(process.env.PEARDOCK_STATS_HISTORY_POINTS) || 120 /** @type {Map>} */ const history = new Map() /** * @param {string} containerId * @param {{ cpu: number, memory: number }} sample */ export function recordSample(containerId, sample) { if (!containerId) return let series = history.get(containerId) if (!series) { series = [] history.set(containerId, series) } series.push({ t: Date.now(), cpu: Number(sample.cpu) || 0, memory: Number(sample.memory) || 0, }) while (series.length > DEFAULT_MAX_POINTS) series.shift() } /** * @param {string} containerId * @param {{ limit?: number }} [opts] */ export function getHistory(containerId, opts = {}) { const series = history.get(containerId) || [] const limit = Math.min(Number(opts.limit) || DEFAULT_MAX_POINTS, DEFAULT_MAX_POINTS) return series.slice(-limit) } /** * @param {string[]} activeIds */ export function pruneMissing(activeIds) { const set = new Set(activeIds) for (const id of history.keys()) { if (!set.has(id)) history.delete(id) } } export function clearHistory() { history.clear() }