xref: /openbmc/telemetry/src/utils/conversion_trigger.cpp (revision 4416fce6fb6539b4e2a6a94a8248934c2eb29820)
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(
26             numeric::stringToType(type), dwellTime,
27             numeric::stringToDirection(direction), 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::stringToSeverity(severity), dwellTime,
39             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::SensorPath>()),
77             val.at_label<ts::SensorMetadata>());
78     });
79 }
80 
81 nlohmann::json labeledThresholdParamsToJson(
82     const LabeledTriggerThresholdParams& labeledThresholdParams)
83 {
84     return std::visit([](const auto& lt) { return nlohmann::json(lt); },
85                       labeledThresholdParams);
86 }
87 
88 } // namespace utils
89