CountMetricProducer.h revision 729093df0dd7d7038ad43b16ecdb59a1404f3b50
144cf27c14880005df093f372491c593e1a9a3c58Yao Chen/*
244cf27c14880005df093f372491c593e1a9a3c58Yao Chen * Copyright (C) 2017 The Android Open Source Project
344cf27c14880005df093f372491c593e1a9a3c58Yao Chen *
444cf27c14880005df093f372491c593e1a9a3c58Yao Chen * Licensed under the Apache License, Version 2.0 (the "License");
544cf27c14880005df093f372491c593e1a9a3c58Yao Chen * you may not use this file except in compliance with the License.
644cf27c14880005df093f372491c593e1a9a3c58Yao Chen * You may obtain a copy of the License at
744cf27c14880005df093f372491c593e1a9a3c58Yao Chen *
844cf27c14880005df093f372491c593e1a9a3c58Yao Chen *      http://www.apache.org/licenses/LICENSE-2.0
944cf27c14880005df093f372491c593e1a9a3c58Yao Chen *
1044cf27c14880005df093f372491c593e1a9a3c58Yao Chen * Unless required by applicable law or agreed to in writing, software
1144cf27c14880005df093f372491c593e1a9a3c58Yao Chen * distributed under the License is distributed on an "AS IS" BASIS,
1244cf27c14880005df093f372491c593e1a9a3c58Yao Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1344cf27c14880005df093f372491c593e1a9a3c58Yao Chen * See the License for the specific language governing permissions and
1444cf27c14880005df093f372491c593e1a9a3c58Yao Chen * limitations under the License.
1544cf27c14880005df093f372491c593e1a9a3c58Yao Chen */
1644cf27c14880005df093f372491c593e1a9a3c58Yao Chen
1744cf27c14880005df093f372491c593e1a9a3c58Yao Chen#ifndef COUNT_METRIC_PRODUCER_H
1844cf27c14880005df093f372491c593e1a9a3c58Yao Chen#define COUNT_METRIC_PRODUCER_H
1944cf27c14880005df093f372491c593e1a9a3c58Yao Chen
2044cf27c14880005df093f372491c593e1a9a3c58Yao Chen#include <unordered_map>
21caf339d004fad667748b68912c254df4e75cdc5aYao Chen
22caf339d004fad667748b68912c254df4e75cdc5aYao Chen#include "../condition/ConditionTracker.h"
23caf339d004fad667748b68912c254df4e75cdc5aYao Chen#include "../matchers/matcher_util.h"
24729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen#include "CountAnomalyTracker.h"
2544cf27c14880005df093f372491c593e1a9a3c58Yao Chen#include "MetricProducer.h"
2644cf27c14880005df093f372491c593e1a9a3c58Yao Chen#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
2744cf27c14880005df093f372491c593e1a9a3c58Yao Chen#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
28729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen#include "stats_util.h"
2944cf27c14880005df093f372491c593e1a9a3c58Yao Chen
30de70169109c57787a23c732ec4b361ade2e9850eDavid Chenusing namespace std;
31de70169109c57787a23c732ec4b361ade2e9850eDavid Chen
3244cf27c14880005df093f372491c593e1a9a3c58Yao Chennamespace android {
3344cf27c14880005df093f372491c593e1a9a3c58Yao Chennamespace os {
3444cf27c14880005df093f372491c593e1a9a3c58Yao Chennamespace statsd {
3544cf27c14880005df093f372491c593e1a9a3c58Yao Chen
3644cf27c14880005df093f372491c593e1a9a3c58Yao Chenclass CountMetricProducer : public MetricProducer {
3744cf27c14880005df093f372491c593e1a9a3c58Yao Chenpublic:
38729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen    // TODO: Pass in the start time from MetricsManager, it should be consistent for all metrics.
39729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen    CountMetricProducer(const CountMetric& countMetric, const int conditionIndex,
40729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen                        const sp<ConditionWizard>& wizard);
4144cf27c14880005df093f372491c593e1a9a3c58Yao Chen
4244cf27c14880005df093f372491c593e1a9a3c58Yao Chen    virtual ~CountMetricProducer();
4344cf27c14880005df093f372491c593e1a9a3c58Yao Chen
44729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen    void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) override;
4544cf27c14880005df093f372491c593e1a9a3c58Yao Chen
46caf339d004fad667748b68912c254df4e75cdc5aYao Chen    void onConditionChanged(const bool conditionMet) override;
47caf339d004fad667748b68912c254df4e75cdc5aYao Chen
4844cf27c14880005df093f372491c593e1a9a3c58Yao Chen    void finish() override;
4944cf27c14880005df093f372491c593e1a9a3c58Yao Chen
50729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen    StatsLogReport onDumpReport() override;
51729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen
52729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen    void onSlicedConditionMayChange() override;
5344cf27c14880005df093f372491c593e1a9a3c58Yao Chen
54de70169109c57787a23c732ec4b361ade2e9850eDavid Chen    // TODO: Implement this later.
55729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen    virtual void notifyAppUpgrade(const string& apk, const int uid, const int version) override{};
56de70169109c57787a23c732ec4b361ade2e9850eDavid Chen
5744cf27c14880005df093f372491c593e1a9a3c58Yao Chenprivate:
5844cf27c14880005df093f372491c593e1a9a3c58Yao Chen    const CountMetric mMetric;
5944cf27c14880005df093f372491c593e1a9a3c58Yao Chen
60a4bc9c4a1e39574ca83a56cf6cfa75c40059b4cbBookatz    CountAnomalyTracker mAnomalyTracker;
61a4bc9c4a1e39574ca83a56cf6cfa75c40059b4cbBookatz
62729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen    // Save the past buckets and we can clear when the StatsLogReport is dumped.
63729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen    std::unordered_map<HashableDimensionKey, std::vector<CountBucketInfo>> mPastBuckets;
64729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen
65729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen    // The current bucket.
66729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen    std::unordered_map<HashableDimensionKey, int> mCurrentSlicedCounter;
67caf339d004fad667748b68912c254df4e75cdc5aYao Chen
68729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen    void flushCounterIfNeeded(const uint64_t newEventTime);
6944cf27c14880005df093f372491c593e1a9a3c58Yao Chen};
7044cf27c14880005df093f372491c593e1a9a3c58Yao Chen
7144cf27c14880005df093f372491c593e1a9a3c58Yao Chen}  // namespace statsd
7244cf27c14880005df093f372491c593e1a9a3c58Yao Chen}  // namespace os
7344cf27c14880005df093f372491c593e1a9a3c58Yao Chen}  // namespace android
7444cf27c14880005df093f372491c593e1a9a3c58Yao Chen#endif  // COUNT_METRIC_PRODUCER_H
75