Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/code/StatisticsBackendTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,8 @@ void set_alert_example()
"my_topic", // Topic name to monitor
AlertKind::NEW_DATA_ALERT, // Kind of alert
0.0, // Threshold (Ignored for new data)
std::chrono::milliseconds(500) // Minimum time between two consecutive triggers (in ms)
);
std::chrono::milliseconds(500), // Minimum time between two consecutive triggers (in ms)
"/some_path/script.sh");
//!--
}
}
Expand Down
10 changes: 10 additions & 0 deletions docs/rst/api-reference/types/alert_info.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.. _api_alert_info:

.. rst-class:: api-ref

AlertInfo
---------

.. doxygenclass:: eprosima::statistics_backend::AlertInfo
:project: fastdds_statistics_backend
:members:
10 changes: 10 additions & 0 deletions docs/rst/api-reference/types/notifier.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.. _api_types_notifier:

.. rst-class:: api-ref

Notifier
---------

.. doxygenclass:: eprosima::statistics_backend::Notifier
:project: fastdds_statistics_backend
:members:
2 changes: 2 additions & 0 deletions docs/rst/api-reference/types/types_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ Types
/rst/api-reference/types/statusdata
/rst/api-reference/types/timestamp
/rst/api-reference/types/jsontags
/rst/api-reference/types/alert_info
/rst/api-reference/types/notifier
3 changes: 3 additions & 0 deletions docs/rst/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ metatraffic
monitorization
monitorizations
namespace
NewDataAlertInfo
NoDataAlertInfo
Ok
ostream
OStream
preprocessed
Qos
QoS
ScriptNotifier
Subclassed
timepoint
Todo
Expand Down
1 change: 1 addition & 0 deletions docs/rst/statistics_backend/set_alert.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Set entity alert
Function |set_alert-api| allows the user to set an alert on a specific entity.
When the alert is triggered, the listener callback *on_alert_triggered* will be called with the corresponding parameters.
In addition, if no entity matches the alert, the listener callback *on_alert_unmatched* will be called periodically
If a valid script is passed as an advanced parameter, it will be executed when the alert is triggered.

.. literalinclude:: /code/StatisticsBackendTests.cpp
:language: c++
Expand Down
73 changes: 0 additions & 73 deletions docs/rst/types/alert_info.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,78 +9,5 @@ Represents an alert configuration and its trigger conditions.
The ``AlertInfo`` class stores metadata and logic for determining
when an alert should be triggered based on value comparisons and timing constraints.

**Private Members:**

- ``AlertId id``
Unique identifier for the alert.

- ``AlertKind alert_kind``
Type or category of the alert.

- ``std::string name``
Name of the alert.

- ``EntityId domain_id``
Identifier of the domain this alert belongs to.

- ``std::string host_name``
Host name related to the alert condition.

- ``std::string user_name``
User name related to the alert condition.

- ``std::string topic_name``
Topic name related to the alert condition.

- ``AlertComparison cmp``
Comparison operator defining the alert trigger logic (e.g., greater than, less than).

- ``double trigger_threshold``
Threshold value for triggering the alert.

- ``std::chrono::system_clock::time_point last_trigger``
Timestamp of the last alert trigger.

- ``std::chrono::milliseconds time_between_triggers``
Minimum time interval between consecutive triggers.

**Constructors:**

.. cpp:function:: AlertInfo()

Default constructor.

.. cpp:function:: AlertInfo(AlertKind alert_kind, std::string name, EntityId domain_id, std::string host_name, std::string user_name, std::string topic_name, AlertComparison cmp, double trigger_threshold, std::chrono::milliseconds time_between_triggers)

Initializes a new alert with the given parameters and sets the last trigger time.

**Public Member Functions:**

.. cpp:function:: void reset_trigger_time()

Resets the last trigger timestamp to the current system time.

.. cpp:function:: bool entity_matches(std::string stat_host, std::string stat_user, std::string stat_topic) const

Checks whether the provided host, user, and topic match the alert’s target entities.
Returns ``true`` if all specified fields match with those defined in the alert. Note that if the AlertInfo
structure does not have a value for a field (i.e., it is an empty string), that field is considered a match.

.. cpp:function:: bool value_triggers(double value) const

Evaluates if a given value satisfies the alert’s comparison condition.

.. cpp:function:: bool time_allows_trigger() const

Checks whether enough time has passed since the last trigger to allow a new one.

.. cpp:function:: bool check_trigger_conditions(std::string host, std::string user, std::string entity, double value) const

Verifies if all trigger conditions (entity match, value threshold, and time interval)
are satisfied simultaneously.

.. cpp:function:: bool trigger()

Marks the alert as triggered by resetting the trigger time.
Always returns ``true``.

17 changes: 17 additions & 0 deletions docs/rst/types/notifier.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.. include:: ../exports/alias.include

.. _types_notifier:

Notifier
========

Alerts can have notification mechanisms attached, so that when they are triggered,
an action is performed. The ``Notifier`` class represents these notification mechanisms.

In the current implementation, the only available notifiers are script notifiers,
that execute a user-defined script when the alert is triggered. The script path is passed as
a parameter in the ´set_alert´ method.




1 change: 1 addition & 0 deletions docs/rst/types/types_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Types
/rst/types/alert_kind
/rst/types/alert_id
/rst/types/alert_info
/rst/types/notifier

