Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add countStateChangesSince/countStateChangesBetween to persistence extensions #3211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,76 @@ public static long countBetween(Item item, ZonedDateTime begin, @Nullable ZonedD
}
}

/**
* Gets the number of changes in 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 state changes for this item
*/
public static long countStateChangesSince(Item item, ZonedDateTime begin) {
return countStateChangesSince(item, begin, getDefaultServiceId());
}

/**
* Gets the number of changes in 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 state changes for this item
*/
public static long countStateChangesSince(Item item, ZonedDateTime begin, String serviceId) {
return countStateChangesBetween(item, begin, null, serviceId);
}

/**
* Gets the number of changes in 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 state changes for this item
*/
public static long countStateChangesBetween(Item item, ZonedDateTime begin, @Nullable ZonedDateTime end) {
return countStateChangesBetween(item, begin, end, getDefaultServiceId());
}

/**
* Gets the number of changes in 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 state changes for this item
*/
public static long countStateChangesBetween(Item item, ZonedDateTime begin, @Nullable ZonedDateTime end,
String serviceId) {
Iterable<HistoricItem> result = getAllStatesBetween(item, begin, end, serviceId);
Iterator<HistoricItem> it = result.iterator();

if (!it.hasNext()) {
return 0;
}

long count = 0;
State previousState = it.next().getState();
while (it.hasNext()) {
HistoricItem historicItem = it.next();
State state = historicItem.getState();
if (!state.equals(previousState)) {
previousState = state;
count++;
}
}
return count;
}

private static @Nullable PersistenceService getService(String serviceId) {
PersistenceService service = null;
if (registry != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -951,4 +951,41 @@ public void testCountSince() {
ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
assertEquals(0, counts);
}

@Test
public void testCountStateChangesSince() {
long counts = PersistenceExtensions.countStateChangesSince(numberItem,
ZonedDateTime.of(1980, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertEquals(32, counts);

counts = PersistenceExtensions.countStateChangesSince(numberItem,
ZonedDateTime.of(2007, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertEquals(5, counts);

counts = PersistenceExtensions.countStateChangesSince(numberItem,
ZonedDateTime.of(2020, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()), TestPersistenceService.ID);
assertEquals(0, counts);

counts = PersistenceExtensions.countStateChangesSince(numberItem,
ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()));
assertEquals(0, counts);
}

@Test
public void testCountStateChangesBetween() {
long counts = PersistenceExtensions.countStateChangesBetween(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.countStateChangesBetween(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(6, counts);

counts = PersistenceExtensions.countStateChangesBetween(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);
}
}