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