Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::stats Namespace Reference

Namespaces

namespace  detail
 

Classes

struct  statistics
 Statistical summary for a collection of values. More...
 

Functions

template<typename T >
percentile (const std::vector< T > &sorted_values, double percentile_value)
 
template<typename T >
statistics< T > compute_sorted (const std::vector< T > &sorted_values)
 Compute statistics from sorted values.
 
template<typename T >
statistics< T > compute (const std::vector< T > &values)
 
template<typename T >
statistics< T > compute_inplace (std::vector< T > &values)
 Compute statistics in place (modifies input)
 

Function Documentation

◆ compute()

template<typename T >
statistics< T > kcenon::monitoring::stats::compute ( const std::vector< T > & values)
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/utils/statistics.h.

Definition at line 228 of file statistics.h.

228 {
229 if (values.empty()) {
230 return compute_sorted<T>({});
231 }
232
233 std::vector<T> sorted = values;
234 std::sort(sorted.begin(), sorted.end());
235 return compute_sorted(sorted);
236}

References compute_sorted().

Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compute_inplace()

template<typename T >
statistics< T > kcenon::monitoring::stats::compute_inplace ( std::vector< T > & values)

Compute statistics in place (modifies input)

This is more efficient when you don't need to preserve the original order.

Template Parameters
TValue type
Parameters
valuesVector of values (will be sorted in place)
Returns
statistics<T> containing all computed statistics
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/utils/statistics.h.

Definition at line 248 of file statistics.h.

248 {
249 if (values.empty()) {
250 return compute_sorted<T>({});
251 }
252
253 std::sort(values.begin(), values.end());
254 return compute_sorted(values);
255}

References compute_sorted().

Referenced by TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compute_sorted()

template<typename T >
statistics< T > kcenon::monitoring::stats::compute_sorted ( const std::vector< T > & sorted_values)

Compute statistics from sorted values.

This is more efficient when you already have sorted data.

Template Parameters
TValue type
Parameters
sorted_valuesVector of sorted values (ascending order)
Returns
statistics<T> containing all computed statistics
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/utils/statistics.h.

Definition at line 173 of file statistics.h.

173 {
174 statistics<T> result{};
175
176 if (sorted_values.empty()) {
177 result.min = detail::zero_value<T>();
178 result.max = detail::zero_value<T>();
179 result.mean = detail::zero_value<T>();
180 result.median = detail::zero_value<T>();
181 result.p95 = detail::zero_value<T>();
182 result.p99 = detail::zero_value<T>();
183 result.total = detail::zero_value<T>();
184 result.count = 0;
185 return result;
186 }
187
188 result.count = sorted_values.size();
189 result.min = sorted_values.front();
190 result.max = sorted_values.back();
191
192 // Calculate total
193 result.total = std::accumulate(sorted_values.begin(), sorted_values.end(),
194 detail::zero_value<T>());
195
196 // Calculate mean
197 result.mean = detail::divide(result.total, result.count);
198
199 // Calculate percentiles
200 result.median = percentile(sorted_values, 50.0);
201 result.p95 = percentile(sorted_values, 95.0);
202 result.p99 = percentile(sorted_values, 99.0);
203
204 return result;
205}
Statistical summary for a collection of values.
Definition statistics.h:34

References kcenon::monitoring::stats::statistics< T >::count, kcenon::monitoring::stats::detail::divide(), percentile(), and kcenon::monitoring::stats::detail::zero_value().

Referenced by compute(), compute_inplace(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ percentile()

template<typename T >
T kcenon::monitoring::stats::percentile ( const std::vector< T > & sorted_values,
double percentile_value )
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/utils/statistics.h.

Definition at line 130 of file statistics.h.

130 {
131 if (sorted_values.empty()) {
132 return detail::zero_value<T>();
133 }
134
135 if (percentile_value <= 0.0) {
136 return sorted_values.front();
137 }
138
139 if (percentile_value >= 100.0) {
140 return sorted_values.back();
141 }
142
143 double rank = (percentile_value / 100.0) * (sorted_values.size() - 1);
144 size_t lower_idx = static_cast<size_t>(rank);
145 size_t upper_idx = lower_idx + 1;
146
147 if (upper_idx >= sorted_values.size()) {
148 return sorted_values[lower_idx];
149 }
150
151 // For chrono types, use simple index-based selection
152 // For numeric types, use linear interpolation
153 if constexpr (detail::is_chrono_duration_v<T>) {
154 // Use nearest-rank method for duration types
155 return sorted_values[static_cast<size_t>(std::round(rank))];
156 } else {
157 double fraction = rank - static_cast<double>(lower_idx);
158 return sorted_values[lower_idx] +
159 static_cast<T>(fraction * (sorted_values[upper_idx] - sorted_values[lower_idx]));
160 }
161}

References kcenon::monitoring::stats::detail::is_chrono_duration_v, and kcenon::monitoring::stats::detail::zero_value().

Referenced by compute_sorted(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function: