job_statistics.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #pragma once
  2. ///
  3. /// @file yt/cpp/mapreduce/interface/job_statistics.h
  4. ///
  5. /// Header containing classes and utility functions to work with
  6. /// [job statistics](https://ytsaurus.tech/docs/ru/user-guide/problems/jobstatistics).
  7. #include "fwd.h"
  8. #include <library/cpp/yson/node/node.h>
  9. #include <util/system/defaults.h>
  10. #include <util/generic/maybe.h>
  11. #include <util/generic/ptr.h>
  12. namespace NYT {
  13. ////////////////////////////////////////////////////////////////////
  14. ///
  15. /// @brief Convert i64 representation of statistics to other type.
  16. ///
  17. /// Library defines this template for types TDuration and i64.
  18. /// Users may define it for their types.
  19. ///
  20. /// @see @ref NYT::TJobStatistics::GetStatisticsAs method.
  21. template <typename T>
  22. T ConvertJobStatisticsEntry(i64 value);
  23. ////////////////////////////////////////////////////////////////////
  24. /// Class representing a collection of job statistics.
  25. class TJobStatistics
  26. {
  27. public:
  28. ///
  29. /// Construct empty statistics.
  30. TJobStatistics();
  31. ///
  32. /// Construct statistics from statistics node.
  33. TJobStatistics(const NYT::TNode& statistics);
  34. TJobStatistics(const TJobStatistics& jobStatistics);
  35. TJobStatistics(TJobStatistics&& jobStatistics);
  36. TJobStatistics& operator=(const TJobStatistics& jobStatistics);
  37. TJobStatistics& operator=(TJobStatistics&& jobStatistics);
  38. ~TJobStatistics();
  39. ///
  40. /// @brief Filter statistics by task name.
  41. ///
  42. /// @param taskNames What task names to include (empty means all).
  43. TJobStatistics TaskName(TVector<TTaskName> taskNames) const;
  44. ///
  45. /// @brief Filter statistics by job state.
  46. ///
  47. /// @param filter What job states to include (empty means all).
  48. ///
  49. /// @note Default statistics include only (successfully) completed jobs.
  50. TJobStatistics JobState(TVector<EJobState> filter) const;
  51. ///
  52. /// @brief Filter statistics by job type.
  53. ///
  54. /// @param filter What job types to include (empty means all).
  55. ///
  56. /// @deprecated Use @ref TJobStatistics::TaskName instead.
  57. ///
  58. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/jobs#obshaya-shema
  59. TJobStatistics JobType(TVector<EJobType> filter) const;
  60. ///
  61. /// @brief Check that given statistics exist.
  62. ///
  63. /// @param name Slash separated statistics name, e.g. "time/total" (like it appears in web interface).
  64. bool HasStatistics(TStringBuf name) const;
  65. ///
  66. /// @brief Get statistics by name.
  67. ///
  68. /// @param name Slash separated statistics name, e.g. "time/total" (like it appears in web interface).
  69. ///
  70. /// @note If statistics is missing an exception is thrown. If because of filters
  71. /// no fields remain the returned value is empty (all fields are `Nothing`).
  72. ///
  73. /// @note We don't use `TMaybe<TJobStatisticsEntry>` here;
  74. /// instead, @ref NYT::TJobStatisticsEntry methods return `TMaybe<i64>`,
  75. /// so user easier use `.GetOrElse`:
  76. /// ```
  77. /// jobStatistics.GetStatistics("some/statistics/name").Max().GetOrElse(0);
  78. /// ```
  79. TJobStatisticsEntry<i64> GetStatistics(TStringBuf name) const;
  80. ///
  81. /// @brief Get statistics by name.
  82. ///
  83. /// @param name Slash separated statistics name, e.g. "time/total" (like it appears in web interface).
  84. ///
  85. /// @note In order to use `GetStatisticsAs` method, @ref NYT::ConvertJobStatisticsEntry function must be defined
  86. /// (the library defines it for `i64` and `TDuration`, user may define it for other types).
  87. template <typename T>
  88. TJobStatisticsEntry<T> GetStatisticsAs(TStringBuf name) const;
  89. ///
  90. /// Get (slash separated) names of statistics.
  91. TVector<TString> GetStatisticsNames() const;
  92. ///
  93. /// @brief Check if given custom statistics exists.
  94. ///
  95. /// @param name Slash separated custom statistics name.
  96. bool HasCustomStatistics(TStringBuf name) const;
  97. ///
  98. /// @brief Get custom statistics (those the user can write in job with @ref NYT::WriteCustomStatistics).
  99. ///
  100. /// @param name Slash separated custom statistics name.
  101. TJobStatisticsEntry<i64> GetCustomStatistics(TStringBuf name) const;
  102. ///
  103. /// @brief Get custom statistics (those the user can write in job with @ref NYT::WriteCustomStatistics).
  104. ///
  105. /// @param name Slash separated custom statistics name.
  106. template <typename T>
  107. TJobStatisticsEntry<T> GetCustomStatisticsAs(TStringBuf name) const;
  108. ///
  109. /// Get names of all custom statistics.
  110. TVector<TString> GetCustomStatisticsNames() const;
  111. private:
  112. class TData;
  113. struct TFilter;
  114. struct TDataEntry {
  115. i64 Max;
  116. i64 Min;
  117. i64 Sum;
  118. i64 Count;
  119. };
  120. static const TString CustomStatisticsNamePrefix_;
  121. private:
  122. TJobStatistics(::TIntrusivePtr<TData> data, ::TIntrusivePtr<TFilter> filter);
  123. TMaybe<TDataEntry> GetStatisticsImpl(TStringBuf name) const;
  124. private:
  125. ::TIntrusivePtr<TData> Data_;
  126. ::TIntrusivePtr<TFilter> Filter_;
  127. private:
  128. template<typename T>
  129. friend class TJobStatisticsEntry;
  130. };
  131. ////////////////////////////////////////////////////////////////////
  132. /// Class representing single statistic.
  133. template <typename T>
  134. class TJobStatisticsEntry
  135. {
  136. public:
  137. TJobStatisticsEntry(TMaybe<TJobStatistics::TDataEntry> data)
  138. : Data_(std::move(data))
  139. { }
  140. /// Sum of the statistic over all jobs.
  141. TMaybe<T> Sum() const
  142. {
  143. if (Data_) {
  144. return ConvertJobStatisticsEntry<T>(Data_->Sum);
  145. }
  146. return Nothing();
  147. }
  148. /// @brief Average of the statistic over all jobs.
  149. ///
  150. /// @note Only jobs that emitted statistics are taken into account.
  151. TMaybe<T> Avg() const
  152. {
  153. if (Data_ && Data_->Count) {
  154. return ConvertJobStatisticsEntry<T>(Data_->Sum / Data_->Count);
  155. }
  156. return Nothing();
  157. }
  158. /// @brief Number of jobs that emitted this statistic.
  159. TMaybe<T> Count() const
  160. {
  161. if (Data_) {
  162. return ConvertJobStatisticsEntry<T>(Data_->Count);
  163. }
  164. return Nothing();
  165. }
  166. /// @brief Maximum value of the statistic over all jobs.
  167. TMaybe<T> Max() const
  168. {
  169. if (Data_) {
  170. return ConvertJobStatisticsEntry<T>(Data_->Max);
  171. }
  172. return Nothing();
  173. }
  174. /// @brief Minimum value of the statistic over all jobs.
  175. TMaybe<T> Min() const
  176. {
  177. if (Data_) {
  178. return ConvertJobStatisticsEntry<T>(Data_->Min);
  179. }
  180. return Nothing();
  181. }
  182. private:
  183. TMaybe<TJobStatistics::TDataEntry> Data_;
  184. private:
  185. friend class TJobStatistics;
  186. };
  187. ////////////////////////////////////////////////////////////////////
  188. template <typename T>
  189. TJobStatisticsEntry<T> TJobStatistics::GetStatisticsAs(TStringBuf name) const
  190. {
  191. return TJobStatisticsEntry<T>(GetStatisticsImpl(name));
  192. }
  193. template <typename T>
  194. TJobStatisticsEntry<T> TJobStatistics::GetCustomStatisticsAs(TStringBuf name) const
  195. {
  196. return TJobStatisticsEntry<T>(GetStatisticsImpl(CustomStatisticsNamePrefix_ + name));
  197. }
  198. ////////////////////////////////////////////////////////////////////
  199. ///
  200. /// @brief Write [custom statistics](https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/jobs#user_stats).
  201. ///
  202. /// @param path Slash-separated path (length must not exceed 512 bytes).
  203. /// @param value Value of the statistic.
  204. ///
  205. /// @note The function must be called in job.
  206. /// Total number of statistics (with different paths) must not exceed 128.
  207. void WriteCustomStatistics(TStringBuf path, i64 value);
  208. ///
  209. /// @brief Write several [custom statistics](https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/jobs#user_stats) at once.
  210. ///
  211. /// @param statistics A tree of map nodes with leaves of type `i64`.
  212. ///
  213. /// @note The call is equivalent to calling @ref NYT::WriteCustomStatistics(TStringBuf, i64) for every path in the given map.
  214. void WriteCustomStatistics(const TNode& statistics);
  215. ///
  216. /// @brief Flush [custom statistics stream](https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/jobs#user_stats)
  217. ///
  218. void FlushCustomStatisticsStream();
  219. ////////////////////////////////////////////////////////////////////
  220. } // namespace NYT