blob: b6136798d2cba12789ec1258669805a686167d25 [file] [log] [blame]
Matthew Barthb86374d2017-04-12 10:57:19 -05001#pragma once
2
3#include <algorithm>
4#include "data_types.hpp"
5
6namespace phosphor
7{
8namespace dbus
9{
10namespace monitoring
11{
12namespace condition
13{
14
Matthew Barthc5736432017-04-17 12:22:50 -050015/**
16 * @brief A condition used to trigger an action when a number of items are at
17 * or above a given value
18 * @details A given group of items is updated with their last known item
19 * value, which then the entire group is checked if there are a given number of
20 * them at or above a value which would cause the condition to be true
21 *
22 * @param[in] items - Group of items
23 * @param[in] path - Path of a item within the group
24 * @param[in] count - Number of items needed at or above value
25 * @param[in] value - Value of items to be at or above
26 *
27 * @return Lambda function
28 * A lambda function to determine if the number of items within the group
29 * are at or above the given value
30 */
Matthew Barthb86374d2017-04-12 10:57:19 -050031template <typename T>
32auto countAtOrAbove(Group& items, const char* path, size_t count, T&& value)
33{
34 return [&items,
35 path,
36 count,
37 value = std::forward<T>(value)](T&& arg)
38 {
39 Group::iterator it =
40 std::find_if(items.begin(),
41 items.end(),
42 [&path](auto const& item)
43 {
44 return std::get<0>(item) == path;
45 });
46 if (it != std::end(items))
47 {
48 std::get<1>(*it) = arg;
49 }
50 size_t condCount =
51 std::count_if(items.begin(),
52 items.end(),
53 [&value](auto const& item)
54 {
55 return std::get<1>(item) >= value;
56 });
57 return condCount >= count;
58 };
59}
60
61} // namespace condition
62} // namespace monitoring
63} // namespace dbus
64} // namespace phosphor