1 /*
2  * Copyright (C) 2024 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 package com.android.adservices.service.stats;
18 
19 import android.annotation.IntDef;
20 
21 import java.lang.annotation.Retention;
22 import java.lang.annotation.RetentionPolicy;
23 
24 /**
25  * Utility class containing status enum types and functions used by various stats objects.
26  *
27  * <p>Those status codes are internal only.
28  *
29  * @hide
30  */
31 public class AdsRelevanceStatusUtils {
32     /** The Ad Filtering is UNSET process. */
33     public static final int FILTER_PROCESS_TYPE_UNSET = 0;
34     /** The Ad Filtering is used in Custom Audiences process. */
35     public static final int FILTER_PROCESS_TYPE_CUSTOM_AUDIENCES = 1;
36     /** The Ad Filtering is used in Contextual Ads process. */
37     public static final int FILTER_PROCESS_TYPE_CONTEXTUAL_ADS = 2;
38 
39     /** The beacon comes from UNSET winner. */
40     public static final int BEACON_SOURCE_UNSET = 0;
41     /** The beacon comes from PROTECTED_SIGNALS winner. */
42     public static final int BEACON_SOURCE_PROTECTED_SIGNALS = 1;
43     /** The beacon comes from CUSTOM_AUDIENCE winner. */
44     public static final int BEACON_SOURCE_CUSTOM_AUDIENCE = 2;
45 
46     /** The status of Json processing is UNSET. */
47     public static final int JSON_PROCESSING_STATUS_UNSET = 0;
48     /** The status of Json processing is SUCCESS. */
49     public static final int JSON_PROCESSING_STATUS_SUCCESS = 1;
50     /** The status of Json processing is TOO_BIG. */
51     public static final int JSON_PROCESSING_STATUS_TOO_BIG = 2;
52     /** The status of Json processing is SYNTACTIC_ERROR. */
53     public static final int JSON_PROCESSING_STATUS_SYNTACTIC_ERROR = 3;
54     /** The status of Json processing is SEMANTIC_ERROR. */
55     public static final int JSON_PROCESSING_STATUS_SEMANTIC_ERROR = 4;
56     /** The status of Json processing is OTHER_ERROR. */
57     public static final int JSON_PROCESSING_STATUS_OTHER_ERROR = 5;
58 
59     /** The status of encoding fetch is UNSET. */
60     public static final int ENCODING_FETCH_STATUS_UNSET = 0;
61     /** The status of encoding fetch is SUCCESS. */
62     public static final int ENCODING_FETCH_STATUS_SUCCESS = 1;
63     /** The status of encoding fetch is TOO_BIG. */
64     public static final int ENCODING_FETCH_STATUS_TOO_BIG = 2;
65     /** The status of encoding fetch is TIMEOUT. */
66     public static final int ENCODING_FETCH_STATUS_TIMEOUT = 3;
67     /** The status of encoding fetch is NETWORK_FAILURE. */
68     public static final int ENCODING_FETCH_STATUS_NETWORK_FAILURE = 4;
69     /** The status of encoding fetch is OTHER_FAILURE. */
70     public static final int ENCODING_FETCH_STATUS_OTHER_FAILURE = 5;
71 
72     /** The status of JavaScript run is UNSET. */
73     public static final int JS_RUN_STATUS_UNSET = 0;
74     /** The status of JavaScript run is SUCCESS. */
75     public static final int JS_RUN_STATUS_SUCCESS = 1;
76     /** The status of JavaScript run is TIMEOUT. */
77     public static final int JS_RUN_STATUS_TIMEOUT = 2;
78     /** The status of JavaScript run is JS_SYNTAX_ERROR. */
79     public static final int JS_RUN_STATUS_JS_SYNTAX_ERROR = 3;
80     /** The status of JavaScript run is OUTPUT_SYNTAX_ERROR. */
81     public static final int JS_RUN_STATUS_OUTPUT_SYNTAX_ERROR = 4;
82     /** The status of JavaScript run is OUTPUT_SEMANTIC_ERROR. */
83     public static final int JS_RUN_STATUS_OUTPUT_SEMANTIC_ERROR = 5;
84     /** The status of JavaScript run is OUTPUT_NON_ZERO_RESULT. */
85     public static final int JS_RUN_STATUS_OUTPUT_NON_ZERO_RESULT = 6;
86     /** The status of JavaScript run is DB_PERSIST_FAILURE. */
87     public static final int JS_RUN_STATUS_DB_PERSIST_FAILURE = 7;
88     /** The status of JavaScript run is OTHER_FAILURE. */
89     public static final int JS_RUN_STATUS_OTHER_FAILURE = 8;
90 
91     /** The status of JavaScript run is JS_REFERENCE_ERROR. */
92     public static final int JS_RUN_STATUS_JS_REFERENCE_ERROR = 9;
93 
94     /** The auction winner type is UNSET. */
95     public static final int WINNER_TYPE_UNSET = 0;
96     /** The auction winner type is NO_WINNER. */
97     public static final int WINNER_TYPE_NO_WINNER = 1;
98     /** The auction winner type is CA_WINNER. */
99     public static final int WINNER_TYPE_CA_WINNER = 2;
100     /** The auction winner type is PAS_WINNER. */
101     public static final int WINNER_TYPE_PAS_WINNER = 3;
102 
103     /** Leaving the following 6 size type.
104      * These not tied to specific numbers gives us flexibility to change them later. */
105     public static final int SIZE_UNSET = 0;
106     public static final int SIZE_VERY_SMALL = 1;
107     public static final int SIZE_SMALL = 2;
108     public static final int SIZE_MEDIUM = 3;
109     public static final int SIZE_LARGE = 4;
110     public static final int SIZE_VERY_LARGE = 5;
111 
112     /** The auction coordinator source is UNSET. */
113     public static final int SERVER_AUCTION_COORDINATOR_SOURCE_UNSET = 0;
114     /** The auction coordinator source is DEFAULT. */
115     public static final int SERVER_AUCTION_COORDINATOR_SOURCE_DEFAULT = 1;
116     /** The auction coordinator source is API. */
117     public static final int SERVER_AUCTION_COORDINATOR_SOURCE_API = 2;
118 
119     // We expect most JSONs to be small, at least initially, so we'll bucket more there.
120     public static final long[] JSON_SIZE_BUCKETS = {100, 300, 1000, 5000};
121 
122     // Buckets for JS download latency in ms
123     public static final long[] JS_DOWNLOAD_LATENCY_BUCKETS = {50, 200, 1000, 2000};
124 
125     // Buckets for JS execution latency in ms
126     public static final long[] JS_EXECUTION_LATENCY_BUCKETS = {50, 200, 1000, 2000};
127 
128     /** The key fetch status is UNSET. */
129     public static final int BACKGROUND_KEY_FETCH_STATUS_UNSET = 0;
130     /** The key fetch status is NO_OP. */
131     public static final int BACKGROUND_KEY_FETCH_STATUS_NO_OP = 1;
132     /** The key fetch status is REFRESH_KEYS_INITIATED. */
133     public static final int BACKGROUND_KEY_FETCH_STATUS_REFRESH_KEYS_INITIATED = 2;
134 
135     /** The server auction key fetch source is UNSET. */
136     public static final int SERVER_AUCTION_KEY_FETCH_SOURCE_UNSET = 0;
137     /** The server auction key fetch source is via a background fetch. */
138     public static final int SERVER_AUCTION_KEY_FETCH_SOURCE_BACKGROUND_FETCH = 1;
139     /** The server auction key fetch source is via an auction. */
140     public static final int SERVER_AUCTION_KEY_FETCH_SOURCE_AUCTION = 2;
141 
142     /** The server auction encryption key source is UNSET. */
143     public static final int SERVER_AUCTION_ENCRYPTION_KEY_SOURCE_UNSET = 0;
144     /** The server auction encryption key source is the database. */
145     public static final int SERVER_AUCTION_ENCRYPTION_KEY_SOURCE_DATABASE = 1;
146     /** The server auction encryption key source is the network. */
147     public static final int SERVER_AUCTION_ENCRYPTION_KEY_SOURCE_NETWORK = 2;
148 
149     /** The kind of winner did the beacon come from. */
150     @IntDef(
151             prefix = {"BEACON_SOURCE_"},
152             value = {
153                     BEACON_SOURCE_UNSET,
154                     BEACON_SOURCE_PROTECTED_SIGNALS,
155                     BEACON_SOURCE_CUSTOM_AUDIENCE
156             })
157     @Retention(RetentionPolicy.SOURCE)
158     public @interface BeaconSource {}
159 
160     /** The status of JSON processing. */
161     @IntDef(
162             prefix = {"JSON_PROCESSING_STATUS_"},
163             value = {
164                 JSON_PROCESSING_STATUS_UNSET,
165                 JSON_PROCESSING_STATUS_SUCCESS,
166                 JSON_PROCESSING_STATUS_TOO_BIG,
167                 JSON_PROCESSING_STATUS_SYNTACTIC_ERROR,
168                 JSON_PROCESSING_STATUS_SEMANTIC_ERROR,
169                 JSON_PROCESSING_STATUS_OTHER_ERROR
170             })
171     @Retention(RetentionPolicy.SOURCE)
172     public @interface JsonProcessingStatus {}
173 
174     /** The status of encoding fetch. */
175     @IntDef(
176             prefix = {"ENCODING_FETCH_STATUS_"},
177             value = {
178                 ENCODING_FETCH_STATUS_UNSET,
179                 ENCODING_FETCH_STATUS_SUCCESS,
180                 ENCODING_FETCH_STATUS_TOO_BIG,
181                 ENCODING_FETCH_STATUS_TIMEOUT,
182                 ENCODING_FETCH_STATUS_NETWORK_FAILURE,
183                 ENCODING_FETCH_STATUS_OTHER_FAILURE
184             })
185     @Retention(RetentionPolicy.SOURCE)
186     public @interface EncodingFetchStatus {}
187 
188     /** The status of JavaScript Run. */
189     @IntDef(
190             prefix = {"JS_RUN_STATUS_"},
191             value = {
192                 JS_RUN_STATUS_UNSET,
193                 JS_RUN_STATUS_SUCCESS,
194                 JS_RUN_STATUS_TIMEOUT,
195                 JS_RUN_STATUS_JS_SYNTAX_ERROR,
196                 JS_RUN_STATUS_OUTPUT_SYNTAX_ERROR,
197                 JS_RUN_STATUS_OUTPUT_SEMANTIC_ERROR,
198                 JS_RUN_STATUS_OUTPUT_NON_ZERO_RESULT,
199                 JS_RUN_STATUS_DB_PERSIST_FAILURE,
200                 JS_RUN_STATUS_OTHER_FAILURE,
201                 JS_RUN_STATUS_JS_REFERENCE_ERROR
202             })
203     @Retention(RetentionPolicy.SOURCE)
204     public @interface JsRunStatus {}
205 
206     /** The type of auction winner. */
207     @IntDef(
208             prefix = {"WINNER_TYPE_"},
209             value = {
210                     WINNER_TYPE_UNSET,
211                     WINNER_TYPE_NO_WINNER,
212                     WINNER_TYPE_CA_WINNER,
213                     WINNER_TYPE_PAS_WINNER
214             })
215     @Retention(RetentionPolicy.SOURCE)
216     public @interface WinnerType {}
217 
218     /** The size type of JSON or JavaScript. */
219     @IntDef(
220             prefix = {"SIZE_"},
221             value = {
222                 SIZE_UNSET,
223                 SIZE_VERY_SMALL,
224                 SIZE_SMALL,
225                 SIZE_MEDIUM,
226                 SIZE_LARGE,
227                 SIZE_VERY_LARGE
228             })
229     @Retention(RetentionPolicy.SOURCE)
230     public @interface Size {}
231 
232     /** Filter processing type */
233     @IntDef(
234             prefix = {"FILTER_PROCESS_TYPE_"},
235             value = {
236                 FILTER_PROCESS_TYPE_UNSET,
237                 FILTER_PROCESS_TYPE_CUSTOM_AUDIENCES,
238                 FILTER_PROCESS_TYPE_CONTEXTUAL_ADS
239             })
240     @Retention(RetentionPolicy.SOURCE)
241     public @interface FilterProcessType {}
242 
243     /** The type of server auction coordinator source. */
244     @IntDef(
245             prefix = {"SERVER_AUCTION_COORDINATOR_SOURCE_"},
246             value = {
247                 SERVER_AUCTION_COORDINATOR_SOURCE_UNSET,
248                 SERVER_AUCTION_COORDINATOR_SOURCE_DEFAULT,
249                 SERVER_AUCTION_COORDINATOR_SOURCE_API
250             })
251     @Retention(RetentionPolicy.SOURCE)
252     public @interface ServerAuctionCoordinatorSource {}
253 
254     /** Returns the size bucket for a raw value. */
255     @Size
computeSize(long rawSize, long[] buckets)256     public static int computeSize(long rawSize, long[] buckets) {
257         if (rawSize < buckets[0]) {
258             return SIZE_VERY_SMALL;
259         } else if (rawSize < buckets[1]) {
260             return SIZE_SMALL;
261         } else if (rawSize < buckets[2]) {
262             return SIZE_MEDIUM;
263         } else if (rawSize < buckets[3]) {
264             return SIZE_LARGE;
265         }
266         return SIZE_VERY_LARGE;
267     }
268 
269     /** The status of the background key fetch. */
270     @IntDef(
271             prefix = {"BACKGROUND_KEY_FETCH_STATUS_"},
272             value = {
273                 BACKGROUND_KEY_FETCH_STATUS_UNSET,
274                 BACKGROUND_KEY_FETCH_STATUS_NO_OP,
275                 BACKGROUND_KEY_FETCH_STATUS_REFRESH_KEYS_INITIATED,
276             })
277     @Retention(RetentionPolicy.SOURCE)
278     public @interface BackgroundKeyFetchStatus {}
279 
280     /** The source of the server auction key fetch. */
281     @IntDef(
282             prefix = {"SERVER_AUCTION_KEY_FETCH_SOURCE_"},
283             value = {
284                 SERVER_AUCTION_KEY_FETCH_SOURCE_UNSET,
285                 SERVER_AUCTION_KEY_FETCH_SOURCE_BACKGROUND_FETCH,
286                 SERVER_AUCTION_KEY_FETCH_SOURCE_AUCTION
287             })
288     @Retention(RetentionPolicy.SOURCE)
289     public @interface ServerAuctionKeyFetchSource {}
290 
291     /** The source of the server auction encryption key. */
292     @IntDef(
293             prefix = {"SERVER_AUCTION_ENCRYPTION_KEY_SOURCE_"},
294             value = {
295                 SERVER_AUCTION_ENCRYPTION_KEY_SOURCE_UNSET,
296                 SERVER_AUCTION_ENCRYPTION_KEY_SOURCE_DATABASE,
297                 SERVER_AUCTION_ENCRYPTION_KEY_SOURCE_NETWORK
298             })
299     @Retention(RetentionPolicy.SOURCE)
300     public @interface ServerAuctionEncryptionKeySource {}
301 }
302