[influxdb] Fix problems when querying old historical data (#10680)

Fix problem with historical  Influx1 data that was generated with addon version <3.0.0 that didn't write generate item tag

Signed-off-by: Joan Pujol <joanpujol@gmail.com>
This commit is contained in:
Joan Pujol 2021-05-14 10:21:12 +02:00 committed by GitHub
parent bafa841390
commit 801100cee1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 14 deletions

View File

@ -14,6 +14,7 @@ package org.openhab.persistence.influxdb.internal;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.StringJoiner;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -61,7 +62,7 @@ public class InfluxDBConfiguration {
token = (String) config.getOrDefault(TOKEN_PARAM, "");
databaseName = (String) config.getOrDefault(DATABASE_PARAM, "openhab");
retentionPolicy = (String) config.getOrDefault(RETENTION_POLICY_PARAM, "autogen");
version = parseInfluxVersion(config.getOrDefault(VERSION_PARAM, InfluxDBVersion.V1.name()));
version = parseInfluxVersion((String) config.getOrDefault(VERSION_PARAM, InfluxDBVersion.V1.name()));
replaceUnderscore = getConfigBooleanValue(config, REPLACE_UNDERSCORE_PARAM, false);
addCategoryTag = getConfigBooleanValue(config, ADD_CATEGORY_TAG_PARAM, false);
@ -80,9 +81,9 @@ public class InfluxDBConfiguration {
}
}
private InfluxDBVersion parseInfluxVersion(@Nullable Object value) {
private InfluxDBVersion parseInfluxVersion(@Nullable String value) {
try {
return InfluxDBVersion.valueOf((String) value);
return Optional.ofNullable(value).map(InfluxDBVersion::valueOf).orElse(InfluxDBVersion.UNKNOWN);
} catch (RuntimeException e) {
logger.warn("Invalid version {}", value);
return InfluxDBVersion.UNKNOWN;

View File

@ -93,7 +93,7 @@ public class InfluxDBStateConvertUtils {
* @return the state of the item represented by the itemName parameter, else the string value of
* the Object parameter
*/
public static State objectToState(Object value, String itemName, @Nullable ItemRegistry itemRegistry) {
public static State objectToState(@Nullable Object value, String itemName, @Nullable ItemRegistry itemRegistry) {
State state = null;
if (itemRegistry != null) {
try {
@ -111,9 +111,10 @@ public class InfluxDBStateConvertUtils {
return state;
}
public static State objectToState(Object value, Item itemToSetState) {
public static State objectToState(@Nullable Object value, Item itemToSetState) {
String valueStr = String.valueOf(value);
@Nullable
Item item = itemToSetState;
if (item instanceof GroupItem) {
item = ((GroupItem) item).getBaseItem();

View File

@ -12,11 +12,14 @@
*/
package org.openhab.persistence.influxdb.internal;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* InfluxDB version
*
* @author Joan Pujol Espinar - Initial contribution
*/
@NonNullByDefault
public enum InfluxDBVersion {
V1,
V2,

View File

@ -15,6 +15,7 @@ package org.openhab.persistence.influxdb.internal;
import java.time.Instant;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/**
* Row data returned from database query
@ -25,9 +26,9 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
public class InfluxRow {
private final String itemName;
private final Instant time;
private final Object value;
private final @Nullable Object value;
public InfluxRow(Instant time, String itemName, Object value) {
public InfluxRow(Instant time, String itemName, @Nullable Object value) {
this.time = time;
this.itemName = itemName;
this.value = value;
@ -41,7 +42,7 @@ public class InfluxRow {
return itemName;
}
public Object getValue() {
public @Nullable Object getValue() {
return value;
}
}

View File

@ -22,6 +22,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -171,8 +173,7 @@ public class InfluxDB1RepositoryImpl implements InfluxDBRepository {
} else {
for (QueryResult.Series series : seriess) {
logger.trace("series {}", series.toString());
String itemName = series.getName();
List<List<Object>> valuess = series.getValues();
List<List<@Nullable Object>> valuess = series.getValues();
if (valuess == null) {
logger.debug("query returned no values");
} else {
@ -196,12 +197,14 @@ public class InfluxDB1RepositoryImpl implements InfluxDBRepository {
throw new IllegalStateException("missing column");
}
for (int i = 0; i < valuess.size(); i++) {
Double rawTime = (Double) valuess.get(i).get(timestampColumn);
Double rawTime = (Double) Objects.requireNonNull(valuess.get(i).get(timestampColumn));
Instant time = Instant.ofEpochMilli(rawTime.longValue());
@Nullable
Object value = valuess.get(i).get(valueColumn);
if (itemNameColumn != null) {
itemName = (String) valuess.get(i).get(itemNameColumn);
}
var currentI = i;
String itemName = Optional.ofNullable(itemNameColumn)
.flatMap(inc -> Optional.ofNullable((String) valuess.get(currentI).get(inc)))
.orElse(series.getName());
logger.trace("adding historic item {}: time {} value {}", itemName, time, value);
rows.add(new InfluxRow(time, itemName, value));
}