summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/entity/ProfilingPointRunEntity.java
blob: 124b61d4eb1a6ffcd90a54adbf2dd2f510ea0ebb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Copyright (c) 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.vts.entity;

import com.android.vts.proto.VtsReportMessage.ProfilingReportMessage;
import com.android.vts.proto.VtsReportMessage.VtsProfilingRegressionMode;
import com.android.vts.proto.VtsReportMessage.VtsProfilingType;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Ignore;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Parent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;

import static com.googlecode.objectify.ObjectifyService.ofy;

@com.googlecode.objectify.annotation.Entity(name = "ProfilingPointRun")
@Cache
@Data
@NoArgsConstructor
@Log4j2
/** Entity describing a profiling point execution. */
public class ProfilingPointRunEntity implements DashboardEntity {

    public static final String KIND = "ProfilingPointRun";

    // Property keys
    public static final String TYPE = "type";
    public static final String REGRESSION_MODE = "regressionMode";
    public static final String LABELS = "labels";
    public static final String VALUES = "values";
    public static final String X_LABEL = "xLabel";
    public static final String Y_LABEL = "yLabel";
    public static final String OPTIONS = "options";

    /** This value will set the limit size of values array field */
    public static final int VALUE_SIZE_LIMIT = 50000;

    @Ignore
    private Key key;

    /** ID field using profilingPointName */
    @Id
    private String name;

    /** parent field based on Test and TestRun key */
    @Parent
    private com.googlecode.objectify.Key<?> parent;

    /** VtsProfilingType in ProfilingPointRunEntity class  */
    private int type;

    /** VtsProfilingType in ProfilingPointRunEntity class  */
    private int regressionMode;

    /** list of label name  */
    private List<String> labels;

    /** list of values  */
    private List<Long> values;

    /** X axis label name */
    private String xLabel;

    /** Y axis label name */
    private String yLabel;

    /** Test Suite file name field */
    private List<String> options;

    /** When this record was created or updated */
    @Index Date updated;

    /**
     * Create a ProfilingPointRunEntity object.
     *
     * @param parentKey The Key object for the parent TestRunEntity in datastore.
     * @param name The name of the profiling point.
     * @param type The (number) type of the profiling point data.
     * @param regressionMode The (number) mode to use for detecting regression.
     * @param labels List of data labels, or null if the data is unlabeled.
     * @param values List of data values.
     * @param xLabel The x axis label.
     * @param yLabel The y axis label.
     * @param options The list of key=value options for the profiling point run.
     */
    public ProfilingPointRunEntity(
            Key parentKey,
            String name,
            int type,
            int regressionMode,
            List<String> labels,
            List<Long> values,
            String xLabel,
            String yLabel,
            List<String> options) {
        this.key = KeyFactory.createKey(parentKey, KIND, name);
        this.name = name;
        this.type = type;
        this.regressionMode = regressionMode;
        this.labels = labels == null ? null : new ArrayList<>(labels);
        this.values = new ArrayList<>(values);
        this.xLabel = xLabel;
        this.yLabel = yLabel;
        this.options = options;
        this.updated = new Date();
    }


    /**
     * Create a ProfilingPointRunEntity object.
     *
     * @param parent The objectify Key for the parent TestRunEntity in datastore.
     * @param name The name of the profiling point.
     * @param type The (number) type of the profiling point data.
     * @param regressionMode The (number) mode to use for detecting regression.
     * @param labels List of data labels, or null if the data is unlabeled.
     * @param values List of data values.
     * @param xLabel The x axis label.
     * @param yLabel The y axis label.
     * @param options The list of key=value options for the profiling point run.
     */
    public ProfilingPointRunEntity(
            com.googlecode.objectify.Key parent,
            String name,
            int type,
            int regressionMode,
            List<String> labels,
            List<Long> values,
            String xLabel,
            String yLabel,
            List<String> options) {
        this.parent = parent;
        this.name = name;
        this.type = type;
        this.regressionMode = regressionMode;
        this.labels = labels == null ? null : new ArrayList<>(labels);
        this.values = new ArrayList<>(values);
        this.xLabel = xLabel;
        this.yLabel = yLabel;
        this.options = options;
        this.updated = new Date();
    }

