From c3666581b5d9d48062c8c77a4969cd37523300c7 Mon Sep 17 00:00:00 2001 From: Joan Pujol Date: Thu, 22 Oct 2020 10:26:21 +0200 Subject: [PATCH] [influxdb] Fixes issue 8798 and 8697 problems storing integer types (#8831) * Update documentation with changed Influx2 RC port * Fix problem with non decimal numeric types Improve documentation with more explicit information about Influx types used Implement toString to InfluxPoint to allow some trace info to be useful in case it's needed Fixes #8697 Fixes #8798 Signed-off-by: Joan Pujol --- .../README.md | 11 +++++++--- .../internal/InfluxDBStateConvertUtils.java | 21 ++----------------- .../influxdb/internal/InfluxPoint.java | 6 ++++++ .../main/resources/OH-INF/config/config.xml | 2 +- .../internal/ConfigurationTestHelper.java | 2 +- .../InfluxDBStateConvertUtilsTest.java | 20 +++++++++++++----- .../influxdb/internal/ItemTestHelper.java | 7 +++++-- .../internal/ItemToStorePointCreatorTest.java | 18 +++++++++++----- 8 files changed, 51 insertions(+), 36 deletions(-) diff --git a/bundles/org.openhab.persistence.influxdb/README.md b/bundles/org.openhab.persistence.influxdb/README.md index beb39efc2..3abf90181 100644 --- a/bundles/org.openhab.persistence.influxdb/README.md +++ b/bundles/org.openhab.persistence.influxdb/README.md @@ -1,14 +1,19 @@ # InfluxDB (0.9 and newer) Persistence -This service allows you to persist and query states using the [InfluxDB](https://www.influxdata.com/products/influxdb-overview/) and [InfluxDB 2.0](https://v2.docs.influxdata.com/v2.0/) time series database. The persisted values can be queried from within openHAB. There also are nice tools on the web for visualizing InfluxDB time series, such as [Grafana](http://grafana.org/). +This service allows you to persist and query states using the [InfluxDB](https://www.influxdata.com/products/influxdb-overview/) and [InfluxDB 2.0](https://v2.docs.influxdata.com/v2.0/) time series database. The persisted values can be queried from within openHAB. +There also are nice tools on the web for visualizing InfluxDB time series, such as [Grafana](http://grafana.org/) and new Influx DB 2.0 version introduces [powerful data processing features.](https://docs.influxdata.com/influxdb/v2.0/process-data/get-started/) ## Database Structure - This service allows you to persist and query states using the time series database. - The states of an item are persisted in *measurements* points with names equal to the name of the item, or the alias, if one is provided. In both variants, a *tag* named "item" is added, containing the item name. - All values are stored in a *field* called "value" using integers or doubles if possible,`OnOffType` and `OpenClosedType` values are stored using 0 or 1. -- If configured extra tags for item category, label or type can be added fore each point. + All values are stored in a *field* called "value" using the following types: + - **float** for DecimalType and QuantityType + - **integer** for `OnOffType` and `OpenClosedType` (values are stored using 0 or 1) and `DateTimeType` (milliseconds since 1970-01-01T00:00:00Z) + - **string** for the rest of types + +- If configured, extra tags for item category, label or type can be added fore each point. Some example entries for an item with the name "speedtest" without any further configuration would look like this: diff --git a/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/InfluxDBStateConvertUtils.java b/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/InfluxDBStateConvertUtils.java index 8670b8a1f..62f096fad 100644 --- a/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/InfluxDBStateConvertUtils.java +++ b/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/InfluxDBStateConvertUtils.java @@ -69,9 +69,9 @@ public class InfluxDBStateConvertUtils { } else if (state instanceof PointType) { value = point2String((PointType) state); } else if (state instanceof DecimalType) { - value = convertBigDecimalToNum(((DecimalType) state).toBigDecimal()); + value = ((DecimalType) state).toBigDecimal(); } else if (state instanceof QuantityType) { - value = convertBigDecimalToNum(((QuantityType) state).toBigDecimal()); + value = ((QuantityType) state).toBigDecimal(); } else if (state instanceof OnOffType) { value = state == OnOffType.ON ? DIGITAL_VALUE_ON : DIGITAL_VALUE_OFF; } else if (state instanceof OpenClosedType) { @@ -166,21 +166,4 @@ public class InfluxDBStateConvertUtils { } return buf.toString(); // latitude, longitude, altitude } - - /** - * This method returns an integer if possible if not a double is returned. This is an optimization - * for influxdb because integers have less overhead. - * - * @param value the BigDecimal to be converted - * @return A double if possible else a double is returned. - */ - private static Object convertBigDecimalToNum(BigDecimal value) { - Object convertedValue; - if (value.scale() == 0) { - convertedValue = value.toBigInteger(); - } else { - convertedValue = value.doubleValue(); - } - return convertedValue; - } } diff --git a/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/InfluxPoint.java b/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/InfluxPoint.java index bfad354d3..531a13a1f 100644 --- a/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/InfluxPoint.java +++ b/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/InfluxPoint.java @@ -88,4 +88,10 @@ public class InfluxPoint { return new InfluxPoint(this); } } + + @Override + public String toString() { + return "InfluxPoint{" + "measurementName='" + measurementName + '\'' + ", time=" + time + ", value=" + value + + ", tags=" + tags + '}'; + } } diff --git a/bundles/org.openhab.persistence.influxdb/src/main/resources/OH-INF/config/config.xml b/bundles/org.openhab.persistence.influxdb/src/main/resources/OH-INF/config/config.xml index b8fe1a0e2..3ee4fd5dd 100644 --- a/bundles/org.openhab.persistence.influxdb/src/main/resources/OH-INF/config/config.xml +++ b/bundles/org.openhab.persistence.influxdb/src/main/resources/OH-INF/config/config.xml @@ -27,7 +27,7 @@ url - The database URL, e.g. http://127.0.0.1:8086 or http://127.0.0.1:9999 + The database URL, e.g. http://127.0.0.1:8086 http://127.0.0.1:8086 diff --git a/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/ConfigurationTestHelper.java b/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/ConfigurationTestHelper.java index c9b287683..639a4e511 100644 --- a/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/ConfigurationTestHelper.java +++ b/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/ConfigurationTestHelper.java @@ -32,7 +32,7 @@ public class ConfigurationTestHelper { public static Map createValidConfigurationParameters() { Map config = new HashMap<>(); - config.put(URL_PARAM, "http://localhost:9999"); + config.put(URL_PARAM, "http://localhost:8086"); config.put(VERSION_PARAM, InfluxDBVersion.V2.name()); config.put(TOKEN_PARAM, "sampletoken"); config.put(DATABASE_PARAM, "openhab"); diff --git a/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/InfluxDBStateConvertUtilsTest.java b/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/InfluxDBStateConvertUtilsTest.java index a7ac2058d..d44032ddc 100644 --- a/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/InfluxDBStateConvertUtilsTest.java +++ b/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/InfluxDBStateConvertUtilsTest.java @@ -13,7 +13,8 @@ package org.openhab.persistence.influxdb.internal; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; import java.math.BigDecimal; import java.time.Instant; @@ -22,6 +23,8 @@ import java.time.ZonedDateTime; import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.openhab.core.library.items.ContactItem; import org.openhab.core.library.items.DateTimeItem; import org.openhab.core.library.items.NumberItem; @@ -40,7 +43,13 @@ public class InfluxDBStateConvertUtilsTest { @Test public void convertDecimalState() { DecimalType decimalType = new DecimalType(new BigDecimal("1.12")); - assertThat((Double) InfluxDBStateConvertUtils.stateToObject(decimalType), closeTo(1.12, 0.01)); + assertThat(InfluxDBStateConvertUtils.stateToObject(decimalType), is(new BigDecimal("1.12"))); + } + + @Test + public void convertIntegerDecimalState() { + DecimalType decimalType = new DecimalType(12L); + assertThat(InfluxDBStateConvertUtils.stateToObject(decimalType), is(new BigDecimal("12"))); } @Test @@ -57,9 +66,10 @@ public class InfluxDBStateConvertUtilsTest { assertThat(InfluxDBStateConvertUtils.stateToObject(type), equalTo(nowInMillis)); } - @Test - public void convertDecimalToState() { - BigDecimal val = new BigDecimal("1.12"); + @ParameterizedTest + @ValueSource(strings = { "1.12", "25" }) + public void convertDecimalToState(String number) { + BigDecimal val = new BigDecimal(number); NumberItem item = new NumberItem("name"); assertThat(InfluxDBStateConvertUtils.objectToState(val, item), equalTo(new DecimalType(val))); } diff --git a/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/ItemTestHelper.java b/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/ItemTestHelper.java index ca2636a75..66d4e3df7 100644 --- a/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/ItemTestHelper.java +++ b/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/ItemTestHelper.java @@ -22,9 +22,12 @@ import org.openhab.core.library.types.DecimalType; @NonNullByDefault public class ItemTestHelper { - public static NumberItem createNumberItem(String name, int value) { + public static NumberItem createNumberItem(String name, Number value) { NumberItem numberItem = new NumberItem(name); - numberItem.setState(new DecimalType(value)); + if (value instanceof Integer || value instanceof Long) + numberItem.setState(new DecimalType(value.longValue())); + else + numberItem.setState(new DecimalType(value.doubleValue())); return numberItem; } } diff --git a/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/ItemToStorePointCreatorTest.java b/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/ItemToStorePointCreatorTest.java index 7198a58aa..747322e0d 100644 --- a/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/ItemToStorePointCreatorTest.java +++ b/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/ItemToStorePointCreatorTest.java @@ -16,8 +16,9 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import static org.mockito.Mockito.when; -import java.math.BigInteger; +import java.math.BigDecimal; import java.util.Map; +import java.util.stream.Stream; import org.eclipse.jdt.annotation.DefaultLocation; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -25,6 +26,8 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.openhab.core.items.Metadata; @@ -62,14 +65,19 @@ public class ItemToStorePointCreatorTest { metadataRegistry = null; } - @Test - public void convertBasicItem() { - NumberItem item = ItemTestHelper.createNumberItem("myitem", 5); + @ParameterizedTest + @MethodSource + public void convertBasicItem(Number number) { + NumberItem item = ItemTestHelper.createNumberItem("myitem", number); InfluxPoint point = instance.convert(item, null); assertThat(point.getMeasurementName(), equalTo(item.getName())); assertThat("Must Store item name", point.getTags(), hasEntry("item", item.getName())); - assertThat(point.getValue(), equalTo(new BigInteger("5"))); + assertThat(point.getValue(), equalTo(new BigDecimal(number.toString()))); + } + + private static Stream convertBasicItem() { + return Stream.of(5, 5.5, 5L); } @Test