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 {
private static final int MILLI_CONV_UNIT = 1000;
private static final String TIME_FORMAT = "HH:mm";
private long timestamp = 0L;
@Override
......@@ -101,15 +103,13 @@ public class CpmanViewMessageHandler extends UiMessageHandler {
for (String deviceId : deviceIds) {
Map<ControlMetricType, Long> data =
populateDeviceMetrics(cpms, cs, DeviceId.deviceId(deviceId));
Map<String, Long> local = Maps.newHashMap();
Map<String, Object> local = Maps.newHashMap();
for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt));
}
// TODO: need to find a way to present device id using long type
String shortId = StringUtils.substring(deviceId,
deviceId.length() - 2, deviceId.length());
local.put(LABEL, Long.valueOf(shortId));
populateMetric(cm.addDataPoint(Long.valueOf(shortId)), local);
local.put(LABEL, deviceId);
populateMetric(cm.addDataPoint(deviceId), local);
}
}
}
......@@ -171,24 +171,25 @@ public class CpmanViewMessageHandler extends UiMessageHandler {
}
}
private void populateMetrics(ChartModel cm, Map<ControlMetricType,
Long[]> data, LocalDateTime time, int numOfDp) {
private void populateMetrics(ChartModel cm,
Map<ControlMetricType, Long[]> data,
LocalDateTime time, int numOfDp) {
for (int i = 0; i < numOfDp; i++) {
Map<String, Long> local = Maps.newHashMap();
Map<String, Object> local = Maps.newHashMap();
for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt)[i]);
}
local.put(LABEL, time.minusMinutes(numOfDp - i).toDateTime().getMillis());
String calculated = time.minusMinutes(numOfDp - i).toString(TIME_FORMAT);
populateMetric(cm.addDataPoint(time.minusMinutes(numOfDp - i)
.toDateTime().getMillis()), local);
local.put(LABEL, calculated);
populateMetric(cm.addDataPoint(calculated), local);
}
}
private void populateMetric(ChartModel.DataPoint dataPoint,
Map<String, Long> data) {
data.forEach((k, v) -> dataPoint.data(k, v.doubleValue()));
Map<String, Object> data) {
data.forEach((k, v) -> dataPoint.data(k, v));
}
}
}
......
......@@ -31,7 +31,7 @@
data[i] = new Array(1);
}
var date, max, merged;
var max;
function ceil(num) {
if (isNaN(num)) {
......@@ -42,6 +42,11 @@
return (Math.ceil(num / pow)) * pow;
}
function maxInArray(array) {
var merged = [].concat.apply([], array);
return Math.max.apply(null, merged);
}
angular.module('ovCpman', ["chart.js"])
.controller('OvCpmanCtrl',
['$log', '$scope', '$location', 'FnService', 'ChartBuilderService', 'NavService',
......@@ -88,37 +93,30 @@
data[4][idx] = cm.request_packet;
data[5][idx] = cm.reply_packet;
if(hasDeviceId) {
date = new Date(cm.label);
labels[idx] = date.getHours() + ":" + date.getMinutes();
} else {
labels[idx] = cm.label;
}
labels[idx] = cm.label;
});
}
merged = [].concat.apply([], data);
max = Math.max.apply(null, merged);
max = maxInArray(data)
$scope.labels = labels;
$scope.data = data;
$scope.options = {
scaleOverride : true,
scaleSteps : 10,
scaleStepWidth : ceil(max) / 10,
scaleStartValue : 0
scaleStartValue : 0,
scaleFontSize : 16
};
$scope.onClick = function (points, evt) {
if (points[0]) {
// TODO: this will be replaced with real device id
var tmpId = 'of:000000000000020' + points[0].label;
ns.navTo('cpman', { devId: tmpId });
ns.navTo('cpman', { devId: points[0].label });
$log.log(points[0].label);
}
};
});
$scope.series = ['INBOUND', 'OUTBOUND', 'FLOW-MOD',
'FLOW-REMOVED', 'STATS-REQUEST', 'STATS-REPLY'];
'FLOW-REMOVED', 'REQUEST', 'REPLY'];
$scope.labels = labels;
$scope.data = data;
......
......@@ -43,7 +43,7 @@ public class ChartModel {
private final Set<String> seriesSet;
private final String[] seriesArray;
private final List<Long> labels = Lists.newArrayList();
private final List<Object> labels = Lists.newArrayList();
private final List<DataPoint> dataPoints = Lists.newArrayList();
private final Map<String, Annot> annotations = new HashMap<>();
......@@ -94,9 +94,10 @@ public class ChartModel {
/**
* Adds a data point to the chart model.
*
* @param label label instance
* @return the data point, for chaining
*/
public DataPoint addDataPoint(Long label) {
public DataPoint addDataPoint(Object label) {
DataPoint dp = new DataPoint();
labels.add(label);
dataPoints.add(dp);
......@@ -127,7 +128,7 @@ public class ChartModel {
* @return an array of labels
*/
public Object[] getLabels() {
return labels.toArray(new Long[labels.size()]);
return labels.toArray(new Object[labels.size()]);
}
/**
......@@ -219,7 +220,7 @@ public class ChartModel {
*/
public class DataPoint {
// values for all series
private final Map<String, Double> data = Maps.newHashMap();
private final Map<String, Object> data = Maps.newHashMap();
/**
* Sets the data value for the given series of this data point.
......@@ -228,7 +229,7 @@ public class ChartModel {
* @param value value to set
* @return self, for chaining
*/
public DataPoint data(String series, Double value) {
public DataPoint data(String series, Object value) {
checkSeries(series);
data.put(series, value);
return this;
......@@ -237,9 +238,10 @@ public class ChartModel {
/**
* Returns the data value with the given series for this data point.
*
* @param series series name
* @return data value
*/
public Double get(String series) {
public Object get(String series) {
return data.get(series);
}
......@@ -248,8 +250,8 @@ public class ChartModel {
*
* @return an array of ordered data values
*/
public Double[] getAll() {
Double[] value = new Double[getSeries().length];
public Object[] getAll() {
Object[] value = new Object[getSeries().length];
int idx = 0;
for (String s : getSeries()) {
value[idx] = get(s);
......@@ -266,5 +268,29 @@ public class ChartModel {
public int size() {
return data.size();
}
/**
* Returns the value of the data point as a string, using the
* formatter appropriate for the series.
*
* @param series series
* @return formatted data point value
*/
public String getAsString(String series) {
return get(series).toString();
}
/**
* Returns the row as an array of formatted strings.
*
* @return the string format of data points
*/
public String[] getAsStrings() {
List<String> formatted = new ArrayList<>(size());
for (String c : seriesArray) {
formatted.add(getAsString(c));
}
return formatted.toArray(new String[formatted.size()]);
}
}
}
......
......@@ -69,7 +69,7 @@ public final class ChartUtils {
public static JsonNode toJsonNode(ChartModel.DataPoint dp, ChartModel cm) {
ObjectNode result = MAPPER.createObjectNode();
String[] series = cm.getSeries();
Double[] values = dp.getAll();
String[] values = dp.getAsStrings();
int n = series.length;
for (int i = 0; i < n; i++) {
result.put(series[i], values[i]);
......
......@@ -29,7 +29,7 @@ public class ChartUtilsTest {
private static final String BAR = "bar";
private static final String ARRAY_AS_STRING =
"[{\"foo\":1.0,\"bar\":2.0},{\"foo\":3.0,\"bar\":4.0}]";
"[{\"foo\":\"1.0\",\"bar\":\"2.0\"},{\"foo\":\"3.0\",\"bar\":\"4.0\"}]";
private static final String NODE_AS_STRING =
"{\"dev1\":\"of:0000000000000001\",\"dev2\":\"of:0000000000000002\"}";
......