Search in sources :

Example 1 with CubeQuery

use of io.cdap.cdap.api.dataset.lib.cube.CubeQuery in project cdap by caskdata.

the class AbstractCubeTest method testMetricsAggregationOptionSum.

@Test
public void testMetricsAggregationOptionSum() throws Exception {
    // two aggregation groups
    Aggregation agg1 = new DefaultAggregation(ImmutableList.of("dim1", "dim2", "dim3"), ImmutableList.of("dim1"));
    Aggregation agg2 = new DefaultAggregation(ImmutableList.of("dim1", "dim2"), ImmutableList.of("dim1"));
    int resolution = 1;
    Cube cube = getCube("testAggOptionSum", new int[] { resolution }, ImmutableMap.of("agg1", agg1, "agg2", agg2));
    Map<String, String> agg1Dims = new LinkedHashMap<>();
    agg1Dims.put("dim1", "tag1");
    agg1Dims.put("dim2", "tag2");
    agg1Dims.put("dim3", "tag3");
    Map<String, String> agg2Dims = new LinkedHashMap<>();
    agg2Dims.put("dim1", "tag1");
    agg2Dims.put("dim2", "tag4");
    // write 100 data points to agg1
    for (int i = 1; i <= 100; i++) {
        writeInc(cube, "metric1", i, 1, agg1Dims);
        writeInc(cube, "metric2", i, 2, agg1Dims);
    }
    // write 50 data points to agg2
    for (int i = 1; i <= 50; i++) {
        writeInc(cube, "metric1", i, 3, agg2Dims);
    }
    // test limit must be greater than 0
    CubeQuery query = new CubeQuery(null, 0, 200, 1, 0, ImmutableMap.of("metric1", AggregationFunction.SUM), agg1Dims, Collections.emptyList(), AggregationOption.SUM, null);
    try {
        cube.query(query);
        Assert.fail();
    } catch (Exception e) {
    // expected
    }
    query = new CubeQuery(null, 0, 200, 1, -10, ImmutableMap.of("metric1", AggregationFunction.SUM), agg1Dims, Collections.emptyList(), AggregationOption.SUM, null);
    try {
        cube.query(query);
        Assert.fail();
    } catch (Exception e) {
    // expected
    }
    // test a limit greater than the number data points, all the data points should be returned
    query = new CubeQuery(null, 0, 200, 1, 200, ImmutableMap.of("metric1", AggregationFunction.SUM, "metric2", AggregationFunction.SUM), agg1Dims, Collections.emptyList(), AggregationOption.SUM, null);
    List<TimeSeries> result = new ArrayList<>(cube.query(query));
    Assert.assertEquals(2, result.size());
    verifySumAggregation(result.get(0), "metric1", 100, 1, 1, 0, 0);
    verifySumAggregation(result.get(1), "metric2", 100, 2, 1, 0, 0);
    // test aggregation option with sum for metric1 and metric2 for agg1, 5 data points for agg1 should get returned
    // for both metric1 and metric2
    query = new CubeQuery(null, 0, 200, 1, 5, ImmutableMap.of("metric1", AggregationFunction.SUM, "metric2", AggregationFunction.SUM), agg1Dims, Collections.emptyList(), AggregationOption.SUM, null);
    result = new ArrayList<>(cube.query(query));
    Assert.assertEquals(2, result.size());
    // metric1 increment by 1 per second, so sum will be 100/5=20, metric2 increment by 2 per second, so sum will be
    // 200/5=40
    verifySumAggregation(result.get(0), "metric1", 5, 20, 20, 0, 0);
    verifySumAggregation(result.get(1), "metric2", 5, 40, 20, 0, 0);
    // test aggregation option with sum for metric1 with tag name dim1, it should return two time series for agg1 and
    // agg2 for metric1, each with 5 data points
    query = new CubeQuery(null, 0, 200, 1, 5, ImmutableMap.of("metric1", AggregationFunction.SUM), ImmutableMap.of("dim1", "tag1"), ImmutableList.of("dim2"), AggregationOption.SUM, null);
    result = new ArrayList<>(cube.query(query));
    Assert.assertEquals(2, result.size());
    // agg1 gets increment by 1 for 100 seconds, so sum will be 100/5=20, agg2 gets increment by 3 for 50 seconds, so
    // sum will be 3*50/5=30
    verifySumAggregation(result.get(0), "metric1", 5, 30, 10, 0, 0);
    verifySumAggregation(result.get(1), "metric1", 5, 20, 20, 0, 0);
    // test metric1 with count 9, this will have a remainder 100%9=1, so there will be 9 aggregated data points, each
    // with partition size 11
    query = new CubeQuery(null, 0, 200, 1, 9, ImmutableMap.of("metric1", AggregationFunction.SUM), agg1Dims, Collections.emptyList(), AggregationOption.SUM, null);
    result = new ArrayList<>(cube.query(query));
    Assert.assertEquals(1, result.size());
    // the rest data points have sum 11
    verifySumAggregation(result.get(0), "metric1", 9, 11, 11, 1, 1);
    // test metric1 with count 70, this will have a remainder 100%70=30, so the result will have last 70 data points,
    // the first 30 data points will be ignored
    query = new CubeQuery(null, 0, 200, 1, 70, ImmutableMap.of("metric1", AggregationFunction.SUM), agg1Dims, Collections.emptyList(), AggregationOption.SUM, null);
    result = new ArrayList<>(cube.query(query));
    Assert.assertEquals(1, result.size());
    // the rest data points have sum 11
    verifySumAggregation(result.get(0), "metric1", 70, 1, 1, 30, 30);
}
Also used : TimeSeries(io.cdap.cdap.api.dataset.lib.cube.TimeSeries) ArrayList(java.util.ArrayList) CubeQuery(io.cdap.cdap.api.dataset.lib.cube.CubeQuery) LinkedHashMap(java.util.LinkedHashMap) Cube(io.cdap.cdap.api.dataset.lib.cube.Cube) Test(org.junit.Test)

