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
30namespace android {
31namespace os {
32namespace statsd {
33
34class EventMetricProducer : public MetricProducer {
35public:
36    // TODO: Pass in the start time from MetricsManager, it should be consistent for all metrics.
37    EventMetricProducer(const ConfigKey& key, const EventMetric& eventMetric,
38                        const int conditionIndex, const sp<ConditionWizard>& wizard,
39                        const int64_t startTimeNs);
40
41    virtual ~EventMetricProducer();
42
43private:
44    void onMatchedLogEventInternalLocked(
45            const size_t matcherIndex, const MetricDimensionKey& eventKey,
46            const ConditionKey& conditionKey, bool condition,
47            const LogEvent& event) override;
48
49    void onDumpReportLocked(const int64_t dumpTimeNs,
50                            const bool include_current_partial_bucket,
51                            std::set<string> *str_set,
52                            android::util::ProtoOutputStream* protoOutput) override;
53    void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
54
55    // Internal interface to handle condition change.
56    void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override;
57
58    // Internal interface to handle sliced condition change.
59    void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
60
61    void dropDataLocked(const int64_t dropTimeNs) override;
62
63    // Internal function to calculate the current used bytes.
64    size_t byteSizeLocked() const override;
65
66    void dumpStatesLocked(FILE* out, bool verbose) const override{};
67
68    // Maps to a EventMetricDataWrapper. Storing atom events in ProtoOutputStream
69    // is more space efficient than storing LogEvent.
70    std::unique_ptr<android::util::ProtoOutputStream> mProto;
71};
72
73}  // namespace statsd
74}  // namespace os
75}  // namespace android
76#endif  // EVENT_METRIC_PRODUCER_H
77