// Persistent multi-resolution history (raw 6 h, minute 30 d, hour 2 y, day 20 y) // and the statistics-only analyzer that feeds the autopilot. #pragma once #include "Model.h" #include enum HMetric { H_CPU, H_IOWAIT, H_LOAD, H_MEM, H_SWAP, H_DISK, H_DISKIO, H_RX, H_TX, H_RD, H_WR, H_TEMP, H_PROCS, H_GPU, H_FAILED, H_COUNT }; extern const char *const hKeys[H_COUNT], *const hNames[H_COUNT]; enum class Tier { Raw, Minute, Hour, Day }; QString tierName(Tier t); #pragma pack(push, 1) struct HPoint { qint64 ts = 0; // seconds; bucket start for aggregates quint32 n = 0, nUp = 0; // samples / online samples float avg[H_COUNT], max[H_COUNT]; double uptimePct() const { return n ? nUp * 100.0 / n : 0; } }; #pragma pack(pop) class HistoryStore { public: static constexpr qint64 step[4] = {1, 60, 3600, 86400}, keep[4] = {6 * 3600, 30 * 86400, 732 * 86400, 7320LL * 86400}; explicit HistoryStore(const QString &dir); void add(qint64 ts, const float *v, bool up); // up=false records downtime only const QVector &tier(Tier t) const { return m_t[int(t)]; } QVector range(qint64 from, qint64 to, Tier t) const; // includes the open bucket QVector range(qint64 from, qint64 to, Tier *used = nullptr) const; // finest tier covering it void removeFiles(); qint64 firstTs() const; qint64 diskBytes() const; private: struct Acc { qint64 bucket = -1; quint32 n = 0, nUp = 0, cnt[H_COUNT] = {}; double sum[H_COUNT] = {}; float mx[H_COUNT]; Acc(qint64 b = -1); void add(const HPoint &p); HPoint point() const; }; qint64 bucketOf(int t, qint64 ts) const; void append(int t, const HPoint &p); void rewrite(int t); QString file(int t) const; QString m_dir; QVector m_t[4]; Acc m_acc[4]; qint64 m_tz; int m_appends[4] = {}; }; struct Stats { double min = NAN, avg = NAN, p95 = NAN, max = NAN; int count = 0; }; // max = peak of max[] Stats stats(const QVector &pts, int metric); // ---- analysis ------------------------------------------------------------------- struct Finding { QString key, severity, category, title, detail, advice, action, actionArg, actionLabel; // action: disk_cleanup|restart_service|renice|kill_pid bool autoEligible = false; }; struct ProcTrend { int pid = 0, minutesTracked = 0; QString name, user; double cpuNow = 0, highCpuMinutes = 0, rssNow = 0, rssGrowth = 0, rssGrowthPct = 0, rssMonotonic = 0; bool kernel = false; }; struct ServiceTrend { QString unit; bool failedNow = false, preexisting = false; int failsLastHour = 0, autoRestartsLastHour = 0; }; struct Anomaly { QString metric, baseline; int hm = 0; double now = 0, mean = 0, sd = 0, z = 0; }; struct Analysis { qint64 when = 0; int health = 100; QString grade = "A", diskBasis; QStringList healthReasons, capacity; QVector findings; QVector anomalies; double diskFullDays = -1, diskSlopePerDay = 0, memSlopePerHour = 0, memTrendR2 = 0; double up[3] = {NAN, NAN, NAN}, down[3] = {}; // 24 h, 7 d, 30 d: availability %, seconds offline Stats cpu30, mem30; double heat[2][7][24]; // weekly pattern: 0 = CPU, 1 = memory int heatN[7][24] = {}; Analysis(); }; Analysis analyze(const HistoryStore &st, const Metrics &m, const AutopilotConfig &ap, const QVector &procs, const QVector &svcs, bool online); double regression(const QVector &pts, int metric, double *r2); // slope per second QString fmtMetric(int metric, double v);