    /**
     * Get VtsProfilingType from int value.
     *
     * @return VtsProfilingType class.
     */
    public VtsProfilingType getVtsProfilingType(int type) {
        return VtsProfilingType.forNumber(type);
    }

    /**
     * Get VtsProfilingRegressionMode from int value.
     *
     * @return VtsProfilingType class.
     */
    public VtsProfilingRegressionMode getVtsProfilingRegressionMode(int regressionMode) {
        return VtsProfilingRegressionMode.forNumber(regressionMode);
    }

    /**
     * Save multi rows function when the record exceed the limit which is 1MB.
     *
     * @return ProfilingPointRunEntity's key value.
     */
    public com.googlecode.objectify.Key<ProfilingPointRunEntity> saveMultiRow() {
        if (this.getValues().size() > VALUE_SIZE_LIMIT) {

            List<List<Long>> partitionedValueList =
                    Lists.partition(this.getValues(), VALUE_SIZE_LIMIT);
            int partitionedValueListSize = partitionedValueList.size();

            List<List<String>> partitionedLabelList = new ArrayList<>();
            if (Objects.nonNull(this.getLabels()) && this.getLabels().size() > VALUE_SIZE_LIMIT) {
                partitionedLabelList = Lists.partition(this.getLabels(), VALUE_SIZE_LIMIT);
            }

            com.googlecode.objectify.Key<ProfilingPointRunEntity> profilingPointRunEntityKey = null;
            if (partitionedValueListSize < VALUE_SIZE_LIMIT) {
                for (int index = 0; index < partitionedValueListSize; index++) {
                    if (index > 0) {
                        this.values.addAll(partitionedValueList.get(index));
                        if (index < partitionedLabelList.size()) {
                            this.labels.addAll(partitionedLabelList.get(index));
                        }
                    } else {
                        this.values = partitionedValueList.get(index);
                        if (index < partitionedLabelList.size()) {
                            this.labels = partitionedLabelList.get(index);
                        }
                    }
                    profilingPointRunEntityKey = ofy().save().entity(this).now();
                }
            }
            return profilingPointRunEntityKey;
        } else {
            return ofy().save().entity(this).now();
        }
    }

    /** Saving function for the instance of this class */
    @Override
    public com.googlecode.objectify.Key<ProfilingPointRunEntity> save() {
        return ofy().save().entity(this).now();
    }

    public Entity toEntity() {
        Entity profilingRun = new Entity(this.key);
        profilingRun.setUnindexedProperty(TYPE, this.type);
        profilingRun.setUnindexedProperty(REGRESSION_MODE, this.regressionMode);
        if (this.labels != null) {
            profilingRun.setUnindexedProperty(LABELS, this.labels);
        }
        profilingRun.setUnindexedProperty(VALUES, this.values);
        profilingRun.setUnindexedProperty(X_LABEL, this.xLabel);
        profilingRun.setUnindexedProperty(Y_LABEL, this.yLabel);
        if (this.options != null) {
            profilingRun.setUnindexedProperty(OPTIONS, this.options);
        }

        return profilingRun;
    }

