Series: lastValue + lastDate

This commit is contained in:
Patrick Haßel 2025-02-26 11:29:05 +01:00
parent 483331f6df
commit a0d15b1aa4
3 changed files with 47 additions and 20 deletions

View File

@ -1,10 +1,11 @@
package de.ph87.data.series; package de.ph87.data.series;
import de.ph87.data.value.Value;
import de.ph87.data.value.*; import de.ph87.data.value.*;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
import java.time.*;
@Entity @Entity
@Getter @Getter
@ToString @ToString
@ -56,24 +57,37 @@ public class Series {
@Column(nullable = false) @Column(nullable = false)
private int count; private int count;
public Series(@NonNull final String name, @NonNull final Unit unit, @NonNull final SeriesType type, @NonNull final Value value) { @Column(nullable = false)
this.name = name; private double lastValue;
this.title = name;
this.unit = unit; @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; this.type = type;
final double converted = value.as(unit).value; final double converted = inbound.value.as(unit).value;
this.min = converted; this.min = converted;
this.max = converted; this.max = converted;
this.avg = converted; this.avg = converted;
this.count = 1; this.count = 1;
this.lastDate = inbound.date;
this.lastValue = converted;
} }
public void update(@NonNull final Value value) { public void update(@NonNull final SeriesInbound inbound) {
final double converted = value.as(unit).value; final double converted = inbound.value.as(unit).value;
this.min = Math.min(this.min, converted); this.min = Math.min(this.min, converted);
this.max = Math.max(this.max, converted); this.max = Math.max(this.max, converted);
this.avg = (this.avg * this.count + converted) / ++this.count; this.avg = (this.avg * this.count + converted) / ++this.count;
if (this.lastDate.isBefore(inbound.date)) {
this.lastDate = inbound.date;
this.lastValue = converted;
}
} }
} }

View File

@ -1,9 +1,10 @@
package de.ph87.data.series; package de.ph87.data.series;
import de.ph87.data.value.*; import de.ph87.data.value.*;
import jakarta.annotation.*;
import lombok.*; import lombok.*;
import java.time.*;
@Getter @Getter
@ToString @ToString
public class SeriesDto { public class SeriesDto {
@ -20,9 +21,22 @@ public class SeriesDto {
public final SeriesType type; 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) { public SeriesDto(@NonNull final Series series) {
this.id = series.getId(); this.id = series.getId();
@ -33,13 +47,12 @@ public class SeriesDto {
this.type = series.getType(); this.type = series.getType();
this.graphZero = series.isGraphZero(); this.graphZero = series.isGraphZero();
this.autoscale = series.isAutoscale(); this.autoscale = series.isAutoscale();
} this.min = series.getMin();
this.max = series.getMax();
public String format(@Nullable final Double value) { this.avg = series.getAvg();
if (value == null || Double.isNaN(value)) { this.count = series.getCount();
return "--- %s".formatted(unit.unit); this.lastDate = series.getLastDate();
} this.lastValue = series.getLastValue();
return "%%.%df %%s".formatted(decimals).formatted(value, unit.unit);
} }
} }

View File

@ -57,14 +57,14 @@ public class SeriesService {
final Series series = seriesRepository final Series series = seriesRepository
.findByName(inbound.getName()) .findByName(inbound.getName())
.orElseGet(() -> { .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); publish(fresh, Action.CREATED);
return fresh; return fresh;
}); });
if (series.getType() != type) { if (series.getType() != type) {
log.warn("Existing Series type does not match requested type: requested={}, series={}", type, series); log.warn("Existing Series type does not match requested type: requested={}, series={}", type, series);
} }
series.update(inbound.value); series.update(inbound);
return series; return series;
} }