"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.metricsHistory = void 0;
const os_1 = __importDefault(require("os"));
// @group Constants : Ring-buffer capacity — 60 points × 3 s = 3 minutes of history
const MAX_POINTS = 60;
// @group MetricsHistory : In-memory ring buffer; key = pm_id (string)
class MetricsHistoryStore {
    constructor() {
        this.store = new Map();
    }
    // @group MetricsHistory : Record a snapshot from the PM2 process list
    record(processList) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
        const totalMem = os_1.default.totalmem();
        const seen = new Set();
        for (const proc of processList) {
            const pm_id = proc.pm_id;
            seen.add(pm_id);
            const cpu = (_b = (_a = proc.monit) === null || _a === void 0 ? void 0 : _a.cpu) !== null && _b !== void 0 ? _b : 0;
            const memory = (_d = (_c = proc.monit) === null || _c === void 0 ? void 0 : _c.memory) !== null && _d !== void 0 ? _d : 0;
            const point = {
                timestamp: Date.now(),
                cpu: parseFloat(cpu.toFixed(2)),
                memory,
                memoryMB: parseFloat((memory / 1048576).toFixed(2)),
                memoryPercent: parseFloat(((memory / totalMem) * 100).toFixed(2)),
            };
            if (!this.store.has(pm_id)) {
                this.store.set(pm_id, {
                    pm_id,
                    name: (_e = proc.name) !== null && _e !== void 0 ? _e : String(pm_id),
                    status: (_g = (_f = proc.pm2_env) === null || _f === void 0 ? void 0 : _f.status) !== null && _g !== void 0 ? _g : 'unknown',
                    history: [],
                });
            }
            const entry = this.store.get(pm_id);
            // Refresh mutable fields on every poll
            entry.name = (_h = proc.name) !== null && _h !== void 0 ? _h : entry.name;
            entry.status = (_k = (_j = proc.pm2_env) === null || _j === void 0 ? void 0 : _j.status) !== null && _k !== void 0 ? _k : entry.status;
            // Append point, trim to ring-buffer size
            entry.history.push(point);
            if (entry.history.length > MAX_POINTS) {
                entry.history.shift();
            }
        }
        // Remove processes that no longer exist in PM2
        for (const id of this.store.keys()) {
            if (!seen.has(id)) {
                this.store.delete(id);
            }
        }
    }
    // @group MetricsHistory : Return history for a single process; null if not found
    getOne(pm_id) {
        var _a;
        return (_a = this.store.get(pm_id)) !== null && _a !== void 0 ? _a : null;
    }
    // @group MetricsHistory : Return history for all tracked processes
    getAll() {
        return Array.from(this.store.values());
    }
}
// @group Exports : Singleton instance used across the server
exports.metricsHistory = new MetricsHistoryStore();
