userver: userver/utils/statistics/percentile_format_json.hpp Source File
Loading...
Searching...
No Matches
percentile_format_json.hpp
1#pragma once
2
3#include <initializer_list>
4
5#include <userver/formats/json/value_builder.hpp>
6#include <userver/utils/assert.hpp>
7#include <userver/utils/meta_light.hpp>
8#include <userver/utils/statistics/metadata.hpp>
9#include <userver/utils/statistics/percentile.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace utils::statistics {
14
15namespace impl {
16
17template <typename T>
18using HasGetPercentileResult = decltype(std::declval<const T&>().GetPercentile(0.0));
19
20} // namespace impl
21
22std::string GetPercentileFieldName(double perc);
23
24template <typename T>
25formats::json::ValueBuilder PercentileToJson(const T& perc, std::initializer_list<double> percents) {
26 static_assert(
27 meta::kIsDetected<impl::HasGetPercentileResult, T>,
28 "T must specify T::GetPercentile(double) returning "
29 "json-serializable value"
30 );
31 formats::json::ValueBuilder result;
32 for (double percent : percents) {
33 result[GetPercentileFieldName(percent)] = perc.GetPercentile(percent);
34 }
35 utils::statistics::SolomonChildrenAreLabelValues(result, "percentile");
36 return result;
37}
38
39template <typename T>
40formats::json::ValueBuilder PercentileToJson(const T& perc) {
41 return PercentileToJson(perc, {0, 50, 90, 95, 98, 99, 99.6, 99.9, 100});
42}
43
44} // namespace utils::statistics
45
46USERVER_NAMESPACE_END