Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 37f1f0e9 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Determine update status for duration metric"

parents 5045b3bb 213c07e3
Loading
Loading
Loading
Loading
+17 −3
Original line number Diff line number Diff line
@@ -516,6 +516,21 @@ bool determineAllMetricUpdateStatuses(const StatsdConfig& config,
            return false;
        }
    }
    for (int i = 0; i < config.duration_metric_size(); i++, metricIndex++) {
        const DurationMetric& metric = config.duration_metric(i);
        set<int64_t> conditionDependencies({metric.what()});
        if (metric.has_condition()) {
            conditionDependencies.insert(metric.condition());
        }
        if (!determineMetricUpdateStatus(
                    config, metric, metric.id(), METRIC_TYPE_DURATION, /*matcherDependencies=*/{},
                    conditionDependencies, metric.slice_by_state(), metric.links(),
                    oldMetricProducerMap, oldMetricProducers, metricToActivationMap,
                    replacedMatchers, replacedConditions, replacedStates,
                    metricsToUpdate[metricIndex])) {
            return false;
        }
    }
    for (int i = 0; i < config.event_metric_size(); i++, metricIndex++) {
        const EventMetric& metric = config.event_metric(i);
        set<int64_t> conditionDependencies;
@@ -538,8 +553,7 @@ bool determineAllMetricUpdateStatuses(const StatsdConfig& config,
        if (metric.has_condition()) {
            conditionDependencies.insert(metric.condition());
        }
        set<int64_t> matcherDependencies;
        matcherDependencies.insert(metric.what());
        set<int64_t> matcherDependencies({metric.what()});
        if (metric.has_trigger_event()) {
            matcherDependencies.insert(metric.trigger_event());
        }
@@ -552,7 +566,7 @@ bool determineAllMetricUpdateStatuses(const StatsdConfig& config,
            return false;
        }
    }
    // TODO: determine update status for value, duration metrics.
    // TODO: determine update status for value metrics.
    return true;
}

+138 −0
Original line number Diff line number Diff line
@@ -155,6 +155,20 @@ GaugeMetric createGaugeMetric(string name, int64_t what, GaugeMetric::SamplingTy
    return metric;
}

DurationMetric createDurationMetric(string name, int64_t what, optional<int64_t> condition,
                                    vector<int64_t> states) {
    DurationMetric metric;
    metric.set_id(StringToId(name));
    metric.set_what(what);
    metric.set_bucket(TEN_MINUTES);
    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) {
@@ -1398,6 +1412,130 @@ TEST_F(ConfigUpdateTest, TestGaugeMetricTriggerEventChanged) {
    EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE);
}

TEST_F(ConfigUpdateTest, TestDurationMetricPreserve) {
    StatsdConfig config;
    AtomMatcher startMatcher = CreateScreenTurnedOnAtomMatcher();
    *config.add_atom_matcher() = startMatcher;
    AtomMatcher stopMatcher = CreateScreenTurnedOffAtomMatcher();
    *config.add_atom_matcher() = stopMatcher;

    Predicate what = CreateScreenIsOnPredicate();
    *config.add_predicate() = what;
    Predicate condition = CreateScreenIsOffPredicate();
    *config.add_predicate() = condition;

    State sliceState = CreateScreenState();
    *config.add_state() = sliceState;

    *config.add_duration_metric() =
            createDurationMetric("DURATION1", what.id(), condition.id(), {sliceState.id()});
    EXPECT_TRUE(initConfig(config));

    unordered_map<int64_t, int> metricToActivationMap;
    vector<UpdateStatus> metricsToUpdate(1, UPDATE_UNKNOWN);
    EXPECT_TRUE(determineAllMetricUpdateStatuses(config, oldMetricProducerMap, oldMetricProducers,
                                                 metricToActivationMap,
                                                 /*replacedMatchers*/ {}, /*replacedConditions=*/{},
                                                 /*replacedStates=*/{}, metricsToUpdate));
    EXPECT_EQ(metricsToUpdate[0], UPDATE_PRESERVE);
}

