File size: 3,661 Bytes
bad179a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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 <cmath>

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<HPoint> &tier(Tier t) const { return m_t[int(t)]; }
    QVector<HPoint> range(qint64 from, qint64 to, Tier t) const;            // includes the open bucket
    QVector<HPoint> 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<HPoint> 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<HPoint> &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<Finding> findings;
    QVector<Anomaly> 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<ProcTrend> &procs,
                 const QVector<ServiceTrend> &svcs, bool online);
double regression(const QVector<HPoint> &pts, int metric, double *r2); // slope per second
QString fmtMetric(int metric, double v);