xref: /openbmc/telemetry/src/utils/conversion_trigger.cpp (revision 51497a0c878d939c9ed2a6ed30d12b465af4d51c)
1 #include "utils/conversion_trigger.hpp"
2 
3 #include "utils/transform.hpp"
4 
5 #include <sdbusplus/exception.hpp>
6 
7 namespace utils
8 {
9 namespace ts = utils::tstring;
10 
11 LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()(
12     const std::monostate& arg) const
13 {
14     throw sdbusplus::exception::SdBusError(
15         static_cast<int>(std::errc::invalid_argument),
16         "Provided threshold parameter is invalid");
17 }
18 
19 LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()(
20     const std::vector<numeric::ThresholdParam>& arg) const
21 {
22     return utils::transform(arg, [](const auto& thresholdParam) {
23         const auto& [type, dwellTime, direction, thresholdValue] =
24             thresholdParam;
25         return numeric::LabeledThresholdParam(numeric::toType(type), dwellTime,
26                                               numeric::toDirection(direction),
27                                               thresholdValue);
28     });
29 }
30 
31 LabeledTriggerThresholdParams ToLabeledThresholdParamConversion::operator()(
32     const std::vector<discrete::ThresholdParam>& arg) const
33 {
34     return utils::transform(arg, [](const auto& thresholdParam) {
35         const auto& [userId, severity, dwellTime, thresholdValue] =
36             thresholdParam;
37         return discrete::LabeledThresholdParam(
38             userId, discrete::toSeverity(severity), dwellTime, thresholdValue);
39     });
40 }
41 
42 TriggerThresholdParams FromLabeledThresholdParamConversion::operator()(
43     const std::vector<numeric::LabeledThresholdParam>& arg) const
44 {
45     return utils::transform(
46         arg, [](const numeric::LabeledThresholdParam& labeledThresholdParam) {
47             return numeric::ThresholdParam(
48                 numeric::typeToString(
49                     labeledThresholdParam.at_label<ts::Type>()),
50                 labeledThresholdParam.at_label<ts::DwellTime>(),
51                 numeric::directionToString(
52                     labeledThresholdParam.at_label<ts::Direction>()),
53                 labeledThresholdParam.at_label<ts::ThresholdValue>());
54         });
55 }
56 
57 TriggerThresholdParams FromLabeledThresholdParamConversion::operator()(
58     const std::vector<discrete::LabeledThresholdParam>& arg) const
59 {
60     return utils::transform(
61         arg, [](const discrete::LabeledThresholdParam& labeledThresholdParam) {
62             return discrete::ThresholdParam(
63                 labeledThresholdParam.at_label<ts::UserId>(),
64                 discrete::severityToString(
65                     labeledThresholdParam.at_label<ts::Severity>()),
66                 labeledThresholdParam.at_label<ts::DwellTime>(),
67                 labeledThresholdParam.at_label<ts::ThresholdValue>());
68         });
69 }
70 
71 SensorsInfo fromLabeledSensorsInfo(const std::vector<LabeledSensorInfo>& infos)
72 {
73     return utils::transform(infos, [](const LabeledSensorInfo& val) {
74         return SensorsInfo::value_type(
75             sdbusplus::message::object_path(val.at_label<ts::SensorPath>()),
76             val.at_label<ts::SensorMetadata>());
77     });
78 }
79 
80 nlohmann::json labeledThresholdParamsToJson(
81     const LabeledTriggerThresholdParams& labeledThresholdParams)
82 {
83     return std::visit([](const auto& lt) { return nlohmann::json(lt); },
84                       labeledThresholdParams);
85 }
86 
87 } // namespace utils
88