Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::moving_window_aggregator< T > Class Template Reference

Time-windowed value collection. More...

#include <stream_aggregator.h>

Collaboration diagram for kcenon::monitoring::moving_window_aggregator< T >:
Collaboration graph

Classes

struct  entry
 

Public Types

using time_point = std::chrono::system_clock::time_point
 
using duration = std::chrono::system_clock::duration
 

Public Member Functions

 moving_window_aggregator (std::chrono::milliseconds window_duration, size_t max_size)
 Constructor.
 
void add_value (const T &value, time_point timestamp)
 Add a value with timestamp.
 
size_t size () const
 Get current size.
 
bool empty () const
 Check if empty.
 
std::vector< T > get_values () const
 Get all values in the window.
 
void clear ()
 Clear all entries.
 

Private Member Functions

void expire_old_entries (time_point current)
 

Private Attributes

std::shared_mutex mutex_
 
std::chrono::milliseconds window_duration_
 
size_t max_size_
 
std::deque< entryentries_
 

Detailed Description

template<typename T>
class kcenon::monitoring::moving_window_aggregator< T >

Time-windowed value collection.

Maintains a sliding window of values with automatic expiration.

Template Parameters
TThe value type

Definition at line 396 of file stream_aggregator.h.

Member Typedef Documentation

◆ duration

template<typename T >
using kcenon::monitoring::moving_window_aggregator< T >::duration = std::chrono::system_clock::duration

Definition at line 399 of file stream_aggregator.h.

◆ time_point

template<typename T >
using kcenon::monitoring::moving_window_aggregator< T >::time_point = std::chrono::system_clock::time_point

Definition at line 398 of file stream_aggregator.h.

Constructor & Destructor Documentation

◆ moving_window_aggregator()

template<typename T >
kcenon::monitoring::moving_window_aggregator< T >::moving_window_aggregator ( std::chrono::milliseconds window_duration,
size_t max_size )
inline

Constructor.

Parameters
window_durationDuration of the sliding window
max_sizeMaximum number of elements

Definition at line 406 of file stream_aggregator.h.

408 : window_duration_(window_duration)
409 , max_size_(max_size) {}

Member Function Documentation

◆ add_value()

template<typename T >
void kcenon::monitoring::moving_window_aggregator< T >::add_value ( const T & value,
time_point timestamp )
inline

Add a value with timestamp.

Parameters
valueThe value to add
timestampThe timestamp

Definition at line 416 of file stream_aggregator.h.

416 {
417 std::unique_lock<std::shared_mutex> lock(mutex_);
418
419 // Remove expired entries
420 expire_old_entries(timestamp);
421
422 // Add new entry
423 if (entries_.size() >= max_size_) {
424 entries_.pop_front();
425 }
426 entries_.push_back({value, timestamp});
427 }

References kcenon::monitoring::moving_window_aggregator< T >::entries_, kcenon::monitoring::moving_window_aggregator< T >::expire_old_entries(), kcenon::monitoring::moving_window_aggregator< T >::max_size_, and kcenon::monitoring::moving_window_aggregator< T >::mutex_.

Referenced by TEST_F(), and TEST_F().

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

◆ clear()

template<typename T >
void kcenon::monitoring::moving_window_aggregator< T >::clear ( )
inline

Clear all entries.

Definition at line 461 of file stream_aggregator.h.

461 {
462 std::unique_lock<std::shared_mutex> lock(mutex_);
463 entries_.clear();
464 }

References kcenon::monitoring::moving_window_aggregator< T >::entries_, and kcenon::monitoring::moving_window_aggregator< T >::mutex_.

◆ empty()

template<typename T >
bool kcenon::monitoring::moving_window_aggregator< T >::empty ( ) const
inline

Check if empty.

Definition at line 440 of file stream_aggregator.h.

440 {
441 std::shared_lock<std::shared_mutex> lock(mutex_);
442 return entries_.empty();
443 }

References kcenon::monitoring::moving_window_aggregator< T >::entries_, and kcenon::monitoring::moving_window_aggregator< T >::mutex_.

◆ expire_old_entries()

template<typename T >
void kcenon::monitoring::moving_window_aggregator< T >::expire_old_entries ( time_point current)
inlineprivate

Definition at line 472 of file stream_aggregator.h.

472 {
473 auto cutoff = current - window_duration_;
474 while (!entries_.empty() && entries_.front().timestamp < cutoff) {
475 entries_.pop_front();
476 }
477 }

References kcenon::monitoring::moving_window_aggregator< T >::entries_, and kcenon::monitoring::moving_window_aggregator< T >::window_duration_.

Referenced by kcenon::monitoring::moving_window_aggregator< T >::add_value().

Here is the caller graph for this function:

◆ get_values()

template<typename T >
std::vector< T > kcenon::monitoring::moving_window_aggregator< T >::get_values ( ) const
inline

Get all values in the window.

Definition at line 448 of file stream_aggregator.h.

448 {
449 std::shared_lock<std::shared_mutex> lock(mutex_);
450 std::vector<T> result;
451 result.reserve(entries_.size());
452 for (const auto& entry : entries_) {
453 result.push_back(entry.value);
454 }
455 return result;
456 }

References kcenon::monitoring::moving_window_aggregator< T >::entries_, kcenon::monitoring::moving_window_aggregator< T >::mutex_, and kcenon::monitoring::moving_window_aggregator< T >::entry::value.

Referenced by TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ size()

template<typename T >
size_t kcenon::monitoring::moving_window_aggregator< T >::size ( ) const
inline

Get current size.

Definition at line 432 of file stream_aggregator.h.

432 {
433 std::shared_lock<std::shared_mutex> lock(mutex_);
434 return entries_.size();
435 }

References kcenon::monitoring::moving_window_aggregator< T >::entries_, and kcenon::monitoring::moving_window_aggregator< T >::mutex_.

Referenced by TEST_F().

Here is the caller graph for this function:

Member Data Documentation

◆ entries_

◆ max_size_

template<typename T >
size_t kcenon::monitoring::moving_window_aggregator< T >::max_size_
private

◆ mutex_

◆ window_duration_

template<typename T >
std::chrono::milliseconds kcenon::monitoring::moving_window_aggregator< T >::window_duration_
private

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