summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/util/BoxPlot.java
blob: 455d778d935cd34a40db61c63bc794d243b55f0b (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
/*
 * 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.util;

import com.android.vts.entity.ProfilingPointRunEntity;
import com.android.vts.proto.VtsReportMessage;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/** Helper object for describing time-series box plot data. */
public class BoxPlot extends Graph {
    private static final String LABEL_KEY = "label";
    private static final String SERIES_KEY = "seriesList";
    private static final String MEAN_KEY = "mean";
    private static final String STD_KEY = "std";
    private static final String COUNT_KEY = "count";

    private static final String DAY = "Day";

    private final GraphType type = GraphType.BOX_PLOT;
    private final String name;
    private final String xLabel;
    private final String yLabel;
    private final Map<String, Map<String, StatSummary>> labelSeriesMap;
    private final Set<String> seriesSet;

    private int count;
    private List<String> labels;

    public BoxPlot(String name, String xLabel, String yLabel) {
        this.name = name;
        this.xLabel = xLabel == null ? DAY : xLabel;
        this.yLabel = yLabel;
        this.count = 0;
        this.labelSeriesMap = new HashMap<>();
        this.seriesSet = new HashSet<>();
        this.labels = new ArrayList<>();
    }

    /**
     * Get the x axis label.
     *
     * @return The x axis label.
     */
    @Override
    public String getXLabel() {
        return xLabel;
    }

    /**
     * Get the graph type.
     *
     * @return The graph type.
     */
    @Override
    public GraphType getType() {
        return type;
    }

    /**
     * Get the name of the graph.
     *
     * @return The name of the graph.
     */
    @Override
    public String getName() {
        return name;
    }

    /**
     * Get the y axis label.
     *
     * @return The y axis label.
     */
    @Override
    public String getYLabel() {
        return yLabel;
    }

    /**
     * Get the number of data points stored in the graph.
     *
     * @return The number of data points stored in the graph.
     */
    @Override
    public int size() {
        return this.count;
    }

    /**
     * Add data to the graph.
     *
     * @param label The name of the category.
     * @param profilingPoint The ProfilingPointRunEntity containing data to add.
     */
    @Override
    public void addData(String label, ProfilingPointRunEntity profilingPoint) {
        StatSummary stat =
                new StatSummary(
                        label, VtsReportMessage.VtsProfilingRegressionMode.UNKNOWN_REGRESSION_MODE);
        for (long value : profilingPoint.getValues()) {
            stat.updateStats(value);
        }
        addSeriesData(label, "", stat);
    }

    public void addSeriesData(String label, String series, StatSummary stats) {
        if (!labelSeriesMap.containsKey(label)) {
            labelSeriesMap.put(label, new HashMap<>());
        }
        Map<String, StatSummary> seriesMap = labelSeriesMap.get(label);
        seriesMap.put(series, stats);
        seriesSet.add(series);
        count += stats.getCount();
    }

    public void setLabels(List<String> labels) {
        this.labels = labels;
    }

    /**
     * Serializes the graph to json format.
     *
     * @return A JsonElement object representing the graph object.
     */
    @Override
    public JsonObject toJson() {
        JsonObject json = super.toJson();
        List<JsonObject> stats = new ArrayList<>();
        List<String> seriesList = new ArrayList<>(seriesSet);
        seriesList.sort(Comparator.naturalOrder());
        for (String label : labels) {
            JsonObject statJson = new JsonObject();
            String boxLabel = label;
            List<JsonObject> statList = new ArrayList<>(seriesList.size());
            Map<String, StatSummary> seriesMap = labelSeriesMap.get(label);
            for (String series : seriesList) {
                JsonObject statSummary = new JsonObject();
                Double mean = null;
                Double std = null;
                Integer count = null;
                if (seriesMap.containsKey(series) && seriesMap.get(series).getCount() > 0) {
                    StatSummary stat = seriesMap.get(series);
                    mean = stat.getMean();
                    std = 0.;
                    if (stat.getCount() > 1) {
                        std = stat.getStd();
                    }
                    count = stat.getCount();
                }
                statSummary.addProperty(MEAN_KEY, mean);
                statSummary.addProperty(STD_KEY, std);
                statSummary.addProperty(COUNT_KEY, count);
                statList.add(statSummary);
            }
            statJson.addProperty(LABEL_KEY, boxLabel);
            statJson.add(VALUE_KEY, new Gson().toJsonTree(statList));
            stats.add(statJson);
        }
        json.add(VALUE_KEY, new Gson().toJsonTree(stats));
        json.add(SERIES_KEY, new Gson().toJsonTree(seriesList));
        return json;
    }
}