Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support map queryresult groupby #345

Merged
merged 2 commits into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/main/java/org/influxdb/impl/InfluxDBResultMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -147,19 +148,32 @@ String getMeasurementName(final Class<?> clazz) {

<T> List<T> parseSeriesAs(final QueryResult.Series series, final Class<T> clazz, final List<T> result) {
int columnSize = series.getColumns().size();
ConcurrentMap<String, Field> colNameAndFieldMap = CLASS_FIELD_CACHE.get(clazz.getName());
try {
T object = null;
for (List<Object> row : series.getValues()) {
for (int i = 0; i < columnSize; i++) {
String resultColumnName = series.getColumns().get(i);
Field correspondingField = CLASS_FIELD_CACHE.get(clazz.getName()).get(resultColumnName);
Field correspondingField = colNameAndFieldMap.get(series.getColumns().get(i)/*InfluxDB columnName*/);
if (correspondingField != null) {
if (object == null) {
object = clazz.newInstance();
}
setFieldValue(object, correspondingField, row.get(i));
}
}
// When the "GROUP BY" clause is used, "tags" are returned as Map<String,String> and
// accordingly with InfluxDB documentation
// https://docs.influxdata.com/influxdb/v1.2/concepts/glossary/#tag-value
// "tag" values are always String.
if (series.getTags() != null && !series.getTags().isEmpty()) {
for (Entry<String, String> entry : series.getTags().entrySet()) {
Field correspondingField = colNameAndFieldMap.get(entry.getKey()/*InfluxDB columnName*/);
if (correspondingField != null) {
// I don't think it is possible to reach here without a valid "object"
setFieldValue(object, correspondingField, entry.getValue());
}
}
}
if (object != null) {
result.add(object);
object = null;
Expand Down Expand Up @@ -233,8 +247,7 @@ <T> boolean fieldValueModified(final Class<?> fieldType, final Field field, fina
}

<T> boolean fieldValueForPrimitivesModified(final Class<?> fieldType, final Field field, final T object,
final Object value)
throws IllegalArgumentException, IllegalAccessException {
final Object value) throws IllegalArgumentException, IllegalAccessException {
if (double.class.isAssignableFrom(fieldType)) {
field.setDouble(object, ((Double) value).doubleValue());
return true;
Expand All @@ -255,8 +268,7 @@ <T> boolean fieldValueForPrimitivesModified(final Class<?> fieldType, final Fiel
}

<T> boolean fieldValueForPrimitiveWrappersModified(final Class<?> fieldType, final Field field, final T object,
final Object value)
throws IllegalArgumentException, IllegalAccessException {
final Object value) throws IllegalArgumentException, IllegalAccessException {
if (Double.class.isAssignableFrom(fieldType)) {
field.set(object, value);
return true;
Expand Down
89 changes: 88 additions & 1 deletion src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import java.time.Instant;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

Expand Down Expand Up @@ -243,7 +245,61 @@ public void testToPOJO_SeriesFromQueryResultIsNull() {
// Then...
assertTrue("there must NO entry in the result list", myList.isEmpty());
}


@Test
public void testToPOJO_QueryResultCreatedByGroupByClause() {
// Given...
mapper.cacheMeasurementClass(GroupByCarrierDeviceOS.class);

List<String> columnList = Arrays.asList("time", "median", "min", "max");

// InfluxDB client returns the time representation as Double.
Double now = Long.valueOf(System.currentTimeMillis()).doubleValue();

List<Object> firstSeriesResult = Arrays.asList(now, new Double("233.8"), new Double("0.0"),
new Double("3090744.0"));
// When the "GROUP BY" clause is used, "tags" are returned as Map<String,String>
Map<String, String> firstSeriesTagMap = new HashMap<>();
firstSeriesTagMap.put("CARRIER", "000/00");
firstSeriesTagMap.put("DEVICE_OS_VERSION", "4.4.2");

List<Object> secondSeriesResult = Arrays.asList(now, new Double("552.0"), new Double("135.0"),
new Double("267705.0"));
Map<String, String> secondSeriesTagMap = new HashMap<>();
secondSeriesTagMap.put("CARRIER", "000/01");
secondSeriesTagMap.put("DEVICE_OS_VERSION", "9.3.5");

QueryResult.Series firstSeries = new QueryResult.Series();
firstSeries.setColumns(columnList);
firstSeries.setValues(Arrays.asList(firstSeriesResult));
firstSeries.setTags(firstSeriesTagMap);
firstSeries.setName("tb_network");

QueryResult.Series secondSeries = new QueryResult.Series();
secondSeries.setColumns(columnList);
secondSeries.setValues(Arrays.asList(secondSeriesResult));
secondSeries.setTags(secondSeriesTagMap);
secondSeries.setName("tb_network");

QueryResult.Result internalResult = new QueryResult.Result();
internalResult.setSeries(Arrays.asList(firstSeries, secondSeries));

QueryResult queryResult = new QueryResult();
queryResult.setResults(Arrays.asList(internalResult));

// When...
List<GroupByCarrierDeviceOS> myList = mapper.toPOJO(queryResult, GroupByCarrierDeviceOS.class);

// Then...
GroupByCarrierDeviceOS firstGroupByEntry = myList.get(0);
assertEquals("field 'carrier' does not match", "000/00", firstGroupByEntry.carrier);
assertEquals("field 'deviceOsVersion' does not match", "4.4.2", firstGroupByEntry.deviceOsVersion);

GroupByCarrierDeviceOS secondGroupByEntry = myList.get(1);
assertEquals("field 'carrier' does not match", "000/01", secondGroupByEntry.carrier);
assertEquals("field 'deviceOsVersion' does not match", "9.3.5", secondGroupByEntry.deviceOsVersion);
}

@Measurement(name = "CustomMeasurement")
static class MyCustomMeasurement {

Expand Down Expand Up @@ -297,4 +353,35 @@ static class MyPojoWithUnsupportedField {
@Column(name = "bar")
private Date myDate;
}

/**
* Class created based on example from https://github.com/influxdata/influxdb-java/issues/343
*/
@Measurement(name = "tb_network")
static class GroupByCarrierDeviceOS {

@Column(name = "time")
private Instant time;

@Column(name = "CARRIER", tag = true)
private String carrier;

@Column(name = "DEVICE_OS_VERSION", tag = true)
private String deviceOsVersion;

@Column(name = "median")
private Double median;

@Column(name = "min")
private Double min;

@Column(name = "max")
private Double max;

@Override
public String toString() {
return "GroupByCarrierDeviceOS [time=" + time + ", carrier=" + carrier + ", deviceOsVersion=" + deviceOsVersion
+ ", median=" + median + ", min=" + min + ", max=" + max + "]";
}
}
}