From d079b656b4719739b2247dcd9d46e9bec793095a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 6 Feb 2023 17:11:34 +0100 Subject: Merging upstream version 1.38.0. Signed-off-by: Daniel Baumann --- ml/ml.cc | 119 +++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 41 deletions(-) (limited to 'ml/ml.cc') diff --git a/ml/ml.cc b/ml/ml.cc index 1a7d6ae25..461c83baa 100644 --- a/ml/ml.cc +++ b/ml/ml.cc @@ -2,6 +2,7 @@ #include "Config.h" #include "Dimension.h" +#include "Chart.h" #include "Host.h" #include @@ -45,56 +46,65 @@ void ml_init(void) { Cfg.RandomNums.push_back(Gen()); } -void ml_new_host(RRDHOST *RH) { +void ml_host_new(RRDHOST *RH) { if (!ml_enabled(RH)) return; Host *H = new Host(RH); - RH->ml_host = static_cast(H); - - H->startAnomalyDetectionThreads(); + RH->ml_host = reinterpret_cast(H); } -void ml_delete_host(RRDHOST *RH) { - Host *H = static_cast(RH->ml_host); +void ml_host_delete(RRDHOST *RH) { + Host *H = reinterpret_cast(RH->ml_host); if (!H) return; - H->stopAnomalyDetectionThreads(); - delete H; RH->ml_host = nullptr; } -void ml_new_dimension(RRDDIM *RD) { - RRDSET *RS = RD->rrdset; - - Host *H = static_cast(RD->rrdset->rrdhost->ml_host); +void ml_chart_new(RRDSET *RS) { + Host *H = reinterpret_cast(RS->rrdhost->ml_host); if (!H) return; - if (static_cast(RD->update_every) != H->updateEvery()) + Chart *C = new Chart(RS); + RS->ml_chart = reinterpret_cast(C); + + H->addChart(C); +} + +void ml_chart_delete(RRDSET *RS) { + Host *H = reinterpret_cast(RS->rrdhost->ml_host); + if (!H) return; - if (simple_pattern_matches(Cfg.SP_ChartsToSkip, rrdset_name(RS))) + Chart *C = reinterpret_cast(RS->ml_chart); + H->removeChart(C); + + delete C; + RS->ml_chart = nullptr; +} + +void ml_dimension_new(RRDDIM *RD) { + Chart *C = reinterpret_cast(RD->rrdset->ml_chart); + if (!C) return; Dimension *D = new Dimension(RD); - RD->ml_dimension = static_cast(D); - H->addDimension(D); + RD->ml_dimension = reinterpret_cast(D); + C->addDimension(D); } -void ml_delete_dimension(RRDDIM *RD) { - Dimension *D = static_cast(RD->ml_dimension); +void ml_dimension_delete(RRDDIM *RD) { + Dimension *D = reinterpret_cast(RD->ml_dimension); if (!D) return; - Host *H = static_cast(RD->rrdset->rrdhost->ml_host); - if (!H) - delete D; - else - H->removeDimension(D); + Chart *C = reinterpret_cast(RD->rrdset->ml_chart); + C->removeDimension(D); + delete D; RD->ml_dimension = nullptr; } @@ -102,7 +112,7 @@ char *ml_get_host_info(RRDHOST *RH) { nlohmann::json ConfigJson; if (RH && RH->ml_host) { - Host *H = static_cast(RH->ml_host); + Host *H = reinterpret_cast(RH->ml_host); H->getConfigAsJson(ConfigJson); } else { ConfigJson["enabled"] = false; @@ -115,7 +125,7 @@ char *ml_get_host_runtime_info(RRDHOST *RH) { nlohmann::json ConfigJson; if (RH && RH->ml_host) { - Host *H = static_cast(RH->ml_host); + Host *H = reinterpret_cast(RH->ml_host); H->getDetectionInfoAsJson(ConfigJson); } else { return nullptr; @@ -128,7 +138,7 @@ char *ml_get_host_models(RRDHOST *RH) { nlohmann::json ModelsJson; if (RH && RH->ml_host) { - Host *H = static_cast(RH->ml_host); + Host *H = reinterpret_cast(RH->ml_host); H->getModelsAsJson(ModelsJson); return strdup(ModelsJson.dump(2, '\t').c_str()); } @@ -136,30 +146,57 @@ char *ml_get_host_models(RRDHOST *RH) { return nullptr; } -bool ml_is_anomalous(RRDDIM *RD, double Value, bool Exists) { - Dimension *D = static_cast(RD->ml_dimension); - if (!D) - return false; +void ml_start_anomaly_detection_threads(RRDHOST *RH) { + if (RH && RH->ml_host) { + Host *H = reinterpret_cast(RH->ml_host); + H->startAnomalyDetectionThreads(); + } +} + +void ml_stop_anomaly_detection_threads(RRDHOST *RH) { + if (RH && RH->ml_host) { + Host *H = reinterpret_cast(RH->ml_host); + H->stopAnomalyDetectionThreads(true); + } +} - return D->predict(Value, Exists); +void ml_cancel_anomaly_detection_threads(RRDHOST *RH) { + if (RH && RH->ml_host) { + Host *H = reinterpret_cast(RH->ml_host); + H->stopAnomalyDetectionThreads(false); + } } -bool ml_streaming_enabled() { - return Cfg.StreamADCharts; +void ml_chart_update_begin(RRDSET *RS) { + Chart *C = reinterpret_cast(RS->ml_chart); + if (!C) + return; + + C->updateBegin(); } -#if defined(ENABLE_ML_TESTS) +void ml_chart_update_end(RRDSET *RS) { + Chart *C = reinterpret_cast(RS->ml_chart); + if (!C) + return; + + C->updateEnd(); +} -#include "gtest/gtest.h" +bool ml_is_anomalous(RRDDIM *RD, time_t CurrT, double Value, bool Exists) { + Dimension *D = reinterpret_cast(RD->ml_dimension); + if (!D) + return false; -int test_ml(int argc, char *argv[]) { - (void) argc; - (void) argv; + Chart *C = reinterpret_cast(RD->rrdset->ml_chart); - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); + bool IsAnomalous = D->predict(CurrT, Value, Exists); + C->updateDimension(D, IsAnomalous); + return IsAnomalous; } -#endif // ENABLE_ML_TESTS +bool ml_streaming_enabled() { + return Cfg.StreamADCharts; +} #include "ml-private.h" -- cgit v1.2.3