ewma.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #pragma once
  2. #include <util/datetime/base.h>
  3. #include <util/generic/ptr.h>
  4. #include <atomic>
  5. namespace NMonitoring {
  6. class IGauge;
  7. class IExpMovingAverage {
  8. public:
  9. virtual ~IExpMovingAverage() = default;
  10. virtual void Tick() = 0;
  11. virtual void Update(i64 value) = 0;
  12. virtual double Rate() const = 0;
  13. virtual void Reset() = 0;
  14. };
  15. using IExpMovingAveragePtr = THolder<IExpMovingAverage>;
  16. class TEwmaMeter {
  17. public:
  18. // Creates a fake EWMA that will always return 0. Mostly for usage convenience
  19. TEwmaMeter();
  20. explicit TEwmaMeter(IExpMovingAveragePtr&& ewma);
  21. TEwmaMeter(TEwmaMeter&& other);
  22. TEwmaMeter& operator=(TEwmaMeter&& other);
  23. void Mark();
  24. void Mark(i64 value);
  25. double Get();
  26. private:
  27. void TickIfNeeded();
  28. private:
  29. IExpMovingAveragePtr Ewma_;
  30. std::atomic<ui64> LastTick_{TInstant::Now().Seconds()};
  31. };
  32. IExpMovingAveragePtr OneMinuteEwma(IGauge* gauge);
  33. IExpMovingAveragePtr FiveMinuteEwma(IGauge* gauge);
  34. IExpMovingAveragePtr FiveteenMinuteEwma(IGauge* gauge);
  35. } // namespace NMonitoring