]> git.basschouten.com Git - openhab-addons.git/commitdiff
[rrd4j] Reliably get the latest value, if the filter end date is set to "now" (#16059)
authorKai Kreuzer <kai@openhab.org>
Thu, 14 Dec 2023 06:23:49 +0000 (07:23 +0100)
committerGitHub <noreply@github.com>
Thu, 14 Dec 2023 06:23:49 +0000 (07:23 +0100)
Signed-off-by: Kai Kreuzer <kai@openhab.org>
bundles/org.openhab.persistence.rrd4j/src/main/java/org/openhab/persistence/rrd4j/internal/RRD4jPersistenceService.java

index 8cc9b5688729ec87469e21bb772c235293773222..d6fb0fe55a1742803acb6d4b1ef82ad70d5acd51 100644 (file)
@@ -15,6 +15,7 @@ package org.openhab.persistence.rrd4j.internal;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.time.Duration;
 import java.time.Instant;
 import java.time.ZoneId;
 import java.time.ZonedDateTime;
@@ -444,15 +445,13 @@ public class RRD4jPersistenceService implements QueryablePersistenceService {
 
         try {
             if (filterBeginDate == null) {
-                // as rrd goes back for years and gets more and more
-                // inaccurate, we only support descending order
-                // and a single return value
-                // if there is no begin date is given - this case is
-                // required specifically for the historicState()
-                // query, which we want to support
+                // as rrd goes back for years and gets more and more inaccurate, we only support descending order
+                // and a single return value if no begin date is given - this case is required specifically for the
+                // historicState() query, which we want to support
                 if (filter.getOrdering() == Ordering.DESCENDING && filter.getPageSize() == 1
                         && filter.getPageNumber() == 0) {
-                    if (filterEndDate == null) {
+                    if (filterEndDate == null || Duration.between(filterEndDate, ZonedDateTime.now()).getSeconds() < db
+                            .getRrdDef().getStep()) {
                         // we are asked only for the most recent value!
                         double lastValue = db.getLastDatasourceValue(DATASOURCE_STATE);
                         if (!Double.isNaN(lastValue)) {