blob: fbd013ebf52895b8865b19025ce59d0ead2912d2 [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"
23#include "parse_util.h"
24
25#include <cutils/log.h>
26#include <limits.h>
27#include <stdlib.h>
28
29using std::unordered_map;
30
31namespace android {
32namespace os {
33namespace statsd {
34
35CountMetricProducer::CountMetricProducer(const CountMetric& metric,
36 const sp<ConditionTracker> condition)
37 : mMetric(metric),
38 mConditionTracker(condition),
39 mStartTime(std::time(nullptr)),
40 mCounter(0),
41 mCurrentBucketStartTime(mStartTime) {
42 // TODO: evaluate initial conditions. and set mConditionMet.
43 if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) {
44 mBucketSize_sec = metric.bucket().bucket_size_millis() / 1000;
45 } else {
46 mBucketSize_sec = LONG_MAX;
47 }
48
49 VLOG("created. bucket size %lu start_time: %lu", mBucketSize_sec, mStartTime);
50}
51
52CountMetricProducer::CountMetricProducer(const CountMetric& metric)
53 : CountMetricProducer(metric, new ConditionTracker()) {
54}
55
56CountMetricProducer::~CountMetricProducer() {
57 VLOG("~CountMetricProducer() called");
58}
59
60void CountMetricProducer::finish() {
61 // TODO: write the StatsLogReport to dropbox using
62 // DropboxWriter.
63 onDumpReport();
64}
65
66void CountMetricProducer::onDumpReport() {
67 VLOG("dump report now...");
68}
69
70void CountMetricProducer::onMatchedLogEvent(const LogEventWrapper& event) {
71 time_t eventTime = event.timestamp_ns / 1000000000;
72
73 // this is old event, maybe statsd restarted?
74 if (eventTime < mStartTime) {
75 return;
76 }
77
78 if (mConditionTracker->isConditionMet()) {
79 flushCounterIfNeeded(eventTime);
80 mCounter++;
81 }
82}
83
84// When a new matched event comes in, we check if it falls into the current bucket. And flush the
85// counter to the StatsLogReport and adjust the bucket if needed.
86void CountMetricProducer::flushCounterIfNeeded(const time_t& eventTime) {
87 if (mCurrentBucketStartTime + mBucketSize_sec > eventTime) {
88 return;
89 }
90
91 // TODO: add a KeyValuePair to StatsLogReport.
92 ALOGD("CountMetric: dump counter %d", mCounter);
93
94 // reset counter
95 mCounter = 0;
96
97 // adjust the bucket start time
98 mCurrentBucketStartTime =
99 mCurrentBucketStartTime +
100 ((eventTime - mCurrentBucketStartTime) / mBucketSize_sec) * mBucketSize_sec;
101
102 VLOG("new bucket start time: %lu", mCurrentBucketStartTime);
103}
104
105} // namespace statsd
106} // namespace os
107} // namespace android