blob: 5568c915773876665d9a5002560120944a57c2d2 [file] [log] [blame]
Andy Hung0f7ad8c2020-01-03 13:24:34 -08001/*
2 * Copyright (C) 2020 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 <media/MediaMetricsItem.h>
20#include <mutex>
21
22namespace android::mediametrics {
23
24/**
25 * AnalyticsActions consists of a map of pairs <trigger, action> which
26 * are evaluated for a given incoming MediaMetrics item.
27 *
28 * A vector of Actions are returned from getActionsForItem() which
29 * should be executed outside of any locks.
30 *
31 * Mediametrics assumes weak consistency, which is fine as the analytics database
32 * is generally strictly increasing in size (until gc removes values that are
33 * supposedly no longer needed).
34 */
35
36class AnalyticsActions {
37public:
38
39 using Elem = mediametrics::Item::Prop::Elem;
40 /**
41 * Trigger: a pair consisting of
42 * std::string: A wildcard url specifying a property in the item,
43 * where '*' indicates 0 or more arbitrary characters
44 * for the item key match.
45 * Elem: A value that needs to match exactly.
46 *
47 * Trigger is used in a map sort; default less with std::string as primary key.
48 * The wildcard accepts a string with '*' as being 0 or more arbitrary
49 * characters for the item key match. A wildcard is preferred over general
50 * regexp for simple fast lookup.
51 *
52 * TODO: incorporate a regexp option.
53 */
54 using Trigger = std::pair<std::string, Elem>;
55
56 /**
57 * Function: The function to be executed.
58 */
59 using Function = std::function<
60 void(const std::shared_ptr<const mediametrics::Item>& item)>;
61
62 /**
63 * Action: An action to execute. This is a shared pointer to Function.
64 */
65 using Action = std::shared_ptr<Function>;
66
67 /**
68 * Adds a new action.
69 *
70 * \param url references a property in the item with wildcards
71 * \param value references a value (cast to Elem automatically)
72 * so be careful of the type. It must be one of
73 * the types acceptable to Elem.
74 * \param action is a function or lambda to execute if the url matches value
75 * in the item.
76 */
77 template <typename T, typename U, typename A>
78 void addAction(T&& url, U&& value, A&& action) {
79 std::lock_guard l(mLock);
80 mFilters[ { std::forward<T>(url), std::forward<U>(value) } ]
81 = std::forward<A>(action);
82 }
83
84 // TODO: remove an action.
85
86 /**
87 * Get all the actions triggered for a particular item.
88 *
89 * \param item to be analyzed for actions.
90 */
91 std::vector<Action>
92 getActionsForItem(const std::shared_ptr<const mediametrics::Item>& item) {
93 std::vector<Action> actions;
94 std::lock_guard l(mLock);
95
96 // Essentially the code looks like this:
97 /*
98 for (auto &[trigger, action] : mFilters) {
99 if (isMatch(trigger, item)) {
100 actions.push_back(action);
101 }
102 }
103 */
104
105 // Optimization: there should only be one match for a non-wildcard url.
106 auto it = mFilters.upper_bound( {item->getKey(), std::monostate{} });
107 if (it != mFilters.end()) {
108 const auto &[trigger, action] = *it;
109 if (isMatch(trigger, item)) {
110 actions.push_back(action);
111 }
112 }
113
114 // Optimization: for wildcard URLs we go backwards until there is no
115 // match with the prefix before the wildcard.
116 while (it != mFilters.begin()) { // this walks backwards, cannot start at begin.
117 const auto &[trigger, action] = *--it; // look backwards
118 int ret = isWildcardMatch(trigger, item);
119 if (ret == mediametrics::Item::RECURSIVE_WILDCARD_CHECK_MATCH_FOUND) {
120 actions.push_back(action); // match found.
121 } else if (ret == mediametrics::Item::RECURSIVE_WILDCARD_CHECK_NO_MATCH_NO_WILDCARD) {
122 break; // no match before wildcard.
123 }
124 // a wildcard was encountered when matching prefix, so we should check again.
125 }
126 return actions;
127 }
128
129private:
130
131 static inline bool isMatch(const Trigger& trigger,
132 const std::shared_ptr<const mediametrics::Item>& item) {
133 const auto& [key, elem] = trigger;
134 if (!startsWith(key, item->getKey())) return false;
135 // The trigger key is in format (item key).propName, so + 1 skips '.' delimeter.
136 const char *propName = key.c_str() + item->getKey().size() + 1;
137 return item->hasPropElem(propName, elem);
138 }
139
140 static inline int isWildcardMatch(const Trigger& trigger,
141 const std::shared_ptr<const mediametrics::Item>& item) {
142 const auto& [key, elem] = trigger;
143 return item->recursiveWildcardCheckElem(key.c_str(), elem);
144 }
145
146 mutable std::mutex mLock;
147 std::map<Trigger, Action> mFilters; // GUARDED_BY mLock
148};
149
150} // namespace android::mediametrics