Series: lastValue + lastDate
This commit is contained in:
parent
483331f6df
commit
a0d15b1aa4
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user