blob: 85e655b08f4d962882fcc3c2eb021947a1cc9245 [file] [log] [blame]
Yao Chenb7041772017-10-20 16:59:25 -07001/*
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-mac93694462018-01-22 20:49:31 -080018#include "dimension.h"
19
Yao Chenb7041772017-10-20 16:59:25 -070020namespace android {
21namespace os {
22namespace statsd {
23
24using std::map;
25
Chenjie Yua7259ab2017-12-10 08:31:05 -080026void MetricProducer::onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event) {
Yao Chenb7041772017-10-20 16:59:25 -070027 uint64_t eventTimeNs = event.GetTimestampNs();
28 // this is old event, maybe statsd restarted?
29 if (eventTimeNs < mStartTimeNs) {
30 return;
31 }
32
Yao Chenb7041772017-10-20 16:59:25 -070033 bool condition;
Yangster-mac94e197c2018-01-02 16:03:03 -080034 ConditionKey conditionKey;
Yangster-mac93694462018-01-22 20:49:31 -080035
36 std::unordered_set<HashableDimensionKey> dimensionKeysInCondition;
Yao Chenb7041772017-10-20 16:59:25 -070037 if (mConditionSliced) {
38 for (const auto& link : mConditionLinks) {
Yangster-mac7ba8fc32018-01-24 16:16:46 -080039 getDimensionKeysForCondition(event, link, &conditionKey[link.condition()]);
Yao Chenb7041772017-10-20 16:59:25 -070040 }
Yangster-mac93694462018-01-22 20:49:31 -080041 auto conditionState =
42 mWizard->query(mConditionTrackerIndex, conditionKey, mDimensionsInCondition,
43 &dimensionKeysInCondition);
44 condition = (conditionState == ConditionState::kTrue);
Yao Chenb7041772017-10-20 16:59:25 -070045 } else {
46 condition = mCondition;
47 }
Yangster-mac20877162017-12-22 17:19:39 -080048
Yangster-mac93694462018-01-22 20:49:31 -080049 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-mac20877162017-12-22 17:19:39 -080059 onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -080060 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-mac20877162017-12-22 17:19:39 -080070 }
71 } else {
Yangster-mac93694462018-01-22 20:49:31 -080072 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-mac20877162017-12-22 17:19:39 -080080 }
Yao Chenb7041772017-10-20 16:59:25 -070081}
82
83} // namespace statsd
84} // namespace os
yro2b0f8862017-11-06 14:27:31 -080085} // namespace android