aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/metrics/ewma.h
blob: 9b2dad7cc5c07585bef3060e213ddb37c3e862d4 (plain) (blame)
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
#pragma once

#include <util/datetime/base.h>
#include <util/generic/ptr.h>

#include <atomic>

namespace NMonitoring {
    class IGauge;

    class IExpMovingAverage {
    public:
        virtual ~IExpMovingAverage() = default;
        virtual void Tick() = 0;
        virtual void Update(i64 value) = 0;
        virtual double Rate() const = 0;
        virtual void Reset() = 0;
    };

    using IExpMovingAveragePtr = THolder<IExpMovingAverage>;

    class TEwmaMeter {
    public:
        // Creates a fake EWMA that will always return 0. Mostly for usage convenience
        TEwmaMeter();
        explicit TEwmaMeter(IExpMovingAveragePtr&& ewma);

        TEwmaMeter(TEwmaMeter&& other);
        TEwmaMeter& operator=(TEwmaMeter&& other);

        void Mark();
        void Mark(i64 value);

        double Get();

    private:
        void TickIfNeeded();

    private:
        IExpMovingAveragePtr Ewma_;
        std::atomic<ui64> LastTick_{TInstant::Now().Seconds()};
    };

    IExpMovingAveragePtr OneMinuteEwma(IGauge* gauge);
    IExpMovingAveragePtr FiveMinuteEwma(IGauge* gauge);
    IExpMovingAveragePtr FiveteenMinuteEwma(IGauge* gauge);
} // namespace NMonitoring