Example 2 with CubeQuery

use of io.cdap.cdap.api.dataset.lib.cube.CubeQuery in project cdap by caskdata.

the class TestAppWithCube method testApp.

@Category(SlowTests.class)
@Test
public void testApp() throws Exception {
    // Deploy the application
    ApplicationManager appManager = deployApplication(AppWithCube.class);
    ServiceManager serviceManager = appManager.getServiceManager(AppWithCube.SERVICE_NAME).start();
    try {
        serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
        URL url = serviceManager.getServiceURL();
        long tsInSec = System.currentTimeMillis() / 1000;
        // round to a minute for testing minute resolution
        tsInSec = (tsInSec / 60) * 60;
        // add couple facts
        add(url, ImmutableList.of(new CubeFact(tsInSec).addDimensionValue("user", "alex").addDimensionValue("action", "click").addMeasurement("count", MeasureType.COUNTER, 1)));
        add(url, ImmutableList.of(new CubeFact(tsInSec).addDimensionValue("user", "alex").addDimensionValue("action", "click").addMeasurement("count", MeasureType.COUNTER, 1), new CubeFact(tsInSec + 1).addDimensionValue("user", "alex").addDimensionValue("action", "back").addMeasurement("count", MeasureType.COUNTER, 1), new CubeFact(tsInSec + 2).addDimensionValue("user", "alex").addDimensionValue("action", "click").addMeasurement("count", MeasureType.COUNTER, 1)));
        // search for tags
        Collection<DimensionValue> tags = searchDimensionValue(url, new CubeExploreQuery(tsInSec - 60, tsInSec + 60, 1, 100, new ArrayList<DimensionValue>()));
        Assert.assertEquals(1, tags.size());
        DimensionValue tv = tags.iterator().next();
        Assert.assertEquals("user", tv.getName());
        Assert.assertEquals("alex", tv.getValue());
        tags = searchDimensionValue(url, CubeExploreQuery.builder().from().resolution(1, TimeUnit.SECONDS).where().dimension("user", "alex").timeRange(tsInSec - 60, tsInSec + 60).limit(100).build());
        Assert.assertEquals(2, tags.size());
        Iterator<DimensionValue> iterator = tags.iterator();
        tv = iterator.next();
        Assert.assertEquals("action", tv.getName());
        Assert.assertEquals("back", tv.getValue());
        tv = iterator.next();
        Assert.assertEquals("action", tv.getName());
        Assert.assertEquals("click", tv.getValue());
        // search for measures
        Collection<String> measures = searchMeasure(url, new CubeExploreQuery(tsInSec - 60, tsInSec + 60, 1, 100, ImmutableList.of(new DimensionValue("user", "alex"))));
        Assert.assertEquals(1, measures.size());
        String measure = measures.iterator().next();
        Assert.assertEquals("count", measure);
        // query for data
        // 1-sec resolution
        Collection<TimeSeries> data = query(url, CubeQuery.builder().select().measurement("count", AggregationFunction.SUM).from(null).resolution(1, TimeUnit.SECONDS).where().dimension("action", "click").timeRange(tsInSec - 60, tsInSec + 60).limit(100).build());
        Assert.assertEquals(1, data.size());
        TimeSeries series = data.iterator().next();
        List<TimeValue> timeValues = series.getTimeValues();
        Assert.assertEquals(2, timeValues.size());
        TimeValue timeValue = timeValues.get(0);
        Assert.assertEquals(tsInSec, timeValue.getTimestamp());
        Assert.assertEquals(2, timeValue.getValue());
        timeValue = timeValues.get(1);
        Assert.assertEquals(tsInSec + 2, timeValue.getTimestamp());
        Assert.assertEquals(1, timeValue.getValue());
        // 60-sec resolution
        data = query(url, new CubeQuery(null, tsInSec - 60, tsInSec + 60, 60, 100, ImmutableMap.of("count", AggregationFunction.SUM), ImmutableMap.of("action", "click"), new ArrayList<String>(), null, null));
        Assert.assertEquals(1, data.size());
        series = data.iterator().next();
        timeValues = series.getTimeValues();
        Assert.assertEquals(1, timeValues.size());
        timeValue = timeValues.get(0);
        Assert.assertEquals(tsInSec, timeValue.getTimestamp());
        Assert.assertEquals(3, timeValue.getValue());
    } finally {
        serviceManager.stop();
        serviceManager.waitForStopped(10, TimeUnit.SECONDS);
    }
}
Also used : ApplicationManager(io.cdap.cdap.test.ApplicationManager) TimeSeries(io.cdap.cdap.api.dataset.lib.cube.TimeSeries) ArrayList(java.util.ArrayList) CubeQuery(io.cdap.cdap.api.dataset.lib.cube.CubeQuery) URL(java.net.URL) CubeFact(io.cdap.cdap.api.dataset.lib.cube.CubeFact) DimensionValue(io.cdap.cdap.api.dataset.lib.cube.DimensionValue) ServiceManager(io.cdap.cdap.test.ServiceManager) CubeExploreQuery(io.cdap.cdap.api.dataset.lib.cube.CubeExploreQuery) TimeValue(io.cdap.cdap.api.dataset.lib.cube.TimeValue) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 3 with CubeQuery

