1 /* 2 * Copyright (c) 2016 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you 5 * may not use this file except in compliance with the License. You may 6 * 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 13 * implied. See the License for the specific language governing 14 * permissions and limitations under the License. 15 */ 16 17 package com.android.vts.util; 18 19 import com.android.vts.entity.ProfilingPointRunEntity; 20 import com.google.gson.Gson; 21 import com.google.gson.JsonObject; 22 import java.util.ArrayList; 23 import java.util.HashMap; 24 import java.util.List; 25 import java.util.Map; 26 27 /** Helper object for describing graph data. */ 28 public class LineGraph extends Graph { 29 public static final String TICKS_KEY = "ticks"; 30 31 private List<ProfilingPointRunEntity> profilingRuns; 32 private List<String> ids; 33 private String xLabel; 34 private String yLabel; 35 private String name; 36 private GraphType type = GraphType.LINE_GRAPH; 37 LineGraph(String name)38 public LineGraph(String name) { 39 this.name = name; 40 profilingRuns = new ArrayList<>(); 41 ids = new ArrayList<>(); 42 } 43 44 /** 45 * Get the x axis label. 46 * 47 * @return The x axis label. 48 */ 49 @Override getXLabel()50 public String getXLabel() { 51 return xLabel; 52 } 53 54 /** 55 * Get the graph type. 56 * 57 * @return The graph type. 58 */ 59 @Override getType()60 public GraphType getType() { 61 return type; 62 } 63 64 /** 65 * Get the name of the graph. 66 * 67 * @return The name of the graph. 68 */ 69 @Override getName()70 public String getName() { 71 return name; 72 } 73 74 /** 75 * Get the y axis label. 76 * 77 * @return The y axis label. 78 */ 79 @Override getYLabel()80 public String getYLabel() { 81 return yLabel; 82 } 83 84 /** 85 * Get the number of data points stored in the graph. 86 * 87 * @return The number of data points stored in the graph. 88 */ 89 @Override size()90 public int size() { 91 return profilingRuns.size(); 92 } 93 94 /** 95 * Add data to the graph. 96 * 97 * @param id The name of the graph. 98 * @param profilingPoint The ProfilingPointRunEntity containing data to add. 99 */ 100 @Override addData(String id, ProfilingPointRunEntity profilingPoint)101 public void addData(String id, ProfilingPointRunEntity profilingPoint) { 102 if (profilingPoint.getValues().size() == 0 103 || profilingPoint.getValues().size() != profilingPoint.getLabels().size()) 104 return; 105 ids.add(id); 106 profilingRuns.add(profilingPoint); 107 xLabel = profilingPoint.getXLabel(); 108 yLabel = profilingPoint.getYLabel(); 109 } 110 111 /** 112 * Serializes the graph to json format. 113 * 114 * @return A JsonElement object representing the graph object. 115 */ 116 @Override toJson()117 public JsonObject toJson() { 118 JsonObject json = super.toJson(); 119 // Use the most recent profiling vector to generate the labels 120 ProfilingPointRunEntity profilingRun = profilingRuns.get(profilingRuns.size() - 1); 121 122 List<String> axisTicks = new ArrayList<>(); 123 Map<String, Integer> tickIndexMap = new HashMap<>(); 124 for (int i = 0; i < profilingRun.getLabels().size(); i++) { 125 String label = profilingRun.getLabels().get(i); 126 axisTicks.add(label); 127 tickIndexMap.put(label, i); 128 } 129 130 long[][] lineGraphValues = new long[axisTicks.size()][profilingRuns.size()]; 131 for (int reportIndex = 0; reportIndex < profilingRuns.size(); reportIndex++) { 132 ProfilingPointRunEntity pt = profilingRuns.get(reportIndex); 133 for (int i = 0; i < pt.getLabels().size(); i++) { 134 String label = pt.getLabels().get(i); 135 136 // Skip value if its label is not present 137 if (!tickIndexMap.containsKey(label)) 138 continue; 139 int labelIndex = tickIndexMap.get(label); 140 141 lineGraphValues[labelIndex][reportIndex] = pt.getValues().get(i); 142 } 143 } 144 json.add(VALUE_KEY, new Gson().toJsonTree(lineGraphValues).getAsJsonArray()); 145 json.add(IDS_KEY, new Gson().toJsonTree(ids).getAsJsonArray()); 146 json.add(TICKS_KEY, new Gson().toJsonTree(axisTicks)); 147 return json; 148 } 149 } 150