]> git.basschouten.com Git - openhab-addons.git/commitdiff
[influxdb] Fix problems when querying old historical data (#10680)
authorJoan Pujol <joanpujol@gmail.com>
Fri, 14 May 2021 08:21:12 +0000 (10:21 +0200)
committerGitHub <noreply@github.com>
Fri, 14 May 2021 08:21:12 +0000 (10:21 +0200)
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>
bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/InfluxDBConfiguration.java
bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/InfluxDBStateConvertUtils.java
bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/InfluxDBVersion.java
bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/InfluxRow.java
bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx1/InfluxDB1RepositoryImpl.java

index 933d657e40efc4dfafba3a2534f6da201ab9de68..b9a7d05770bc195aea9f84145f6f2202c7007685 100644 (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;
index b9e9ce5f504f185be06f7c5dbde830ba67bd50e7..da83e6d5c8177571de6341fd271b9d9d44e97208 100644 (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();
index f03cd9319f7e0d15605f394f8797c4c55f340931..7d9975aeb659135959023cc07dd54ec9372302f3 100644 (file)
  */
 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,
index 78093330ccd70eb7b8e7978d9e7acd9809bea1ae..bdb193cd8f11dc31e9dbf8ceee50f1904c4d9510 100644 (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;
     }
 }
index 094c8ab25c4481f71e07039d81ad2cebb445cb28..90b38ce1a05ef259b00035c67e78c78643bb33a6 100644 (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));
                             }