MetricProducer.cpp revision 13fb7e4eeaf7aee408821afe7ee55a5167e49e59
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#define DEBUG true  // STOPSHIP if true
18#include "Log.h"
19#include "MetricProducer.h"
20
21namespace android {
22namespace os {
23namespace statsd {
24
25using std::map;
26
27void MetricProducer::onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event) {
28    uint64_t eventTimeNs = event.GetElapsedTimestampNs();
29    // this is old event, maybe statsd restarted?
30    if (eventTimeNs < mStartTimeNs) {
31        return;
32    }
33
34    bool condition;
35    ConditionKey conditionKey;
36
37    std::unordered_set<HashableDimensionKey> dimensionKeysInCondition;
38    if (mConditionSliced) {
39        for (const auto& link : mMetric2ConditionLinks) {
40            getDimensionForCondition(event.getValues(), link, &conditionKey[link.conditionId]);
41        }
42        auto conditionState =
43            mWizard->query(mConditionTrackerIndex, conditionKey, mDimensionsInCondition,
44                           !mSameConditionDimensionsInTracker,
45                           !mHasLinksToAllConditionDimensionsInTracker,
46                           &dimensionKeysInCondition);
47        condition = (conditionState == ConditionState::kTrue);
48    } else {
49        condition = mCondition;
50    }
51
52    if (mDimensionsInCondition.empty() && condition) {
53        dimensionKeysInCondition.insert(DEFAULT_DIMENSION_KEY);
54    }
55
56    if (mContainANYPositionInDimensionsInWhat) {
57        vector<HashableDimensionKey> dimensionInWhatValues;
58        if (!mDimensionsInWhat.empty()) {
59            filterValues(mDimensionsInWhat, event.getValues(), &dimensionInWhatValues);
60        } else {
61            dimensionInWhatValues.push_back(DEFAULT_DIMENSION_KEY);
62        }
63
64        for (const auto& whatDimension : dimensionInWhatValues) {
65            for (const auto& conditionDimensionKey : dimensionKeysInCondition) {
66                onMatchedLogEventInternalLocked(
67                        matcherIndex, MetricDimensionKey(whatDimension, conditionDimensionKey),
68                        conditionKey, condition, event);
69            }
70            if (dimensionKeysInCondition.empty()) {
71                onMatchedLogEventInternalLocked(
72                        matcherIndex, MetricDimensionKey(whatDimension, DEFAULT_DIMENSION_KEY),
73                         conditionKey, condition, event);
74            }
75        }
76    } else {
77        HashableDimensionKey dimensionInWhat;
78        filterValues(mDimensionsInWhat, event.getValues(), &dimensionInWhat);
79        MetricDimensionKey metricKey(dimensionInWhat, DEFAULT_DIMENSION_KEY);
80        for (const auto& conditionDimensionKey : dimensionKeysInCondition) {
81            metricKey.setDimensionKeyInCondition(conditionDimensionKey);
82            onMatchedLogEventInternalLocked(
83                    matcherIndex, metricKey, conditionKey, condition, event);
84        }
85        if (dimensionKeysInCondition.empty()) {
86            onMatchedLogEventInternalLocked(
87                    matcherIndex, metricKey, conditionKey, condition, event);
88        }
89    }
90
91 }
92
93}  // namespace statsd
94}  // namespace os
95}  // namespace android
96