xref: /openbmc/telemetry/src/utils/conversion_trigger.cpp (revision 583ba441654657bb4ba9d051b747144a7258c159)
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 
operator ()(const std::monostate & arg) const12 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 
operator ()(const std::vector<numeric::ThresholdParam> & arg) const20 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(
27             numeric::toType(type), dwellTime, numeric::toDirection(direction),
28             thresholdValue);
29     });
30 }
31 
operator ()(const std::vector<discrete::ThresholdParam> & arg) const32 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, utils::toSeverity(severity), dwellTime, thresholdValue);
40     });
41 }
42 
operator ()(const std::vector<numeric::LabeledThresholdParam> & arg) const43 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 
operator ()(const std::vector<discrete::LabeledThresholdParam> & arg) const58 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                 utils::enumToString(
66                     labeledThresholdParam.at_label<ts::Severity>()),
67                 labeledThresholdParam.at_label<ts::DwellTime>(),
68                 labeledThresholdParam.at_label<ts::ThresholdValue>());
69         });
70 }
71 
fromLabeledSensorsInfo(const std::vector<LabeledSensorInfo> & infos)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 
fromLabeledThresholdParam(const std::vector<LabeledThresholdParam> & params)81 TriggerThresholdParams fromLabeledThresholdParam(
82     const std::vector<LabeledThresholdParam>& params)
83 {
84     namespace ts = utils::tstring;
85     if (params.empty())
86     {
87         return std::vector<numeric::ThresholdParam>();
88     }
89 
90     if (isFirstElementOfType<std::monostate>(params))
91     {
92         return std::vector<discrete::ThresholdParam>();
93     }
94 
95     if (isFirstElementOfType<discrete::LabeledThresholdParam>(params))
96     {
97         return utils::transform(params, [](const auto& param) {
98             const discrete::LabeledThresholdParam* paramUnpacked =
99                 std::get_if<discrete::LabeledThresholdParam>(&param);
100             if (!paramUnpacked)
101             {
102                 throw std::runtime_error(
103                     "Mixing threshold types is not allowed");
104             }
105             return discrete::ThresholdParam(
106                 paramUnpacked->at_label<ts::UserId>(),
107                 utils::enumToString(paramUnpacked->at_label<ts::Severity>()),
108                 paramUnpacked->at_label<ts::DwellTime>(),
109                 paramUnpacked->at_label<ts::ThresholdValue>());
110         });
111     }
112 
113     if (isFirstElementOfType<numeric::LabeledThresholdParam>(params))
114     {
115         return utils::transform(params, [](const auto& param) {
116             const numeric::LabeledThresholdParam* paramUnpacked =
117                 std::get_if<numeric::LabeledThresholdParam>(&param);
118             if (!paramUnpacked)
119             {
120                 throw std::runtime_error(
121                     "Mixing threshold types is not allowed");
122             }
123             return numeric::ThresholdParam(
124                 numeric::typeToString(paramUnpacked->at_label<ts::Type>()),
125                 paramUnpacked->at_label<ts::DwellTime>(),
126                 numeric::directionToString(
127                     paramUnpacked->at_label<ts::Direction>()),
128                 paramUnpacked->at_label<ts::ThresholdValue>());
129         });
130     }
131 
132     throw std::runtime_error("Incorrect threshold params");
133 }
134 
labeledThresholdParamsToJson(const LabeledTriggerThresholdParams & labeledThresholdParams)135 nlohmann::json labeledThresholdParamsToJson(
136     const LabeledTriggerThresholdParams& labeledThresholdParams)
137 {
138     return std::visit([](const auto& lt) { return nlohmann::json(lt); },
139                       labeledThresholdParams);
140 }
141 
stodStrict(const std::string & str)142 double stodStrict(const std::string& str)
143 {
144     size_t pos = 0;
145     double result = std::stod(str, &pos);
146     if (pos < str.length())
147     {
148         throw std::invalid_argument(
149             "non-numeric characters at the end of string");
150     }
151     return result;
152 }
153 
154 } // namespace utils
155