summaryrefslogtreecommitdiffstats
path: root/ml/Host.h
blob: 289cb5ab7e49f2bc15e760cb787e90c584469b31 (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
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef ML_HOST_H
#define ML_HOST_H

#include "Mutex.h"
#include "Config.h"
#include "Dimension.h"
#include "Chart.h"
#include "Queue.h"

#include "ml-private.h"
#include "json/single_include/nlohmann/json.hpp"

namespace ml
{

class Host {

friend void* train_main(void *);
friend void *detect_main(void *);

public:
    Host(RRDHOST *RH) :
        RH(RH),
        MLS(),
        TS(),
        HostAnomalyRate(0.0),
        ThreadsRunning(false),
        ThreadsCancelled(false),
        ThreadsJoined(false)
        {}

    void addChart(Chart *C);
    void removeChart(Chart *C);

    void getConfigAsJson(nlohmann::json &Json) const;
    void getModelsAsJson(nlohmann::json &Json);
    void getDetectionInfoAsJson(nlohmann::json &Json) const;

    void startAnomalyDetectionThreads();
    void stopAnomalyDetectionThreads(bool join);

    void scheduleForTraining(TrainingRequest TR);
    void train();

    void detect();
    void detectOnce();

private:
    RRDHOST *RH;
    MachineLearningStats MLS;
    TrainingStats TS;
    CalculatedNumber HostAnomalyRate{0.0};
    std::atomic<bool> ThreadsRunning;
    std::atomic<bool> ThreadsCancelled;
    std::atomic<bool> ThreadsJoined;

    Queue<TrainingRequest> TrainingQueue;

    Mutex M;
    std::unordered_map<RRDSET *, Chart *> Charts;

    netdata_thread_t TrainingThread;
    netdata_thread_t DetectionThread;
};

} // namespace ml

#endif /* ML_HOST_H */