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 EVENT_METRIC_PRODUCER_H
18 #define EVENT_METRIC_PRODUCER_H
19 
20 #include <unordered_map>
21 
22 #include <android/util/ProtoOutputStream.h>
23 
24 #include "../condition/ConditionTracker.h"
25 #include "../matchers/matcher_util.h"
26 #include "MetricProducer.h"
27 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
28 #include "stats_util.h"
29 
30 namespace android {
31 namespace os {
32 namespace statsd {
33 
34 class EventMetricProducer : public MetricProducer {
35 public:
36     EventMetricProducer(
37             const ConfigKey& key, const EventMetric& eventMetric, const int conditionIndex,
38             const vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
39             const int64_t startTimeNs,
40             const std::unordered_map<int, std::shared_ptr<Activation>>& eventActivationMap = {},
41             const std::unordered_map<int, std::vector<std::shared_ptr<Activation>>>&
42                     eventDeactivationMap = {},
43             const vector<int>& slicedStateAtoms = {},
44             const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap = {});
45 
46     virtual ~EventMetricProducer();
47 
48 private:
49     void onMatchedLogEventInternalLocked(
50             const size_t matcherIndex, const MetricDimensionKey& eventKey,
51             const ConditionKey& conditionKey, bool condition, const LogEvent& event,
52             const std::map<int, HashableDimensionKey>& statePrimaryKeys) override;
53 
54     void onDumpReportLocked(const int64_t dumpTimeNs,
55                             const bool include_current_partial_bucket,
56                             const bool erase_data,
57                             const DumpLatency dumpLatency,
58                             std::set<string> *str_set,
59                             android::util::ProtoOutputStream* protoOutput) override;
60     void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
61 
62     // Internal interface to handle condition change.
63     void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override;
64 
65     // Internal interface to handle sliced condition change.
66     void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
67 
68     void dropDataLocked(const int64_t dropTimeNs) override;
69 
70     // Internal function to calculate the current used bytes.
71     size_t byteSizeLocked() const override;
72 
dumpStatesLocked(FILE * out,bool verbose)73     void dumpStatesLocked(FILE* out, bool verbose) const override{};
74 
75     // Maps to a EventMetricDataWrapper. Storing atom events in ProtoOutputStream
76     // is more space efficient than storing LogEvent.
77     std::unique_ptr<android::util::ProtoOutputStream> mProto;
78 };
79 
80 }  // namespace statsd
81 }  // namespace os
82 }  // namespace android
83 #endif  // EVENT_METRIC_PRODUCER_H
84