use of io.cdap.cdap.api.dataset.lib.cube.CubeQuery in project cdap by caskdata.

the class AbstractCubeTest method testMetricsAggregationOptionLatest.

@Test
public void testMetricsAggregationOptionLatest() throws Exception {
    Aggregation agg = new DefaultAggregation(ImmutableList.of("dim1", "dim2", "dim3"), ImmutableList.of("dim1"));
    int resolution = 1;
    Cube cube = getCube("testAggOptionLatest", new int[] { resolution }, ImmutableMap.of("agg", agg));
    Map<String, String> aggDims = new LinkedHashMap<>();
    aggDims.put("dim1", "tag1");
    aggDims.put("dim2", "tag2");
    aggDims.put("dim3", "tag3");
    // write 100 data points to agg
    for (int i = 1; i <= 100; i++) {
        writeGauge(cube, "metric1", i, i, aggDims);
    }
    // query for latest, should have the latest value for each interval, 20, 40, 60, 80, 100
    CubeQuery query = new CubeQuery(null, 0, 200, 1, 5, ImmutableMap.of("metric1", AggregationFunction.SUM), aggDims, Collections.emptyList(), AggregationOption.LATEST, null);
    List<TimeSeries> result = new ArrayList<>(cube.query(query));
    Assert.assertEquals(1, result.size());
    List<TimeValue> timeValues = result.get(0).getTimeValues();
    for (int i = 0; i < timeValues.size(); i++) {
        Assert.assertEquals(20 * (i + 1), timeValues.get(i).getValue());
    }
}
Also used : TimeSeries(io.cdap.cdap.api.dataset.lib.cube.TimeSeries) ArrayList(java.util.ArrayList) CubeQuery(io.cdap.cdap.api.dataset.lib.cube.CubeQuery) LinkedHashMap(java.util.LinkedHashMap) Cube(io.cdap.cdap.api.dataset.lib.cube.Cube) TimeValue(io.cdap.cdap.api.dataset.lib.cube.TimeValue) Test(org.junit.Test)

