Host.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef ML_HOST_H
  3. #define ML_HOST_H
  4. #include "Mutex.h"
  5. #include "Config.h"
  6. #include "Dimension.h"
  7. #include "Chart.h"
  8. #include "Queue.h"
  9. #include "ml-private.h"
  10. #include "json/single_include/nlohmann/json.hpp"
  11. namespace ml
  12. {
  13. class Host {
  14. friend void* train_main(void *);
  15. friend void *detect_main(void *);
  16. public:
  17. Host(RRDHOST *RH) :
  18. RH(RH),
  19. MLS(),
  20. TS(),
  21. HostAnomalyRate(0.0),
  22. ThreadsRunning(false),
  23. ThreadsCancelled(false),
  24. ThreadsJoined(false)
  25. {}
  26. void addChart(Chart *C);
  27. void removeChart(Chart *C);
  28. void getConfigAsJson(nlohmann::json &Json) const;
  29. void getModelsAsJson(nlohmann::json &Json);
  30. void getDetectionInfoAsJson(nlohmann::json &Json) const;
  31. void startAnomalyDetectionThreads();
  32. void stopAnomalyDetectionThreads(bool join);
  33. void scheduleForTraining(TrainingRequest TR);
  34. void train();
  35. void detect();
  36. void detectOnce();
  37. private:
  38. RRDHOST *RH;
  39. MachineLearningStats MLS;
  40. TrainingStats TS;
  41. CalculatedNumber HostAnomalyRate{0.0};
  42. std::atomic<bool> ThreadsRunning;
  43. std::atomic<bool> ThreadsCancelled;
  44. std::atomic<bool> ThreadsJoined;
  45. Queue<TrainingRequest> TrainingQueue;
  46. Mutex M;
  47. std::unordered_map<RRDSET *, Chart *> Charts;
  48. netdata_thread_t TrainingThread;
  49. netdata_thread_t DetectionThread;
  50. };
  51. } // namespace ml
  52. #endif /* ML_HOST_H */