summaryrefslogtreecommitdiffstats
path: root/ml/Dimension.h
blob: 44b348e9b22ecf8bd63bf5e2c7a5a69e0602b08a (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef ML_DIMENSION_H
#define ML_DIMENSION_H

#include "BitBufferCounter.h"
#include "Config.h"

#include "ml-private.h"

namespace ml {

class RrdDimension {
public:
    RrdDimension(RRDDIM *RD) : RD(RD), Ops(&RD->state->query_ops) { }

    RRDDIM *getRD() const { return RD; }

    time_t latestTime() { return Ops->latest_time(RD); }

    time_t oldestTime() { return Ops->oldest_time(RD); }

    unsigned updateEvery() const { return RD->update_every; }

    const std::string getID() const {
        RRDSET *RS = RD->rrdset;

        std::stringstream SS;
        SS << RS->context << "|" << RS->id << "|" << RD->name;
        return SS.str();
    }

    void setAnomalyRateRD(RRDDIM *ARRD) { AnomalyRateRD = ARRD; }
    RRDDIM *getAnomalyRateRD() const { return AnomalyRateRD; }

    void setAnomalyRateRDName(const char *Name) const {
        rrddim_set_name(AnomalyRateRD->rrdset, AnomalyRateRD, Name);
    }

    virtual ~RrdDimension() {
        rrddim_free_custom(AnomalyRateRD->rrdset, AnomalyRateRD, 0);
    }

private:
    RRDDIM *RD;
    RRDDIM *AnomalyRateRD;

    struct rrddim_volatile::rrddim_query_ops *Ops;

    std::string ID;
};

enum class MLResult {
    Success = 0,
    MissingData,
    NaN,
};

class TrainableDimension : public RrdDimension {
public:
    TrainableDimension(RRDDIM *RD) :
        RrdDimension(RD), TrainEvery(Cfg.TrainEvery * updateEvery()) {}

    MLResult trainModel();

    CalculatedNumber computeAnomalyScore(SamplesBuffer &SB) {
        return Trained ? KM.anomalyScore(SB) : 0.0;
    }

    bool shouldTrain(const TimePoint &TP) const {
        if (ConstantModel)
            return false;

        return (LastTrainedAt + TrainEvery) < TP;
    }

    bool isTrained() const { return Trained; }

private:
    std::pair<CalculatedNumber *, size_t> getCalculatedNumbers();

public:
    TimePoint LastTrainedAt{Seconds{0}};

protected:
    std::atomic<bool> ConstantModel{false};

private:
    Seconds TrainEvery;
    KMeans KM;

    std::atomic<bool> Trained{false};
};

class PredictableDimension : public TrainableDimension {
public:
    PredictableDimension(RRDDIM *RD) : TrainableDimension(RD) {}

    std::pair<MLResult, bool> predict();

    void addValue(CalculatedNumber Value, bool Exists);

    bool isAnomalous() { return AnomalyBit; }

    void updateAnomalyBitCounter(RRDSET *RS, unsigned Elapsed, bool IsAnomalous) {
        AnomalyBitCounter += IsAnomalous;

        if (Elapsed == Cfg.DBEngineAnomalyRateEvery) {
            double AR = static_cast<double>(AnomalyBitCounter) / Cfg.DBEngineAnomalyRateEvery;
            rrddim_set_by_pointer(RS, getAnomalyRateRD(), AR * 1000);
            AnomalyBitCounter = 0;
        }
    }

private:
    CalculatedNumber AnomalyScore{0.0};
    std::atomic<bool> AnomalyBit{false};
    unsigned AnomalyBitCounter{0};

    std::vector<CalculatedNumber> CNs;
};

class DetectableDimension : public PredictableDimension {
public:
    DetectableDimension(RRDDIM *RD) : PredictableDimension(RD) {}

    std::pair<bool, double> detect(size_t WindowLength, bool Reset) {
        bool AnomalyBit = isAnomalous();

        if (Reset)
            NumSetBits = BBC.numSetBits();

        NumSetBits += AnomalyBit;
        BBC.insert(AnomalyBit);

        double AnomalyRate = static_cast<double>(NumSetBits) / WindowLength;
        return { AnomalyBit, AnomalyRate };
    }

private:
    BitBufferCounter BBC{static_cast<size_t>(Cfg.ADMinWindowSize)};
    size_t NumSetBits{0};
};

using Dimension = DetectableDimension;

} // namespace ml

#endif /* ML_DIMENSION_H */