1 /*
2  * Copyright (C) 2015 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.compatibility.common.util;
18 
19 import com.android.tradefed.build.IBuildInfo;
20 import com.android.tradefed.util.FileUtil;
21 
22 import java.io.File;
23 import java.util.List;
24 
25 /**
26  * A {@link ReportLog} that can be used with the in memory metrics store used for host side metrics.
27  *
28  * @deprecated Do not use, those metrics are not reported anywhere in final report.
29  */
30 @Deprecated
31 public final class MetricsReportLog extends ReportLog {
32     private final String mAbi;
33     private final String mClassMethodName;
34     private final IBuildInfo mBuildInfo;
35 
36     // Temporary folder must match the temp-dir value configured in ReportLogCollector target
37     // preparer in cts/harness/tools/cts-tradefed/res/config/cts-oreconditions.xml
38     private static final String TEMPORARY_REPORT_FOLDER = "temp-report-logs";
39     private ReportLogHostInfoStore store;
40 
41     /**
42      * @param buildInfo the test build info.
43      * @param abi abi the test was run on.
44      * @param classMethodName class name and method name of the test in class#method format.
45      *        Note that ReportLog.getClassMethodNames() provide this.
46      * @param reportLogName the name of the report log file. Metrics will be written out to this.
47      * @param streamName the key for the JSON object of the set of metrics to be logged.
48      */
MetricsReportLog(IBuildInfo buildInfo, String abi, String classMethodName, String reportLogName, String streamName)49     public MetricsReportLog(IBuildInfo buildInfo, String abi, String classMethodName,
50             String reportLogName, String streamName) {
51         this(buildInfo, abi, classMethodName, reportLogName, streamName, false);
52     }
53 
54     /**
55      * @param buildInfo the test build info.
56      * @param abi abi the test was run on.
57      * @param classMethodName class name and method name of the test in class#method format.
58      *        Note that ReportLog.getClassMethodNames() provide this.
59      * @param reportLogName the name of the report log file. Metrics will be written out to this.
60      * @param streamName the key for the JSON object of the set of metrics to be logged.
61      * @param deviceDir whether to create unique directory for each device
62      */
MetricsReportLog(IBuildInfo buildInfo, String abi, String classMethodName, String reportLogName, String streamName, boolean deviceDir)63     public MetricsReportLog(IBuildInfo buildInfo, String abi, String classMethodName,
64             String reportLogName, String streamName, boolean deviceDir) {
65         super(reportLogName, streamName);
66         mBuildInfo = buildInfo;
67         mAbi = abi;
68         mClassMethodName = classMethodName;
69         try {
70             String tmpDirName = TEMPORARY_REPORT_FOLDER;
71             if (deviceDir) {
72                 tmpDirName += "-" + buildInfo.getDeviceSerial();
73             }
74             final File dir = FileUtil.createNamedTempDir(tmpDirName);
75             File jsonFile = new File(dir, mReportLogName + ".reportlog.json");
76             store = new ReportLogHostInfoStore(jsonFile, mStreamName);
77             store.open();
78         } catch (Exception e) {
79             e.printStackTrace();
80         }
81     }
82 
83     /**
84      * Adds a double metric to the report.
85      */
86     @Override
addValue(String source, String message, double value, ResultType type, ResultUnit unit)87     public void addValue(String source, String message, double value, ResultType type,
88             ResultUnit unit) {
89         super.addValue(source, message, value, type, unit);
90         try {
91             store.addResult(message, value);
92         } catch (Exception e) {
93             e.printStackTrace();
94         }
95     }
96 
97     /**
98      * Adds a double metric to the report.
99      */
100     @Override
addValue(String message, double value, ResultType type, ResultUnit unit)101     public void addValue(String message, double value, ResultType type, ResultUnit unit) {
102         super.addValue(message, value, type, unit);
103         try {
104             store.addResult(message, value);
105         } catch (Exception e) {
106             e.printStackTrace();
107         }
108 
109     }
110 
111     /**
112      * Adds a double array of metrics to the report.
113      */
114     @Override
addValues(String source, String message, double[] values, ResultType type, ResultUnit unit)115     public void addValues(String source, String message, double[] values, ResultType type,
116                           ResultUnit unit) {
117         super.addValues(source, message, values, type, unit);
118         try {
119             store.addArrayResult(message, values);
120         } catch (Exception e) {
121             e.printStackTrace();
122         }
123     }
124 
125     /**
126      * Adds a double array of metrics to the report.
127      */
128     @Override
addValues(String message, double[] values, ResultType type, ResultUnit unit)129     public void addValues(String message, double[] values, ResultType type, ResultUnit unit) {
130         super.addValues(message, values, type, unit);
131         try {
132             store.addArrayResult(message, values);
133         } catch (Exception e) {
134             e.printStackTrace();
135         }
136     }
137 
138     /**
139      * Adds an int metric to the report.
140      */
141     @Override
addValue(String message, int value, ResultType type, ResultUnit unit)142     public void addValue(String message, int value, ResultType type, ResultUnit unit) {
143         try {
144             store.addResult(message, value);
145         } catch (Exception e) {
146             e.printStackTrace();
147         }
148     }
149 
150     /**
151      * Adds a long metric to the report.
152      */
153     @Override
addValue(String message, long value, ResultType type, ResultUnit unit)154     public void addValue(String message, long value, ResultType type, ResultUnit unit) {
155         try {
156             store.addResult(message, value);
157         } catch (Exception e) {
158             e.printStackTrace();
159         }
160     }
161 
162     /**
163      * Adds a float metric to the report.
164      */
165     @Override
addValue(String message, float value, ResultType type, ResultUnit unit)166     public void addValue(String message, float value, ResultType type, ResultUnit unit) {
167         try {
168             store.addResult(message, value);
169         } catch (Exception e) {
170             e.printStackTrace();
171         }
172     }
173 
174     /**
175      * Adds a boolean metric to the report.
176      */
177     @Override
addValue(String message, boolean value, ResultType type, ResultUnit unit)178     public void addValue(String message, boolean value, ResultType type, ResultUnit unit) {
179         try {
180             store.addResult(message, value);
181         } catch (Exception e) {
182             e.printStackTrace();
183         }
184     }
185 
186     /**
187      * Adds a String metric to the report.
188      */
189     @Override
addValue(String message, String value, ResultType type, ResultUnit unit)190     public void addValue(String message, String value, ResultType type, ResultUnit unit) {
191         try {
192             store.addResult(message, value);
193         } catch (Exception e) {
194             e.printStackTrace();
195         }
196     }
197 
198     /**
199      * Adds an int array of metrics to the report.
200      */
201     @Override
addValues(String message, int[] values, ResultType type, ResultUnit unit)202     public void addValues(String message, int[] values, ResultType type, ResultUnit unit) {
203         try {
204             store.addArrayResult(message, values);
205         } catch (Exception e) {
206             e.printStackTrace();
207         }
208     }
209 
210     /**
211      * Adds a long array of metrics to the report.
212      */
213     @Override
addValues(String message, long[] values, ResultType type, ResultUnit unit)214     public void addValues(String message, long[] values, ResultType type, ResultUnit unit) {
215         try {
216             store.addArrayResult(message, values);
217         } catch (Exception e) {
218             e.printStackTrace();
219         }
220     }
221 
222     /**
223      * Adds a float array of metrics to the report.
224      */
225     @Override
addValues(String message, float[] values, ResultType type, ResultUnit unit)226     public void addValues(String message, float[] values, ResultType type, ResultUnit unit) {
227         try {
228             store.addArrayResult(message, values);
229         } catch (Exception e) {
230             e.printStackTrace();
231         }
232     }
233 
234     /**
235      * Adds a boolean array of metrics to the report.
236      */
237     @Override
addValues(String message, boolean[] values, ResultType type, ResultUnit unit)238     public void addValues(String message, boolean[] values, ResultType type, ResultUnit unit) {
239         try {
240             store.addArrayResult(message, values);
241         } catch (Exception e) {
242             e.printStackTrace();
243         }
244     }
245 
246     /**
247      * Adds a String List of metrics to the report.
248      */
249     @Override
addValues(String message, List<String> values, ResultType type, ResultUnit unit)250     public void addValues(String message, List<String> values, ResultType type, ResultUnit unit) {
251         try {
252             store.addListResult(message, values);
253         } catch (Exception e) {
254             e.printStackTrace();
255         }
256     }
257 
258     /**
259      * Sets the summary double metric of the report.
260      *
261      * NOTE: messages over {@value Metric#MAX_MESSAGE_LENGTH} chars will be trimmed.
262      */
263     @Override
setSummary(String message, double value, ResultType type, ResultUnit unit)264     public void setSummary(String message, double value, ResultType type, ResultUnit unit) {
265         super.setSummary(message, value, type, unit);
266         try {
267             store.addResult(message, value);
268         } catch (Exception e) {
269             e.printStackTrace();
270         }
271     }
272 
273     /**
274      * Closes report file and submits report.
275      */
submit()276     public void submit() {
277         try {
278             store.close();
279             MetricsStore.storeResult(mBuildInfo, mAbi, mClassMethodName, this);
280         } catch (Exception e) {
281             e.printStackTrace();
282         }
283     }
284 }
285