From bb50acdcb8073654ea667b8c0272e335bd43f844 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 14 Apr 2022 20:12:14 +0200 Subject: Merging upstream version 1.34.0. Signed-off-by: Daniel Baumann --- ml/Dimension.h | 49 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) (limited to 'ml/Dimension.h') diff --git a/ml/Dimension.h b/ml/Dimension.h index fdf923ccc..44b348e9b 100644 --- a/ml/Dimension.h +++ b/ml/Dimension.h @@ -12,11 +12,7 @@ namespace ml { class RrdDimension { public: - RrdDimension(RRDDIM *RD) : RD(RD), Ops(&RD->state->query_ops) { - std::stringstream SS; - SS << RD->rrdset->id << "|" << RD->name; - ID = SS.str(); - } + RrdDimension(RRDDIM *RD) : RD(RD), Ops(&RD->state->query_ops) { } RRDDIM *getRD() const { return RD; } @@ -26,12 +22,29 @@ public: unsigned updateEvery() const { return RD->update_every; } - const std::string getID() const { return ID; } + const std::string getID() const { + RRDSET *RS = RD->rrdset; + + std::stringstream SS; + SS << RS->context << "|" << RS->id << "|" << RD->name; + return SS.str(); + } - virtual ~RrdDimension() {} + 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; @@ -55,27 +68,28 @@ public: } bool shouldTrain(const TimePoint &TP) const { + if (ConstantModel) + return false; + return (LastTrainedAt + TrainEvery) < TP; } bool isTrained() const { return Trained; } - double updateTrainingDuration(double Duration) { - return TrainingDuration.exchange(Duration); - } - private: std::pair getCalculatedNumbers(); public: TimePoint LastTrainedAt{Seconds{0}}; +protected: + std::atomic ConstantModel{false}; + private: Seconds TrainEvery; KMeans KM; std::atomic Trained{false}; - std::atomic TrainingDuration{0.0}; }; class PredictableDimension : public TrainableDimension { @@ -88,9 +102,20 @@ public: bool isAnomalous() { return AnomalyBit; } + void updateAnomalyBitCounter(RRDSET *RS, unsigned Elapsed, bool IsAnomalous) { + AnomalyBitCounter += IsAnomalous; + + if (Elapsed == Cfg.DBEngineAnomalyRateEvery) { + double AR = static_cast(AnomalyBitCounter) / Cfg.DBEngineAnomalyRateEvery; + rrddim_set_by_pointer(RS, getAnomalyRateRD(), AR * 1000); + AnomalyBitCounter = 0; + } + } + private: CalculatedNumber AnomalyScore{0.0}; std::atomic AnomalyBit{false}; + unsigned AnomalyBitCounter{0}; std::vector CNs; }; -- cgit v1.2.3