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 =
19 decltype(std::declval<const T&>().GetPercentile(0.0));
20
21} // namespace impl
22
23std::string GetPercentileFieldName(double perc);
24
25template <typename T>
26formats::json::ValueBuilder PercentileToJson(
27 const T& perc, std::initializer_list<double> percents) {
28 static_assert(meta::kIsDetected<impl::HasGetPercentileResult, T>,
29 "T must specify T::GetPercentile(double) returning "
30 "json-serializable value");
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