aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/persqueue/working_time_counter.h
blob: 4f4d81d263dbf39e0b6a03cc8ee7b3da7a7dc77b (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
48
49
50
51
52
53
#pragma once

#include <ydb/core/protos/counters_pq.pb.h>

#include <library/cpp/monlib/dynamic_counters/counters.h>

namespace NKikimr {
namespace NPQ {


class TWorkingTimeCounter {
private:
    bool IsInWorkingState;
    NMonitoring::TDynamicCounters::TCounterPtr WorkingTimeMicroSec;
    TInstant LastUpdateTimestamp;
public:

    TWorkingTimeCounter(NMonitoring::TDynamicCounters::TCounterPtr counter)
    : IsInWorkingState(false)
    , WorkingTimeMicroSec(counter)
    {}

    void UpdateState(bool state) {
        IsInWorkingState = state;
    }

    void UpdateWorkingTime(const TInstant now) {
        if (!WorkingTimeMicroSec) //no counter
            return;
        if (IsInWorkingState && LastUpdateTimestamp > TInstant::Zero()) {
            TDuration res = now - LastUpdateTimestamp;
            (*WorkingTimeMicroSec) += res.MicroSeconds();
            LastUpdateTimestamp += res;
        } else {
            LastUpdateTimestamp = now;
        }
    }

    void SetCounter(NMonitoring::TDynamicCounterPtr counter,
                    const TVector<std::pair<TString, TString>>& subgroups,
                    const std::tuple<TString, TString, bool>& expiring) {
        for (const auto& subgroup : subgroups) {
           counter = counter->GetSubgroup(subgroup.first, subgroup.second);
        }

        WorkingTimeMicroSec = counter->GetExpiringNamedCounter(std::get<0>(expiring),
                                                               std::get<1>(expiring),
                                                               std::get<2>(expiring));
    }
};

} //NPQ
} //NKikimr