xref: /openbmc/telemetry/src/utils/conversion_trigger.cpp (revision 94f71c5190b64bb47aa34cdce4eb4cca71d36faa)
1 #include "utils/conversion_trigger.hpp"
2 
3 #include "utils/transform.hpp"
4 #include "utils/tstring.hpp"
5 
6 #include <sdbusplus/exception.hpp>
7 
8 namespace utils
9 {
10 namespace ts = utils::tstring;
11 
12 LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()(
13     const std::monostate& arg) const
14 {
15     throw sdbusplus::exception::SdBusError(
16         static_cast<int>(std::errc::invalid_argument),
17         "Provided threshold parameter is invalid");
18 }
19 
20 LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()(
21     const std::vector<numeric::ThresholdParam>& arg) const
22 {
23     return utils::transform(arg, [](const auto& thresholdParam) {
24         const auto& [type, dwellTime, direction, thresholdValue] =
25             thresholdParam;
26         return numeric::LabeledThresholdParam(numeric::toType(type), dwellTime,
27                                               numeric::toDirection(direction),
28                                               thresholdValue);
29     });
30 }
31 
32 LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()(
33     const std::vector<discrete::ThresholdParam>& arg) const
34 {
35     return utils::transform(arg, [](const auto& thresholdParam) {
36         const auto& [userId, severity, dwellTime, thresholdValue] =
37             thresholdParam;
38         return discrete::LabeledThresholdParam(
39             userId, discrete::toSeverity(severity), dwellTime, thresholdValue);
40     });
41 }
42 
43 TriggerThresholdParams FromLabeledThresholdParamConversion::operator()(
44     const std::vector<numeric::LabeledThresholdParam>& arg) const
45 {
46     return utils::transform(
47         arg, [](const numeric::LabeledThresholdParam& labeledThresholdParam) {
48             return numeric::ThresholdParam(
49                 numeric::typeToString(
50                     labeledThresholdParam.at_label<ts::Type>()),
51                 labeledThresholdParam.at_label<ts::DwellTime>(),
52                 numeric::directionToString(
53                     labeledThresholdParam.at_label<ts::Direction>()),
54                 labeledThresholdParam.at_label<ts::ThresholdValue>());
55         });
56 }
57 
58 TriggerThresholdParams FromLabeledThresholdParamConversion::operator()(
59     const std::vector<discrete::LabeledThresholdParam>& arg) const
60 {
61     return utils::transform(
62         arg, [](const discrete::LabeledThresholdParam& labeledThresholdParam) {
63             return discrete::ThresholdParam(
64                 labeledThresholdParam.at_label<ts::UserId>(),
65                 discrete::severityToString(
66                     labeledThresholdParam.at_label<ts::Severity>()),
67                 labeledThresholdParam.at_label<ts::DwellTime>(),
68                 labeledThresholdParam.at_label<ts::ThresholdValue>());
69         });
70 }
71 
72 SensorsInfo fromLabeledSensorsInfo(const std::vector<LabeledSensorInfo>& infos)
73 {
74     return utils::transform(infos, [](const LabeledSensorInfo& val) {
75         return SensorsInfo::value_type(
76             sdbusplus::message::object_path(val.at_label<ts::Path>()),
77             val.at_label<ts::Metadata>());
78     });
79 }
80 
81 TriggerThresholdParams
82     fromLabeledThresholdParam(const std::vector<LabeledThresholdParam>& params)
83 {
84     namespace ts = utils::tstring;
85     if (isFirstElementOfType<std::monostate>(params))
86     {
87         return std::vector<discrete::ThresholdParam>();
88     }
89 
90     if (isFirstElementOfType<discrete::LabeledThresholdParam>(params))
91     {
92         return utils::transform(params, [](const auto& param) {
93             const discrete::LabeledThresholdParam* paramUnpacked =
94                 std::get_if<discrete::LabeledThresholdParam>(&param);
95             if (!paramUnpacked)
96             {
97                 throw std::runtime_error(
98                     "Mixing threshold types is not allowed");
99             }
100             return discrete::ThresholdParam(
101                 paramUnpacked->at_label<ts::UserId>(),
102                 discrete::severityToString(
103                     paramUnpacked->at_label<ts::Severity>()),
104                 paramUnpacked->at_label<ts::DwellTime>(),
105                 paramUnpacked->at_label<ts::ThresholdValue>());
106         });
107     }
108 
109     if (isFirstElementOfType<numeric::LabeledThresholdParam>(params))
110     {
111         return utils::transform(params, [](const auto& param) {
112             const numeric::LabeledThresholdParam* paramUnpacked =
113                 std::get_if<numeric::LabeledThresholdParam>(&param);
114             if (!paramUnpacked)
115             {
116                 throw std::runtime_error(
117                     "Mixing threshold types is not allowed");
118             }
119             return numeric::ThresholdParam(
120                 numeric::typeToString(paramUnpacked->at_label<ts::Type>()),
121                 paramUnpacked->at_label<ts::DwellTime>(),
122                 numeric::directionToString(
123                     paramUnpacked->at_label<ts::Direction>()),
124                 paramUnpacked->at_label<ts::ThresholdValue>());
125         });
126     }
127 
128     throw std::runtime_error("Incorrect threshold params");
129 }
130 
131 nlohmann::json labeledThresholdParamsToJson(
132     const LabeledTriggerThresholdParams& labeledThresholdParams)
133 {
134     return std::visit([](const auto& lt) { return nlohmann::json(lt); },
135                       labeledThresholdParams);
136 }
137 
138 } // namespace utils
139