Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 1 | #include "Thresholds.hpp" |
| 2 | |
| 3 | #include "VariantVisitors.hpp" |
| 4 | #include "sensor.hpp" |
| 5 | |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 6 | #include <array> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 7 | #include <boost/algorithm/string/replace.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 8 | #include <boost/container/flat_map.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 9 | #include <boost/lexical_cast.hpp> |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 10 | #include <cmath> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 11 | #include <fstream> |
| 12 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | #include <stdexcept> |
| 15 | #include <string> |
| 16 | #include <utility> |
| 17 | #include <variant> |
| 18 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 19 | |
| 20 | static constexpr bool DEBUG = false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | namespace thresholds |
| 22 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 23 | unsigned int toBusValue(const Level& level) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 24 | { |
| 25 | switch (level) |
| 26 | { |
| 27 | case (Level::WARNING): |
| 28 | { |
| 29 | return 0; |
| 30 | } |
| 31 | case (Level::CRITICAL): |
| 32 | { |
| 33 | return 1; |
| 34 | } |
| 35 | default: |
| 36 | { |
| 37 | return -1; |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 42 | std::string toBusValue(const Direction& direction) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 43 | { |
| 44 | switch (direction) |
| 45 | { |
| 46 | case (Direction::LOW): |
| 47 | { |
| 48 | return "less than"; |
| 49 | } |
| 50 | case (Direction::HIGH): |
| 51 | { |
| 52 | return "greater than"; |
| 53 | } |
| 54 | default: |
| 55 | { |
| 56 | return "err"; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 61 | bool parseThresholdsFromConfig( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 62 | const SensorData& sensorData, |
| 63 | std::vector<thresholds::Threshold>& thresholdVector, |
| 64 | const std::string* matchLabel) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 65 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 66 | for (const auto& item : sensorData) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 67 | { |
| 68 | if (item.first.find("Thresholds") == std::string::npos) |
| 69 | { |
| 70 | continue; |
| 71 | } |
| 72 | if (matchLabel != nullptr) |
| 73 | { |
| 74 | auto labelFind = item.second.find("Label"); |
| 75 | if (labelFind == item.second.end()) |
| 76 | continue; |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 77 | if (std::visit(VariantToStringVisitor(), labelFind->second) != |
| 78 | *matchLabel) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 79 | continue; |
| 80 | } |
| 81 | auto directionFind = item.second.find("Direction"); |
| 82 | auto severityFind = item.second.find("Severity"); |
| 83 | auto valueFind = item.second.find("Value"); |
| 84 | if (valueFind == item.second.end() || |
| 85 | severityFind == item.second.end() || |
| 86 | directionFind == item.second.end()) |
| 87 | { |
| 88 | std::cerr << "Malformed threshold in configuration\n"; |
| 89 | return false; |
| 90 | } |
| 91 | Level level; |
| 92 | Direction direction; |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 93 | if (std::visit(VariantToUnsignedIntVisitor(), severityFind->second) == |
| 94 | 0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 95 | { |
| 96 | level = Level::WARNING; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | level = Level::CRITICAL; |
| 101 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 102 | if (std::visit(VariantToStringVisitor(), directionFind->second) == |
| 103 | "less than") |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 104 | { |
| 105 | direction = Direction::LOW; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | direction = Direction::HIGH; |
| 110 | } |
James Feist | 13f340b | 2019-03-07 16:36:11 -0800 | [diff] [blame] | 111 | double val = std::visit(VariantToDoubleVisitor(), valueFind->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 112 | |
| 113 | thresholdVector.emplace_back(level, direction, val); |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 118 | void persistThreshold(const std::string& path, const std::string& baseInterface, |
| 119 | const thresholds::Threshold& threshold, |
James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 120 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame^] | 121 | size_t thresholdCount, const std::string& labelMatch) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 122 | { |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 123 | for (size_t ii = 0; ii < thresholdCount; ii++) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 124 | { |
| 125 | std::string thresholdInterface = |
| 126 | baseInterface + ".Thresholds" + std::to_string(ii); |
| 127 | conn->async_method_call( |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame^] | 128 | [&, path, threshold, thresholdInterface, labelMatch]( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 129 | const boost::system::error_code& ec, |
| 130 | const boost::container::flat_map<std::string, BasicVariantType>& |
| 131 | result) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 132 | if (ec) |
| 133 | { |
| 134 | return; // threshold not supported |
| 135 | } |
| 136 | |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame^] | 137 | if (!labelMatch.empty()) |
| 138 | { |
| 139 | auto labelFind = result.find("Label"); |
| 140 | if (labelFind == result.end()) |
| 141 | { |
| 142 | std::cerr << "No label in threshold configuration\n"; |
| 143 | return; |
| 144 | } |
| 145 | std::string label = |
| 146 | std::visit(VariantToStringVisitor(), labelFind->second); |
| 147 | if (label != labelMatch) |
| 148 | { |
| 149 | return; |
| 150 | } |
| 151 | } |
| 152 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 153 | auto directionFind = result.find("Direction"); |
| 154 | auto severityFind = result.find("Severity"); |
| 155 | auto valueFind = result.find("Value"); |
| 156 | if (valueFind == result.end() || severityFind == result.end() || |
| 157 | directionFind == result.end()) |
| 158 | { |
| 159 | std::cerr << "Malformed threshold in configuration\n"; |
| 160 | return; |
| 161 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 162 | unsigned int level = std::visit(VariantToUnsignedIntVisitor(), |
| 163 | severityFind->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 164 | |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 165 | std::string dir = |
| 166 | std::visit(VariantToStringVisitor(), directionFind->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 167 | if ((toBusValue(threshold.level) != level) || |
| 168 | (toBusValue(threshold.direction) != dir)) |
| 169 | { |
| 170 | return; // not the droid we're looking for |
| 171 | } |
| 172 | |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 173 | std::variant<double> value(threshold.value); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 174 | conn->async_method_call( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 175 | [](const boost::system::error_code& ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 176 | if (ec) |
| 177 | { |
| 178 | std::cerr << "Error setting threshold " << ec |
| 179 | << "\n"; |
| 180 | } |
| 181 | }, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 182 | entityManagerName, path, "org.freedesktop.DBus.Properties", |
| 183 | "Set", thresholdInterface, "Value", value); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 184 | }, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 185 | entityManagerName, path, "org.freedesktop.DBus.Properties", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 186 | "GetAll", thresholdInterface); |
| 187 | } |
| 188 | } |
| 189 | |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 190 | void updateThresholds(Sensor* sensor) |
| 191 | { |
| 192 | if (sensor->thresholds.empty()) |
| 193 | { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | for (const auto& threshold : sensor->thresholds) |
| 198 | { |
| 199 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface; |
| 200 | std::string property; |
| 201 | if (threshold.level == thresholds::Level::CRITICAL) |
| 202 | { |
| 203 | interface = sensor->thresholdInterfaceCritical; |
| 204 | if (threshold.direction == thresholds::Direction::HIGH) |
| 205 | { |
| 206 | property = "CriticalHigh"; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | property = "CriticalLow"; |
| 211 | } |
| 212 | } |
| 213 | else if (threshold.level == thresholds::Level::WARNING) |
| 214 | { |
| 215 | interface = sensor->thresholdInterfaceWarning; |
| 216 | if (threshold.direction == thresholds::Direction::HIGH) |
| 217 | { |
| 218 | property = "WarningHigh"; |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | property = "WarningLow"; |
| 223 | } |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | continue; |
| 228 | } |
| 229 | if (!interface) |
| 230 | { |
| 231 | continue; |
| 232 | } |
| 233 | interface->set_property(property, threshold.value); |
| 234 | } |
| 235 | } |
| 236 | |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 237 | // Debugging counters |
| 238 | static int cHiTrue = 0; |
| 239 | static int cHiFalse = 0; |
| 240 | static int cHiMidstate = 0; |
| 241 | static int cLoTrue = 0; |
| 242 | static int cLoFalse = 0; |
| 243 | static int cLoMidstate = 0; |
| 244 | static int cDebugThrottle = 0; |
| 245 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 246 | static std::vector<std::pair<Threshold, bool>> checkThresholds(Sensor* sensor, |
| 247 | double value) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 248 | { |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 249 | std::vector<std::pair<Threshold, bool>> thresholdChanges; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 250 | if (sensor->thresholds.empty()) |
| 251 | { |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 252 | return thresholdChanges; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 253 | } |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 254 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 255 | for (auto& threshold : sensor->thresholds) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 256 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 257 | // Use "Schmitt trigger" logic to avoid threshold trigger spam, |
| 258 | // if value is noisy while hovering very close to a threshold. |
| 259 | // When a threshold is crossed, indicate true immediately, |
| 260 | // but require more distance to be crossed the other direction, |
| 261 | // before resetting the indicator back to false. |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 262 | if (threshold.direction == thresholds::Direction::HIGH) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 263 | { |
James Feist | 551087a | 2019-12-09 11:17:12 -0800 | [diff] [blame] | 264 | if (value >= threshold.value) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 265 | { |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 266 | thresholdChanges.push_back(std::make_pair(threshold, true)); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 267 | ++cHiTrue; |
| 268 | } |
| 269 | else if (value < (threshold.value - sensor->hysteresisTrigger)) |
| 270 | { |
| 271 | thresholdChanges.push_back(std::make_pair(threshold, false)); |
| 272 | ++cHiFalse; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 273 | } |
Patrick Venture | 66235d4 | 2019-10-11 08:31:27 -0700 | [diff] [blame] | 274 | else |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 275 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 276 | ++cHiMidstate; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 279 | else if (threshold.direction == thresholds::Direction::LOW) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 280 | { |
James Feist | 551087a | 2019-12-09 11:17:12 -0800 | [diff] [blame] | 281 | if (value <= threshold.value) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 282 | { |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 283 | thresholdChanges.push_back(std::make_pair(threshold, true)); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 284 | ++cLoTrue; |
| 285 | } |
| 286 | else if (value > (threshold.value + sensor->hysteresisTrigger)) |
| 287 | { |
| 288 | thresholdChanges.push_back(std::make_pair(threshold, false)); |
| 289 | ++cLoFalse; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 290 | } |
Patrick Venture | 66235d4 | 2019-10-11 08:31:27 -0700 | [diff] [blame] | 291 | else |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 292 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 293 | ++cLoMidstate; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 296 | else |
| 297 | { |
| 298 | std::cerr << "Error determining threshold direction\n"; |
| 299 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 300 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 301 | |
| 302 | if constexpr (DEBUG) |
| 303 | { |
| 304 | // Throttle debug output, so that it does not continuously spam |
| 305 | ++cDebugThrottle; |
| 306 | if (cDebugThrottle >= 1000) |
| 307 | { |
| 308 | cDebugThrottle = 0; |
| 309 | std::cerr << "checkThresholds: High T=" << cHiTrue |
| 310 | << " F=" << cHiFalse << " M=" << cHiMidstate |
| 311 | << ", Low T=" << cLoTrue << " F=" << cLoFalse |
| 312 | << " M=" << cLoMidstate << "\n"; |
| 313 | } |
| 314 | } |
| 315 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 316 | return thresholdChanges; |
| 317 | } |
| 318 | |
| 319 | bool checkThresholds(Sensor* sensor) |
| 320 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 321 | bool status = true; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 322 | std::vector<std::pair<Threshold, bool>> changes = |
| 323 | checkThresholds(sensor, sensor->value); |
| 324 | for (const auto& [threshold, asserted] : changes) |
| 325 | { |
| 326 | assertThresholds(sensor, threshold.level, threshold.direction, |
| 327 | asserted); |
| 328 | if (threshold.level == thresholds::Level::CRITICAL && asserted) |
| 329 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 330 | status = false; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 334 | return status; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 335 | } |
| 336 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 337 | void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer) |
| 338 | { |
| 339 | |
| 340 | std::vector<std::pair<Threshold, bool>> changes = |
| 341 | checkThresholds(sensor, sensor->value); |
| 342 | for (const auto& [threshold, asserted] : changes) |
| 343 | { |
| 344 | if (asserted) |
| 345 | { |
| 346 | thresholdTimer.startTimer(threshold); |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | assertThresholds(sensor, threshold.level, threshold.direction, |
| 351 | false); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 356 | void assertThresholds(Sensor* sensor, thresholds::Level level, |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 357 | thresholds::Direction direction, bool assert) |
| 358 | { |
| 359 | std::string property; |
| 360 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface; |
| 361 | if (level == thresholds::Level::WARNING && |
| 362 | direction == thresholds::Direction::HIGH) |
| 363 | { |
| 364 | property = "WarningAlarmHigh"; |
| 365 | interface = sensor->thresholdInterfaceWarning; |
| 366 | } |
| 367 | else if (level == thresholds::Level::WARNING && |
| 368 | direction == thresholds::Direction::LOW) |
| 369 | { |
| 370 | property = "WarningAlarmLow"; |
| 371 | interface = sensor->thresholdInterfaceWarning; |
| 372 | } |
| 373 | else if (level == thresholds::Level::CRITICAL && |
| 374 | direction == thresholds::Direction::HIGH) |
| 375 | { |
| 376 | property = "CriticalAlarmHigh"; |
| 377 | interface = sensor->thresholdInterfaceCritical; |
| 378 | } |
| 379 | else if (level == thresholds::Level::CRITICAL && |
| 380 | direction == thresholds::Direction::LOW) |
| 381 | { |
| 382 | property = "CriticalAlarmLow"; |
| 383 | interface = sensor->thresholdInterfaceCritical; |
| 384 | } |
| 385 | else |
| 386 | { |
| 387 | std::cerr << "Unknown threshold, level " << level << "direction " |
| 388 | << direction << "\n"; |
| 389 | return; |
| 390 | } |
| 391 | if (!interface) |
| 392 | { |
| 393 | std::cout << "trying to set uninitialized interface\n"; |
| 394 | return; |
| 395 | } |
| 396 | interface->set_property(property, assert); |
| 397 | } |
| 398 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 399 | static constexpr std::array<const char*, 4> attrTypes = {"lcrit", "min", "max", |
| 400 | "crit"}; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 401 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 402 | bool parseThresholdsFromAttr( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 403 | std::vector<thresholds::Threshold>& thresholdVector, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 404 | const std::string& inputPath, const double& scaleFactor, |
| 405 | const double& offset) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 406 | { |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 407 | for (const std::string& type : attrTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 408 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 409 | auto attrPath = boost::replace_all_copy(inputPath, "input", type); |
| 410 | std::ifstream attrFile(attrPath); |
| 411 | if (!attrFile.good()) |
| 412 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 413 | continue; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 414 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 415 | std::string attr; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 416 | std::getline(attrFile, attr); |
| 417 | attrFile.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 418 | |
| 419 | Level level; |
| 420 | Direction direction; |
Jae Hyun Yoo | 7fa17c4 | 2019-09-09 13:20:37 -0700 | [diff] [blame] | 421 | double val; |
| 422 | try |
| 423 | { |
| 424 | val = std::stod(attr) / scaleFactor; |
| 425 | } |
| 426 | catch (const std::invalid_argument&) |
| 427 | { |
| 428 | return false; |
| 429 | } |
| 430 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 431 | if (type == "min" || type == "max") |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 432 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 433 | level = Level::WARNING; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 434 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 435 | else |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 436 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 437 | level = Level::CRITICAL; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 438 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 439 | if (type == "min" || type == "lcrit") |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 440 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 441 | direction = Direction::LOW; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 442 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 443 | else |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 444 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 445 | direction = Direction::HIGH; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 446 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 447 | |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 448 | if (type == "crit") |
| 449 | { |
| 450 | val += offset; |
| 451 | } |
| 452 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 453 | if (DEBUG) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 454 | { |
| 455 | std::cout << "Threshold: " << attrPath << ": " << val << "\n"; |
| 456 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 457 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 458 | thresholdVector.emplace_back(level, direction, val); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 459 | } |
Jae Hyun Yoo | 7fa17c4 | 2019-09-09 13:20:37 -0700 | [diff] [blame] | 460 | // no thresholds is allowed, not an error so return true. |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 461 | return true; |
| 462 | } |
| 463 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 464 | bool hasCriticalInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 465 | const std::vector<thresholds::Threshold>& thresholdVector) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 466 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 467 | for (auto& threshold : thresholdVector) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 468 | { |
| 469 | if (threshold.level == Level::CRITICAL) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 470 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 471 | return true; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 472 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 473 | } |
| 474 | return false; |
| 475 | } |
| 476 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 477 | bool hasWarningInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 478 | const std::vector<thresholds::Threshold>& thresholdVector) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 479 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 480 | for (auto& threshold : thresholdVector) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 481 | { |
| 482 | if (threshold.level == Level::WARNING) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 483 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 484 | return true; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 485 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 486 | } |
| 487 | return false; |
| 488 | } |
| 489 | } // namespace thresholds |