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

The InfluxDBResultMapper is able to handle results with a different time precision #501

Merged
merged 3 commits into from
Sep 6, 2018
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 2.13 [unreleased]

### Fixes

- The InfluxDBResultMapper is able to handle results with a different time precision [PR #501](https://github.com/influxdata/influxdb-java/pull/501)

### Features

- Support for Basic Authentication [PR #492](https://github.com/influxdata/influxdb-java/pull/492)
Expand Down
84 changes: 74 additions & 10 deletions src/main/java/org/influxdb/impl/InfluxDBResultMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import java.time.temporal.ChronoField;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -84,9 +84,33 @@ public class InfluxDBResultMapper {
* possible to define the values of your POJO (e.g. due to an unsupported field type).
*/
public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz) throws InfluxDBMapperException {
return toPOJO(queryResult, clazz, TimeUnit.MILLISECONDS);
}

/**
* <p>
* Process a {@link QueryResult} object returned by the InfluxDB client inspecting the internal
* data structure and creating the respective object instances based on the Class passed as
* parameter.
* </p>
*
* @param queryResult the InfluxDB result object
* @param clazz the Class that will be used to hold your measurement data
* @param precision the time precision of results
* @param <T> the target type
*
* @return a {@link List} of objects from the same Class passed as parameter and sorted on the
* same order as received from InfluxDB.
*
* @throws InfluxDBMapperException If {@link QueryResult} parameter contain errors,
* <tt>clazz</tt> parameter is not annotated with &#64;Measurement or it was not
* possible to define the values of your POJO (e.g. due to an unsupported field type).
*/
public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz,
final TimeUnit precision) throws InfluxDBMapperException {
throwExceptionIfMissingAnnotation(clazz);
String measurementName = getMeasurementName(clazz);
return this.toPOJO(queryResult, clazz, measurementName);
return this.toPOJO(queryResult, clazz, measurementName, precision);
}

/**
Expand All @@ -110,6 +134,32 @@ public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz) t
*/
public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz, final String measurementName)
throws InfluxDBMapperException {
return toPOJO(queryResult, clazz, measurementName, TimeUnit.MILLISECONDS);
}

/**
* <p>
* Process a {@link QueryResult} object returned by the InfluxDB client inspecting the internal
* data structure and creating the respective object instances based on the Class passed as
* parameter.
* </p>
*
* @param queryResult the InfluxDB result object
* @param clazz the Class that will be used to hold your measurement data
* @param <T> the target type
* @param measurementName name of the Measurement
* @param precision the time precision of results
*
* @return a {@link List} of objects from the same Class passed as parameter and sorted on the
* same order as received from InfluxDB.
*
* @throws InfluxDBMapperException If {@link QueryResult} parameter contain errors,
* <tt>clazz</tt> parameter is not annotated with &#64;Measurement or it was not
* possible to define the values of your POJO (e.g. due to an unsupported field type).
*/
public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz, final String measurementName,
final TimeUnit precision)
throws InfluxDBMapperException {

Objects.requireNonNull(measurementName, "measurementName");
Objects.requireNonNull(queryResult, "queryResult");
Expand All @@ -126,7 +176,7 @@ public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz, f
internalResult.getSeries().stream()
.filter(series -> series.getName().equals(measurementName))
.forEachOrdered(series -> {
parseSeriesAs(series, clazz, result);
parseSeriesAs(series, clazz, result, precision);
});
});

Expand Down Expand Up @@ -177,6 +227,11 @@ String getMeasurementName(final Class<?> clazz) {
}

<T> List<T> parseSeriesAs(final QueryResult.Series series, final Class<T> clazz, final List<T> result) {
return parseSeriesAs(series, clazz, result, TimeUnit.MILLISECONDS);
}

