aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/interface/job_statistics.h
blob: 8af751604fd483e9ca600f44d16e76a7fdabb95a (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#pragma once

///
/// @file yt/cpp/mapreduce/interface/job_statistics.h
///
/// Header containing classes and utility functions to work with
/// [job statistics](https://docs.yandex-team.ru/yt/problems/jobstatistics).

#include "fwd.h"

#include <library/cpp/yson/node/node.h>

#include <util/system/defaults.h>
#include <util/generic/maybe.h>
#include <util/generic/ptr.h>

namespace NYT {

////////////////////////////////////////////////////////////////////

///
/// @brief Convert i64 representation of statistics to other type.
///
/// Library defines this template for types TDuration and i64.
/// Users may define it for their types.
///
/// @see @ref NYT::TJobStatistics::GetStatisticsAs method.
template <typename T>
T ConvertJobStatisticsEntry(i64 value);

////////////////////////////////////////////////////////////////////

/// Class representing a collection of job statistics.
class TJobStatistics
{
public:
    ///
    /// Construct empty statistics.
    TJobStatistics();

    ///
    /// Construct statistics from statistics node.
    TJobStatistics(const NYT::TNode& statistics);

    TJobStatistics(const TJobStatistics& jobStatistics);
    TJobStatistics(TJobStatistics&& jobStatistics);

    TJobStatistics& operator=(const TJobStatistics& jobStatistics);
    TJobStatistics& operator=(TJobStatistics&& jobStatistics);

    ~TJobStatistics();

    ///
    /// @brief Filter statistics by task name.
    ///
    /// @param taskNames What task names to include (empty means all).
    TJobStatistics TaskName(TVector<TTaskName> taskNames) const;

    ///
    /// @brief Filter statistics by job state.
    ///
    /// @param filter What job states to include (empty means all).
    ///
    /// @note Default statistics include only (successfully) completed jobs.
    TJobStatistics JobState(TVector<EJobState> filter) const;

    ///
    /// @brief Filter statistics by job type.
    ///
    /// @param filter What job types to include (empty means all).
    ///
    /// @deprecated Use @ref TJobStatistics::TaskName instead.
    ///
    /// @see https://yt.yandex-team.ru/docs/description/mr/jobs#obshaya-shema
    TJobStatistics JobType(TVector<EJobType> filter) const;

    ///
    /// @brief Check that given statistics exist.
    ///
    /// @param name Slash separated statistics name, e.g. "time/total" (like it appears in web interface).
    bool HasStatistics(TStringBuf name) const;

    ///
    /// @brief Get statistics by name.
    ///
    /// @param name Slash separated statistics name, e.g. "time/total" (like it appears in web interface).
    ///
    /// @note If statistics is missing an exception is thrown. If because of filters
    /// no fields remain the returned value is empty (all fields are `Nothing`).
    ///
    /// @note We don't use `TMaybe<TJobStatisticsEntry>` here;
    /// instead, @ref NYT::TJobStatisticsEntry methods return `TMaybe<i64>`,
    /// so user easier use `.GetOrElse`:
    /// ```
    ///     jobStatistics.GetStatistics("some/statistics/name").Max().GetOrElse(0);
    /// ```
    TJobStatisticsEntry<i64> GetStatistics(TStringBuf name) const;

    ///
    /// @brief Get statistics by name.
    ///
    /// @param name Slash separated statistics name, e.g. "time/total" (like it appears in web interface).
    ///
    /// @note In order to use `GetStatisticsAs` method, @ref NYT::ConvertJobStatisticsEntry function must be defined
    /// (the library defines it for `i64` and `TDuration`, user may define it for other types).
    template <typename T>
    TJobStatisticsEntry<T> GetStatisticsAs(TStringBuf name) const;

    ///
    /// Get (slash separated) names of statistics.
    TVector<TString> GetStatisticsNames() const;

    ///
    /// @brief Check if given custom statistics exists.
    ///
    /// @param name Slash separated custom statistics name.
    bool HasCustomStatistics(TStringBuf name) const;

    ///
    /// @brief Get custom statistics (those the user can write in job with @ref NYT::WriteCustomStatistics).
    ///
    /// @param name Slash separated custom statistics name.
    TJobStatisticsEntry<i64> GetCustomStatistics(TStringBuf name) const;

    ///
    /// @brief Get custom statistics (those the user can write in job with @ref NYT::WriteCustomStatistics).
    ///
    /// @param name Slash separated custom statistics name.
    template <typename T>
    TJobStatisticsEntry<T> GetCustomStatisticsAs(TStringBuf name) const;

    ///
    /// Get names of all custom statistics.
    TVector<TString> GetCustomStatisticsNames() const;

private:
    class TData;
    struct TFilter;

    struct TDataEntry {
        i64 Max;
        i64 Min;
        i64 Sum;
        i64 Count;
    };

    static const TString CustomStatisticsNamePrefix_;

private:
    TJobStatistics(::TIntrusivePtr<TData> data, ::TIntrusivePtr<TFilter> filter);

    TMaybe<TDataEntry> GetStatisticsImpl(TStringBuf name) const;

private:
    ::TIntrusivePtr<TData> Data_;
    ::TIntrusivePtr<TFilter> Filter_;

private:
    template<typename T>
    friend class TJobStatisticsEntry;
};

////////////////////////////////////////////////////////////////////

/// Class representing single statistic.
template <typename T>
class TJobStatisticsEntry
{
public:
    TJobStatisticsEntry(TMaybe<TJobStatistics::TDataEntry> data)
        : Data_(std::move(data))
    { }

    /// Sum of the statistic over all jobs.
    TMaybe<T> Sum() const
    {
        if (Data_) {
            return ConvertJobStatisticsEntry<T>(Data_->Sum);
        }
        return Nothing();
    }

    /// @brief Average of the statistic over all jobs.
    ///
    /// @note Only jobs that emitted statistics are taken into account.
    TMaybe<T> Avg() const
    {
        if (Data_ && Data_->Count) {
            return ConvertJobStatisticsEntry<T>(Data_->Sum / Data_->Count);
        }
        return Nothing();
    }

    /// @brief Number of jobs that emitted this statistic.
    TMaybe<T> Count() const
    {
        if (Data_) {
            return ConvertJobStatisticsEntry<T>(Data_->Count);
        }
        return Nothing();
    }

    /// @brief Maximum value of the statistic over all jobs.
    TMaybe<T> Max() const
    {
        if (Data_) {
            return ConvertJobStatisticsEntry<T>(Data_->Max);
        }
        return Nothing();
    }

    /// @brief Minimum value of the statistic over all jobs.
    TMaybe<T> Min() const
    {
        if (Data_) {
            return ConvertJobStatisticsEntry<T>(Data_->Min);
        }
        return Nothing();
    }

private:
    TMaybe<TJobStatistics::TDataEntry> Data_;

private:
    friend class TJobStatistics;
};

////////////////////////////////////////////////////////////////////

template <typename T>
TJobStatisticsEntry<T> TJobStatistics::GetStatisticsAs(TStringBuf name) const
{
    return TJobStatisticsEntry<T>(GetStatisticsImpl(name));
}

template <typename T>
TJobStatisticsEntry<T> TJobStatistics::GetCustomStatisticsAs(TStringBuf name) const
{
    return TJobStatisticsEntry<T>(GetStatisticsImpl(CustomStatisticsNamePrefix_ + name));
}

////////////////////////////////////////////////////////////////////

///
/// @brief Write [custom statistics](https://yt.yandex-team.ru/docs/description/mr/jobs#user_stats).
///
/// @param path Slash-separated path (length must not exceed 512 bytes).
/// @param value Value of the statistic.
///
/// @note The function must be called in job.
/// Total number of statistics (with different paths) must not exceed 128.
void WriteCustomStatistics(TStringBuf path, i64 value);

///
/// @brief Write several [custom statistics](https://yt.yandex-team.ru/docs/description/mr/jobs#user_stats) at once.
///
/// @param statistics A tree of map nodes with leaves of type `i64`.
///
/// @note The call is equivalent to calling @ref NYT::WriteCustomStatistics(TStringBuf, i64) for every path in the given map.
void WriteCustomStatistics(const TNode& statistics);

///
/// @brief Flush [custom statistics stream](https://yt.yandex-team.ru/docs/description/mr/jobs#user_stats)
///
void FlushCustomStatisticsStream();
////////////////////////////////////////////////////////////////////

} // namespace NYT