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

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

Merge "Refactor MetricProducer so that a big chunk of boiler plate code can be removed."

parents fd3044a1 b704177d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ statsd_common_src := \
    src/matchers/matcher_util.cpp \
    src/matchers/SimpleLogMatchingTracker.cpp \
    src/metrics/CountAnomalyTracker.cpp \
    src/metrics/MetricProducer.cpp \
    src/metrics/CountMetricProducer.cpp \
    src/metrics/DurationMetricProducer.cpp \
    src/metrics/MetricsManager.cpp \
+11 −40
Original line number Diff line number Diff line
@@ -124,52 +124,23 @@ void CountMetricProducer::onConditionChanged(const bool conditionMet) {
    mCondition = conditionMet;
}

void CountMetricProducer::onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) {
void CountMetricProducer::onMatchedLogEventInternal(
        const size_t matcherIndex, const HashableDimensionKey& eventKey,
        const map<string, HashableDimensionKey>& conditionKey, bool condition,
        const LogEvent& event) {
    uint64_t eventTimeNs = event.GetTimestampNs();
    // this is old event, maybe statsd restarted?
    if (eventTimeNs < mStartTimeNs) {
        return;
    }

    flushCounterIfNeeded(eventTimeNs);

    if (mConditionSliced) {
        map<string, HashableDimensionKey> conditionKeys;
        for (const auto& link : mConditionLinks) {
            VLOG("Condition link key_in_main size %d", link.key_in_main_size());
            HashableDimensionKey conditionKey = getDimensionKeyForCondition(event, link);
            conditionKeys[link.condition()] = conditionKey;
        }
        if (mWizard->query(mConditionTrackerIndex, conditionKeys) != ConditionState::kTrue) {
            VLOG("metric %lld sliced condition not met", mMetric.metric_id());
            return;
        }
    } else {
        if (!mCondition) {
            VLOG("metric %lld condition not met", mMetric.metric_id());
    if (condition == false) {
        return;
    }
    }

    HashableDimensionKey hashableKey;

    if (mDimension.size() > 0) {
        vector<KeyValuePair> key = getDimensionKey(event, mDimension);
        hashableKey = getHashableKey(key);
        // Add the HashableDimensionKey->vector<KeyValuePair> to the map, because StatsLogReport
        // expects vector<KeyValuePair>.
        if (mDimensionKeyMap.find(hashableKey) == mDimensionKeyMap.end()) {
            mDimensionKeyMap[hashableKey] = key;
        }
    } else {
        hashableKey = DEFAULT_DIMENSION_KEY;
    }

    auto it = mCurrentSlicedCounter.find(hashableKey);
    auto it = mCurrentSlicedCounter.find(eventKey);

    if (it == mCurrentSlicedCounter.end()) {
        // create a counter for the new key
        mCurrentSlicedCounter[hashableKey] = 1;
        mCurrentSlicedCounter[eventKey] = 1;

    } else {
        // increment the existing value
@@ -177,8 +148,8 @@ void CountMetricProducer::onMatchedLogEvent(const size_t matcherIndex, const Log
        count++;
    }

    VLOG("metric %lld %s->%d", mMetric.metric_id(), hashableKey.c_str(),
         mCurrentSlicedCounter[hashableKey]);
    VLOG("metric %lld %s->%d", mMetric.metric_id(), eventKey.c_str(),
         mCurrentSlicedCounter[eventKey]);
}

// When a new matched event comes in, we check if event falls into the current
+5 −2
Original line number Diff line number Diff line
@@ -41,8 +41,6 @@ public:

    virtual ~CountMetricProducer();

    void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) override;

    void onConditionChanged(const bool conditionMet) override;

    void finish() override;
@@ -54,6 +52,11 @@ public:
    // TODO: Implement this later.
    virtual void notifyAppUpgrade(const string& apk, const int uid, const int version) override{};

protected:
    void onMatchedLogEventInternal(const size_t matcherIndex, const HashableDimensionKey& eventKey,
                                   const std::map<std::string, HashableDimensionKey>& conditionKey,
                                   bool condition, const LogEvent& event) override;

private:
    const CountMetric mMetric;

+13 −43
Original line number Diff line number Diff line
@@ -137,11 +137,10 @@ StatsLogReport DurationMetricProducer::onDumpReport() {
    return report;
};

void DurationMetricProducer::onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) {
    if (event.GetTimestampNs() < mStartTimeNs) {
        return;
    }

void DurationMetricProducer::onMatchedLogEventInternal(
        const size_t matcherIndex, const HashableDimensionKey& eventKey,
        const map<string, HashableDimensionKey>& conditionKeys, bool condition,
        const LogEvent& event) {
    flushDurationIfNeeded(event.GetTimestampNs());

    if (matcherIndex == mStopAllIndex) {
@@ -149,49 +148,20 @@ void DurationMetricProducer::onMatchedLogEvent(const size_t matcherIndex, const
        return;
    }

    HashableDimensionKey hashableKey;
    if (mDimension.size() > 0) {
        // hook up sliced counter with AnomalyMonitor.
        vector<KeyValuePair> key = getDimensionKey(event, mDimension);
        hashableKey = getHashableKey(key);
        // Add the HashableDimensionKey->DimensionKey to the map, because StatsLogReport expects
        // vector<KeyValuePair>.
        if (mDimensionKeyMap.find(hashableKey) == mDimensionKeyMap.end()) {
            mDimensionKeyMap[hashableKey] = key;
        }
    } else {
        hashableKey = DEFAULT_DIMENSION_KEY;
    }

    if (mCurrentSlicedDuration.find(hashableKey) == mCurrentSlicedDuration.end() &&
        mConditionSliced) {
    if (mCurrentSlicedDuration.find(eventKey) == mCurrentSlicedDuration.end() && mConditionSliced) {
        // add the durationInfo for the current bucket.
        auto& durationInfo = mCurrentSlicedDuration[hashableKey];
        auto& conditionKeys = durationInfo.conditionKeys;
        // get and cache the keys for query condition.
        for (const auto& link : mConditionLinks) {
            HashableDimensionKey conditionKey = getDimensionKeyForCondition(event, link);
            conditionKeys[link.condition()] = conditionKey;
        }
    }

    bool conditionMet;
    if (mConditionSliced) {
        const auto& conditionKeys = mCurrentSlicedDuration[hashableKey].conditionKeys;
        conditionMet =
                mWizard->query(mConditionTrackerIndex, conditionKeys) == ConditionState::kTrue;
    } else {
        conditionMet = mCondition;
        auto& durationInfo = mCurrentSlicedDuration[eventKey];
        durationInfo.conditionKeys = conditionKeys;
    }

    if (matcherIndex == mStartIndex) {
        VLOG("Metric %lld Key: %s Start, Condition %d", mMetric.metric_id(), hashableKey.c_str(),
             conditionMet);
        noteStart(hashableKey, conditionMet, event.GetTimestampNs());
        VLOG("Metric %lld Key: %s Start, Condition %d", mMetric.metric_id(), eventKey.c_str(),
             condition);
        noteStart(eventKey, condition, event.GetTimestampNs());
    } else if (matcherIndex == mStopIndex) {
        VLOG("Metric %lld Key: %s Stop, Condition %d", mMetric.metric_id(), hashableKey.c_str(),
             conditionMet);
        noteStop(hashableKey, event.GetTimestampNs());
        VLOG("Metric %lld Key: %s Stop, Condition %d", mMetric.metric_id(), eventKey.c_str(),
             condition);
        noteStop(eventKey, event.GetTimestampNs());
    }
}

+5 −2
Original line number Diff line number Diff line
@@ -61,8 +61,6 @@ public:

    virtual ~DurationMetricProducer();

    void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) override;

    void onConditionChanged(const bool conditionMet) override;

    void finish() override;
@@ -74,6 +72,11 @@ public:
    // TODO: Implement this later.
    virtual void notifyAppUpgrade(const string& apk, const int uid, const int version) override{};

protected:
    void onMatchedLogEventInternal(const size_t matcherIndex, const HashableDimensionKey& eventKey,
                                   const std::map<std::string, HashableDimensionKey>& conditionKeys,
                                   bool condition, const LogEvent& event) override;

private:
    const DurationMetric mMetric;

Loading