Jian Li
Committed by Gerrit Code Review

Change the data type of Data Point value from Double to Object

Change-Id: If5243fcb25c9893601695743b562ce88b844c64d
...@@ -62,6 +62,8 @@ public class CpmanViewMessageHandler extends UiMessageHandler { ...@@ -62,6 +62,8 @@ public class CpmanViewMessageHandler extends UiMessageHandler {
62 62
63 private static final int MILLI_CONV_UNIT = 1000; 63 private static final int MILLI_CONV_UNIT = 1000;
64 64
65 + private static final String TIME_FORMAT = "HH:mm";
66 +
65 private long timestamp = 0L; 67 private long timestamp = 0L;
66 68
67 @Override 69 @Override
...@@ -101,15 +103,13 @@ public class CpmanViewMessageHandler extends UiMessageHandler { ...@@ -101,15 +103,13 @@ public class CpmanViewMessageHandler extends UiMessageHandler {
101 for (String deviceId : deviceIds) { 103 for (String deviceId : deviceIds) {
102 Map<ControlMetricType, Long> data = 104 Map<ControlMetricType, Long> data =
103 populateDeviceMetrics(cpms, cs, DeviceId.deviceId(deviceId)); 105 populateDeviceMetrics(cpms, cs, DeviceId.deviceId(deviceId));
104 - Map<String, Long> local = Maps.newHashMap(); 106 + Map<String, Object> local = Maps.newHashMap();
105 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) { 107 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
106 local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt)); 108 local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt));
107 } 109 }
108 - // TODO: need to find a way to present device id using long type 110 +
109 - String shortId = StringUtils.substring(deviceId, 111 + local.put(LABEL, deviceId);
110 - deviceId.length() - 2, deviceId.length()); 112 + populateMetric(cm.addDataPoint(deviceId), local);
111 - local.put(LABEL, Long.valueOf(shortId));
112 - populateMetric(cm.addDataPoint(Long.valueOf(shortId)), local);
113 } 113 }
114 } 114 }
115 } 115 }
...@@ -171,24 +171,25 @@ public class CpmanViewMessageHandler extends UiMessageHandler { ...@@ -171,24 +171,25 @@ public class CpmanViewMessageHandler extends UiMessageHandler {
171 } 171 }
172 } 172 }
173 173
174 - private void populateMetrics(ChartModel cm, Map<ControlMetricType, 174 + private void populateMetrics(ChartModel cm,
175 - Long[]> data, LocalDateTime time, int numOfDp) { 175 + Map<ControlMetricType, Long[]> data,
176 + LocalDateTime time, int numOfDp) {
176 for (int i = 0; i < numOfDp; i++) { 177 for (int i = 0; i < numOfDp; i++) {
177 - Map<String, Long> local = Maps.newHashMap(); 178 + Map<String, Object> local = Maps.newHashMap();
178 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) { 179 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
179 local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt)[i]); 180 local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt)[i]);
180 } 181 }
181 182
182 - local.put(LABEL, time.minusMinutes(numOfDp - i).toDateTime().getMillis()); 183 + String calculated = time.minusMinutes(numOfDp - i).toString(TIME_FORMAT);
183 184
184 - populateMetric(cm.addDataPoint(time.minusMinutes(numOfDp - i) 185 + local.put(LABEL, calculated);
185 - .toDateTime().getMillis()), local); 186 + populateMetric(cm.addDataPoint(calculated), local);
186 } 187 }
187 } 188 }
188 189
189 private void populateMetric(ChartModel.DataPoint dataPoint, 190 private void populateMetric(ChartModel.DataPoint dataPoint,
190 - Map<String, Long> data) { 191 + Map<String, Object> data) {
191 - data.forEach((k, v) -> dataPoint.data(k, v.doubleValue())); 192 + data.forEach((k, v) -> dataPoint.data(k, v));
192 } 193 }
193 } 194 }
194 } 195 }
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
31 data[i] = new Array(1); 31 data[i] = new Array(1);
32 } 32 }
33 33
34 - var date, max, merged; 34 + var max;
35 35
36 function ceil(num) { 36 function ceil(num) {
37 if (isNaN(num)) { 37 if (isNaN(num)) {
...@@ -42,6 +42,11 @@ ...@@ -42,6 +42,11 @@
42 return (Math.ceil(num / pow)) * pow; 42 return (Math.ceil(num / pow)) * pow;
43 } 43 }
44 44
45 + function maxInArray(array) {
46 + var merged = [].concat.apply([], array);
47 + return Math.max.apply(null, merged);
48 + }
49 +
45 angular.module('ovCpman', ["chart.js"]) 50 angular.module('ovCpman', ["chart.js"])
46 .controller('OvCpmanCtrl', 51 .controller('OvCpmanCtrl',
47 ['$log', '$scope', '$location', 'FnService', 'ChartBuilderService', 'NavService', 52 ['$log', '$scope', '$location', 'FnService', 'ChartBuilderService', 'NavService',
...@@ -88,37 +93,30 @@ ...@@ -88,37 +93,30 @@
88 data[4][idx] = cm.request_packet; 93 data[4][idx] = cm.request_packet;
89 data[5][idx] = cm.reply_packet; 94 data[5][idx] = cm.reply_packet;
90 95
91 - if(hasDeviceId) { 96 + labels[idx] = cm.label;
92 - date = new Date(cm.label);
93 - labels[idx] = date.getHours() + ":" + date.getMinutes();
94 - } else {
95 - labels[idx] = cm.label;
96 - }
97 }); 97 });
98 } 98 }
99 99
100 - merged = [].concat.apply([], data); 100 + max = maxInArray(data)
101 - max = Math.max.apply(null, merged);
102 $scope.labels = labels; 101 $scope.labels = labels;
103 $scope.data = data; 102 $scope.data = data;
104 $scope.options = { 103 $scope.options = {
105 scaleOverride : true, 104 scaleOverride : true,
106 scaleSteps : 10, 105 scaleSteps : 10,
107 scaleStepWidth : ceil(max) / 10, 106 scaleStepWidth : ceil(max) / 10,
108 - scaleStartValue : 0 107 + scaleStartValue : 0,
108 + scaleFontSize : 16
109 }; 109 };
110 $scope.onClick = function (points, evt) { 110 $scope.onClick = function (points, evt) {
111 if (points[0]) { 111 if (points[0]) {
112 - // TODO: this will be replaced with real device id 112 + ns.navTo('cpman', { devId: points[0].label });
113 - var tmpId = 'of:000000000000020' + points[0].label;
114 - ns.navTo('cpman', { devId: tmpId });
115 $log.log(points[0].label); 113 $log.log(points[0].label);
116 } 114 }
117 }; 115 };
118 }); 116 });
119 117
120 $scope.series = ['INBOUND', 'OUTBOUND', 'FLOW-MOD', 118 $scope.series = ['INBOUND', 'OUTBOUND', 'FLOW-MOD',
121 - 'FLOW-REMOVED', 'STATS-REQUEST', 'STATS-REPLY']; 119 + 'FLOW-REMOVED', 'REQUEST', 'REPLY'];
122 $scope.labels = labels; 120 $scope.labels = labels;
123 $scope.data = data; 121 $scope.data = data;
124 122
......
...@@ -43,7 +43,7 @@ public class ChartModel { ...@@ -43,7 +43,7 @@ public class ChartModel {
43 43
44 private final Set<String> seriesSet; 44 private final Set<String> seriesSet;
45 private final String[] seriesArray; 45 private final String[] seriesArray;
46 - private final List<Long> labels = Lists.newArrayList(); 46 + private final List<Object> labels = Lists.newArrayList();
47 private final List<DataPoint> dataPoints = Lists.newArrayList(); 47 private final List<DataPoint> dataPoints = Lists.newArrayList();
48 private final Map<String, Annot> annotations = new HashMap<>(); 48 private final Map<String, Annot> annotations = new HashMap<>();
49 49
...@@ -94,9 +94,10 @@ public class ChartModel { ...@@ -94,9 +94,10 @@ public class ChartModel {
94 /** 94 /**
95 * Adds a data point to the chart model. 95 * Adds a data point to the chart model.
96 * 96 *
97 + * @param label label instance
97 * @return the data point, for chaining 98 * @return the data point, for chaining
98 */ 99 */
99 - public DataPoint addDataPoint(Long label) { 100 + public DataPoint addDataPoint(Object label) {
100 DataPoint dp = new DataPoint(); 101 DataPoint dp = new DataPoint();
101 labels.add(label); 102 labels.add(label);
102 dataPoints.add(dp); 103 dataPoints.add(dp);
...@@ -127,7 +128,7 @@ public class ChartModel { ...@@ -127,7 +128,7 @@ public class ChartModel {
127 * @return an array of labels 128 * @return an array of labels
128 */ 129 */
129 public Object[] getLabels() { 130 public Object[] getLabels() {
130 - return labels.toArray(new Long[labels.size()]); 131 + return labels.toArray(new Object[labels.size()]);
131 } 132 }
132 133
133 /** 134 /**
...@@ -219,7 +220,7 @@ public class ChartModel { ...@@ -219,7 +220,7 @@ public class ChartModel {
219 */ 220 */
220 public class DataPoint { 221 public class DataPoint {
221 // values for all series 222 // values for all series
222 - private final Map<String, Double> data = Maps.newHashMap(); 223 + private final Map<String, Object> data = Maps.newHashMap();
223 224
224 /** 225 /**
225 * Sets the data value for the given series of this data point. 226 * Sets the data value for the given series of this data point.
...@@ -228,7 +229,7 @@ public class ChartModel { ...@@ -228,7 +229,7 @@ public class ChartModel {
228 * @param value value to set 229 * @param value value to set
229 * @return self, for chaining 230 * @return self, for chaining
230 */ 231 */
231 - public DataPoint data(String series, Double value) { 232 + public DataPoint data(String series, Object value) {
232 checkSeries(series); 233 checkSeries(series);
233 data.put(series, value); 234 data.put(series, value);
234 return this; 235 return this;
...@@ -237,9 +238,10 @@ public class ChartModel { ...@@ -237,9 +238,10 @@ public class ChartModel {
237 /** 238 /**
238 * Returns the data value with the given series for this data point. 239 * Returns the data value with the given series for this data point.
239 * 240 *
241 + * @param series series name
240 * @return data value 242 * @return data value
241 */ 243 */
242 - public Double get(String series) { 244 + public Object get(String series) {
243 return data.get(series); 245 return data.get(series);
244 } 246 }
245 247
...@@ -248,8 +250,8 @@ public class ChartModel { ...@@ -248,8 +250,8 @@ public class ChartModel {
248 * 250 *
249 * @return an array of ordered data values 251 * @return an array of ordered data values
250 */ 252 */
251 - public Double[] getAll() { 253 + public Object[] getAll() {
252 - Double[] value = new Double[getSeries().length]; 254 + Object[] value = new Object[getSeries().length];
253 int idx = 0; 255 int idx = 0;
254 for (String s : getSeries()) { 256 for (String s : getSeries()) {
255 value[idx] = get(s); 257 value[idx] = get(s);
...@@ -266,5 +268,29 @@ public class ChartModel { ...@@ -266,5 +268,29 @@ public class ChartModel {
266 public int size() { 268 public int size() {
267 return data.size(); 269 return data.size();
268 } 270 }
271 +
272 + /**
273 + * Returns the value of the data point as a string, using the
274 + * formatter appropriate for the series.
275 + *
276 + * @param series series
277 + * @return formatted data point value
278 + */
279 + public String getAsString(String series) {
280 + return get(series).toString();
281 + }
282 +
283 + /**
284 + * Returns the row as an array of formatted strings.
285 + *
286 + * @return the string format of data points
287 + */
288 + public String[] getAsStrings() {
289 + List<String> formatted = new ArrayList<>(size());
290 + for (String c : seriesArray) {
291 + formatted.add(getAsString(c));
292 + }
293 + return formatted.toArray(new String[formatted.size()]);
294 + }
269 } 295 }
270 } 296 }
......
...@@ -69,7 +69,7 @@ public final class ChartUtils { ...@@ -69,7 +69,7 @@ public final class ChartUtils {
69 public static JsonNode toJsonNode(ChartModel.DataPoint dp, ChartModel cm) { 69 public static JsonNode toJsonNode(ChartModel.DataPoint dp, ChartModel cm) {
70 ObjectNode result = MAPPER.createObjectNode(); 70 ObjectNode result = MAPPER.createObjectNode();
71 String[] series = cm.getSeries(); 71 String[] series = cm.getSeries();
72 - Double[] values = dp.getAll(); 72 + String[] values = dp.getAsStrings();
73 int n = series.length; 73 int n = series.length;
74 for (int i = 0; i < n; i++) { 74 for (int i = 0; i < n; i++) {
75 result.put(series[i], values[i]); 75 result.put(series[i], values[i]);
......
...@@ -29,7 +29,7 @@ public class ChartUtilsTest { ...@@ -29,7 +29,7 @@ public class ChartUtilsTest {
29 private static final String BAR = "bar"; 29 private static final String BAR = "bar";
30 30
31 private static final String ARRAY_AS_STRING = 31 private static final String ARRAY_AS_STRING =
32 - "[{\"foo\":1.0,\"bar\":2.0},{\"foo\":3.0,\"bar\":4.0}]"; 32 + "[{\"foo\":\"1.0\",\"bar\":\"2.0\"},{\"foo\":\"3.0\",\"bar\":\"4.0\"}]";
33 private static final String NODE_AS_STRING = 33 private static final String NODE_AS_STRING =
34 "{\"dev1\":\"of:0000000000000001\",\"dev2\":\"of:0000000000000002\"}"; 34 "{\"dev1\":\"of:0000000000000001\",\"dev2\":\"of:0000000000000002\"}";
35 35
......