Example 4 with CubeQuery

use of io.cdap.cdap.api.dataset.lib.cube.CubeQuery in project cdap by caskdata.

the class AbstractCubeTest method verifyCountQuery.

private void verifyCountQuery(Cube cube, String aggregation, long startTs, long endTs, int resolution, String measureName, AggregationFunction aggFunction, Map<String, String> dimValues, List<String> groupByDims, Collection<TimeSeries> expected, Interpolator interpolator) throws Exception {
    CubeQuery query = CubeQuery.builder().select().measurement(measureName, aggFunction).from(aggregation).resolution(resolution, TimeUnit.SECONDS).where().dimensions(dimValues).timeRange(startTs, endTs).groupBy().dimensions(groupByDims).limit(Integer.MAX_VALUE).interpolator(interpolator).build();
    Collection<TimeSeries> result = cube.query(query);
    Assert.assertEquals(String.format("expected: %s, found: %s", expected, result), expected.size(), result.size());
    Assert.assertTrue(String.format("expected: %s, found: %s", expected, result), expected.containsAll(result));
}
Also used : TimeSeries(io.cdap.cdap.api.dataset.lib.cube.TimeSeries) CubeQuery(io.cdap.cdap.api.dataset.lib.cube.CubeQuery)

Aggregations

CubeQuery (io.cdap.cdap.api.dataset.lib.cube.CubeQuery)4 TimeSeries (io.cdap.cdap.api.dataset.lib.cube.TimeSeries)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Cube (io.cdap.cdap.api.dataset.lib.cube.Cube)2 TimeValue (io.cdap.cdap.api.dataset.lib.cube.TimeValue)2 LinkedHashMap (java.util.LinkedHashMap)2 CubeExploreQuery (io.cdap.cdap.api.dataset.lib.cube.CubeExploreQuery)1 CubeFact (io.cdap.cdap.api.dataset.lib.cube.CubeFact)1 DimensionValue (io.cdap.cdap.api.dataset.lib.cube.DimensionValue)1 ApplicationManager (io.cdap.cdap.test.ApplicationManager)1 ServiceManager (io.cdap.cdap.test.ServiceManager)1 URL (java.net.URL)1 Category (org.junit.experimental.categories.Category)1