forked from snxraven/peardock
Ship remaining roadmap items: encrypted registry vault, peer invite/revoke, Swarm/plugins behind flags, binary streams, engine create validation, deploy rollback, schema validation, fleet/access UI, metrics, fuzz/load/soak tests, systemd packaging, and release tooling. Mark ROADMAP fully complete.
51 lines
1.2 KiB
JavaScript
51 lines
1.2 KiB
JavaScript
/**
|
|
* Ring-buffer stats history for per-container charts.
|
|
*/
|
|
const DEFAULT_MAX_POINTS = Number(process.env.PEARDOCK_STATS_HISTORY_POINTS) || 120
|
|
|
|
/** @type {Map<string, Array<{ t: number, cpu: number, memory: number }>>} */
|
|
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()
|
|
}
|