blob: 865398127b47de0375c33cff9a7fa56a3a8a9c34 [file] [log] [blame]
Chenjie Yub3dda412017-10-24 13:41:59 -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#pragma once
18
19#include <utils/threads.h>
20#include <list>
21#include "../condition/ConditionTracker.h"
22#include "../external/PullDataReceiver.h"
23#include "../external/StatsPullerManager.h"
24#include "CountAnomalyTracker.h"
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
29namespace android {
30namespace os {
31namespace statsd {
32
33class ValueMetricProducer : public virtual MetricProducer, public virtual PullDataReceiver {
34public:
35 ValueMetricProducer(const ValueMetric& valueMetric, const int conditionIndex,
Chenjie Yu5305e1d2017-10-31 13:49:36 -070036 const sp<ConditionWizard>& wizard, const int pullTagId);
Chenjie Yub3dda412017-10-24 13:41:59 -070037
38 virtual ~ValueMetricProducer();
39
40 void onConditionChanged(const bool condition, const uint64_t eventTime) override;
41
42 void finish() override;
43
44 StatsLogReport onDumpReport() override;
45
46 void onSlicedConditionMayChange(const uint64_t eventTime);
47
48 void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data) override;
49 // TODO: Implement this later.
50 size_t byteSize() override{return 0;};
51
52 // TODO: Implement this later.
53 virtual void notifyAppUpgrade(const string& apk, const int uid, const int version) override{};
54 // TODO: Implement this later.
55 virtual void notifyAppRemoved(const string& apk, const int uid) override{};
56
57protected:
58 void onMatchedLogEventInternal(const size_t matcherIndex, const HashableDimensionKey& eventKey,
59 const std::map<std::string, HashableDimensionKey>& conditionKey,
60 bool condition, const LogEvent& event,
61 bool scheduledPull) override;
62
63private:
64 const ValueMetric mMetric;
65
66 StatsPullerManager& mStatsPullerManager = StatsPullerManager::GetInstance();
67
68 Mutex mLock;
69
Chenjie Yu5305e1d2017-10-31 13:49:36 -070070 // tagId for pulled data. -1 if this is not pulled
71 const int mPullTagId;
Chenjie Yub3dda412017-10-24 13:41:59 -070072
73 // internal state of a bucket.
74 typedef struct {
75 std::vector<std::pair<long, long>> raw;
76 } Interval;
77
78 std::unordered_map<HashableDimensionKey, Interval> mCurrentSlicedBucket;
79 // If condition is true and pulling on schedule, the previous bucket value needs to be carried
80 // over to the next bucket.
81 std::unordered_map<HashableDimensionKey, Interval> mNextSlicedBucket;
82
83 // Save the past buckets and we can clear when the StatsLogReport is dumped.
84 std::unordered_map<HashableDimensionKey, std::vector<ValueBucketInfo>> mPastBuckets;
85
86 long get_value(const LogEvent& event);
87
88 void flush_if_needed(const uint64_t eventTimeNs);
89};
90
91} // namespace statsd
92} // namespace os
93} // namespace android