userver: userver/utils/statistics/percentile_format_json.hpp Source File
Loading...
Searching...
No Matches
percentile_format_json.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/statistics/percentile_format_json.hpp
4/// @brief JSON formatting helpers for percentile metrics
5
6#include <initializer_list>
7
8#include <userver/formats/json/value_builder.hpp>
9#include <userver/utils/assert.hpp>
10#include <userver/utils/statistics/metadata.hpp>
11#include <userver/utils/statistics/percentile.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace utils::statistics {
16
17std::string GetPercentileFieldName(double perc);
18
19template <typename T>
20formats::json::ValueBuilder PercentileToJson(const T& perc, std::initializer_list<double> percents) {
21 static_assert(
22 requires(const T& t) { t.GetPercentile(0.0); },
23 "T must specify T::GetPercentile(double) returning "
24 "json-serializable value"
25 );
26 formats::json::ValueBuilder result;
27 for (const double percent : percents) {
28 result[GetPercentileFieldName(percent)] = perc.GetPercentile(percent);
29 }
31 return result;
32}
33
34template <typename T>
35formats::json::ValueBuilder PercentileToJson(const T& perc) {
36 return PercentileToJson(perc, {0, 50, 90, 95, 98, 99, 99.6, 99.9, 100});
37}
38
39} // namespace utils::statistics
40
41USERVER_NAMESPACE_END