49 static std::pair<int, double>
findPair(
const std::vector<std::pair<uint64_t, double>>& vect, uint64_t timestamp)
55 if (!std::is_sorted(vect.begin(), vect.end(), [](
const std::pair<uint64_t, double>& lhs,
const std::pair<uint64_t, double>& rhs) ->
bool { return lhs.first < rhs.first; })) {
56 LOG(fatal) <<
"Vector is not sorted, we cannot execute the findPair function";
58 auto lower = std::lower_bound(vect.begin(), vect.end(), timestamp, [](
const std::pair<uint64_t, double>& p, uint64_t
value) { return p.first < value; });
59 if ((*lower).first == timestamp) {
60 LOG(
debug) <<
"We found the element for the exact timestamp";
61 return std::make_pair(
SameAsRequested, vect[std::distance(vect.begin(), lower)].second);
62 }
else if (lower == vect.end()) {
63 LOG(
debug) <<
"All values are smaller than the queried one " << timestamp <<
", we return the closest available from below: " << vect.back().first;
65 }
else if (lower == vect.begin()) {
66 LOG(
debug) <<
"All values are greater that the queried one " << timestamp <<
", we return the closest available from above: " << (*vect.begin()).
first;
70 const auto&
p1 = vect[std::distance(vect.begin(), lower) - 1];
71 const auto&
p2 = vect[std::distance(vect.begin(), lower)];
74 auto val1 =
p1.second;
75 auto val2 =
p2.second;
77 LOG(
debug) <<
"times are the same, cannot interpolate, returning closest from below";
81 double val = t2 >
t1 ? (timestamp -
t1) * (val2 - val1) / (t2 -
t1) + val1 : (timestamp -
t1) * (val1 - val2) / (
t1 - t2) + val1;
82 LOG(
debug) <<
"Doing interpolation between (" <<
t1 <<
", " << val1 <<
") and (" << t2 <<
", " << val2 <<
") for t = " << timestamp <<
" --> " <<
val;
85 LOG(error) <<
"Something went wrong!";
86 return std::make_pair(
Invalid, 0);