mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-10 21:31:53 +01:00
Add countSince/countBetween to persistence extensions (#3145)
* Add countSince/countBetween to persistence extensions Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
parent
6479e4f818
commit
0d9b678f36
@ -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 <code>serviceId</code> 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 <code>serviceId</code> 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<HistoricItem> 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) {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user