Committed by
Gerrit Code Review
Add annotation to ChartModel
Change-Id: I7c299ccb3a6363fac1c66ce001813e2197029e1e
Showing
4 changed files
with
101 additions
and
2 deletions
| ... | @@ -20,7 +20,10 @@ import com.google.common.collect.Lists; | ... | @@ -20,7 +20,10 @@ import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Maps; | 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Sets; | 21 | import com.google.common.collect.Sets; |
| 22 | 22 | ||
| 23 | +import java.util.ArrayList; | ||
| 23 | import java.util.Arrays; | 24 | import java.util.Arrays; |
| 25 | +import java.util.Collection; | ||
| 26 | +import java.util.HashMap; | ||
| 24 | import java.util.List; | 27 | import java.util.List; |
| 25 | import java.util.Map; | 28 | import java.util.Map; |
| 26 | import java.util.Set; | 29 | import java.util.Set; |
| ... | @@ -30,7 +33,6 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -30,7 +33,6 @@ import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | 33 | ||
| 31 | /** | 34 | /** |
| 32 | * A simple model of time series chart data. | 35 | * A simple model of time series chart data. |
| 33 | - * | ||
| 34 | * <p> | 36 | * <p> |
| 35 | * Note that this is not a full MVC type model; the expected usage pattern | 37 | * Note that this is not a full MVC type model; the expected usage pattern |
| 36 | * is to create an empty chart, add data points (by consulting the business model), | 38 | * is to create an empty chart, add data points (by consulting the business model), |
| ... | @@ -43,6 +45,7 @@ public class ChartModel { | ... | @@ -43,6 +45,7 @@ public class ChartModel { |
| 43 | private final String[] seriesArray; | 45 | private final String[] seriesArray; |
| 44 | private final List<Long> labels = Lists.newArrayList(); | 46 | private final List<Long> labels = Lists.newArrayList(); |
| 45 | private final List<DataPoint> dataPoints = Lists.newArrayList(); | 47 | private final List<DataPoint> dataPoints = Lists.newArrayList(); |
| 48 | + private final Map<String, Annot> annotations = new HashMap<>(); | ||
| 46 | 49 | ||
| 47 | /** | 50 | /** |
| 48 | * Constructs a chart model with initialized series set. | 51 | * Constructs a chart model with initialized series set. |
| ... | @@ -146,6 +149,72 @@ public class ChartModel { | ... | @@ -146,6 +149,72 @@ public class ChartModel { |
| 146 | } | 149 | } |
| 147 | 150 | ||
| 148 | /** | 151 | /** |
| 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 | + /** | ||
| 149 | * A class of data point. | 218 | * A class of data point. |
| 150 | */ | 219 | */ |
| 151 | public class DataPoint { | 220 | public class DataPoint { |
| ... | @@ -156,7 +225,7 @@ public class ChartModel { | ... | @@ -156,7 +225,7 @@ public class ChartModel { |
| 156 | * Sets the data value for the given series of this data point. | 225 | * Sets the data value for the given series of this data point. |
| 157 | * | 226 | * |
| 158 | * @param series series name | 227 | * @param series series name |
| 159 | - * @param value value to set | 228 | + * @param value value to set |
| 160 | * @return self, for chaining | 229 | * @return self, for chaining |
| 161 | */ | 230 | */ |
| 162 | public DataPoint data(String series, Double value) { | 231 | public DataPoint data(String series, Double value) { | ... | ... |
| ... | @@ -31,6 +31,8 @@ public abstract class ChartRequestHandler extends RequestHandler { | ... | @@ -31,6 +31,8 @@ public abstract class ChartRequestHandler extends RequestHandler { |
| 31 | private final String nodeName; | 31 | private final String nodeName; |
| 32 | protected static final String LABEL = "label"; | 32 | protected static final String LABEL = "label"; |
| 33 | 33 | ||
| 34 | + private static final String ANNOTS = "annots"; | ||
| 35 | + | ||
| 34 | /** | 36 | /** |
| 35 | * Constructs a chart model handler for a specific graph view. When chart | 37 | * Constructs a chart model handler for a specific graph view. When chart |
| 36 | * requests come in, the handler will generate the appropriate chart data | 38 | * requests come in, the handler will generate the appropriate chart data |
| ... | @@ -53,6 +55,7 @@ public abstract class ChartRequestHandler extends RequestHandler { | ... | @@ -53,6 +55,7 @@ public abstract class ChartRequestHandler extends RequestHandler { |
| 53 | 55 | ||
| 54 | ObjectNode rootNode = MAPPER.createObjectNode(); | 56 | ObjectNode rootNode = MAPPER.createObjectNode(); |
| 55 | rootNode.set(nodeName, ChartUtils.generateDataPointArrayNode(cm)); | 57 | rootNode.set(nodeName, ChartUtils.generateDataPointArrayNode(cm)); |
| 58 | + rootNode.set(ANNOTS, ChartUtils.generateAnnotObjectNode(cm)); | ||
| 56 | sendMessage(respType, 0, rootNode); | 59 | sendMessage(respType, 0, rootNode); |
| 57 | } | 60 | } |
| 58 | 61 | ... | ... |
| ... | @@ -46,6 +46,20 @@ public final class ChartUtils { | ... | @@ -46,6 +46,20 @@ public final class ChartUtils { |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | /** | 48 | /** |
| 49 | + * Generates a JSON object node from the annotations of the given chart model. | ||
| 50 | + * | ||
| 51 | + * @param cm the chart model | ||
| 52 | + * @return the object node representation of the annotations | ||
| 53 | + */ | ||
| 54 | + public static ObjectNode generateAnnotObjectNode(ChartModel cm) { | ||
| 55 | + ObjectNode node = MAPPER.createObjectNode(); | ||
| 56 | + for (ChartModel.Annot a : cm.getAnnotations()) { | ||
| 57 | + node.put(a.key(), a.valueAsString()); | ||
| 58 | + } | ||
| 59 | + return node; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + /** | ||
| 49 | * Generate a JSON node from the data point and given chart model. | 63 | * Generate a JSON node from the data point and given chart model. |
| 50 | * | 64 | * |
| 51 | * @param dp the data point | 65 | * @param dp the data point | ... | ... |
| ... | @@ -16,6 +16,7 @@ | ... | @@ -16,6 +16,7 @@ |
| 16 | package org.onosproject.ui.chart; | 16 | package org.onosproject.ui.chart; |
| 17 | 17 | ||
| 18 | import com.fasterxml.jackson.databind.node.ArrayNode; | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 19 | import org.junit.Assert; | 20 | import org.junit.Assert; |
| 20 | import org.junit.Test; | 21 | import org.junit.Test; |
| 21 | 22 | ||
| ... | @@ -29,6 +30,8 @@ public class ChartUtilsTest { | ... | @@ -29,6 +30,8 @@ public class ChartUtilsTest { |
| 29 | 30 | ||
| 30 | private static final String ARRAY_AS_STRING = | 31 | private static final String ARRAY_AS_STRING = |
| 31 | "[{\"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 = | ||
| 34 | + "{\"dev1\":\"of:0000000000000001\",\"dev2\":\"of:0000000000000002\"}"; | ||
| 32 | 35 | ||
| 33 | @Test | 36 | @Test |
| 34 | public void basic() { | 37 | public void basic() { |
| ... | @@ -39,4 +42,14 @@ public class ChartUtilsTest { | ... | @@ -39,4 +42,14 @@ public class ChartUtilsTest { |
| 39 | ArrayNode array = ChartUtils.generateDataPointArrayNode(cm); | 42 | ArrayNode array = ChartUtils.generateDataPointArrayNode(cm); |
| 40 | Assert.assertEquals("wrong results", ARRAY_AS_STRING, array.toString()); | 43 | Assert.assertEquals("wrong results", ARRAY_AS_STRING, array.toString()); |
| 41 | } | 44 | } |
| 45 | + | ||
| 46 | + @Test | ||
| 47 | + public void annot() { | ||
| 48 | + ChartModel cm = new ChartModel(FOO, BAR); | ||
| 49 | + cm.addAnnotation("dev1", "of:0000000000000001"); | ||
| 50 | + cm.addAnnotation("dev2", "of:0000000000000002"); | ||
| 51 | + | ||
| 52 | + ObjectNode node = ChartUtils.generateAnnotObjectNode(cm); | ||
| 53 | + Assert.assertEquals("wrong results", NODE_AS_STRING, node.toString()); | ||
| 54 | + } | ||
| 42 | } | 55 | } | ... | ... |
-
Please register or login to post a comment