blob: 3dad8e7686c26d16f5c0d3366e5e72ba49cc80f7 [file] [log] [blame]
Brad Bishop4041d722017-05-21 10:06:07 -04001#pragma once
2
3#include "callback.hpp"
4#include "data_types.hpp"
5
Ratan Gupta882d7412018-02-19 23:43:44 +05306#include <algorithm>
Andrew Geisslerf3367682020-05-16 14:02:45 -05007#include <cstddef>
Brad Bishop35e75f02018-02-26 09:43:41 -05008#include <functional>
Ratan Gupta882d7412018-02-19 23:43:44 +05309
Brad Bishop4041d722017-05-21 10:06:07 -040010namespace phosphor
11{
12namespace dbus
13{
14namespace monitoring
15{
16
17/** @class CountCondition
18 * @brief Count properties that satisfy a condition.
19 *
20 * When invoked, a count class instance performs its condition
21 * test in two passes.
22 *
23 * In pass one, apply a C++ relational operator to the value of
24 * each property in the index and a value provided by the
25 * configuration file.
26 *
27 * Count the number of properties that pass the test in pass
28 * one. In pass two, apply a second C++ relational operator
29 * to the number of properties that pass the test from pass one
30 * to a count provided by the configuration file.
Matt Spinlerc458dee2018-02-19 13:09:10 -060031 *
32 * If the oneshot parameter is true, then this condition won't pass
33 * again until it fails at least once.
Brad Bishop4041d722017-05-21 10:06:07 -040034 */
Patrick Venture3d6d3182018-08-31 09:33:09 -070035template <typename T>
36class CountCondition : public IndexedConditional
Brad Bishop4041d722017-05-21 10:06:07 -040037{
Brad Bishopd1eac882018-03-29 10:34:05 -040038 public:
39 CountCondition() = delete;
40 CountCondition(const CountCondition&) = default;
41 CountCondition(CountCondition&&) = default;
42 CountCondition& operator=(const CountCondition&) = default;
43 CountCondition& operator=(CountCondition&&) = default;
44 ~CountCondition() = default;
Brad Bishop4041d722017-05-21 10:06:07 -040045
Brad Bishopd1eac882018-03-29 10:34:05 -040046 CountCondition(const PropertyIndex& conditionIndex,
47 const std::function<bool(size_t)>& _countOp,
48 const std::function<bool(T)>& _propertyOp,
49 bool oneshot = false) :
50 IndexedConditional(conditionIndex),
51 countOp(_countOp), propertyOp(_propertyOp), oneshot(oneshot)
George Liu3fe976c2022-06-21 09:37:04 +080052 {}
Brad Bishop4041d722017-05-21 10:06:07 -040053
Brad Bishopd1eac882018-03-29 10:34:05 -040054 bool operator()() override
55 {
56 // Count the number of properties in the index that
57 // pass the condition specified in the config file.
Patrick Williamsc5fe26a2023-05-10 07:50:25 -050058 auto count = std::count_if(index.cbegin(), index.cend(),
59 [this](const auto& item)
60 // *INDENT-OFF*
Patrick Williams7fe55e92023-10-20 11:18:36 -050061 {
Patrick Williamsc5fe26a2023-05-10 07:50:25 -050062 // Get the property value from storage[0],
63 // and save the op result in storage[1].
64 const auto& storage = std::get<storageIndex>(item.second);
65 // Don't count properties that don't exist.
66 if (!std::get<valueIndex>(storage.get()).has_value())
Matt Spinlerc458dee2018-02-19 13:09:10 -060067 {
Patrick Williamsc5fe26a2023-05-10 07:50:25 -050068 return false;
69 }
70 const auto& value =
71 std::any_cast<T>(std::get<valueIndex>(storage.get()));
72 auto r = propertyOp(value);
Matt Spinlerc458dee2018-02-19 13:09:10 -060073
Patrick Williamsc5fe26a2023-05-10 07:50:25 -050074 std::get<resultIndex>(storage.get()) = r;
Brad Bishopd1eac882018-03-29 10:34:05 -040075
Patrick Williamsc5fe26a2023-05-10 07:50:25 -050076 return r;
77 });
Brad Bishopd1eac882018-03-29 10:34:05 -040078 // *INDENT-ON*
79
80 // Now apply the count condition to the count.
81 auto result = countOp(count);
82
83 // If this was a oneshot and the the condition has already
84 // passed, then don't let it pass again until the condition
85 // has gone back to false.
86 if (oneshot && result && lastResult)
87 {
88 return false;
Brad Bishop4041d722017-05-21 10:06:07 -040089 }
90
Brad Bishopd1eac882018-03-29 10:34:05 -040091 lastResult = result;
92 return result;
93 }
94
95 private:
96 /** @brief The comparison to perform on the count. */
97 std::function<bool(size_t)> countOp;
98 /** @brief The comparison to perform on each property. */
99 std::function<bool(T)> propertyOp;
100 /** @brief If the condition can be allowed to pass again
101 on subsequent checks that are also true. */
102 const bool oneshot;
103 /** @brief The result of the previous check. */
104 bool lastResult = false;
Brad Bishop4041d722017-05-21 10:06:07 -0400105};
106} // namespace monitoring
107} // namespace dbus
108} // namespace phosphor