Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef COUNT_METRIC_PRODUCER_H |
| 18 | #define COUNT_METRIC_PRODUCER_H |
| 19 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 20 | #include <unordered_map> |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 21 | |
Bookatz | a4bc9c4 | 2017-10-04 11:45:57 -0700 | [diff] [blame] | 22 | #include "CountAnomalyTracker.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 23 | #include "../condition/ConditionTracker.h" |
| 24 | #include "../matchers/matcher_util.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 25 | #include "MetricProducer.h" |
| 26 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
| 27 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 28 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 29 | using namespace std; |
| 30 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace os { |
| 33 | namespace statsd { |
| 34 | |
| 35 | class CountMetricProducer : public MetricProducer { |
| 36 | public: |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 37 | CountMetricProducer(const CountMetric& countMetric, const bool hasCondition); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 38 | |
| 39 | virtual ~CountMetricProducer(); |
| 40 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame^] | 41 | void onMatchedLogEvent(const LogEvent& event) override; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 42 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 43 | void onConditionChanged(const bool conditionMet) override; |
| 44 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 45 | void finish() override; |
| 46 | |
| 47 | void onDumpReport() override; |
| 48 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 49 | // TODO: Implement this later. |
| 50 | virtual void notifyAppUpgrade(const string& apk, const int uid, const int version) override {}; |
| 51 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 52 | private: |
| 53 | const CountMetric mMetric; |
| 54 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 55 | const time_t mStartTime; |
| 56 | // TODO: Add dimensions. |
Bookatz | a4bc9c4 | 2017-10-04 11:45:57 -0700 | [diff] [blame] | 57 | // Counter value for the current bucket. |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 58 | int mCounter; |
| 59 | |
| 60 | time_t mCurrentBucketStartTime; |
| 61 | |
| 62 | long mBucketSize_sec; |
| 63 | |
Bookatz | a4bc9c4 | 2017-10-04 11:45:57 -0700 | [diff] [blame] | 64 | CountAnomalyTracker mAnomalyTracker; |
| 65 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 66 | bool mCondition; |
| 67 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 68 | void flushCounterIfNeeded(const time_t& newEventTime); |
| 69 | }; |
| 70 | |
| 71 | } // namespace statsd |
| 72 | } // namespace os |
| 73 | } // namespace android |
| 74 | #endif // COUNT_METRIC_PRODUCER_H |