<T> List<T> parseSeriesAs(final QueryResult.Series series, final Class<T> clazz, final List<T> result,
final TimeUnit precision) {
int columnSize = series.getColumns().size();
ConcurrentMap<String, Field> colNameAndFieldMap = CLASS_FIELD_CACHE.get(clazz.getName());
try {
Expand All @@ -188,7 +243,7 @@ <T> List<T> parseSeriesAs(final QueryResult.Series series, final Class<T> clazz,
if (object == null) {
object = clazz.newInstance();
}
setFieldValue(object, correspondingField, row.get(i));
setFieldValue(object, correspondingField, row.get(i), precision);
}
}
// When the "GROUP BY" clause is used, "tags" are returned as Map<String,String> and
Expand All @@ -200,7 +255,7 @@ <T> List<T> parseSeriesAs(final QueryResult.Series series, final Class<T> clazz,
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());
setFieldValue(object, correspondingField, entry.getValue(), precision);
}
}
}
Expand All @@ -223,10 +278,11 @@ <T> List<T> parseSeriesAs(final QueryResult.Series series, final Class<T> clazz,
* @param object
* @param field
* @param value
* @param precision
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
<T> void setFieldValue(final T object, final Field field, final Object value)
<T> void setFieldValue(final T object, final Field field, final Object value, final TimeUnit precision)
throws IllegalArgumentException, IllegalAccessException {
if (value == null) {
return;
Expand All @@ -236,7 +292,7 @@ <T> void setFieldValue(final T object, final Field field, final Object value)
if (!field.isAccessible()) {
field.setAccessible(true);
}
if (fieldValueModified(fieldType, field, object, value)
if (fieldValueModified(fieldType, field, object, value, precision)
|| fieldValueForPrimitivesModified(fieldType, field, object, value)
|| fieldValueForPrimitiveWrappersModified(fieldType, field, object, value)) {
return;
Expand All @@ -252,7 +308,8 @@ <T> void setFieldValue(final T object, final Field field, final Object value)
}
}

<T> boolean fieldValueModified(final Class<?> fieldType, final Field field, final T object, final Object value)
<T> boolean fieldValueModified(final Class<?> fieldType, final Field field, final T object, final Object value,
final TimeUnit precision)
throws IllegalArgumentException, IllegalAccessException {
if (String.class.isAssignableFrom(fieldType)) {
field.set(object, String.valueOf(value));
Expand All @@ -263,9 +320,11 @@ <T> boolean fieldValueModified(final Class<?> fieldType, final Field field, fina
if (value instanceof String) {
instant = Instant.from(ISO8601_FORMATTER.parse(String.valueOf(value)));
} else if (value instanceof Long) {
instant = Instant.ofEpochMilli((Long) value);
instant = Instant.ofEpochMilli(toMillis((long) value, precision));
} else if (value instanceof Double) {
instant = Instant.ofEpochMilli(((Double) value).longValue());
instant = Instant.ofEpochMilli(toMillis(((Double) value).longValue(), precision));
} else if (value instanceof Integer) {
instant = Instant.ofEpochMilli(toMillis(((Integer) value).longValue(), precision));
} else {
throw new InfluxDBMapperException("Unsupported type " + field.getClass() + " for field " + field.getName());
}
Expand Down Expand Up @@ -316,4 +375,9 @@ <T> boolean fieldValueForPrimitiveWrappersModified(final Class<?> fieldType, fin
}
return false;
}

private Long toMillis(final long value, final TimeUnit precision) {

return TimeUnit.MILLISECONDS.convert(value, precision);
}
}
76 changes: 75 additions & 1 deletion src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.influxdb.InfluxDBMapperException;
import org.influxdb.annotation.Column;
import org.influxdb.annotation.Measurement;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBResultMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
Expand Down Expand Up @@ -220,6 +220,26 @@ public void testFieldValueModified_DateAsISO8601() {
Assertions.assertTrue(result.size() == 1);
}

@Test
public void testFieldValueModified_DateAsInteger() {
// Given...
mapper.cacheMeasurementClass(MyCustomMeasurement.class);

List<String> columnList = Arrays.asList("time");
List<Object> firstSeriesResult = Arrays.asList(1_000);

QueryResult.Series series = new QueryResult.Series();
series.setColumns(columnList);
series.setValues(Arrays.asList(firstSeriesResult));

//When...
List<MyCustomMeasurement> result = new LinkedList<>();
mapper.parseSeriesAs(series, MyCustomMeasurement.class, result);

//Then...
Assertions.assertTrue(result.size() == 1);
}

@Test
public void testUnsupportedField() {
// Given...
Expand Down Expand Up @@ -335,6 +355,60 @@ public void testToPOJO_ticket363() {
Assertions.assertEquals(1, result.get(0).time.getNano(), "incorrect value for the nanoseconds field");
}

@Test
void testToPOJO_Precision() {
// Given...
mapper.cacheMeasurementClass(MyCustomMeasurement.class);

List<String> columnList = Arrays.asList("time");
List<Object> firstSeriesResult = Arrays.asList(1_500_000L);

QueryResult.Series series = new QueryResult.Series();
series.setName("CustomMeasurement");
series.setColumns(columnList);
series.setValues(Arrays.asList(firstSeriesResult));

QueryResult.Result internalResult = new QueryResult.Result();
internalResult.setSeries(Arrays.asList(series));

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

// When...
List<MyCustomMeasurement> result = mapper.toPOJO(queryResult, MyCustomMeasurement.class, TimeUnit.SECONDS);

// Then...
Assertions.assertEquals(1, result.size(), "incorrect number of elements");
Assertions.assertEquals(1_500_000_000L, result.get(0).time.toEpochMilli(), "incorrect value for the millis field");
}

@Test
void testToPOJO_SetMeasureName() {
// Given...
mapper.cacheMeasurementClass(MyCustomMeasurement.class);

List<String> columnList = Arrays.asList("uuid");
List<Object> firstSeriesResult = Arrays.asList(UUID.randomUUID().toString());

QueryResult.Series series = new QueryResult.Series();
series.setName("MySeriesName");
series.setColumns(columnList);
series.setValues(Arrays.asList(firstSeriesResult));

QueryResult.Result internalResult = new QueryResult.Result();
internalResult.setSeries(Arrays.asList(series));

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

//When...
List<MyCustomMeasurement> result =
mapper.toPOJO(queryResult, MyCustomMeasurement.class, "MySeriesName");

//Then...
Assertions.assertTrue(result.size() == 1);
}

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

Expand Down