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 "HashableDimensionKey.h"
27 #include "MetricProducer.h"
28 #include "src/statsd_config.pb.h"
29 #include "stats_util.h"
30 
31 namespace android {
32 namespace os {
33 namespace statsd {
34 
35 class EventMetricProducer : public MetricProducer {
36 public:
37     EventMetricProducer(
38             const ConfigKey& key, const EventMetric& eventMetric, int conditionIndex,
39             const vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
40             const uint64_t protoHash, int64_t startTimeNs,
41             const wp<ConfigMetadataProvider> configMetadataProvider,
42             const std::unordered_map<int, std::shared_ptr<Activation>>& eventActivationMap = {},
43             const std::unordered_map<int, std::vector<std::shared_ptr<Activation>>>&
44                     eventDeactivationMap = {},
45             const vector<int>& slicedStateAtoms = {},
46             const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap = {});
47 
48     virtual ~EventMetricProducer();
49 
getMetricType()50     MetricType getMetricType() const override {
51         return METRIC_TYPE_EVENT;
52     }
53 
54 private:
55     void onMatchedLogEventInternalLocked(
56             const size_t matcherIndex, const MetricDimensionKey& eventKey,
57             const ConditionKey& conditionKey, bool condition, const LogEvent& event,
58             const std::map<int, HashableDimensionKey>& statePrimaryKeys) override;
59 
60     void onDumpReportLocked(const int64_t dumpTimeNs,
61                             const bool include_current_partial_bucket,
62                             const bool erase_data,
63                             const DumpLatency dumpLatency,
64                             std::set<string> *str_set,
65                             android::util::ProtoOutputStream* protoOutput) override;
66     void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
67 
68     // Internal interface to handle condition change.
69     void onConditionChangedLocked(const bool conditionMet, int64_t eventTime) override;
70 
71     // Internal interface to handle sliced condition change.
72     void onSlicedConditionMayChangeLocked(bool overallCondition, int64_t eventTime) override;
73 
74     optional<InvalidConfigReason> onConfigUpdatedLocked(
75             const StatsdConfig& config, int configIndex, int metricIndex,
76             const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
77             const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
78             const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
79             const sp<EventMatcherWizard>& matcherWizard,
80             const std::vector<sp<ConditionTracker>>& allConditionTrackers,
81             const std::unordered_map<int64_t, int>& conditionTrackerMap,
82             const sp<ConditionWizard>& wizard,
83             const std::unordered_map<int64_t, int>& metricToActivationMap,
84             std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
85             std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
86             std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
87             std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
88             std::vector<int>& metricsWithActivation) override;
89 
90     void dropDataLocked(const int64_t dropTimeNs) override;
91 
92     // Internal function to calculate the current used bytes.
93     size_t byteSizeLocked() const override;
94 
dumpStatesLocked(int out,bool verbose)95     void dumpStatesLocked(int out, bool verbose) const override{};
96 
97     DataCorruptionSeverity determineCorruptionSeverity(DataCorruptedReason reason,
98                                                        LostAtomType atomType) const override;
99 
100     // Maps the field/value pairs of an atom to a list of timestamps used to deduplicate atoms.
101     std::unordered_map<AtomDimensionKey, std::vector<int64_t>> mAggregatedAtoms;
102 
103     const int mSamplingPercentage;
104 };
105 
106 }  // namespace statsd
107 }  // namespace os
108 }  // namespace android
109 #endif  // EVENT_METRIC_PRODUCER_H
110