aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/metrics/histogram_snapshot.cpp
blob: 65bb262b98e90b071d38cb9c30d0aaba9a4d1687 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "histogram_snapshot.h"

#include <util/stream/output.h>

#include <iostream>


namespace NMonitoring {

    IHistogramSnapshotPtr ExplicitHistogramSnapshot(TConstArrayRef<TBucketBound> bounds, TConstArrayRef<TBucketValue> values, bool shrinkBuckets) {
        Y_ENSURE(bounds.size() == values.size(),
                 "mismatched sizes: bounds(" << bounds.size() <<
                 ") != buckets(" << values.size() << ')');

        size_t requiredSize = shrinkBuckets ? std::min(bounds.size(), static_cast<size_t>(HISTOGRAM_MAX_BUCKETS_COUNT)) : bounds.size();
        auto snapshot = TExplicitHistogramSnapshot::New(requiredSize);
        if (requiredSize < bounds.size()) {
            auto remains = bounds.size() % requiredSize;
            auto divided = bounds.size() / requiredSize;
            size_t idx{bounds.size()};

            for (size_t i = requiredSize; i > 0; --i) {
                Y_ENSURE(idx > 0);
                (*snapshot)[i - 1].first = bounds[idx - 1];
                (*snapshot)[i - 1].second = 0;

                auto repeat = divided;
                if (remains > 0) {
                    ++repeat;
                    --remains;
                }
                for (; repeat > 0; --repeat) {
                    Y_ENSURE(idx > 0);
                    (*snapshot)[i - 1].second += values[idx - 1];
                    --idx;
                }
            }
        } else {
            for (size_t i = 0; i != bounds.size(); ++i) {
                (*snapshot)[i].first = bounds[i];
                (*snapshot)[i].second = values[i];
            }
        }

        return snapshot;
    }

} // namespace NMonitoring

namespace {

template <typename TStream>
auto& Output(TStream& os, const NMonitoring::IHistogramSnapshot& hist) {
    os << TStringBuf("{");

    ui32 i = 0;
    ui32 count = hist.Count();

    if (count > 0) {
        for (; i < count - 1; ++i) {
            os << hist.UpperBound(i) << TStringBuf(": ") << hist.Value(i);
            os << TStringBuf(", ");
        }

        if (hist.UpperBound(i) == Max<NMonitoring::TBucketBound>()) {
            os << TStringBuf("inf: ") << hist.Value(i);
        } else {
            os << hist.UpperBound(i) << TStringBuf(": ") << hist.Value(i);
        }
    }

    os << TStringBuf("}");

    return os;
}

} // namespace

std::ostream& operator<<(std::ostream& os, const NMonitoring::IHistogramSnapshot& hist) {
    return Output(os, hist);
}

template <>
void Out<NMonitoring::IHistogramSnapshot>(IOutputStream& os, const NMonitoring::IHistogramSnapshot& hist) {
    Output(os, hist);
}