Skip to content

Commit 84d1a51

Browse files
committed
[persistence] use Instant instead of ZonedDateTime in Riemann sum methods
Signed-off-by: Jörg Sautter [email protected]
1 parent 08b3fb2 commit 84d1a51

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/QueryablePersistenceService.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.openhab.core.persistence;
1414

15+
import java.time.Instant;
16+
import java.time.ZoneId;
1517
import java.time.ZonedDateTime;
1618
import java.util.Iterator;
1719
import java.util.Set;
@@ -22,7 +24,6 @@
2224
import org.eclipse.jdt.annotation.Nullable;
2325
import org.openhab.core.persistence.FilterCriteria.Ordering;
2426
import org.openhab.core.types.State;
25-
import org.openhab.core.types.UnDefType;
2627

2728
/**
2829
* A queryable persistence service which can be used to store and retrieve
@@ -65,6 +66,11 @@ default Iterable<HistoricItem> query(FilterCriteria filter, @Nullable String ali
6566
FilterCriteria aliasFilter = new FilterCriteria(filter).setItemName(alias);
6667
return StreamSupport.stream(query(aliasFilter).spliterator(), false).map(hi -> new HistoricItem() {
6768

69+
@Override
70+
public Instant getInstant() {
71+
return hi.getInstant();
72+
}
73+
6874
@Override
6975
public ZonedDateTime getTimestamp() {
7076
return hi.getTimestamp();
@@ -111,28 +117,32 @@ public String getName() {
111117
* @return a {@link PersistedItem} or null if the item has not been persisted
112118
*/
113119
default @Nullable PersistedItem persistedItem(String itemName, @Nullable String alias) {
114-
State currentState = UnDefType.NULL;
115-
ZonedDateTime lastUpdate = null;
120+
State currentState;
121+
Instant lastUpdate;
116122

117123
FilterCriteria filter = new FilterCriteria().setItemName(itemName).setEndDate(ZonedDateTime.now())
118124
.setOrdering(Ordering.DESCENDING).setPageSize(1).setPageNumber(0);
119125
Iterator<HistoricItem> it = query(filter, alias).iterator();
120126
if (it.hasNext()) {
121127
HistoricItem historicItem = it.next();
122128
currentState = historicItem.getState();
123-
lastUpdate = historicItem.getTimestamp();
129+
lastUpdate = historicItem.getInstant();
124130
} else {
125131
return null;
126132
}
127133

128134
final State state = currentState;
129-
final ZonedDateTime lastStateUpdate = lastUpdate;
135+
final Instant lastStateUpdate = lastUpdate;
130136

131137
return new PersistedItem() {
138+
@Override
139+
public Instant getInstant() {
140+
return lastStateUpdate;
141+
}
132142

133143
@Override
134144
public ZonedDateTime getTimestamp() {
135-
return lastStateUpdate;
145+
return lastStateUpdate.atZone(ZoneId.systemDefault());
136146
}
137147

138148
@Override

bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.math.BigDecimal;
1616
import java.math.MathContext;
1717
import java.time.Duration;
18+
import java.time.Instant;
1819
import java.time.ZoneId;
1920
import java.time.ZonedDateTime;
2021
import java.util.ArrayList;
@@ -71,6 +72,7 @@
7172
* @author Mark Herwege - add median methods
7273
* @author Mark Herwege - use item lastChange and lastUpdate methods if not in peristence
7374
* @author Mark Herwege - add Riemann sum methods
75+
* @author Jörg Sautter - use Instant instead of ZonedDateTime in Riemann sum methods
7476
*/
7577
@Component(immediate = true)
7678
@NonNullByDefault
@@ -1926,7 +1928,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable
19261928

19271929
private static @Nullable BigDecimal average(ZonedDateTime begin, ZonedDateTime end, Iterator<HistoricItem> it,
19281930
@Nullable Unit<?> unit, @Nullable RiemannType type) {
1929-
BigDecimal sum = riemannSum(begin, end, it, unit, type);
1931+
BigDecimal sum = riemannSum(begin.toInstant(), end.toInstant(), it, unit, type);
19301932
BigDecimal totalDuration = BigDecimal.valueOf(Duration.between(begin, end).toMillis());
19311933
if (totalDuration.signum() == 0) {
19321934
return null;
@@ -2195,35 +2197,39 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable
21952197
Item baseItem = item instanceof GroupItem groupItem ? groupItem.getBaseItem() : item;
21962198
Unit<?> unit = (baseItem instanceof NumberItem numberItem)
21972199
&& (numberItem.getUnit() instanceof Unit<?> numberItemUnit) ? numberItemUnit.getSystemUnit() : null;
2198-
BigDecimal sum = riemannSum(beginTime, endTime, it, unit, type).scaleByPowerOfTen(-3);
2200+
BigDecimal sum = riemannSum(beginTime.toInstant(), endTime.toInstant(), it, unit, type).scaleByPowerOfTen(-3);
21992201
if (unit != null) {
22002202
return new QuantityType<>(sum, unit.multiply(Units.SECOND));
22012203
}
22022204
return new DecimalType(sum);
22032205
}
22042206

2205-
private static BigDecimal riemannSum(ZonedDateTime begin, ZonedDateTime end, Iterator<HistoricItem> it,
2206-
@Nullable Unit<?> unit, @Nullable RiemannType type) {
2207+
private static BigDecimal riemannSum(Instant begin, Instant end, Iterator<HistoricItem> it, @Nullable Unit<?> unit,
2208+
@Nullable RiemannType type) {
22072209
RiemannType riemannType = type == null ? RiemannType.LEFT : type;
22082210

22092211
BigDecimal sum = BigDecimal.ZERO;
22102212
HistoricItem prevItem = null;
2211-
HistoricItem nextItem = null;
2213+
HistoricItem nextItem;
22122214
DecimalType prevState = null;
2213-
DecimalType nextState = null;
2215+
DecimalType nextState;
2216+
Instant prevInstant = null;
2217+
Instant nextInstant;
22142218
Duration prevDuration = Duration.ZERO;
2215-
Duration nextDuration = Duration.ZERO;
2219+
Duration nextDuration;
22162220

22172221
boolean midpointStartBucket = true; // The start and end buckets for the midpoint calculation should be
22182222
// considered for the full length, this flag is used to find the start
22192223
// bucket
22202224
if ((riemannType == RiemannType.MIDPOINT) && it.hasNext()) {
22212225
prevItem = it.next();
2226+
prevInstant = prevItem.getInstant();
22222227
prevState = getPersistedValue(prevItem, unit);
22232228
}
22242229

22252230
while (it.hasNext()) {
22262231
nextItem = it.next();
2232+
nextInstant = nextItem.getInstant();
22272233
BigDecimal weight = BigDecimal.ZERO;
22282234
BigDecimal value = BigDecimal.ZERO;
22292235
switch (riemannType) {
@@ -2232,24 +2238,24 @@ private static BigDecimal riemannSum(ZonedDateTime begin, ZonedDateTime end, Ite
22322238
prevState = getPersistedValue(prevItem, unit);
22332239
if (prevState != null) {
22342240
value = prevState.toBigDecimal();
2235-
weight = BigDecimal.valueOf(
2236-
Duration.between(prevItem.getTimestamp(), nextItem.getTimestamp()).toMillis());
2241+
weight = BigDecimal.valueOf(Duration.between(prevInstant, nextInstant).toMillis());
22372242
}
22382243
}
22392244
prevItem = nextItem;
2245+
prevInstant = nextInstant;
22402246
break;
22412247
case RIGHT:
22422248
nextState = getPersistedValue(nextItem, unit);
22432249
if (nextState != null) {
22442250
value = nextState.toBigDecimal();
22452251
if (prevItem == null) {
2246-
weight = BigDecimal.valueOf(Duration.between(begin, nextItem.getTimestamp()).toMillis());
2252+
weight = BigDecimal.valueOf(Duration.between(begin, nextInstant).toMillis());
22472253
} else {
2248-
weight = BigDecimal.valueOf(
2249-
Duration.between(prevItem.getTimestamp(), nextItem.getTimestamp()).toMillis());
2254+
weight = BigDecimal.valueOf(Duration.between(prevInstant, nextInstant).toMillis());
22502255
}
22512256
}
22522257
prevItem = nextItem;
2258+
prevInstant = nextInstant;
22532259
break;
22542260
case TRAPEZOIDAL:
22552261
if (prevItem != null) {
@@ -2258,11 +2264,11 @@ private static BigDecimal riemannSum(ZonedDateTime begin, ZonedDateTime end, Ite
22582264
if (prevState != null && nextState != null) {
22592265
value = prevState.toBigDecimal().add(nextState.toBigDecimal())
22602266
.divide(BigDecimal.valueOf(2));
2261-
weight = BigDecimal.valueOf(
2262-
Duration.between(prevItem.getTimestamp(), nextItem.getTimestamp()).toMillis());
2267+
weight = BigDecimal.valueOf(Duration.between(prevInstant, nextInstant).toMillis());
22632268
}
22642269
}
22652270
prevItem = nextItem;
2271+
prevInstant = nextInstant;
22662272
break;
22672273
case MIDPOINT:
22682274
if (prevItem != null) {
@@ -2272,19 +2278,20 @@ private static BigDecimal riemannSum(ZonedDateTime begin, ZonedDateTime end, Ite
22722278
if (midpointStartBucket && !prevDuration.isZero() && prevState != null) {
22732279
// Add half of the start bucket with the start value (left approximation)
22742280
sum = sum.add(prevState.toBigDecimal()
2275-
.multiply(BigDecimal.valueOf(prevDuration.dividedBy(2).toMillis())));
2281+
.multiply(BigDecimal.valueOf(prevDuration.toMillis() / 2)));
22762282
midpointStartBucket = false;
22772283
}
2278-
nextDuration = Duration.between(prevItem.getTimestamp(), nextItem.getTimestamp());
2284+
nextDuration = Duration.between(prevInstant, nextInstant);
22792285
weight = prevDuration.isZero() || nextDuration.isZero() ? BigDecimal.ZERO
2280-
: BigDecimal.valueOf(prevDuration.plus(nextDuration).dividedBy(2).toMillis());
2286+
: BigDecimal.valueOf(prevDuration.plus(nextDuration).toMillis() / 2);
22812287
if (!nextDuration.isZero()) {
22822288
prevDuration = nextDuration;
22832289
}
22842290
prevState = currentState;
22852291
}
22862292
}
22872293
prevItem = nextItem;
2294+
prevInstant = nextInstant;
22882295
break;
22892296
}
22902297
sum = sum.add(value.multiply(weight));
@@ -2295,7 +2302,7 @@ private static BigDecimal riemannSum(ZonedDateTime begin, ZonedDateTime end, Ite
22952302
DecimalType dtState = getPersistedValue(prevItem, unit);
22962303
if (dtState != null) {
22972304
BigDecimal value = dtState.toBigDecimal();
2298-
BigDecimal weight = BigDecimal.valueOf(prevDuration.dividedBy(2).toMillis());
2305+
BigDecimal weight = BigDecimal.valueOf(prevDuration.toMillis() / 2);
22992306
sum = sum.add(value.multiply(weight));
23002307
}
23012308
}

0 commit comments

Comments
 (0)