    /**
     * Convert an Entity object to a ProflilingPointRunEntity.
     *
     * @param e The entity to process.
     * @return ProfilingPointRunEntity object with the properties from e, or null if incompatible.
     */
    @SuppressWarnings("unchecked")
    public static ProfilingPointRunEntity fromEntity(Entity e) {
        if (!e.getKind().equals(KIND)
                || e.getKey().getName() == null
                || !e.hasProperty(TYPE)
                || !e.hasProperty(REGRESSION_MODE)
                || !e.hasProperty(VALUES)
                || !e.hasProperty(X_LABEL)
                || !e.hasProperty(Y_LABEL)) {
            log.error("Missing profiling point attributes in entity: " + e.toString());
            return null;
        }
        try {
            Key parentKey = e.getParent();
            String name = e.getKey().getName();
            int type = (int) (long) e.getProperty(TYPE);
            int regressionMode = (int) (long) e.getProperty(REGRESSION_MODE);
            List<Long> values = (List<Long>) e.getProperty(VALUES);
            String xLabel = (String) e.getProperty(X_LABEL);
            String yLabel = (String) e.getProperty(Y_LABEL);
            List<String> labels = null;
            if (e.hasProperty(LABELS)) {
                labels = (List<String>) e.getProperty(LABELS);
            }
            List<String> options = null;
            if (e.hasProperty(OPTIONS)) {
                options = (List<String>) e.getProperty(OPTIONS);
            }
            return new ProfilingPointRunEntity(
                    parentKey, name, type, regressionMode, labels, values, xLabel, yLabel, options);
        } catch (ClassCastException exception) {
            // Invalid cast
            log.warn("Error parsing profiling point run entity.", exception);
        }
        return null;
    }

    /**
     * Convert a coverage report to a CoverageEntity.
     *
     * @param parent The ancestor objectify key for the coverage entity.
     * @param profilingReport The profiling report containing profiling data.
     * @return The ProfilingPointRunEntity for the profiling report message, or null if incompatible
     */
    public static ProfilingPointRunEntity fromProfilingReport(
            com.googlecode.objectify.Key parent, ProfilingReportMessage profilingReport) {
        if (!profilingReport.hasName()
                || !profilingReport.hasType()
                || profilingReport.getType() == VtsProfilingType.UNKNOWN_VTS_PROFILING_TYPE
                || !profilingReport.hasRegressionMode()
                || !profilingReport.hasXAxisLabel()
                || !profilingReport.hasYAxisLabel()) {
            return null; // invalid profiling report;
        }
        String name = profilingReport.getName().toStringUtf8();
        VtsProfilingType type = profilingReport.getType();
        VtsProfilingRegressionMode regressionMode = profilingReport.getRegressionMode();
        String xLabel = profilingReport.getXAxisLabel().toStringUtf8();
        String yLabel = profilingReport.getYAxisLabel().toStringUtf8();
        List<Long> values;
        List<String> labels = null;
        switch (type) {
            case VTS_PROFILING_TYPE_TIMESTAMP:
                if (!profilingReport.hasStartTimestamp()
                        || !profilingReport.hasEndTimestamp()
                        || profilingReport.getEndTimestamp()
                                < profilingReport.getStartTimestamp()) {
                    return null; // missing timestamp
                }
                long value =
                        profilingReport.getEndTimestamp() - profilingReport.getStartTimestamp();
                values = new ArrayList<>();
                values.add(value);
                break;
            case VTS_PROFILING_TYPE_LABELED_VECTOR:
                if (profilingReport.getValueCount() != profilingReport.getLabelCount()) {
                    return null; // jagged data
                }
                labels = new ArrayList<>();
                for (ByteString label : profilingReport.getLabelList()) {
                    labels.add(label.toStringUtf8());
                }
                values = profilingReport.getValueList();
                break;
            case VTS_PROFILING_TYPE_UNLABELED_VECTOR:
                values = profilingReport.getValueList();
                break;
            default: // should never happen
                return null;
        }
        if (values.size() > VALUE_SIZE_LIMIT) {
            values = values.subList(0, VALUE_SIZE_LIMIT);
        }
        List<String> options = null;
        if (profilingReport.getOptionsCount() > 0) {
            options = new ArrayList<>();
            for (ByteString optionBytes : profilingReport.getOptionsList()) {
                options.add(optionBytes.toStringUtf8());
            }
        }
        return new ProfilingPointRunEntity(
                parent,
                name,
                type.getNumber(),
                regressionMode.getNumber(),
                labels,
                values,
                xLabel,
                yLabel,
                options);
    }
}