summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/util/LineGraph.java
blob: 47889610618cde5e9004cba935a85abe8f455002 (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
/*
 * Copyright (c) 2016 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.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** Helper object for describing graph data. */
public class LineGraph extends Graph {
    public static final String TICKS_KEY = "ticks";

    private List<ProfilingPointRunEntity> profilingRuns;
    private List<String> ids;
    private String xLabel;
    private String yLabel;
    private String name;
    private GraphType type = GraphType.LINE_GRAPH;

    public LineGraph(String name) {
        this.name = name;
        profilingRuns = new ArrayList<>();
        ids = 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 profilingRuns.size();
    }

    /**
     * Add data to the graph.
     *
     * @param id The name of the graph.
     * @param profilingPoint The ProfilingPointRunEntity containing data to add.
     */
    @Override
    public void addData(String id, ProfilingPointRunEntity profilingPoint) {
        if (profilingPoint.getValues().size() == 0
                || profilingPoint.getValues().size() != profilingPoint.getLabels().size())
            return;
        ids.add(id);
        profilingRuns.add(profilingPoint);
        xLabel = profilingPoint.getXLabel();
        yLabel = profilingPoint.getYLabel();
    }

    /**
     * Serializes the graph to json format.
     *
     * @return A JsonElement object representing the graph object.
     */
    @Override
    public JsonObject toJson() {
        JsonObject json = super.toJson();
        // Use the most recent profiling vector to generate the labels
        ProfilingPointRunEntity profilingRun = profilingRuns.get(profilingRuns.size() - 1);

        List<String> axisTicks = new ArrayList<>();
        Map<String, Integer> tickIndexMap = new HashMap<>();
        for (int i = 0; i < profilingRun.getLabels().size(); i++) {
            String label = profilingRun.getLabels().get(i);
            axisTicks.add(label);
            tickIndexMap.put(label, i);
        }

        long[][] lineGraphValues = new long[axisTicks.size()][profilingRuns.size()];
        for (int reportIndex = 0; reportIndex < profilingRuns.size(); reportIndex++) {
            ProfilingPointRunEntity pt = profilingRuns.get(reportIndex);
            for (int i = 0; i < pt.getLabels().size(); i++) {
                String label = pt.getLabels().get(i);

                // Skip value if its label is not present
                if (!tickIndexMap.containsKey(label))
                    continue;
                int labelIndex = tickIndexMap.get(label);

                lineGraphValues[labelIndex][reportIndex] = pt.getValues().get(i);
            }
        }
        json.add(VALUE_KEY, new Gson().toJsonTree(lineGraphValues).getAsJsonArray());
        json.add(IDS_KEY, new Gson().toJsonTree(ids).getAsJsonArray());
        json.add(TICKS_KEY, new Gson().toJsonTree(axisTicks));
        return json;
    }
}