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 false  // STOPSHIP if true
18 #include "Log.h"
19 #include "MetricProducer.h"
20 
21 namespace android {
22 namespace os {
23 namespace statsd {
24 
25 using std::map;
26 
onMatchedLogEventLocked(const size_t matcherIndex,const LogEvent & event)27 void MetricProducer::onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event) {
28     int64_t eventTimeNs = event.GetElapsedTimestampNs();
29     // this is old event, maybe statsd restarted?
30     if (eventTimeNs < mTimeBaseNs) {
31         return;
32     }
33 
34     bool condition;
35     ConditionKey conditionKey;
36     std::unordered_set<HashableDimensionKey> dimensionKeysInCondition;
37     if (mConditionSliced) {
38         for (const auto& link : mMetric2ConditionLinks) {
39             getDimensionForCondition(event.getValues(), link, &conditionKey[link.conditionId]);
40         }
41         auto conditionState =
42             mWizard->query(mConditionTrackerIndex, conditionKey, mDimensionsInCondition,
43                            !mSameConditionDimensionsInTracker,
44                            !mHasLinksToAllConditionDimensionsInTracker,
45                            &dimensionKeysInCondition);
46         condition = (conditionState == ConditionState::kTrue);
47     } else {
48         condition = mCondition;
49     }
50 
51     if (mDimensionsInCondition.empty() && condition) {
52         dimensionKeysInCondition.insert(DEFAULT_DIMENSION_KEY);
53     }
54 
55     HashableDimensionKey dimensionInWhat;
56     filterValues(mDimensionsInWhat, event.getValues(), &dimensionInWhat);
57     MetricDimensionKey metricKey(dimensionInWhat, DEFAULT_DIMENSION_KEY);
58     for (const auto& conditionDimensionKey : dimensionKeysInCondition) {
59         metricKey.setDimensionKeyInCondition(conditionDimensionKey);
60         onMatchedLogEventInternalLocked(
61                 matcherIndex, metricKey, conditionKey, condition, event);
62     }
63     if (dimensionKeysInCondition.empty()) {
64         onMatchedLogEventInternalLocked(
65                 matcherIndex, metricKey, conditionKey, condition, event);
66     }
67 
68  }
69 
70 }  // namespace statsd
71 }  // namespace os
72 }  // namespace android
73