Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1 | #include "metrics/collection_function.hpp" |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 2 | |
| 3 | #include <cmath> |
| 4 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 5 | namespace metrics |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 6 | { |
| 7 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 8 | class FunctionMinimum : public CollectionFunction |
| 9 | { |
| 10 | public: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 11 | double calculate(const std::vector<ReadingItem>& readings, |
| 12 | Milliseconds) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 13 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 14 | return std::min_element( |
| 15 | readings.begin(), readings.end(), |
| 16 | [](const auto& left, const auto& right) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 17 | return std::make_tuple(!std::isfinite(left.second), left.second) < |
| 18 | std::make_tuple(!std::isfinite(right.second), right.second); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 19 | }) |
| 20 | ->second; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 21 | } |
| 22 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 23 | double calculateForStartupInterval(std::vector<ReadingItem>& readings, |
| 24 | Milliseconds timestamp) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 25 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 26 | readings.assign( |
| 27 | {ReadingItem(timestamp, calculate(readings, timestamp))}); |
| 28 | return readings.back().second; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 29 | } |
| 30 | }; |
| 31 | |
| 32 | class FunctionMaximum : public CollectionFunction |
| 33 | { |
| 34 | public: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 35 | double calculate(const std::vector<ReadingItem>& readings, |
| 36 | Milliseconds) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 37 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 38 | return std::max_element( |
| 39 | readings.begin(), readings.end(), |
| 40 | [](const auto& left, const auto& right) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 41 | return std::make_tuple(std::isfinite(left.second), left.second) < |
| 42 | std::make_tuple(std::isfinite(right.second), right.second); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 43 | }) |
| 44 | ->second; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 47 | double calculateForStartupInterval(std::vector<ReadingItem>& readings, |
| 48 | Milliseconds timestamp) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 49 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 50 | readings.assign( |
| 51 | {ReadingItem(timestamp, calculate(readings, timestamp))}); |
| 52 | return readings.back().second; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 53 | } |
| 54 | }; |
| 55 | |
| 56 | class FunctionAverage : public CollectionFunction |
| 57 | { |
| 58 | public: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 59 | double calculate(const std::vector<ReadingItem>& readings, |
| 60 | Milliseconds timestamp) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 61 | { |
| 62 | auto valueSum = 0.0; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 63 | auto timeSum = Milliseconds{0}; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 64 | for (auto it = readings.begin(); it != std::prev(readings.end()); ++it) |
| 65 | { |
| 66 | if (std::isfinite(it->second)) |
| 67 | { |
| 68 | const auto kt = std::next(it); |
| 69 | const auto duration = kt->first - it->first; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 70 | valueSum += it->second * duration.count(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 71 | timeSum += duration; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | const auto duration = timestamp - readings.back().first; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 76 | valueSum += readings.back().second * duration.count(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 77 | timeSum += duration; |
| 78 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 79 | return valueSum / std::max(timeSum.count(), uint64_t{1u}); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 82 | double calculateForStartupInterval(std::vector<ReadingItem>& readings, |
| 83 | Milliseconds timestamp) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 84 | { |
| 85 | auto result = calculate(readings, timestamp); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 86 | if (std::isfinite(result)) |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 87 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 88 | readings.assign({ReadingItem(readings.front().first, result), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 89 | ReadingItem(timestamp, readings.back().second)}); |
| 90 | } |
| 91 | return result; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | class FunctionSummation : public CollectionFunction |
| 96 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 97 | using Multiplier = std::chrono::duration<double>; |
| 98 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 99 | public: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 100 | double calculate(const std::vector<ReadingItem>& readings, |
| 101 | const Milliseconds timestamp) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 102 | { |
| 103 | auto valueSum = 0.0; |
| 104 | for (auto it = readings.begin(); it != std::prev(readings.end()); ++it) |
| 105 | { |
| 106 | if (std::isfinite(it->second)) |
| 107 | { |
| 108 | const auto kt = std::next(it); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 109 | const auto multiplier = |
| 110 | calculateMultiplier(kt->first - it->first); |
| 111 | valueSum += it->second * multiplier.count(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 115 | const auto multiplier = |
| 116 | calculateMultiplier(timestamp - readings.back().first); |
| 117 | valueSum += readings.back().second * multiplier.count(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 118 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 119 | return valueSum; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 122 | double |
| 123 | calculateForStartupInterval(std::vector<ReadingItem>& readings, |
| 124 | const Milliseconds timestamp) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 125 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 126 | const auto result = calculate(readings, timestamp); |
| 127 | if (readings.size() > 2 && std::isfinite(result)) |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 128 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 129 | const auto multiplier = |
| 130 | calculateMultiplier(timestamp - readings.front().first).count(); |
| 131 | if (multiplier > 0.) |
| 132 | { |
| 133 | const auto prevValue = result / multiplier; |
| 134 | readings.assign( |
| 135 | {ReadingItem(readings.front().first, prevValue), |
| 136 | ReadingItem(timestamp, readings.back().second)}); |
| 137 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 138 | } |
| 139 | return result; |
| 140 | } |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 141 | |
| 142 | private: |
| 143 | static constexpr Multiplier calculateMultiplier(Milliseconds duration) |
| 144 | { |
| 145 | constexpr auto m = Multiplier{Seconds{1}}; |
| 146 | return Multiplier{duration / m}; |
| 147 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | std::shared_ptr<CollectionFunction> |
| 151 | makeCollectionFunction(OperationType operationType) |
| 152 | { |
| 153 | using namespace std::string_literals; |
| 154 | |
| 155 | switch (operationType) |
| 156 | { |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 157 | case OperationType::min: |
| 158 | return std::make_shared<FunctionMinimum>(); |
| 159 | case OperationType::max: |
| 160 | return std::make_shared<FunctionMaximum>(); |
| 161 | case OperationType::avg: |
| 162 | return std::make_shared<FunctionAverage>(); |
| 163 | case OperationType::sum: |
| 164 | return std::make_shared<FunctionSummation>(); |
| 165 | default: |
| 166 | throw std::runtime_error("op: "s + |
| 167 | utils::enumToString(operationType) + |
| 168 | " is not supported"s); |
| 169 | } |
| 170 | } |
| 171 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 172 | } // namespace metrics |