From a77887dfcfb3b8d0064e1f79a9db910fe904fa95 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Tue, 17 Jan 2023 15:35:59 +0100 Subject: [PATCH 1/2] Catch IAE thrown if e.g. invalid start/end time given Signed-off-by: Christoph Weitkamp --- .../persistence/rrd4j/internal/RRD4jPersistenceService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/org.openhab.persistence.rrd4j/src/main/java/org/openhab/persistence/rrd4j/internal/RRD4jPersistenceService.java b/bundles/org.openhab.persistence.rrd4j/src/main/java/org/openhab/persistence/rrd4j/internal/RRD4jPersistenceService.java index b2a40a6aeb461..b38fc4fbd574c 100644 --- a/bundles/org.openhab.persistence.rrd4j/src/main/java/org/openhab/persistence/rrd4j/internal/RRD4jPersistenceService.java +++ b/bundles/org.openhab.persistence.rrd4j/src/main/java/org/openhab/persistence/rrd4j/internal/RRD4jPersistenceService.java @@ -330,7 +330,7 @@ public Iterable query(FilterCriteria filter) { ts += step; } return items; - } catch (IOException e) { + } catch (IllegalArgumentException | IOException e) { logger.warn("Could not query rrd4j database for item '{}': {}", itemName, e.getMessage()); return List.of(); } finally { From 41e5ba16a3b1de88fb58f523e63fb9465dfcf254 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Fri, 20 Jan 2023 08:53:17 +0100 Subject: [PATCH 2/2] Incorporated comments from review Signed-off-by: Christoph Weitkamp --- .../internal/RRD4jPersistenceService.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.persistence.rrd4j/src/main/java/org/openhab/persistence/rrd4j/internal/RRD4jPersistenceService.java b/bundles/org.openhab.persistence.rrd4j/src/main/java/org/openhab/persistence/rrd4j/internal/RRD4jPersistenceService.java index b38fc4fbd574c..4359dc8b6a753 100644 --- a/bundles/org.openhab.persistence.rrd4j/src/main/java/org/openhab/persistence/rrd4j/internal/RRD4jPersistenceService.java +++ b/bundles/org.openhab.persistence.rrd4j/src/main/java/org/openhab/persistence/rrd4j/internal/RRD4jPersistenceService.java @@ -280,7 +280,10 @@ public Iterable query(FilterCriteria filter) { } long start = 0L; - long end = filter.getEndDate() == null ? System.currentTimeMillis() / 1000 + // set end to {@link Instant#MAX} instead of current timestamp to enable requesting future time ranges including + // boundary values via REST API + // see discussion in https://github.com/openhab/openhab-addons/pull/14238 + long end = filter.getEndDate() == null ? Instant.MAX.getEpochSecond() : filter.getEndDate().toInstant().getEpochSecond(); try { @@ -308,13 +311,21 @@ public Iterable query(FilterCriteria filter) { start = end; } } else { - throw new UnsupportedOperationException("rrd4j does not allow querys without a begin date, " - + "unless order is descending and a single value is requested"); + throw new UnsupportedOperationException( + "rrd4j does not allow querys without a begin date, unless order is descending and a single value is requested"); } } else { start = filter.getBeginDate().toInstant().getEpochSecond(); } + // do not call method {@link RrdDb#createFetchRequest(ConsolFun, long, long, long)} if start > end to avoid + // an IAE to be thrown + if (start > end) { + logger.warn("Could not query rrd4j database for item '{}': start ({}) > end ({})", itemName, start, + end); + return List.of(); + } + FetchRequest request = db.createFetchRequest(getConsolidationFunction(db), start, end, 1); FetchData result = request.fetchData(); @@ -330,7 +341,7 @@ public Iterable query(FilterCriteria filter) { ts += step; } return items; - } catch (IllegalArgumentException | IOException e) { + } catch (IOException e) { logger.warn("Could not query rrd4j database for item '{}': {}", itemName, e.getMessage()); return List.of(); } finally {