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 com.android.tradefed.util.proto;
17 
18 import com.android.tradefed.log.LogUtil.CLog;
19 import com.android.tradefed.metrics.proto.MetricMeasurement.DataType;
20 import com.android.tradefed.metrics.proto.MetricMeasurement.Directionality;
21 import com.android.tradefed.metrics.proto.MetricMeasurement.Measurements;
22 import com.android.tradefed.metrics.proto.MetricMeasurement.Measurements.MeasurementCase;
23 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
24 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric.Builder;
25 
26 import java.util.HashMap;
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29 
30 /** Utility class to help with the Map<String, String> to Map<String, Metric> transition. */
31 public class TfMetricProtoUtil {
32 
33     /**
34      * Conversion of Map<String, Metric> to Map<String, String>. All the single value string
35      * representation are used, list representation are not converted and will be lost.
36      */
compatibleConvert(Map<String, Metric> map)37     public static Map<String, String> compatibleConvert(Map<String, Metric> map) {
38         Map<String, String> oldFormat = new LinkedHashMap<>();
39         for (String key : map.keySet()) {
40             Measurements measures = map.get(key).getMeasurements();
41             MeasurementCase set = measures.getMeasurementCase();
42             String value = "";
43             switch (set) {
44                 case SINGLE_DOUBLE:
45                     value = Double.toString(measures.getSingleDouble());
46                     break;
47                 case SINGLE_INT:
48                     value = Long.toString(measures.getSingleInt());
49                     break;
50                 case SINGLE_STRING:
51                     value = measures.getSingleString();
52                     break;
53                 case MEASUREMENT_NOT_SET:
54                     CLog.d("No measurements was set for key '%s'", key);
55                     continue;
56                 default:
57                     CLog.d(
58                             "Could not convert complex '%s' type to String. Use the new metric "
59                                     + "interface.",
60                             set);
61                     continue;
62             }
63             oldFormat.put(key, value);
64         }
65         return oldFormat;
66     }
67 
68     /**
69      * Conversion from Map<String, String> to HashMap<String, Metric>. In order to go to the new
70      * interface. Information might only be partially populated because of the old format
71      * limitations.
72      */
upgradeConvert(Map<String, String> metrics)73     public static HashMap<String, Metric> upgradeConvert(Map<String, String> metrics) {
74         HashMap<String, Metric> newFormat = new LinkedHashMap<>();
75         for (String key : metrics.keySet()) {
76             newFormat.put(key, stringToMetric(metrics.get(key)));
77         }
78         return newFormat;
79     }
80 
81     /**
82      * Convert a simple String metric (old format) to a {@link Metric} (new format).
83      *
84      * @param metric The string containing a metric.
85      * @return The created {@link Metric}
86      */
stringToMetric(String metric)87     public static Metric stringToMetric(String metric) {
88         Measurements measures = Measurements.newBuilder().setSingleString(metric).build();
89         Metric m =
90                 Metric.newBuilder()
91                         .setMeasurements(measures)
92                         .setDirection(Directionality.DIRECTIONALITY_UNSPECIFIED)
93                         .setType(DataType.RAW)
94                         .build();
95         return m;
96     }
97 
98     /**
99      * Create a {@link Metric} for a single long/int value, and optionally provide a unit.
100      *
101      * @param value The value that will be stored.
102      * @param unit the unit of the value, or null if no unit.
103      * @return a {@link Metric} populated with the informations.
104      */
createSingleValue(long value, String unit)105     public static Metric createSingleValue(long value, String unit) {
106         Measurements measure = Measurements.newBuilder().setSingleInt(value).build();
107         Builder metricBuilder = Metric.newBuilder().setType(DataType.RAW).setMeasurements(measure);
108         if (unit != null) {
109             metricBuilder.setUnit(unit);
110         }
111         return metricBuilder.build();
112     }
113 }
114