use of com.srotya.sidewinder.core.api.grafana.TargetSeries in project sidewinder by srotya.
the class TestMiscUtils method testExtractTargetFromQuery.
@Test
public void testExtractTargetFromQuery() {
TargetSeries series = MiscUtils.extractTargetFromQuery("cpu.value.tes=2|tes=3");
assertEquals("cpu", series.getMeasurementName());
assertEquals("value", series.getFieldName());
try {
series = MiscUtils.extractTargetFromQuery("cpuvalue|tes=3");
fail("Invalid request must throw an exception");
} catch (BadRequestException e) {
}
}
use of com.srotya.sidewinder.core.api.grafana.TargetSeries in project sidewinder by srotya.
the class DatabaseOpsApi method querySeries.
@Path("/{" + DB_NAME + "}/query")
@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.TEXT_PLAIN })
public String querySeries(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, String query) {
try {
String[] queryParts = query.split("<=?");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
long endTs = System.currentTimeMillis();
long startTs = endTs;
String startTime = queryParts[0];
String endTime = queryParts[2];
if (startTime.contains("now")) {
String[] split = startTime.split("-");
int offset = Integer.parseInt(split[1].charAt(0) + "");
startTs = startTs - (offset * 3600 * 1000);
} else if (startTime.matches("\\d+")) {
startTs = Long.parseLong(startTime);
endTs = Long.parseLong(endTime);
} else {
startTs = sdf.parse(startTime).getTime();
endTs = sdf.parse(endTime).getTime();
}
// cpu.load.host=v1.domain=test\.com=>derivative,10,mean
TargetSeries tagSeries = MiscUtils.extractTargetFromQuery(query);
List<Series> points = storageEngine.queryDataPoints(dbName, tagSeries.getMeasurementName(), tagSeries.getFieldName(), startTs, endTs, tagSeries.getTagFilter(), null, tagSeries.getAggregationFunction());
return new Gson().toJson(points);
} catch (ItemNotFoundException e) {
throw new NotFoundException(e);
} catch (BadRequestException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
throw new InternalServerErrorException(e);
}
}
use of com.srotya.sidewinder.core.api.grafana.TargetSeries in project sidewinder by srotya.
the class MiscUtils method extractTargetFromQuery.
public static TargetSeries extractTargetFromQuery(String query) {
if (query == null || query.isEmpty()) {
return null;
}
String[] queryParts = query.split("<=?");
if (queryParts.length > 1) {
query = queryParts[1];
}
String[] parts = query.split("=>");
// select part
query = parts[0];
String[] splits = query.split("\\.");
if (splits.length < 2) {
throw new BadRequestException("Invalid query string:" + query + ". Must contain measurement and value field name");
}
String measurementName = splits[0];
String valueFieldName = splits[1];
TagFilter tagFilter = null;
if (splits.length >= 3) {
try {
tagFilter = buildTagFilter(splits[2]);
} catch (InvalidFilterException e) {
throw new BadRequestException(e);
}
}
Function aggregationFunction = null;
if (parts.length > 1) {
try {
aggregationFunction = createFunctionChain(parts, 1);
} catch (Exception e) {
throw new BadRequestException(e);
}
}
return new TargetSeries(measurementName, valueFieldName, tagFilter, aggregationFunction, false);
}
Aggregations