Skip to content

Commit

Permalink
[influxdb] Fix problems when querying old historical data (openhab#10680
Browse files Browse the repository at this point in the history
)

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 <[email protected]>
  • Loading branch information
lujop authored and computergeek1507 committed Jul 13, 2021
1 parent 2ea987b commit 586f006
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.StringJoiner;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -61,7 +62,7 @@ public InfluxDBConfiguration(Map<String, Object> config) {
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);
Expand All @@ -80,9 +81,9 @@ private static boolean getConfigBooleanValue(Map<String, Object> config, String
}
}

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static Object stateToObject(State state) {
* @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 {
Expand All @@ -111,9 +111,10 @@ public static State objectToState(Object value, String itemName, @Nullable ItemR
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.time.Instant;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* Row data returned from database query
Expand All @@ -25,9 +26,9 @@
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;
Expand All @@ -41,7 +42,7 @@ public String getItemName() {
return itemName;
}

public Object getValue() {
public @Nullable Object getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
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;
Expand Down Expand Up @@ -171,8 +173,7 @@ private List<InfluxRow> convertClientResutToRepository(List<QueryResult.Result>
} 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 {
Expand All @@ -196,12 +197,14 @@ private List<InfluxRow> convertClientResutToRepository(List<QueryResult.Result>
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));
}
Expand Down

0 comments on commit 586f006

Please sign in to comment.