.. todo::
.. /rst/types/database_dumps
4 changes: 3 additions & 1 deletion include/fastdds_statistics_backend/StatisticsBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ class StatisticsBackend
* @param alert_kind Kind of alert.
* @param threshold Threshold to trigger the alert.
* @param t_between_triggers Minimum time between two consecutive triggers of the alert.
* @param script_path Path to a script that will be executed when the alert is triggered.
*/
FASTDDS_STATISTICS_BACKEND_DllAPI
static void set_alert(
Expand All @@ -709,7 +710,8 @@ class StatisticsBackend
const std::string& topic_name,
const AlertKind& alert_kind,
const double& threshold,
const std::chrono::milliseconds& t_between_triggers);
const std::chrono::milliseconds& t_between_triggers,
const std::string& script_path);

/**
* @brief Remove an alert from the backend.
Expand Down
55 changes: 55 additions & 0 deletions include/fastdds_statistics_backend/types/Alerts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef FASTDDS_STATISTICS_BACKEND_TYPES__ALERTS_HPP
#define FASTDDS_STATISTICS_BACKEND_TYPES__ALERTS_HPP

#include <fastdds_statistics_backend/types/Notifiers.hpp>
#include <fastdds_statistics_backend/types/types.hpp>
#include <fastdds/rtps/common/Guid.hpp>

Expand All @@ -33,15 +34,22 @@ typedef uint32_t AlertId;
*/
enum class AlertKind
{
// Invalid alert kind
INVALID_ALERT,
// Alert triggered when new data is received
NEW_DATA_ALERT,
// Alert triggered when no data is received for a certain time
NO_DATA_ALERT
};

// Comparison kinds for alert triggering
enum class AlertComparison
{
// Invalid comparison
INVALID_ALERT_CMP,
// Greater than
GT_ALERT_CMP,
// Less than
LT_ALERT_CMP
};

Expand All @@ -68,11 +76,15 @@ class AlertInfo
// Last trigger
std::chrono::system_clock::time_point last_trigger;
std::chrono::milliseconds time_between_triggers;
// List of notifiers
std::vector<NotifierId> notifiers;

public:

//! Default constructor
AlertInfo() = default;

//! Constructor
AlertInfo(
AlertKind alert_kind,
std::string name,
Expand All @@ -96,11 +108,13 @@ class AlertInfo
reset_trigger_time();
}

//! Reset the last trigger time to now
void reset_trigger_time()
{
last_trigger = std::chrono::system_clock::now();
}

//! Check if the entity parameters match (empty parameters are wildcards)
bool entity_matches(
std::string stat_host,
std::string stat_user,
Expand All @@ -122,6 +136,7 @@ class AlertInfo
return match;
}

//! Check if the value triggers the alert based on the comparison and threshold
bool value_triggers(
double value) const
{
Expand All @@ -136,6 +151,7 @@ class AlertInfo
}
}

//! Check if the value triggers the alert based on the comparison and threshold
bool value_triggers(
uint64_t value) const
{
Expand All @@ -150,12 +166,14 @@ class AlertInfo
}
}

//! Check if enough time has passed since the last trigger
bool time_allows_trigger() const
{
auto now_ts = std::chrono::system_clock::now();
return (now_ts - last_trigger) > time_between_triggers;
}

//! Check trigger conditions for a specific alert that uses a double value
bool check_trigger_conditions(
std::string host,
std::string user,
Expand All @@ -165,6 +183,7 @@ class AlertInfo
return entity_matches(host, user, topic) && value_triggers(value) && time_allows_trigger();
}

//! Check trigger conditions for a specific alert that uses an integer value
bool check_trigger_conditions(
std::string host,
std::string user,
Expand All @@ -174,67 +193,101 @@ class AlertInfo
return entity_matches(host, user, topic) && value_triggers(value) && time_allows_trigger();
}

//! Trigger the alert (reset time and return true)
bool trigger()
{
reset_trigger_time();
return true;
}

//! Set the alert ID
void set_id(
AlertId alert_id)
{
id = alert_id;
}

//! Get the alert ID
AlertId get_alert_id() const
{
return id;
}

//! Get the alert kind
AlertKind get_alert_kind() const
{
return alert_kind;
}

//! Get the domain id
EntityId get_domain_id() const
{
return domain_id;
}

//! Get the alert name
std::string get_alert_name() const
{
return name;
}

//! Get the host name
std::string get_host_name() const
{
return host_name;
}

//! Get the user name
std::string get_user_name() const
{
return user_name;
}

//! Get the topic name
std::string get_topic_name() const
{
return topic_name;
}

//! Get the trigger threshold
double get_trigger_threshold() const
{
return trigger_threshold;
}

//! Get the minimum time between triggers
std::chrono::milliseconds get_time_between_triggers() const
{
return time_between_triggers;
}

//! Add a notifier to the alert
void add_notifier(
NotifierId notifier_id)
{
notifiers.push_back(notifier_id);
}

//! Remove a notifier from the alert
void remove_notifier(
NotifierId notifier_id)
{
notifiers.erase(std::remove(notifiers.begin(), notifiers.end(), notifier_id), notifiers.end());
}

//! Get the list of notifiers
const std::vector<NotifierId>& get_notifiers() const
{
return notifiers;
}

};

//! Specialization for NewDataAlert
struct NewDataAlertInfo : AlertInfo
{

//! Constructor with default values for NewDataAlert
NewDataAlertInfo(
std::string name,
EntityId domain_id,
Expand All @@ -250,8 +303,10 @@ struct NewDataAlertInfo : AlertInfo

};

//! Specialization for NoDataAlert
struct NoDataAlertInfo : AlertInfo
{
//! Constructor with default values for NoDataAlert
NoDataAlertInfo(
std::string name,
EntityId domain_id,
Expand Down
Loading
Loading