Data/src/main/java/de/ph87/data/series/Series.java

100 lines
2.1 KiB
Java

package de.ph87.data.series;
import de.ph87.data.value.Unit;
import jakarta.annotation.Nullable;
import jakarta.persistence.*;
import lombok.*;
import java.time.ZonedDateTime;
@Entity
@Getter
@ToString
@NoArgsConstructor
public class Series {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Setter
@NonNull
@Column(nullable = false, unique = true)
private String name;
@Setter
@NonNull
@Column(nullable = false, unique = true)
private String title;
@NonNull
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private Unit unit;
@Setter
@Column(nullable = false)
private int decimals = 1;
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private SeriesType type;
@Column
@Nullable
public final Double yMin = null;
@Column
@Nullable
public final Double yMax = null;
@Column(nullable = false)
private boolean autoscale = false;
@Column(nullable = false)
private double min;
@Column(nullable = false)
private double max;
@Column(nullable = false)
private double avg;
@Column(nullable = false)
private int count;
@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 = 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 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;
}
}
}