From 0d9b678f367d19fb26d13053d7f6fd670af0ec52 Mon Sep 17 00:00:00 2001 From: J-N-K Date: Sun, 11 Dec 2022 00:14:06 +0100 Subject: [PATCH] Add countSince/countBetween to persistence extensions (#3145) * Add countSince/countBetween to persistence extensions Signed-off-by: Jan N. Klug --- .../extensions/PersistenceExtensions.java | 59 +++++++++++++++++++ .../extensions/PersistenceExtensionsTest.java | 37 ++++++++++++ 2 files changed, 96 insertions(+) diff --git a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java index 25ee25892..034993e4f 100644 --- a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java +++ b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java @@ -16,8 +16,10 @@ import java.math.BigDecimal; import java.math.MathContext; import java.time.Instant; import java.time.ZonedDateTime; +import java.util.Collection; import java.util.Iterator; import java.util.List; +import java.util.stream.StreamSupport; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.items.Item; @@ -1091,6 +1093,63 @@ public class PersistenceExtensions { return null; } + /** + * Gets the number of available historic data points of a given {@link Item} from a point in time until now. + * The default {@link PersistenceService} is used. + * + * @param item the {@link Item} to query + * @param begin the beginning point in time + * @return the number of values persisted for this item + */ + public static long countSince(Item item, ZonedDateTime begin) { + return countSince(item, begin, getDefaultServiceId()); + } + + /** + * Gets the number of available historic data points of a given {@link Item} from a point in time until now. + * The {@link PersistenceService} identified by the serviceId is used. + * + * @param item the {@link Item} to query + * @param begin the beginning point in time + * @param serviceId the name of the {@link PersistenceService} to use + * @return the number of values persisted for this item + */ + public static long countSince(Item item, ZonedDateTime begin, String serviceId) { + return countBetween(item, begin, null, serviceId); + } + + /** + * Gets the number of available historic data points of a given {@link Item} between two points in time. + * The default {@link PersistenceService} is used. + * + * @param item the {@link Item} to query + * @param begin the beginning point in time + * @param end the end point in time + * @return the number of values persisted for this item + */ + public static long countBetween(Item item, ZonedDateTime begin, @Nullable ZonedDateTime end) { + return countBetween(item, begin, end, getDefaultServiceId()); + } + + /** + * Gets the number of available historic data points of a given {@link Item} between two points in time. + * The {@link PersistenceService} identified by the serviceId is used. + * + * @param item the {@link Item} to query + * @param begin the beginning point in time + * @param end the end point in time + * @param serviceId the name of the {@link PersistenceService} to use + * @return the number of values persisted for this item + */ + public static long countBetween(Item item, ZonedDateTime begin, @Nullable ZonedDateTime end, String serviceId) { + Iterable historicItems = getAllStatesBetween(item, begin, end, serviceId); + if (historicItems instanceof Collection) { + return ((Collection) historicItems).size(); + } else { + return StreamSupport.stream(historicItems.spliterator(), false).count(); + } + } + private static @Nullable PersistenceService getService(String serviceId) { PersistenceService service = null; if (registry != null) { diff --git a/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/extensions/PersistenceExtensionsTest.java b/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/extensions/PersistenceExtensionsTest.java index ac2fc3434..b983db6ad 100644 --- a/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/extensions/PersistenceExtensionsTest.java +++ b/bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/extensions/PersistenceExtensionsTest.java @@ -914,4 +914,41 @@ public class PersistenceExtensionsTest { ZonedDateTime.of(2011, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault())); assertFalse(updated); } + + @Test + public void testCountBetween() { + long counts = PersistenceExtensions.countBetween(numberItem, + ZonedDateTime.of(1940, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), + ZonedDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID); + assertEquals(0, counts); + + counts = PersistenceExtensions.countBetween(numberItem, + ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), + ZonedDateTime.of(2011, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID); + assertEquals(7, counts); + + counts = PersistenceExtensions.countBetween(numberItem, + ZonedDateTime.of(2005, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), + ZonedDateTime.of(2011, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault())); + assertEquals(0, counts); + } + + @Test + public void testCountSince() { + long counts = PersistenceExtensions.countSince(numberItem, + ZonedDateTime.of(1980, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID); + assertEquals(33, counts); + + counts = PersistenceExtensions.countSince(numberItem, + ZonedDateTime.of(2007, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID); + assertEquals(6, counts); + + counts = PersistenceExtensions.countSince(numberItem, + ZonedDateTime.of(2020, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID); + assertEquals(0, counts); + + counts = PersistenceExtensions.countSince(numberItem, + ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault())); + assertEquals(0, counts); + } }