Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 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 | #include "MetricProducer.h" |
| 17 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame^] | 18 | #include "dimension.h" |
| 19 | |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 20 | namespace android { |
| 21 | namespace os { |
| 22 | namespace statsd { |
| 23 | |
| 24 | using std::map; |
| 25 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 26 | void MetricProducer::onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event) { |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 27 | uint64_t eventTimeNs = event.GetTimestampNs(); |
| 28 | // this is old event, maybe statsd restarted? |
| 29 | if (eventTimeNs < mStartTimeNs) { |
| 30 | return; |
| 31 | } |
| 32 | |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 33 | bool condition; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 34 | ConditionKey conditionKey; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame^] | 35 | |
| 36 | std::unordered_set<HashableDimensionKey> dimensionKeysInCondition; |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 37 | if (mConditionSliced) { |
| 38 | for (const auto& link : mConditionLinks) { |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 39 | getDimensionKeysForCondition(event, link, &conditionKey[link.condition()]); |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 40 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame^] | 41 | auto conditionState = |
| 42 | mWizard->query(mConditionTrackerIndex, conditionKey, mDimensionsInCondition, |
| 43 | &dimensionKeysInCondition); |
| 44 | condition = (conditionState == ConditionState::kTrue); |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 45 | } else { |
| 46 | condition = mCondition; |
| 47 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 48 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame^] | 49 | vector<DimensionsValue> dimensionInWhatValues; |
| 50 | if (mDimensionsInWhat.has_field() && mDimensionsInWhat.child_size() > 0) { |
| 51 | getDimensionKeys(event, mDimensionsInWhat, &dimensionInWhatValues); |
| 52 | } |
| 53 | |
| 54 | if (dimensionInWhatValues.empty() && dimensionKeysInCondition.empty()) { |
| 55 | onMatchedLogEventInternalLocked( |
| 56 | matcherIndex, DEFAULT_METRIC_DIMENSION_KEY, conditionKey, condition, event); |
| 57 | } else if (dimensionKeysInCondition.empty()) { |
| 58 | for (const DimensionsValue& whatValue : dimensionInWhatValues) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 59 | onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame^] | 60 | matcherIndex, |
| 61 | MetricDimensionKey(HashableDimensionKey(whatValue), DEFAULT_DIMENSION_KEY), |
| 62 | conditionKey, condition, event); |
| 63 | } |
| 64 | } else if (dimensionInWhatValues.empty()) { |
| 65 | for (const auto& conditionDimensionKey : dimensionKeysInCondition) { |
| 66 | onMatchedLogEventInternalLocked( |
| 67 | matcherIndex, |
| 68 | MetricDimensionKey(DEFAULT_DIMENSION_KEY, conditionDimensionKey), |
| 69 | conditionKey, condition, event); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 70 | } |
| 71 | } else { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame^] | 72 | for (const DimensionsValue& whatValue : dimensionInWhatValues) { |
| 73 | for (const auto& conditionDimensionKey : dimensionKeysInCondition) { |
| 74 | onMatchedLogEventInternalLocked( |
| 75 | matcherIndex, |
| 76 | MetricDimensionKey(HashableDimensionKey(whatValue), conditionDimensionKey), |
| 77 | conditionKey, condition, event); |
| 78 | } |
| 79 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 80 | } |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | } // namespace statsd |
| 84 | } // namespace os |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 85 | } // namespace android |