Skip to content

Commit

Permalink
[HWKMETRICS-13]Adding InfluxDB java client support test
Browse files Browse the repository at this point in the history
  • Loading branch information
jkandasa committed Mar 31, 2015
1 parent 2ea68ee commit ca92ede
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 10 deletions.
Expand Up @@ -90,7 +90,7 @@
*
* @author Heiko W. Rupp
*/
@Path("/tenants/{tenantId}/influx/series")
@Path("/db/{tenantId}/series")
@Produces(APPLICATION_JSON)
@ApplicationScoped
public class InfluxSeriesHandler {
Expand Down
6 changes: 6 additions & 0 deletions rest-tests/pom.xml
Expand Up @@ -237,6 +237,12 @@
<version>0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>1.5</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Expand Up @@ -37,7 +37,7 @@ class InfluxITest extends RESTTest {

def influxQuery = 'select value from "' + timeseriesName + '" order asc'

def response = hawkularMetrics.get(path: "tenants/${tenantId}/influx/series", query: [q: influxQuery])
def response = hawkularMetrics.get(path: "db/${tenantId}/series", query: [q: influxQuery])
assertEquals(200, response.status)

assertEquals(
Expand Down Expand Up @@ -66,7 +66,7 @@ class InfluxITest extends RESTTest {

def influxQuery = 'select value from "' + timeseriesName + '"'

def response = hawkularMetrics.get(path: "tenants/${tenantId}/influx/series", query: [q: influxQuery])
def response = hawkularMetrics.get(path: "db/${tenantId}/series", query: [q: influxQuery])
assertEquals(200, response.status)

assertEquals(
Expand Down Expand Up @@ -95,7 +95,7 @@ class InfluxITest extends RESTTest {

def influxQuery = 'select mean(value) from "' + timeseriesName + '" where time > now() - 30s group by time(30s) '

def response = hawkularMetrics.get(path: "tenants/${tenantId}/influx/series", query: [q: influxQuery])
def response = hawkularMetrics.get(path: "db/${tenantId}/series", query: [q: influxQuery])
assertEquals(200, response.status)

assertEquals(
Expand All @@ -120,7 +120,7 @@ class InfluxITest extends RESTTest {

def influxQuery = 'select value from "' + timeseriesName + '" limit 2 order asc '

def response = hawkularMetrics.get(path: "tenants/${tenantId}/influx/series", query: [q: influxQuery])
def response = hawkularMetrics.get(path: "db/${tenantId}/series", query: [q: influxQuery])
assertEquals(200, response.status)

assertEquals(
Expand Down Expand Up @@ -148,7 +148,7 @@ class InfluxITest extends RESTTest {
and time < '2013-08-13' group by time(30s) '''


def response = hawkularMetrics.get(path: "tenants/${tenantId}/influx/series", query: [q: influxQuery])
def response = hawkularMetrics.get(path: "db/${tenantId}/series", query: [q: influxQuery])
assertEquals(200, response.status)

assertEquals(
Expand All @@ -170,7 +170,7 @@ class InfluxITest extends RESTTest {

def influxQuery = 'select top(value, 3) from "' + timeseriesName + '" where time > now() - 30s group by time(30s)'

def response = hawkularMetrics.get(path: "tenants/${tenantId}/influx/series", query: [q: influxQuery])
def response = hawkularMetrics.get(path: "db/${tenantId}/series", query: [q: influxQuery])
assertEquals(200, response.status)

assertEquals(
Expand All @@ -196,7 +196,7 @@ class InfluxITest extends RESTTest {

def influxQuery = 'select bottom(value, 3) from "' + timeseriesName + '" where time > now() - 30s group by time(30s)'

def response = hawkularMetrics.get(path: "tenants/${tenantId}/influx/series", query: [q: influxQuery])
def response = hawkularMetrics.get(path: "db/${tenantId}/series", query: [q: influxQuery])
assertEquals(200, response.status)

assertEquals(
Expand All @@ -223,7 +223,7 @@ class InfluxITest extends RESTTest {

def influxQuery = 'select stddev(value) from "' + timeseriesName + '" where time > now() - 30s group by time(30s)'

def response = hawkularMetrics.get(path: "tenants/${tenantId}/influx/series", query: [q: influxQuery])
def response = hawkularMetrics.get(path: "db/${tenantId}/series", query: [q: influxQuery])
assertEquals(200, response.status)

assertEquals(
Expand All @@ -241,7 +241,7 @@ class InfluxITest extends RESTTest {
}

private static void postData(String timeseriesName, DateTime start) {
def response = hawkularMetrics.post(path: "tenants/${tenantId}/influx/series", body: [
def response = hawkularMetrics.post(path: "db/${tenantId}/series", body: [
[
name: timeseriesName,
columns: ["time", "value"],
Expand Down
@@ -0,0 +1,38 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.test;

import java.util.UUID;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.junit.BeforeClass;
/**
* @author Jeeva Kandasamy
*/
public class InfluxDBTest {
static String baseURI = System.getProperty("hawkular-metrics.base-uri","127.0.0.1:8080/hawkular-metrics");
static final String DB_PREFIX = UUID.randomUUID().toString();
static InfluxDB influxDB = null;

@BeforeClass
public static void initClient() {
if(influxDB == null){
influxDB = InfluxDBFactory.connect("http://"+baseURI+"/", "hawkular", "hawkular");
}
}
}
@@ -0,0 +1,231 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.test;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.influxdb.dto.Serie;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Jeeva Kandasamy
*/
public class InfluxDatabaseITest extends InfluxDBTest{
private static String dbName = DB_PREFIX+"influxdbjavaclienttest";

/**
* Test that writing of a simple Serie works.
*/
@Test
public void testWrite() {
String timeSeries = "writeTest";
long startTime = System.currentTimeMillis();
Serie generatedSerie = getSerie(timeSeries, startTime);
influxDB.write(dbName, TimeUnit.MILLISECONDS, generatedSerie);
List<Serie> series = influxDB.query(dbName, "select * from "+timeSeries, TimeUnit.MILLISECONDS);
Assert.assertNotNull(series);
}

@Test
public void testQueryOrderedAsc(){
String timeSeries = "queryTestOrderedAsc";
long startTime = System.currentTimeMillis();
Serie generatedSerie = getSerie(timeSeries, startTime);
influxDB.write(dbName, TimeUnit.MILLISECONDS, generatedSerie);
startTime = startTime-9000L;
Serie serieSource = new Serie.Builder(timeSeries)
.columns("time", "value")
.values(new Double(startTime), 10.9)
.values(new Double(startTime+1000L), 11.9)
.values(new Double(startTime+2000L), 12.9)
.values(new Double(startTime+3000L), 13.9)
.values(new Double(startTime+4000L), 14.9)
.values(new Double(startTime+5000L), 15.9)
.values(new Double(startTime+6000L), 16.9)
.values(new Double(startTime+7000L), 17.9)
.values(new Double(startTime+8000L), 18.9)
.build();
List<Serie> series = influxDB.query(dbName,
"select value from "+timeSeries+" order asc",
TimeUnit.MILLISECONDS);
Assert.assertNotNull(series);
Assert.assertEquals(serieSource.toString(), series.get(0).toString());
}

@Test
public void testQueryOrderedDesc(){
String timeSeries = "queryTestOrderedDesc";
long startTime = System.currentTimeMillis();
Serie generatedSerie = getSerie(timeSeries, startTime);
influxDB.write(dbName, TimeUnit.MILLISECONDS, generatedSerie);
startTime = startTime-9000L;
Serie serieSource = new Serie.Builder(timeSeries)
.columns("time", "value")
.values(new Double(startTime+8000L), 18.9)
.values(new Double(startTime+7000L), 17.9)
.values(new Double(startTime+6000L), 16.9)
.values(new Double(startTime+5000L), 15.9)
.values(new Double(startTime+4000L), 14.9)
.values(new Double(startTime+3000L), 13.9)
.values(new Double(startTime+2000L), 12.9)
.values(new Double(startTime+1000L), 11.9)
.values(new Double(startTime), 10.9)
.build();
List<Serie> series = influxDB.query(dbName,
"select value from "+timeSeries+" order desc",
TimeUnit.MILLISECONDS);
Assert.assertNotNull(series);
Assert.assertEquals(serieSource.toString(), series.get(0).toString());
}

@Test
public void testQueryLimitClause(){
String timeSeries = "queryTestLimitClause";
long startTime = System.currentTimeMillis();
Serie generatedSerie = getSerie(timeSeries, startTime);
influxDB.write(dbName, TimeUnit.MILLISECONDS, generatedSerie);
startTime = startTime-9000L;
Serie serieSource = new Serie.Builder(timeSeries)
.columns("time", "value")
.values(new Double(startTime), 10.9)
.values(new Double(startTime+1000L), 11.9)
.values(new Double(startTime+2000L), 12.9)
.build();
List<Serie> series = influxDB.query(dbName,
"select value from "+timeSeries+" limit 3 order asc",
TimeUnit.MILLISECONDS);
Assert.assertNotNull(series);
Assert.assertEquals(serieSource.toString(), series.get(0).toString());
}

@Test
public void testQueryTop(){
String timeSeries = "queryTestTop";
long startTime = System.currentTimeMillis();
Serie generatedSerie = getSerie(timeSeries, startTime);
influxDB.write(dbName, TimeUnit.MILLISECONDS, generatedSerie);
startTime = startTime-9000L;
Serie serieSource = new Serie.Builder(timeSeries)
.columns("time", "top")
.values(new Double(startTime+8000L), 18.9)
.values(new Double(startTime+7000L), 17.9)
.values(new Double(startTime+6000L), 16.9)
.build();
List<Serie> series = influxDB.query(dbName,
"select top(value, 3) from "+timeSeries
+" where time > now() - 30s group by time(30s)",
TimeUnit.MILLISECONDS);
Assert.assertNotNull(series);
Assert.assertEquals(serieSource.toString(), series.get(0).toString());
}

@Test
public void testQueryBottom(){
String timeSeries = "queryTestBottom";
long startTime = System.currentTimeMillis();
Serie generatedSerie = getSerie(timeSeries, startTime);
influxDB.write(dbName, TimeUnit.MILLISECONDS, generatedSerie);
startTime = startTime-9000L;
Serie serieSource = new Serie.Builder(timeSeries)
.columns("time", "bottom")
.values(new Double(startTime), 10.9)
.values(new Double(startTime+1000L), 11.9)
.values(new Double(startTime+2000L), 12.9)
.build();
List<Serie> series = influxDB.query(dbName,
"select bottom(value, 3) from "+timeSeries
+" where time > now() - 30s group by time(30s)",
TimeUnit.MILLISECONDS);
Assert.assertNotNull(series);
Assert.assertEquals(serieSource.toString(), series.get(0).toString());
}

@Test
public void testQueryStddev(){
String timeSeries = "queryTestStddev";
long startTime = System.currentTimeMillis();
Serie generatedSerie = getSerie(timeSeries, startTime);
influxDB.write(dbName, TimeUnit.MILLISECONDS, generatedSerie);
startTime = startTime-9000L;
Serie serieSource = new Serie.Builder(timeSeries)
.columns("time", "stddev")
.values(new Double(startTime+8000L), 2.7386127875258297)
.build();
List<Serie> series = influxDB.query(dbName,
"select stddev(value) from "+timeSeries
+" where time > now() - 30s group by time(30s)",
TimeUnit.MILLISECONDS);
Assert.assertNotNull(series);
Assert.assertEquals(serieSource.toString(), series.get(0).toString());
}

@Test
public void testQueryMean(){
String timeSeries = "queryTestMean";
long startTime = System.currentTimeMillis();
Serie generatedSerie = getSerie(timeSeries, startTime);
influxDB.write(dbName, TimeUnit.MILLISECONDS, generatedSerie);
startTime = startTime-9000L;
Serie serieSource = new Serie.Builder(timeSeries)
.columns("time", "mean")
.values(new Double(startTime+8000L), 14.9D)
.build();
List<Serie> series = influxDB.query(dbName,
"select mean(value) from "+timeSeries
+" where time > now() - 30s group by time(30s)",
TimeUnit.MILLISECONDS);
Assert.assertNotNull(series);
Assert.assertEquals(serieSource.toString(), series.get(0).toString());
}

@Test
public void testQuerySum(){
String timeSeries = "queryTestSum";
long startTime = System.currentTimeMillis();
Serie generatedSerie = getSerie(timeSeries, startTime);
influxDB.write(dbName, TimeUnit.MILLISECONDS, generatedSerie);
startTime = startTime-9000L;
Serie serieSource = new Serie.Builder(timeSeries)
.columns("time", "sum")
.values(new Double(startTime+8000L), 134.1D)
.build();
List<Serie> series = influxDB.query(dbName,
"select sum(value) from "+timeSeries
+" where time > now() - 30s group by time(30s)",
TimeUnit.MILLISECONDS);
Assert.assertNotNull(series);
Assert.assertEquals(serieSource.toString(), series.get(0).toString());
}

private static Serie getSerie(String timeSeries, long startTime){
startTime = startTime-9000L;//Minus N milliseconds from 'start time' based on number of rows going to write.
Serie serie = new Serie.Builder(timeSeries)
.columns("time", "value")
.values(startTime, 10.9)
.values(startTime+1000L, 11.9)
.values(startTime+2000L, 12.9)
.values(startTime+3000L, 13.9)
.values(startTime+4000L, 14.9)
.values(startTime+5000L, 15.9)
.values(startTime+6000L, 16.9)
.values(startTime+7000L, 17.9)
.values(startTime+8000L, 18.9)
.build();
return serie;
}
}

0 comments on commit ca92ede

Please sign in to comment.