Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 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 org.onosproject.ui.chart; |
| 18 | |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 22 | |
Jian Li | 1077dd7 | 2016-04-26 11:10:20 -0700 | [diff] [blame] | 23 | import java.util.ArrayList; |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 24 | import java.util.Arrays; |
Jian Li | 1077dd7 | 2016-04-26 11:10:20 -0700 | [diff] [blame] | 25 | import java.util.Collection; |
| 26 | import java.util.HashMap; |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 27 | import java.util.List; |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 28 | import java.util.Map; |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 29 | import java.util.Set; |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 30 | |
| 31 | import static com.google.common.base.Preconditions.checkArgument; |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | |
| 34 | /** |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 35 | * A simple model of time series chart data. |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 36 | * <p> |
| 37 | * Note that this is not a full MVC type model; the expected usage pattern |
| 38 | * is to create an empty chart, add data points (by consulting the business model), |
| 39 | * and produce the list of data points which contain a label and a set of data |
| 40 | * values for all serials. |
| 41 | */ |
| 42 | public class ChartModel { |
| 43 | |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 44 | private final Set<String> seriesSet; |
| 45 | private final String[] seriesArray; |
| 46 | private final List<Long> labels = Lists.newArrayList(); |
| 47 | private final List<DataPoint> dataPoints = Lists.newArrayList(); |
Jian Li | 1077dd7 | 2016-04-26 11:10:20 -0700 | [diff] [blame] | 48 | private final Map<String, Annot> annotations = new HashMap<>(); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Constructs a chart model with initialized series set. |
| 52 | * |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 53 | * @param series a set of series |
| 54 | */ |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 55 | public ChartModel(String... series) { |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 56 | checkNotNull(series, "series cannot be null"); |
| 57 | checkArgument(series.length > 0, "must be at least one series"); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 58 | |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 59 | seriesSet = Sets.newHashSet(series); |
| 60 | |
| 61 | if (seriesSet.size() != series.length) { |
| 62 | throw new IllegalArgumentException("duplicate series detected"); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 65 | this.seriesArray = Arrays.copyOf(series, series.length); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | private void checkDataPoint(DataPoint dataPoint) { |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 69 | checkArgument(dataPoint.size() == seriesCount(), |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 70 | "data size should be equal to number of series"); |
| 71 | } |
| 72 | |
| 73 | /** |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 74 | * Checks the validity of the given series. |
| 75 | * |
| 76 | * @param series series name |
| 77 | */ |
| 78 | private void checkSeries(String series) { |
| 79 | checkNotNull(series, "must provide a series name"); |
| 80 | if (!seriesSet.contains(series)) { |
| 81 | throw new IllegalArgumentException("unknown series: " + series); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 86 | * Returns the number of series in this chart model. |
| 87 | * |
| 88 | * @return number of series |
| 89 | */ |
| 90 | public int seriesCount() { |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 91 | return seriesSet.size(); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 95 | * Adds a data point to the chart model. |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 96 | * |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 97 | * @return the data point, for chaining |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 98 | */ |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 99 | public DataPoint addDataPoint(Long label) { |
| 100 | DataPoint dp = new DataPoint(); |
| 101 | labels.add(label); |
| 102 | dataPoints.add(dp); |
| 103 | return dp; |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns all of series. |
| 108 | * |
| 109 | * @return an array of series |
| 110 | */ |
| 111 | public String[] getSeries() { |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 112 | return seriesArray; |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 116 | * Returns all of data points in order. |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 117 | * |
| 118 | * @return an array of data points |
| 119 | */ |
| 120 | public DataPoint[] getDataPoints() { |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 121 | return dataPoints.toArray(new DataPoint[dataPoints.size()]); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns all of labels in order. |
| 126 | * |
| 127 | * @return an array of labels |
| 128 | */ |
| 129 | public Object[] getLabels() { |
| 130 | return labels.toArray(new Long[labels.size()]); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Returns the number of data points in this chart model. |
| 135 | * |
| 136 | * @return number of data points |
| 137 | */ |
| 138 | public int dataPointCount() { |
| 139 | return dataPoints.size(); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns the last element inside all of data points. |
| 144 | * |
| 145 | * @return data point |
| 146 | */ |
| 147 | public DataPoint getLastDataPoint() { |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 148 | return dataPoints.get(dataPoints.size() - 1); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
Jian Li | 1077dd7 | 2016-04-26 11:10:20 -0700 | [diff] [blame] | 152 | * Inserts a new annotation. |
| 153 | * |
| 154 | * @param key key of annotation |
| 155 | * @param value value of annotation |
| 156 | */ |
| 157 | public void addAnnotation(String key, Object value) { |
| 158 | annotations.put(key, new Annot(key, value)); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns the annotations in this chart. |
| 163 | * |
| 164 | * @return annotations |
| 165 | */ |
| 166 | public Collection<Annot> getAnnotations() { |
| 167 | return new ArrayList<>(annotations.values()); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Model of an annotation. |
| 172 | */ |
| 173 | public class Annot { |
| 174 | private final String key; |
| 175 | private final Object value; |
| 176 | |
| 177 | /** |
| 178 | * Constructs an annotation with the given key and value. |
| 179 | * |
| 180 | * @param key the key |
| 181 | * @param value the value |
| 182 | */ |
| 183 | public Annot(String key, Object value) { |
| 184 | this.key = key; |
| 185 | this.value = value; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Returns the annotation's key. |
| 190 | * |
| 191 | * @return key |
| 192 | */ |
| 193 | public String key() { |
| 194 | return key; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Returns the annotation's value. |
| 199 | * |
| 200 | * @return value |
| 201 | */ |
| 202 | public Object value() { |
| 203 | return value; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Returns the value as a string. |
| 208 | * This default implementation uses the value's toString() method. |
| 209 | * |
| 210 | * @return the value as a string |
| 211 | */ |
| 212 | public String valueAsString() { |
| 213 | return value.toString(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 218 | * A class of data point. |
| 219 | */ |
| 220 | public class DataPoint { |
| 221 | // values for all series |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 222 | private final Map<String, Double> data = Maps.newHashMap(); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 223 | |
| 224 | /** |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 225 | * Sets the data value for the given series of this data point. |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 226 | * |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 227 | * @param series series name |
Jian Li | 1077dd7 | 2016-04-26 11:10:20 -0700 | [diff] [blame] | 228 | * @param value value to set |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 229 | * @return self, for chaining |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 230 | */ |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 231 | public DataPoint data(String series, Double value) { |
| 232 | checkSeries(series); |
| 233 | data.put(series, value); |
| 234 | return this; |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /** |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 238 | * Returns the data value with the given series for this data point. |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 239 | * |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 240 | * @return data value |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 241 | */ |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 242 | public Double get(String series) { |
| 243 | return data.get(series); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Return the data value with the same order of series. |
| 248 | * |
| 249 | * @return an array of ordered data values |
| 250 | */ |
| 251 | public Double[] getAll() { |
| 252 | Double[] value = new Double[getSeries().length]; |
| 253 | int idx = 0; |
| 254 | for (String s : getSeries()) { |
| 255 | value[idx] = get(s); |
| 256 | idx++; |
| 257 | } |
| 258 | return value; |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns the size of data point. |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 263 | * |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 264 | * @return the size of data point |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 265 | */ |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 266 | public int size() { |
| 267 | return data.size(); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } |