Skip to content

Commit

Permalink
Merge pull request #577 from GeorgeJahad/george/fix_mplot_queries
Browse files Browse the repository at this point in the history
George/fix mplot queries
  • Loading branch information
GeorgeJahad committed Dec 16, 2015
2 parents 9874182 + 21033d2 commit e89a1fa
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 10 deletions.
Expand Up @@ -157,6 +157,7 @@ public void run() {

if (executionContext.wasSuccessful()) {
this.scheduleCtx.clearFromRunning(parentSlotKey);
log.info("Successful completion of rollups for (gran,slot,shard) {} in {}", new Object[] {parentSlotKey, System.currentTimeMillis() - waitStart});
} else {
log.error("Performing BasicRollups for {} failed", parentSlotKey);
this.scheduleCtx.pushBackToScheduled(parentSlotKey, false);
Expand Down
Expand Up @@ -51,7 +51,7 @@ public void testMultiplotQuery() throws Exception {
" {\n" +
" \"numPoints\": 1,\n" +
" \"timestamp\": 1382400000,\n" +
" \"average\": 397\n" +
" \"latest\": 397\n" +
" }\n" +
" ],\n" +
" \"type\": \"number\"\n" +
Expand All @@ -63,7 +63,7 @@ public void testMultiplotQuery() throws Exception {
" {\n" +
" \"numPoints\": 1,\n" +
" \"timestamp\": 1382400000,\n" +
" \"average\": 56\n" +
" \"latest\": 56\n" +
" }\n" +
" ],\n" +
" \"type\": \"number\"\n" +
Expand Down
Expand Up @@ -36,12 +36,14 @@ public JSONObject transformRollupData(Map<Locator, MetricData> metricData, Set<M
final JSONObject globalJSON = new JSONObject();
final JSONArray metricsArray = new JSONArray();


for (Map.Entry<Locator, MetricData> one : metricData.entrySet()) {
final JSONObject singleMetricJSON = new JSONObject();
singleMetricJSON.put("metric", one.getKey().getMetricName());
singleMetricJSON.put("unit", one.getValue().getUnit() == null ? Util.UNKNOWN : one.getValue().getUnit());
singleMetricJSON.put("type", one.getValue().getType());
JSONArray values = transformDataToJSONArray(one.getValue(), filterStats);
Set<MetricStat> oneFilterStats = fixFilterStats(one.getValue(), filterStats);
JSONArray values = transformDataToJSONArray(one.getValue(), oneFilterStats);
singleMetricJSON.put("data", values);
metricsArray.add(singleMetricJSON);
}
Expand Down
Expand Up @@ -96,6 +96,17 @@ public static Points<BluefloodCounterRollup> generateFakeCounterRollupPoints() {
return points;
}

public static Points<BluefloodEnumRollup> generateFakeEnumRollupPoints() {
Points<BluefloodEnumRollup> points = new Points<BluefloodEnumRollup>();
long startTime = 1234567L;
for (int i = 0; i < 5; i++) {
long timeNow = startTime + i*1000;
Points.Point<BluefloodEnumRollup> point = new Points.Point<BluefloodEnumRollup>(timeNow, new BluefloodEnumRollup().withEnumValue("enum_value_" + i));
points.add(point);
}
return points;
}

public static Points<BluefloodSetRollup> generateFakeSetRollupPoints() {
Points<BluefloodSetRollup> points = new Points<BluefloodSetRollup>();
long startTime = 1234567L;
Expand Down
Expand Up @@ -31,13 +31,8 @@

public class JSONBasicRollupsOutputSerializer implements BasicRollupsOutputSerializer<JSONObject> {
private static final Logger log = LoggerFactory.getLogger(JSONBasicRollupsOutputSerializer.class);

@Override
public JSONObject transformRollupData(MetricData metricData, Set<MetricStat> filterStats)
throws SerializationException {
final JSONObject globalJSON = new JSONObject();
final JSONObject metaObject = new JSONObject();


protected Set<MetricStat> fixFilterStats(MetricData metricData, Set<MetricStat> filterStats) {
// if no stats were entered, figure out what type we are dealing with and select out default stats.
if (metricData.getData().getPoints().size() > 0 && filterStats == PlotRequestParser.DEFAULT_STATS) {
Class dataClass = metricData.getData().getDataClass();
Expand All @@ -56,6 +51,16 @@ else if (dataClass.equals(BluefloodEnumRollup.class))
// else, I got nothing.
}

return filterStats;
}

@Override
public JSONObject transformRollupData(MetricData metricData, Set<MetricStat> filterStats)
throws SerializationException {
final JSONObject globalJSON = new JSONObject();
final JSONObject metaObject = new JSONObject();
filterStats = fixFilterStats(metricData, filterStats);

final JSONArray valuesArray = transformDataToJSONArray(metricData, filterStats);

metaObject.put("count", valuesArray.size());
Expand Down
Expand Up @@ -17,6 +17,7 @@
package com.rackspacecloud.blueflood.outputs.serializers;

import com.rackspacecloud.blueflood.outputs.formats.MetricData;
import com.rackspacecloud.blueflood.outputs.utils.PlotRequestParser;
import com.rackspacecloud.blueflood.types.Locator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
Expand Down Expand Up @@ -65,4 +66,32 @@ public void testBatchedMetricsSerialization() throws Exception {
Assert.assertTrue(data != null);
}
}

@Test
public void testBatchedEnumMetricsSerialization() throws Exception {
final BatchedMetricsJSONOutputSerializer serializer = new BatchedMetricsJSONOutputSerializer();

final Map<Locator, MetricData> metrics = new HashMap<Locator, MetricData>();
for (int i = 0; i < 2; i++) {
final MetricData metricData = new MetricData(FakeMetricDataGenerator.generateFakeEnumRollupPoints(), "unknown",
MetricData.Type.ENUM);

metrics.put(Locator.createLocatorFromPathComponents(tenantId, String.valueOf(i)), metricData);
}

JSONObject jsonMetrics = serializer.transformRollupData(metrics, PlotRequestParser.DEFAULT_STATS);
Assert.assertTrue(jsonMetrics.get("metrics") != null);
JSONArray jsonMetricsArray = (JSONArray) jsonMetrics.get("metrics");

Iterator<JSONObject> metricsObjects = jsonMetricsArray.iterator();
Assert.assertTrue(metricsObjects.hasNext());

while (metricsObjects.hasNext()) {
JSONObject singleMetricObject = metricsObjects.next();
Assert.assertTrue(singleMetricObject.get("unit").equals("unknown"));
Assert.assertTrue(singleMetricObject.get("type").equals("enum"));
JSONArray data = (JSONArray) singleMetricObject.get("data");
Assert.assertTrue(data != null);
}
}
}
Expand Up @@ -31,6 +31,7 @@

import java.util.HashSet;
import java.util.Set;
import java.util.Map;

public class JSONBasicRollupOutputSerializerTest {
private final Set<MetricStat> filterStats;
Expand Down Expand Up @@ -161,6 +162,33 @@ public void testCounters() throws Exception {
}
}

@Test
public void testEnums() throws Exception {
final JSONBasicRollupsOutputSerializer serializer = new JSONBasicRollupsOutputSerializer();
final MetricData metricData = new MetricData(
FakeMetricDataGenerator.generateFakeEnumRollupPoints(),
"unknown",
MetricData.Type.ENUM);
JSONObject metricDataJSON = serializer.transformRollupData(metricData, PlotRequestParser.DEFAULT_STATS);
final JSONArray data = (JSONArray)metricDataJSON.get("values");

Assert.assertEquals(5, data.size());
for (int i = 0; i < data.size(); i++) {
final JSONObject dataJSON = (JSONObject)data.get(i);
final Map<String,Long> evJSON = (Map<String, Long>)dataJSON.get("enum_values");
Set<String> keys = evJSON.keySet();

Assert.assertEquals(1, keys.size());
for (String key : keys) {
Assert.assertEquals(key, "enum_value_" + i);
Assert.assertEquals((long)evJSON.get(key), 1);
}
Assert.assertNotNull(dataJSON.get("numPoints"));
Assert.assertEquals(1, dataJSON.get("numPoints"));
Assert.assertEquals(MetricData.Type.ENUM, dataJSON.get("type"));
}
}

@Test
public void testGauges() throws Exception {
final JSONBasicRollupsOutputSerializer serializer = new JSONBasicRollupsOutputSerializer();
Expand Down

0 comments on commit e89a1fa

Please sign in to comment.