Skip to content

Commit

Permalink
Reset rate to 0 when new run starts (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
awegrzyn authored Aug 8, 2019
1 parent e11f3d2 commit 5b35618
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/DerivedMetrics.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@
#include <map>
#include <memory>
#include <string>
#include <variant>
#include <vector>
#include "VariantVisitorAdd.h"
#include "VariantVisitorRate.h"

template <class... Ts>
struct overloaded : Ts... {
using Ts::operator()...;
};
template <class... Ts>
overloaded(Ts...)->overloaded<Ts...>;

namespace o2
{
/// ALICE O2 Monitoring system
Expand Down Expand Up @@ -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));
Expand Down
25 changes: 24 additions & 1 deletion test/testDerived.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ BOOST_AUTO_TEST_CASE(derivedRateInt)
}
}

BOOST_AUTO_TEST_CASE(derivedRateInt_newRun)
{
struct RateResults {
int value;
int rate;
};
std::vector<RateResults> 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<double>(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 {
Expand All @@ -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<double>(derived.getValue()), results[i].rate, 1.0);
BOOST_WARN_CLOSE(std::get<double>(derivedTagged.getValue()), resultsTagged[i].rate, 5.0);
BOOST_WARN_CLOSE(std::get<double>(derivedTagged.getValue()), resultsTagged[i].rate, 1.0);
} catch (MonitoringException& e) {
BOOST_CHECK_EQUAL(e.what(), std::string("Not enough values"));
}
Expand Down

0 comments on commit 5b35618

Please sign in to comment.