From 8fa18790bf6560cc65de2681b9bb2b992ac8b595 Mon Sep 17 00:00:00 2001 From: Tej Singh Date: Mon, 19 Oct 2020 18:27:43 -0700 Subject: [PATCH] Determine value metric update status Test: atest statsd_test Bug: 162321957 Change-Id: Ifa6c77e605f45f1494d6d27f2cac45632378b004 --- .../parsing_utils/config_update_utils.cpp | 16 ++- .../config_update_utils_test.cpp | 126 ++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) diff --git a/cmds/statsd/src/metrics/parsing_utils/config_update_utils.cpp b/cmds/statsd/src/metrics/parsing_utils/config_update_utils.cpp index cfc6e3f008d54..b6e5d8846fd9e 100644 --- a/cmds/statsd/src/metrics/parsing_utils/config_update_utils.cpp +++ b/cmds/statsd/src/metrics/parsing_utils/config_update_utils.cpp @@ -546,7 +546,21 @@ bool determineAllMetricUpdateStatuses(const StatsdConfig& config, return false; } } - + for (int i = 0; i < config.value_metric_size(); i++, metricIndex++) { + const ValueMetric& metric = config.value_metric(i); + set conditionDependencies; + if (metric.has_condition()) { + conditionDependencies.insert(metric.condition()); + } + if (!determineMetricUpdateStatus( + config, metric, metric.id(), METRIC_TYPE_VALUE, {metric.what()}, + conditionDependencies, metric.slice_by_state(), metric.links(), + oldMetricProducerMap, oldMetricProducers, metricToActivationMap, + replacedMatchers, replacedConditions, replacedStates, + metricsToUpdate[metricIndex])) { + return false; + } + } for (int i = 0; i < config.gauge_metric_size(); i++, metricIndex++) { const GaugeMetric& metric = config.gauge_metric(i); set conditionDependencies; diff --git a/cmds/statsd/tests/metrics/parsing_utils/config_update_utils_test.cpp b/cmds/statsd/tests/metrics/parsing_utils/config_update_utils_test.cpp index dc951be72fe16..0066030ade549 100644 --- a/cmds/statsd/tests/metrics/parsing_utils/config_update_utils_test.cpp +++ b/cmds/statsd/tests/metrics/parsing_utils/config_update_utils_test.cpp @@ -170,6 +170,23 @@ DurationMetric createDurationMetric(string name, int64_t what, optional } return metric; } + +ValueMetric createValueMetric(string name, const AtomMatcher& what, optional condition, + vector states) { + ValueMetric metric; + metric.set_id(StringToId(name)); + metric.set_what(what.id()); + metric.set_bucket(TEN_MINUTES); + metric.mutable_value_field()->set_field(what.simple_atom_matcher().atom_id()); + metric.mutable_value_field()->add_child()->set_field(2); + if (condition) { + metric.set_condition(condition.value()); + } + for (const int64_t state : states) { + metric.add_slice_by_state(state); + } + return metric; +} } // anonymous namespace TEST_F(ConfigUpdateTest, TestSimpleMatcherPreserve) { @@ -1537,6 +1554,115 @@ TEST_F(ConfigUpdateTest, TestDurationMetricStateChange) { EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE); } +TEST_F(ConfigUpdateTest, TestValueMetricPreserve) { + StatsdConfig config; + AtomMatcher startMatcher = CreateScreenTurnedOnAtomMatcher(); + *config.add_atom_matcher() = startMatcher; + AtomMatcher stopMatcher = CreateScreenTurnedOffAtomMatcher(); + *config.add_atom_matcher() = stopMatcher; + AtomMatcher whatMatcher = CreateTemperatureAtomMatcher(); + *config.add_atom_matcher() = whatMatcher; + + Predicate predicate = CreateScreenIsOnPredicate(); + *config.add_predicate() = predicate; + State sliceState = CreateScreenState(); + *config.add_state() = sliceState; + + *config.add_value_metric() = + createValueMetric("VALUE1", whatMatcher, predicate.id(), {sliceState.id()}); + EXPECT_TRUE(initConfig(config)); + + unordered_map metricToActivationMap; + vector metricsToUpdate(1, UPDATE_UNKNOWN); + EXPECT_TRUE(determineAllMetricUpdateStatuses(config, oldMetricProducerMap, oldMetricProducers, + metricToActivationMap, + /*replacedMatchers*/ {}, /*replacedConditions=*/{}, + /*replacedStates=*/{}, metricsToUpdate)); + EXPECT_EQ(metricsToUpdate[0], UPDATE_PRESERVE); +} + +TEST_F(ConfigUpdateTest, TestValueMetricDefinitionChange) { + StatsdConfig config; + AtomMatcher whatMatcher = CreateScreenBrightnessChangedAtomMatcher(); + *config.add_atom_matcher() = whatMatcher; + + *config.add_value_metric() = createValueMetric("VALUE1", whatMatcher, nullopt, {}); + EXPECT_TRUE(initConfig(config)); + + // Change skip zero diff output, which should change the proto, causing replacement. + config.mutable_value_metric(0)->set_skip_zero_diff_output(true); + + unordered_map metricToActivationMap; + vector metricsToUpdate(1, UPDATE_UNKNOWN); + EXPECT_TRUE(determineAllMetricUpdateStatuses(config, oldMetricProducerMap, oldMetricProducers, + metricToActivationMap, + /*replacedMatchers*/ {}, /*replacedConditions=*/{}, + /*replacedStates=*/{}, metricsToUpdate)); + EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE); +} + +TEST_F(ConfigUpdateTest, TestValueMetricWhatChanged) { + StatsdConfig config; + AtomMatcher whatMatcher = CreateTemperatureAtomMatcher(); + *config.add_atom_matcher() = whatMatcher; + + *config.add_value_metric() = createValueMetric("VALUE1", whatMatcher, nullopt, {}); + EXPECT_TRUE(initConfig(config)); + + unordered_map metricToActivationMap; + vector metricsToUpdate(1, UPDATE_UNKNOWN); + EXPECT_TRUE(determineAllMetricUpdateStatuses( + config, oldMetricProducerMap, oldMetricProducers, metricToActivationMap, + /*replacedMatchers*/ {whatMatcher.id()}, /*replacedConditions=*/{}, + /*replacedStates=*/{}, metricsToUpdate)); + EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE); +} + +TEST_F(ConfigUpdateTest, TestValueMetricConditionChanged) { + StatsdConfig config; + AtomMatcher startMatcher = CreateScreenTurnedOnAtomMatcher(); + *config.add_atom_matcher() = startMatcher; + AtomMatcher stopMatcher = CreateScreenTurnedOffAtomMatcher(); + *config.add_atom_matcher() = stopMatcher; + AtomMatcher whatMatcher = CreateTemperatureAtomMatcher(); + *config.add_atom_matcher() = whatMatcher; + + Predicate predicate = CreateScreenIsOnPredicate(); + *config.add_predicate() = predicate; + + *config.add_value_metric() = createValueMetric("VALUE1", whatMatcher, predicate.id(), {}); + EXPECT_TRUE(initConfig(config)); + + unordered_map metricToActivationMap; + vector metricsToUpdate(1, UPDATE_UNKNOWN); + EXPECT_TRUE(determineAllMetricUpdateStatuses( + config, oldMetricProducerMap, oldMetricProducers, metricToActivationMap, + /*replacedMatchers*/ {}, /*replacedConditions=*/{predicate.id()}, + /*replacedStates=*/{}, metricsToUpdate)); + EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE); +} + +TEST_F(ConfigUpdateTest, TestValueMetricStateChanged) { + StatsdConfig config; + AtomMatcher whatMatcher = CreateScreenBrightnessChangedAtomMatcher(); + *config.add_atom_matcher() = whatMatcher; + + State sliceState = CreateScreenState(); + *config.add_state() = sliceState; + + *config.add_value_metric() = + createValueMetric("VALUE1", whatMatcher, nullopt, {sliceState.id()}); + EXPECT_TRUE(initConfig(config)); + + unordered_map metricToActivationMap; + vector metricsToUpdate(1, UPDATE_UNKNOWN); + EXPECT_TRUE(determineAllMetricUpdateStatuses( + config, oldMetricProducerMap, oldMetricProducers, metricToActivationMap, + /*replacedMatchers*/ {}, /*replacedConditions=*/{}, + /*replacedStates=*/{sliceState.id()}, metricsToUpdate)); + EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE); +} + TEST_F(ConfigUpdateTest, TestUpdateEventMetrics) { StatsdConfig config;