1 /*
2  * Copyright (C) 2018 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 package android.cts.statsd.metric;
17 
18 import static com.google.common.truth.Truth.assertWithMessage;
19 
20 import com.android.internal.os.StatsdConfigProto;
21 import com.android.internal.os.StatsdConfigProto.AtomMatcher;
22 import com.android.internal.os.StatsdConfigProto.EventActivation;
23 import com.android.internal.os.StatsdConfigProto.FieldValueMatcher;
24 import com.android.internal.os.StatsdConfigProto.SimpleAtomMatcher;
25 import com.android.os.AtomsProto.Atom;
26 import com.android.os.AtomsProto.AppBreadcrumbReported;
27 import com.google.protobuf.Message;
28 import com.google.protobuf.Descriptors.Descriptor;
29 import com.google.protobuf.Descriptors.FieldDescriptor;
30 
31 public class MetricsUtils {
32     public static final long COUNT_METRIC_ID = 3333;
33     public static final long DURATION_METRIC_ID = 4444;
34     public static final long GAUGE_METRIC_ID = 5555;
35     public static final long VALUE_METRIC_ID = 6666;
36 
getAtomMatcher(int atomId)37     public static AtomMatcher.Builder getAtomMatcher(int atomId) {
38         AtomMatcher.Builder builder = AtomMatcher.newBuilder();
39         builder.setSimpleAtomMatcher(SimpleAtomMatcher.newBuilder()
40                         .setAtomId(atomId));
41         return builder;
42     }
43 
startAtomMatcher(int id)44     public static AtomMatcher startAtomMatcher(int id) {
45       return AtomMatcher.newBuilder()
46           .setId(id)
47           .setSimpleAtomMatcher(
48               SimpleAtomMatcher.newBuilder()
49                   .setAtomId(Atom.APP_BREADCRUMB_REPORTED_FIELD_NUMBER)
50                   .addFieldValueMatcher(FieldValueMatcher.newBuilder()
51                                             .setField(AppBreadcrumbReported.STATE_FIELD_NUMBER)
52                                             .setEqInt(AppBreadcrumbReported.State.START.ordinal())))
53           .build();
54     }
55 
startAtomMatcherWithLabel(int id, int label)56     public static AtomMatcher startAtomMatcherWithLabel(int id, int label) {
57         return appBreadcrumbMatcherWithLabelAndState(id, label, AppBreadcrumbReported.State.START);
58     }
59 
stopAtomMatcher(int id)60     public static AtomMatcher stopAtomMatcher(int id) {
61       return AtomMatcher.newBuilder()
62           .setId(id)
63           .setSimpleAtomMatcher(
64               SimpleAtomMatcher.newBuilder()
65                   .setAtomId(Atom.APP_BREADCRUMB_REPORTED_FIELD_NUMBER)
66                   .addFieldValueMatcher(FieldValueMatcher.newBuilder()
67                                             .setField(AppBreadcrumbReported.STATE_FIELD_NUMBER)
68                                             .setEqInt(AppBreadcrumbReported.State.STOP.ordinal())))
69           .build();
70     }
71 
stopAtomMatcherWithLabel(int id, int label)72     public static AtomMatcher stopAtomMatcherWithLabel(int id, int label) {
73         return appBreadcrumbMatcherWithLabelAndState(id, label, AppBreadcrumbReported.State.STOP);
74     }
75 
unspecifiedAtomMatcher(int id)76     public static AtomMatcher unspecifiedAtomMatcher(int id) {
77         return AtomMatcher.newBuilder()
78                 .setId(id)
79                 .setSimpleAtomMatcher(SimpleAtomMatcher.newBuilder()
80                         .setAtomId(Atom.APP_BREADCRUMB_REPORTED_FIELD_NUMBER)
81                         .addFieldValueMatcher(FieldValueMatcher.newBuilder()
82                                 .setField(AppBreadcrumbReported.STATE_FIELD_NUMBER)
83                                 .setEqInt(AppBreadcrumbReported.State.UNSPECIFIED.ordinal())))
84                 .build();
85     }
86 
simpleAtomMatcher(int id)87     public static AtomMatcher simpleAtomMatcher(int id) {
88       return AtomMatcher.newBuilder()
89           .setId(id)
90           .setSimpleAtomMatcher(
91               SimpleAtomMatcher.newBuilder().setAtomId(Atom.APP_BREADCRUMB_REPORTED_FIELD_NUMBER))
92           .build();
93     }
94 
appBreadcrumbMatcherWithLabel(int id, int label)95     public static AtomMatcher appBreadcrumbMatcherWithLabel(int id, int label) {
96         return AtomMatcher.newBuilder()
97                 .setId(id)
98                 .setSimpleAtomMatcher(SimpleAtomMatcher.newBuilder()
99                         .setAtomId(Atom.APP_BREADCRUMB_REPORTED_FIELD_NUMBER)
100                         .addFieldValueMatcher(FieldValueMatcher.newBuilder()
101                                 .setField(AppBreadcrumbReported.LABEL_FIELD_NUMBER)
102                                 .setEqInt(label)))
103                 .build();
104     }
105 
appBreadcrumbMatcherWithLabelAndState(int id, int label, final AppBreadcrumbReported.State state)106     public static AtomMatcher appBreadcrumbMatcherWithLabelAndState(int id, int label,
107             final AppBreadcrumbReported.State state) {
108 
109         return AtomMatcher.newBuilder()
110                 .setId(id)
111                 .setSimpleAtomMatcher(SimpleAtomMatcher.newBuilder()
112                         .setAtomId(Atom.APP_BREADCRUMB_REPORTED_FIELD_NUMBER)
113                         .addFieldValueMatcher(FieldValueMatcher.newBuilder()
114                                 .setField(AppBreadcrumbReported.STATE_FIELD_NUMBER)
115                                 .setEqInt(state.ordinal()))
116                         .addFieldValueMatcher(FieldValueMatcher.newBuilder()
117                                 .setField(AppBreadcrumbReported.LABEL_FIELD_NUMBER)
118                                 .setEqInt(label)))
119                 .build();
120     }
121 
simpleAtomMatcher(int id, int label)122     public static AtomMatcher simpleAtomMatcher(int id, int label) {
123       return AtomMatcher.newBuilder()
124           .setId(id)
125           .setSimpleAtomMatcher(SimpleAtomMatcher.newBuilder()
126                   .setAtomId(Atom.APP_BREADCRUMB_REPORTED_FIELD_NUMBER)
127                   .addFieldValueMatcher(FieldValueMatcher.newBuilder()
128                             .setField(AppBreadcrumbReported.LABEL_FIELD_NUMBER)
129                             .setEqInt(label)
130                   )
131           )
132           .build();
133     }
134 
createEventActivation(int ttlSecs, int matcherId, int cancelMatcherId)135     public static EventActivation.Builder createEventActivation(int ttlSecs, int matcherId,
136             int cancelMatcherId) {
137         return EventActivation.newBuilder()
138                 .setAtomMatcherId(matcherId)
139                 .setTtlSeconds(ttlSecs)
140                 .setDeactivationAtomMatcherId(cancelMatcherId);
141     }
142 
StringToId(String str)143     public static long StringToId(String str) {
144       return str.hashCode();
145     }
146 
assertBucketTimePresent(Message bucketInfo)147     public static void assertBucketTimePresent(Message bucketInfo) {
148         Descriptor descriptor = bucketInfo.getDescriptorForType();
149         boolean found = false;
150         FieldDescriptor bucketNum = descriptor.findFieldByName("bucket_num");
151         FieldDescriptor startMillis = descriptor.findFieldByName("start_bucket_elapsed_millis");
152         FieldDescriptor endMillis = descriptor.findFieldByName("end_bucket_elapsed_millis");
153         if (bucketNum != null && bucketInfo.hasField(bucketNum)) {
154             found = true;
155         } else if (startMillis != null && bucketInfo.hasField(startMillis) &&
156                    endMillis != null && bucketInfo.hasField(endMillis)) {
157             found = true;
158         }
159         assertWithMessage(
160                 "Bucket info did not have either bucket num or start and end elapsed millis"
161         ).that(found).isTrue();
162     }
163 }
164