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