TEST_F(ConfigUpdateTest, TestDurationMetricDefinitionChange) {
    StatsdConfig config;
    AtomMatcher startMatcher = CreateScreenTurnedOnAtomMatcher();
    *config.add_atom_matcher() = startMatcher;
    AtomMatcher stopMatcher = CreateScreenTurnedOffAtomMatcher();
    *config.add_atom_matcher() = stopMatcher;

    Predicate what = CreateScreenIsOnPredicate();
    *config.add_predicate() = what;

    *config.add_duration_metric() = createDurationMetric("DURATION1", what.id(), nullopt, {});
    EXPECT_TRUE(initConfig(config));

    config.mutable_duration_metric(0)->set_aggregation_type(DurationMetric::MAX_SPARSE);

    unordered_map<int64_t, int> metricToActivationMap;
    vector<UpdateStatus> metricsToUpdate(1, UPDATE_UNKNOWN);
    EXPECT_TRUE(determineAllMetricUpdateStatuses(config, oldMetricProducerMap, oldMetricProducers,
                                                 metricToActivationMap, /*replacedMatchers*/ {},
                                                 /*replacedConditions=*/{}, /*replacedStates=*/{},
                                                 metricsToUpdate));
    EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE);
}

TEST_F(ConfigUpdateTest, TestDurationMetricWhatChanged) {
    StatsdConfig config;
    AtomMatcher startMatcher = CreateScreenTurnedOnAtomMatcher();
    *config.add_atom_matcher() = startMatcher;
    AtomMatcher stopMatcher = CreateScreenTurnedOffAtomMatcher();
    *config.add_atom_matcher() = stopMatcher;

    Predicate what = CreateScreenIsOnPredicate();
    *config.add_predicate() = what;

    *config.add_duration_metric() = createDurationMetric("DURATION1", what.id(), nullopt, {});
    EXPECT_TRUE(initConfig(config));

    unordered_map<int64_t, int> metricToActivationMap;
    vector<UpdateStatus> metricsToUpdate(1, UPDATE_UNKNOWN);
    EXPECT_TRUE(determineAllMetricUpdateStatuses(
            config, oldMetricProducerMap, oldMetricProducers, metricToActivationMap,
            /*replacedMatchers*/ {}, /*replacedConditions=*/{what.id()},
            /*replacedStates=*/{}, metricsToUpdate));
    EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE);
}

TEST_F(ConfigUpdateTest, TestDurationMetricConditionChanged) {
    StatsdConfig config;
    AtomMatcher startMatcher = CreateScreenTurnedOnAtomMatcher();
    *config.add_atom_matcher() = startMatcher;
    AtomMatcher stopMatcher = CreateScreenTurnedOffAtomMatcher();
    *config.add_atom_matcher() = stopMatcher;

    Predicate what = CreateScreenIsOnPredicate();
    *config.add_predicate() = what;
    Predicate condition = CreateScreenIsOffPredicate();
    *config.add_predicate() = condition;

    *config.add_duration_metric() = createDurationMetric("DURATION", what.id(), condition.id(), {});
    EXPECT_TRUE(initConfig(config));

    unordered_map<int64_t, int> metricToActivationMap;
    vector<UpdateStatus> metricsToUpdate(1, UPDATE_UNKNOWN);
    EXPECT_TRUE(determineAllMetricUpdateStatuses(
            config, oldMetricProducerMap, oldMetricProducers, metricToActivationMap,
            /*replacedMatchers*/ {}, /*replacedConditions=*/{condition.id()},
            /*replacedStates=*/{}, metricsToUpdate));
    EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE);
}

TEST_F(ConfigUpdateTest, TestDurationMetricStateChange) {
    StatsdConfig config;
    AtomMatcher startMatcher = CreateScreenTurnedOnAtomMatcher();
    *config.add_atom_matcher() = startMatcher;
    AtomMatcher stopMatcher = CreateScreenTurnedOffAtomMatcher();
    *config.add_atom_matcher() = stopMatcher;

    Predicate what = CreateScreenIsOnPredicate();
    *config.add_predicate() = what;

    State sliceState = CreateScreenState();
    *config.add_state() = sliceState;

    *config.add_duration_metric() =
            createDurationMetric("DURATION1", what.id(), nullopt, {sliceState.id()});
    EXPECT_TRUE(initConfig(config));

    unordered_map<int64_t, int> metricToActivationMap;
    vector<UpdateStatus> 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;