RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
AlertManager.h
Go to the documentation of this file.
1#pragma once
2#include "Common.h"
3#include "Data/AlertEvent.h"
4
5namespace RumPi
6{
16 {
17 protected:
19 std::mutex myMutex;
20
22 std::map<std::size_t, std::function<void(const AlertEvent&)>> mySubscribers;
23
25 std::map<std::string, EAlertLevel> myLastLevels;
26
28 std::size_t myNextSubscriberId;
29
48 EAlertLevel Evaluate(const float value, const float warningThreshold, const float alertThreshold, const bool higherIsWorse);
49
67 std::string BuildMessage(const std::string& metricName, const float value, const float threshold);
68
83 void CopySubscribersLocked(std::vector<std::function<void(const AlertEvent&)>>& outSubscribers) const;
84
85 private:
97
108 virtual ~AlertManager();
109
110 public:
123 static AlertManager& GetInstance();
124
139 std::size_t Subscribe(std::function<void(const AlertEvent&)> callback);
140
153 void Unsubscribe(std::size_t subscription);
154
172 void Check(const std::string& metricName, const float value, const float warningThreshold, const float alertThreshold, const bool higherIsWorse);
173
189 void Report(const EAlertLevel level, const std::string& message);
190
206 static std::string LevelToString(const EAlertLevel level);
207
209 AlertManager(const AlertManager&) = delete;
211 };
212} // namespace RumPi
A single alert event: a metric crossing (or recovering from) a threshold, with the reading that trigg...
Definition: AlertEvent.h:13
A singleton that components hand readings to.
Definition: AlertManager.h:16
void Unsubscribe(std::size_t subscription)
Removes a previously registered callback by its handle; unknown or already-removed handles are ignore...
Definition: AlertManager.cpp:94
std::size_t myNextSubscriberId
Monotonic source of subscription handles; only ever increments so a handle is never reused.
Definition: AlertManager.h:28
std::size_t Subscribe(std::function< void(const AlertEvent &)> callback)
Registers a callback (e.g.
Definition: AlertManager.cpp:70
AlertManager(const AlertManager &)=delete
Singletons must not be copied or moved.
virtual ~AlertManager()
Destroys the alert manager.
Definition: AlertManager.cpp:31
void CopySubscribersLocked(std::vector< std::function< void(const AlertEvent &)> > &outSubscribers) const
Copies the current subscriber callbacks into outSubscribers so the dispatch loop can run them after t...
Definition: AlertManager.cpp:115
std::map< std::size_t, std::function< void(const AlertEvent &)> > mySubscribers
Who to notify, keyed by the handle Subscribe() hands out so Unsubscribe() can remove one.
Definition: AlertManager.h:22
AlertManager()
Builds the alert manager, starting subscription handles at 1.
Definition: AlertManager.cpp:16
std::string BuildMessage(const std::string &metricName, const float value, const float threshold)
Builds a "metric = value (threshold N)" summary string for an alert event.
Definition: AlertManager.cpp:354
EAlertLevel Evaluate(const float value, const float warningThreshold, const float alertThreshold, const bool higherIsWorse)
Evaluates a reading against its warning/alert thresholds, using higherIsWorse to choose the direction...
Definition: AlertManager.cpp:142
std::mutex myMutex
Guards the subscriber map, the per-metric levels, and the subscription-id counter below.
Definition: AlertManager.h:19
static AlertManager & GetInstance()
Gets the singleton instance, constructing it thread-safely on first call.
Definition: AlertManager.cpp:48
AlertManager & operator=(const AlertManager &)=delete
void Report(const EAlertLevel level, const std::string &message)
Logs a free-text message (a failure, error, or trace) through the alert system.
Definition: AlertManager.cpp:272
static std::string LevelToString(const EAlertLevel level)
Converts an alert level to its display string, useful for subscribers that want to format the level.
Definition: AlertManager.cpp:317
void Check(const std::string &metricName, const float value, const float warningThreshold, const float alertThreshold, const bool higherIsWorse)
A component calls this after each read: evaluates the reading, and only when the metric changes level...
Definition: AlertManager.cpp:191
std::map< std::string, EAlertLevel > myLastLevels
Last level seen per metric.
Definition: AlertManager.h:25
RumPi is the library's top-level facade.
Definition: AlertManager.h:6
EAlertLevel
Severity of a metric relative to its thresholds.
Definition: Common.h:94