From a0d15b1aa4daa0c8c82edd518ad77d222211734f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Wed, 26 Feb 2025 11:29:05 +0100 Subject: [PATCH] Series: lastValue + lastDate --- src/main/java/de/ph87/data/series/Series.java | 30 ++++++++++++----- .../java/de/ph87/data/series/SeriesDto.java | 33 +++++++++++++------ .../de/ph87/data/series/SeriesService.java | 4 +-- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/main/java/de/ph87/data/series/Series.java b/src/main/java/de/ph87/data/series/Series.java index 6d3cd62..275022e 100644 --- a/src/main/java/de/ph87/data/series/Series.java +++ b/src/main/java/de/ph87/data/series/Series.java @@ -1,10 +1,11 @@ package de.ph87.data.series; -import de.ph87.data.value.Value; import de.ph87.data.value.*; import jakarta.persistence.*; import lombok.*; +import java.time.*; + @Entity @Getter @ToString @@ -56,24 +57,37 @@ public class Series { @Column(nullable = false) private int count; - public Series(@NonNull final String name, @NonNull final Unit unit, @NonNull final SeriesType type, @NonNull final Value value) { - this.name = name; - this.title = name; - this.unit = unit; + @Column(nullable = false) + private double lastValue; + + @NonNull + @Column(nullable = false) + private ZonedDateTime lastDate; + + public Series(@NonNull final SeriesInbound inbound, @NonNull final SeriesType type) { + this.name = inbound.name; + this.title = inbound.name; + this.unit = inbound.value.unit; this.type = type; - final double converted = value.as(unit).value; + final double converted = inbound.value.as(unit).value; this.min = converted; this.max = converted; this.avg = converted; this.count = 1; + this.lastDate = inbound.date; + this.lastValue = converted; } - public void update(@NonNull final Value value) { - final double converted = value.as(unit).value; + public void update(@NonNull final SeriesInbound inbound) { + final double converted = inbound.value.as(unit).value; this.min = Math.min(this.min, converted); this.max = Math.max(this.max, converted); this.avg = (this.avg * this.count + converted) / ++this.count; + if (this.lastDate.isBefore(inbound.date)) { + this.lastDate = inbound.date; + this.lastValue = converted; + } } } diff --git a/src/main/java/de/ph87/data/series/SeriesDto.java b/src/main/java/de/ph87/data/series/SeriesDto.java index ba64bce..e93e97b 100644 --- a/src/main/java/de/ph87/data/series/SeriesDto.java +++ b/src/main/java/de/ph87/data/series/SeriesDto.java @@ -1,9 +1,10 @@ package de.ph87.data.series; import de.ph87.data.value.*; -import jakarta.annotation.*; import lombok.*; +import java.time.*; + @Getter @ToString public class SeriesDto { @@ -20,9 +21,22 @@ public class SeriesDto { public final SeriesType type; - private final boolean graphZero; + public final boolean graphZero; - private final boolean autoscale; + public final boolean autoscale; + + public final double min; + + public final double max; + + public final double avg; + + public final int count; + + public final double lastValue; + + @NonNull + public final ZonedDateTime lastDate; public SeriesDto(@NonNull final Series series) { this.id = series.getId(); @@ -33,13 +47,12 @@ public class SeriesDto { this.type = series.getType(); this.graphZero = series.isGraphZero(); this.autoscale = series.isAutoscale(); - } - - public String format(@Nullable final Double value) { - if (value == null || Double.isNaN(value)) { - return "--- %s".formatted(unit.unit); - } - return "%%.%df %%s".formatted(decimals).formatted(value, unit.unit); + this.min = series.getMin(); + this.max = series.getMax(); + this.avg = series.getAvg(); + this.count = series.getCount(); + this.lastDate = series.getLastDate(); + this.lastValue = series.getLastValue(); } } diff --git a/src/main/java/de/ph87/data/series/SeriesService.java b/src/main/java/de/ph87/data/series/SeriesService.java index 51a046a..c4721be 100644 --- a/src/main/java/de/ph87/data/series/SeriesService.java +++ b/src/main/java/de/ph87/data/series/SeriesService.java @@ -57,14 +57,14 @@ public class SeriesService { final Series series = seriesRepository .findByName(inbound.getName()) .orElseGet(() -> { - final Series fresh = seriesRepository.save(new Series(inbound.name, inbound.value.unit, type, inbound.value)); + final Series fresh = seriesRepository.save(new Series(inbound, type)); publish(fresh, Action.CREATED); return fresh; }); if (series.getType() != type) { log.warn("Existing Series type does not match requested type: requested={}, series={}", type, series); } - series.update(inbound.value); + series.update(inbound); return series; }