blob: 6f4941f717eedeec98e25e0b712602ca2b87c947 [file] [log] [blame]
Yao Chend5aa01b32017-12-19 16:46:36 -08001/*
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#pragma once
18
19#include <utils/JenkinsHash.h>
Yao Chen8a8d16c2018-02-08 14:50:40 -080020#include <vector>
21#include "FieldValue.h"
22#include "android-base/stringprintf.h"
23#include "logd/LogEvent.h"
Yao Chend5aa01b32017-12-19 16:46:36 -080024
25namespace android {
26namespace os {
27namespace statsd {
28
Yao Chen8a8d16c2018-02-08 14:50:40 -080029using android::base::StringPrintf;
30
31struct Metric2Condition {
32 int64_t conditionId;
33 std::vector<Matcher> metricFields;
34 std::vector<Matcher> conditionFields;
35};
36
Yao Chend5aa01b32017-12-19 16:46:36 -080037class HashableDimensionKey {
38public:
Yao Chen8a8d16c2018-02-08 14:50:40 -080039 explicit HashableDimensionKey(const std::vector<FieldValue>& values) {
40 mValues = values;
41 }
Yao Chend5aa01b32017-12-19 16:46:36 -080042
Yangster-mac932ecec2018-02-01 10:23:52 -080043 HashableDimensionKey() {};
Yao Chend5aa01b32017-12-19 16:46:36 -080044
Yao Chen8a8d16c2018-02-08 14:50:40 -080045 HashableDimensionKey(const HashableDimensionKey& that) : mValues(that.getValues()){};
Yao Chend5aa01b32017-12-19 16:46:36 -080046
Yao Chen8a8d16c2018-02-08 14:50:40 -080047 inline void addValue(const FieldValue& value) {
48 mValues.push_back(value);
49 }
50
51 inline const std::vector<FieldValue>& getValues() const {
52 return mValues;
53 }
54
55 inline std::vector<FieldValue>* mutableValues() {
56 return &mValues;
57 }
58
59 inline FieldValue* mutableValue(size_t i) {
60 if (i >= 0 && i < mValues.size()) {
61 return &(mValues[i]);
62 }
63 return nullptr;
64 }
Yao Chend5aa01b32017-12-19 16:46:36 -080065
66 std::string toString() const;
67
Yao Chend5aa01b32017-12-19 16:46:36 -080068 bool operator==(const HashableDimensionKey& that) const;
69
70 bool operator<(const HashableDimensionKey& that) const;
71
Yao Chen8a8d16c2018-02-08 14:50:40 -080072 bool contains(const HashableDimensionKey& that) const;
Yao Chend5aa01b32017-12-19 16:46:36 -080073
74private:
Yao Chen8a8d16c2018-02-08 14:50:40 -080075 std::vector<FieldValue> mValues;
Yao Chend5aa01b32017-12-19 16:46:36 -080076};
77
Yangster-mac93694462018-01-22 20:49:31 -080078class MetricDimensionKey {
79 public:
80 explicit MetricDimensionKey(const HashableDimensionKey& dimensionKeyInWhat,
81 const HashableDimensionKey& dimensionKeyInCondition)
82 : mDimensionKeyInWhat(dimensionKeyInWhat),
83 mDimensionKeyInCondition(dimensionKeyInCondition) {};
84
85 MetricDimensionKey(){};
86
87 MetricDimensionKey(const MetricDimensionKey& that)
88 : mDimensionKeyInWhat(that.getDimensionKeyInWhat()),
89 mDimensionKeyInCondition(that.getDimensionKeyInCondition()) {};
90
91 MetricDimensionKey& operator=(const MetricDimensionKey& from) = default;
92
93 std::string toString() const;
94
95 inline const HashableDimensionKey& getDimensionKeyInWhat() const {
96 return mDimensionKeyInWhat;
97 }
98
99 inline const HashableDimensionKey& getDimensionKeyInCondition() const {
100 return mDimensionKeyInCondition;
101 }
102
Yangster13fb7e42018-03-07 17:30:49 -0800103 inline void setDimensionKeyInCondition(const HashableDimensionKey& key) {
104 mDimensionKeyInCondition = key;
105 }
106
Yangster-mac93694462018-01-22 20:49:31 -0800107 bool hasDimensionKeyInCondition() const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800108 return mDimensionKeyInCondition.getValues().size() > 0;
Yangster-mac93694462018-01-22 20:49:31 -0800109 }
110
111 bool operator==(const MetricDimensionKey& that) const;
112
113 bool operator<(const MetricDimensionKey& that) const;
114
Yangster-mac93694462018-01-22 20:49:31 -0800115 private:
116 HashableDimensionKey mDimensionKeyInWhat;
117 HashableDimensionKey mDimensionKeyInCondition;
118};
119
Yao Chen8a8d16c2018-02-08 14:50:40 -0800120android::hash_t hashDimension(const HashableDimensionKey& key);
Yangster-mac93694462018-01-22 20:49:31 -0800121
Yao Chen8a8d16c2018-02-08 14:50:40 -0800122/**
123 * Creating HashableDimensionKeys from FieldValues using matcher.
124 *
Yangster-mace06cfd72018-03-10 23:22:59 -0800125 * This function may make modifications to the Field if the matcher has Position=FIRST,LAST or ALL
126 * in it. This is because: for example, when we create dimension from last uid in attribution chain,
Yao Chen8a8d16c2018-02-08 14:50:40 -0800127 * In one event, uid 1000 is at position 5 and it's the last
128 * In another event, uid 1000 is at position 6, and it's the last
129 * these 2 events should be mapped to the same dimension. So we will remove the original position
130 * from the dimension key for the uid field (by applying 0x80 bit mask).
131 */
132bool filterValues(const std::vector<Matcher>& matcherFields, const std::vector<FieldValue>& values,
Yangster13fb7e42018-03-07 17:30:49 -0800133 HashableDimensionKey* output);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800134
135/**
136 * Filter the values from FieldValues using the matchers.
137 *
138 * In contrast to the above function, this function will not do any modification to the original
139 * data. Considering it as taking a snapshot on the atom event.
140 */
141void filterGaugeValues(const std::vector<Matcher>& matchers, const std::vector<FieldValue>& values,
142 std::vector<FieldValue>* output);
143
Yangster-mac53928882018-02-25 23:02:56 -0800144void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
145 const Metric2Condition& links,
Yangster13fb7e42018-03-07 17:30:49 -0800146 HashableDimensionKey* conditionDimension);
Yangster-mac20877162017-12-22 17:19:39 -0800147
Yao Chend5aa01b32017-12-19 16:46:36 -0800148} // namespace statsd
149} // namespace os
150} // namespace android
151
152namespace std {
153
154using android::os::statsd::HashableDimensionKey;
Yangster-mac93694462018-01-22 20:49:31 -0800155using android::os::statsd::MetricDimensionKey;
Yao Chend5aa01b32017-12-19 16:46:36 -0800156
157template <>
158struct hash<HashableDimensionKey> {
159 std::size_t operator()(const HashableDimensionKey& key) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800160 return hashDimension(key);
Yao Chend5aa01b32017-12-19 16:46:36 -0800161 }
162};
163
Yangster-mac93694462018-01-22 20:49:31 -0800164template <>
165struct hash<MetricDimensionKey> {
166 std::size_t operator()(const MetricDimensionKey& key) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800167 android::hash_t hash = hashDimension(key.getDimensionKeyInWhat());
168 hash = android::JenkinsHashMix(hash, hashDimension(key.getDimensionKeyInCondition()));
Yangster-mac93694462018-01-22 20:49:31 -0800169 return android::JenkinsHashWhiten(hash);
170 }
171};
172} // namespace std