blob: 635777fe2267b340571851bcad31de4647214301 [file] [log] [blame]
Yao Chen44cf27c2017-09-14 22:32:50 -07001/*
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#define LOG_TAG "CountMetric"
18#define DEBUG true // STOPSHIP if true
19#define VLOG(...) \
20 if (DEBUG) ALOGD(__VA_ARGS__);
21
22#include "CountMetricProducer.h"
Bookatza4bc9c42017-10-04 11:45:57 -070023#include "CountAnomalyTracker.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070024#include "parse_util.h"
25
26#include <cutils/log.h>
27#include <limits.h>
28#include <stdlib.h>
29
30using std::unordered_map;
31
32namespace android {
33namespace os {
34namespace statsd {
35
36CountMetricProducer::CountMetricProducer(const CountMetric& metric,
37 const sp<ConditionTracker> condition)
38 : mMetric(metric),
39 mConditionTracker(condition),
40 mStartTime(std::time(nullptr)),
41 mCounter(0),
Bookatza4bc9c42017-10-04 11:45:57 -070042 mCurrentBucketStartTime(mStartTime),
43 // TODO: read mAnomalyTracker parameters from config file.
44 mAnomalyTracker(6, 10) {
Yao Chen44cf27c2017-09-14 22:32:50 -070045 // TODO: evaluate initial conditions. and set mConditionMet.
46 if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) {
47 mBucketSize_sec = metric.bucket().bucket_size_millis() / 1000;
48 } else {
49 mBucketSize_sec = LONG_MAX;
50 }
51
52 VLOG("created. bucket size %lu start_time: %lu", mBucketSize_sec, mStartTime);
53}
54
55CountMetricProducer::CountMetricProducer(const CountMetric& metric)
56 : CountMetricProducer(metric, new ConditionTracker()) {
57}
58
59CountMetricProducer::~CountMetricProducer() {
60 VLOG("~CountMetricProducer() called");
61}
62
63void CountMetricProducer::finish() {
64 // TODO: write the StatsLogReport to dropbox using
65 // DropboxWriter.
66 onDumpReport();
67}
68
69void CountMetricProducer::onDumpReport() {
70 VLOG("dump report now...");
71}
72
73void CountMetricProducer::onMatchedLogEvent(const LogEventWrapper& event) {
74 time_t eventTime = event.timestamp_ns / 1000000000;
75
76 // this is old event, maybe statsd restarted?
77 if (eventTime < mStartTime) {
78 return;
79 }
80
81 if (mConditionTracker->isConditionMet()) {
82 flushCounterIfNeeded(eventTime);
83 mCounter++;
Bookatza4bc9c42017-10-04 11:45:57 -070084 mAnomalyTracker.checkAnomaly(mCounter);
Yao Chen44cf27c2017-09-14 22:32:50 -070085 }
86}
87
88// When a new matched event comes in, we check if it falls into the current bucket. And flush the
89// counter to the StatsLogReport and adjust the bucket if needed.
90void CountMetricProducer::flushCounterIfNeeded(const time_t& eventTime) {
91 if (mCurrentBucketStartTime + mBucketSize_sec > eventTime) {
92 return;
93 }
94
95 // TODO: add a KeyValuePair to StatsLogReport.
96 ALOGD("CountMetric: dump counter %d", mCounter);
97
Yao Chen44cf27c2017-09-14 22:32:50 -070098 // adjust the bucket start time
Bookatza4bc9c42017-10-04 11:45:57 -070099 time_t numBucketsForward = (eventTime - mCurrentBucketStartTime)
100 / mBucketSize_sec;
101
102 mCurrentBucketStartTime = mCurrentBucketStartTime +
103 (numBucketsForward) * mBucketSize_sec;
104
105 // reset counter
106 mAnomalyTracker.addPastBucket(mCounter, numBucketsForward);
107 mCounter = 0;
Yao Chen44cf27c2017-09-14 22:32:50 -0700108
109 VLOG("new bucket start time: %lu", mCurrentBucketStartTime);
110}
111
112} // namespace statsd
113} // namespace os
114} // namespace android