From 5b35618604c09ccb1d14f261c4c3de01784fc20b Mon Sep 17 00:00:00 2001 From: Adam Wegrzynek Date: Thu, 8 Aug 2019 11:44:59 +0200 Subject: [PATCH] Reset rate to 0 when new run starts (#159) --- src/DerivedMetrics.cxx | 17 ++++++++++++++++- test/testDerived.cxx | 25 ++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/DerivedMetrics.cxx b/src/DerivedMetrics.cxx index 46b277d95..f4b61c738 100644 --- a/src/DerivedMetrics.cxx +++ b/src/DerivedMetrics.cxx @@ -19,11 +19,17 @@ #include #include #include -#include #include #include "VariantVisitorAdd.h" #include "VariantVisitorRate.h" +template +struct overloaded : Ts... { + using Ts::operator()...; +}; +template +overloaded(Ts...)->overloaded; + namespace o2 { /// ALICE O2 Monitoring system @@ -82,6 +88,15 @@ Metric DerivedMetrics::process(Metric& metric, DerivedMetricMode mode) auto previous = search->second.getValue(); auto rate = std::visit(VariantVisitorRate(timestampCount), current, previous); + // handle situation when a new run starts + auto isZero = std::visit(overloaded{ + [](auto arg) { return arg == 0; }, + [](const std::string& arg) { return arg == ""; }}, + current); + if (rate < 0 && isZero) { + rate = 0; + } + // swap metrics mStorage.erase(key); mStorage.insert(std::make_pair(key, metric)); diff --git a/test/testDerived.cxx b/test/testDerived.cxx index a49f03af8..fca73a867 100644 --- a/test/testDerived.cxx +++ b/test/testDerived.cxx @@ -51,6 +51,29 @@ BOOST_AUTO_TEST_CASE(derivedRateInt) } } +BOOST_AUTO_TEST_CASE(derivedRateInt_newRun) +{ + struct RateResults { + int value; + int rate; + }; + std::vector results = {{10, 0}, {20, 100}, {30, 100}, {50, 200}, {0, 0}, {10, 100}, {20, 100}, {30, 100}, {50, 200}}; + o2::monitoring::DerivedMetrics derivedHandler; + std::string name("metricInt"); + + for (auto const result : results) { + try { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + o2::monitoring::Metric metric(result.value, name); + o2::monitoring::Metric derived = derivedHandler.process(metric, DerivedMetricMode::RATE); + BOOST_CHECK_EQUAL(derived.getName(), "metricIntRate"); + BOOST_WARN_CLOSE(std::get(derived.getValue()), result.rate, 1.0); + } catch (MonitoringException& e) { + BOOST_CHECK_EQUAL(e.what(), std::string("Not enough values")); + } + } +} + BOOST_AUTO_TEST_CASE(derivedRateDouble) { struct RateResults { @@ -70,7 +93,7 @@ BOOST_AUTO_TEST_CASE(derivedRateDouble) o2::monitoring::Metric derivedTagged = derivedHandler.process(metricTagged, DerivedMetricMode::RATE); BOOST_CHECK_EQUAL(derived.getName(), "metricDoubleRate"); BOOST_WARN_CLOSE(std::get(derived.getValue()), results[i].rate, 1.0); - BOOST_WARN_CLOSE(std::get(derivedTagged.getValue()), resultsTagged[i].rate, 5.0); + BOOST_WARN_CLOSE(std::get(derivedTagged.getValue()), resultsTagged[i].rate, 1.0); } catch (MonitoringException& e) { BOOST_CHECK_EQUAL(e.what(), std::string("Not enough values")); }