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

Trigger based on rate of change of values. More...

#include <alert_triggers.h>

Inheritance diagram for kcenon::monitoring::rate_of_change_trigger:
Inheritance graph
Collaboration diagram for kcenon::monitoring::rate_of_change_trigger:
Collaboration graph

Classes

struct  sample
 

Public Types

enum class  rate_direction { increasing , decreasing , either }
 Direction of rate change to monitor. More...
 

Public Member Functions

 rate_of_change_trigger (double rate_threshold, std::chrono::milliseconds window, rate_direction direction=rate_direction::either, size_t min_samples=2)
 Construct a rate of change trigger.
 
bool evaluate (double value) const override
 Evaluate the trigger condition.
 
std::string type_name () const override
 Get trigger type name.
 
std::string description () const override
 Get human-readable description.
 
void reset ()
 Clear accumulated samples.
 
- Public Member Functions inherited from kcenon::monitoring::alert_trigger
virtual ~alert_trigger ()=default
 

Private Member Functions

double calculate_rate () const
 

Private Attributes

double rate_threshold_
 
std::chrono::milliseconds window_
 
rate_direction direction_
 
size_t min_samples_
 
std::mutex mutex_
 
std::deque< samplesamples_
 

Detailed Description

Trigger based on rate of change of values.

Monitors how quickly a metric value is changing and triggers when the rate exceeds a threshold. Useful for detecting rapid increases or decreases in metrics.

Definition at line 241 of file alert_triggers.h.

Member Enumeration Documentation

◆ rate_direction

Direction of rate change to monitor.

Enumerator
increasing 

Positive rate of change.

decreasing 

Negative rate of change.

either 

Absolute rate of change.

Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_triggers.h.

Definition at line 247 of file alert_triggers.h.

Constructor & Destructor Documentation

◆ rate_of_change_trigger()

kcenon::monitoring::rate_of_change_trigger::rate_of_change_trigger ( double rate_threshold,
std::chrono::milliseconds window,
rate_direction direction = rate_direction::either,
size_t min_samples = 2 )
inline

Construct a rate of change trigger.

Parameters
rate_thresholdRate threshold per time window
windowTime window for rate calculation
directionDirection to monitor
min_samplesMinimum samples before triggering
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_triggers.h.

Definition at line 260 of file alert_triggers.h.

Member Function Documentation

◆ calculate_rate()

double kcenon::monitoring::rate_of_change_trigger::calculate_rate ( ) const
inlineprivate
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_triggers.h.

Definition at line 332 of file alert_triggers.h.

332 {
333 if (samples_.size() < 2) {
334 return 0.0;
335 }
336
337 // Use linear regression for smoother rate calculation
338 double sum_x = 0.0, sum_y = 0.0, sum_xy = 0.0, sum_xx = 0.0;
339 auto base_time = samples_.front().timestamp;
340
341 for (const auto& s : samples_) {
342 double x = std::chrono::duration<double, std::milli>(
343 s.timestamp - base_time).count();
344 double y = s.value;
345 sum_x += x;
346 sum_y += y;
347 sum_xy += x * y;
348 sum_xx += x * x;
349 }
350
351 double n = static_cast<double>(samples_.size());
352 double denominator = n * sum_xx - sum_x * sum_x;
353
354 if (std::abs(denominator) < 1e-10) {
355 return 0.0;
356 }
357
358 // Slope is rate of change per millisecond
359 double slope = (n * sum_xy - sum_x * sum_y) / denominator;
360
361 // Convert to rate per window
362 return slope * static_cast<double>(window_.count());
363 }

References samples_, and window_.

Referenced by evaluate().

Here is the caller graph for this function:

◆ description()

std::string kcenon::monitoring::rate_of_change_trigger::description ( ) const
inlineoverridevirtual

Get human-readable description.

Returns
Description string

Implements kcenon::monitoring::alert_trigger.

Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_triggers.h.

Definition at line 307 of file alert_triggers.h.

307 {
308 std::string dir_str;
309 switch (direction_) {
310 case rate_direction::increasing: dir_str = "increase"; break;
311 case rate_direction::decreasing: dir_str = "decrease"; break;
312 case rate_direction::either: dir_str = "change"; break;
313 }
314 return dir_str + " rate > " + std::to_string(rate_threshold_) +
315 " per " + std::to_string(window_.count()) + "ms";
316 }

References decreasing, direction_, either, increasing, rate_threshold_, and window_.

◆ evaluate()

bool kcenon::monitoring::rate_of_change_trigger::evaluate ( double value) const
inlineoverridevirtual

Evaluate the trigger condition.

Parameters
valueCurrent metric value
Returns
True if trigger condition is met

Implements kcenon::monitoring::alert_trigger.

Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_triggers.h.

Definition at line 269 of file alert_triggers.h.

269 {
270 auto now = std::chrono::steady_clock::now();
271
272 std::lock_guard<std::mutex> lock(mutex_);
273
274 // Add new sample
275 samples_.push_back({value, now});
276
277 // Remove old samples outside window
278 auto cutoff = now - window_;
279 while (!samples_.empty() && samples_.front().timestamp < cutoff) {
280 samples_.pop_front();
281 }
282
283 // Need minimum samples to calculate rate
284 if (samples_.size() < min_samples_) {
285 return false;
286 }
287
288 // Calculate rate of change
289 double rate = calculate_rate();
290
291 switch (direction_) {
293 return rate > rate_threshold_;
295 return rate < -rate_threshold_;
297 return std::abs(rate) > rate_threshold_;
298 default:
299 return false;
300 }
301 }

References calculate_rate(), decreasing, direction_, either, increasing, min_samples_, mutex_, rate_threshold_, samples_, and window_.

Here is the call graph for this function:

◆ reset()

void kcenon::monitoring::rate_of_change_trigger::reset ( )
inline

Clear accumulated samples.

Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_triggers.h.

Definition at line 321 of file alert_triggers.h.

321 {
322 std::lock_guard<std::mutex> lock(mutex_);
323 samples_.clear();
324 }

References mutex_, and samples_.

◆ type_name()

std::string kcenon::monitoring::rate_of_change_trigger::type_name ( ) const
inlineoverridevirtual

Get trigger type name.

Returns
Type name string

Implements kcenon::monitoring::alert_trigger.

Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_triggers.h.

Definition at line 303 of file alert_triggers.h.

303 {
304 return "rate_of_change";
305 }

Member Data Documentation

◆ direction_

rate_direction kcenon::monitoring::rate_of_change_trigger::direction_
private

◆ min_samples_

size_t kcenon::monitoring::rate_of_change_trigger::min_samples_
private

◆ mutex_

std::mutex kcenon::monitoring::rate_of_change_trigger::mutex_
mutableprivate

◆ rate_threshold_

double kcenon::monitoring::rate_of_change_trigger::rate_threshold_
private

◆ samples_

std::deque<sample> kcenon::monitoring::rate_of_change_trigger::samples_
mutableprivate

◆ window_

std::chrono::milliseconds kcenon::monitoring::rate_of_change_trigger::window_
private

The documentation for this class was generated from the following file: