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

Classes

class  time_series_ring_buffer
 Generic time-series ring buffer base template. More...
 

Functions

double calculate_percentile (const std::vector< double > &sorted_values, double percentile)
 Calculate percentile from sorted values.
 
time_series_statistics calculate_basic_statistics (const std::vector< double > &values, std::chrono::system_clock::time_point oldest_timestamp, std::chrono::system_clock::time_point newest_timestamp)
 Calculate basic statistics from a vector of double values.
 

Function Documentation

◆ calculate_basic_statistics()

time_series_statistics kcenon::monitoring::detail::calculate_basic_statistics ( const std::vector< double > & values,
std::chrono::system_clock::time_point oldest_timestamp,
std::chrono::system_clock::time_point newest_timestamp )
inline

Calculate basic statistics from a vector of double values.

Parameters
valuesVector of values to analyze
oldest_timestampTimestamp of oldest sample
newest_timestampTimestamp of newest sample
Returns
Calculated statistics

Definition at line 140 of file time_series_buffer.h.

143 {
144
146 stats.sample_count = values.size();
147
148 if (values.empty()) {
149 stats.min_value = 0.0;
150 stats.max_value = 0.0;
151 return stats;
152 }
153
154 stats.oldest_timestamp = oldest_timestamp;
155 stats.newest_timestamp = newest_timestamp;
156
157 double sum = 0.0;
158 for (double val : values) {
159 sum += val;
160 stats.min_value = (std::min)(stats.min_value, val);
161 stats.max_value = (std::max)(stats.max_value, val);
162 }
163 stats.avg = sum / values.size();
164
165 double variance = 0.0;
166 for (double val : values) {
167 double diff = val - stats.avg;
168 variance += diff * diff;
169 }
170 stats.stddev = std::sqrt(variance / values.size());
171
172 std::vector<double> sorted_values = values;
173 std::sort(sorted_values.begin(), sorted_values.end());
174 stats.p95 = calculate_percentile(sorted_values, 95.0);
175 stats.p99 = calculate_percentile(sorted_values, 99.0);
176
177 return stats;
178}
Statistics calculated from time series data.
std::chrono::system_clock::time_point newest_timestamp
std::chrono::system_clock::time_point oldest_timestamp

References kcenon::monitoring::time_series_statistics::avg, calculate_percentile(), kcenon::monitoring::time_series_statistics::max_value, kcenon::monitoring::time_series_statistics::min_value, kcenon::monitoring::time_series_statistics::newest_timestamp, kcenon::monitoring::time_series_statistics::oldest_timestamp, kcenon::monitoring::time_series_statistics::p95, kcenon::monitoring::time_series_statistics::p99, kcenon::monitoring::time_series_statistics::sample_count, and kcenon::monitoring::time_series_statistics::stddev.

Referenced by kcenon::monitoring::load_average_history::calculate_statistics(), and kcenon::monitoring::time_series_buffer< T >::calculate_statistics().

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

◆ calculate_percentile()

double kcenon::monitoring::detail::calculate_percentile ( const std::vector< double > & sorted_values,
double percentile )
inline

Calculate percentile from sorted values.

Parameters
sorted_valuesPre-sorted vector of values
percentilePercentile to calculate (0-100)
Returns
Calculated percentile value

Definition at line 110 of file time_series_buffer.h.

111 {
112 if (sorted_values.empty()) {
113 return 0.0;
114 }
115 if (sorted_values.size() == 1) {
116 return sorted_values[0];
117 }
118
119 double rank = (percentile / 100.0) * (sorted_values.size() - 1);
120 size_t lower_idx = static_cast<size_t>(rank);
121 size_t upper_idx = lower_idx + 1;
122 double fraction = rank - lower_idx;
123
124 if (upper_idx >= sorted_values.size()) {
125 return sorted_values[lower_idx];
126 }
127
128 return sorted_values[lower_idx] +
129 fraction * (sorted_values[upper_idx] - sorted_values[lower_idx]);
130}

Referenced by calculate_basic_statistics().

Here is the caller graph for this function: