Aktueller Stand

This commit is contained in:
tobias 2025-02-25 23:14:36 +01:00
parent c4b8ea7106
commit 8b547d9798
4 changed files with 99 additions and 1 deletions

View File

@ -86,7 +86,7 @@ public class FillAggregateTableMain {
while (rs.next() && currentTimestamp <= endTimestamp) {
final long rsTimestamp = rs.getLong("timestamp");
final long rsValue = rs.getLong("value");
final long tsDiff = rsTimestamp - Math.min(endTimestamp, currentTimestamp);
final long tsDiff = Math.min(rsTimestamp - currentTimestamp, endTimestamp - currentTimestamp);
currentTimestamp = rsTimestamp;
if (rsValue > 0) {
wattMillisecondsPos += (rsValue * tsDiff);

View File

@ -2,6 +2,8 @@ package info.peper.vz.rest;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
@ -13,15 +15,20 @@ import org.springframework.web.bind.annotation.RestController;
import info.peper.vz.rest.bo.Sums;
import info.peper.vz.rest.bo.db.Aggregate;
import info.peper.vz.rest.bo.db.Data;
@RestController
class VzRestController {
private static final Logger log = LoggerFactory.getLogger(VzRestController.class);
private final AggregateRepository aggregateRep;
private final DataRepository dataRep;
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private JdbcTemplate jdbcTemplateWithLimitedRows;
public VzRestController(final AggregateRepository aggregateRep, final DataRepository dataRep) {
this.aggregateRep = aggregateRep;
@ -32,17 +39,44 @@ class VzRestController {
Sums getData(@RequestParam("timestampStart")final long timestampStart,
@RequestParam("timestampEnd")final long timestampEnd,
@RequestParam("channelId")final int channelId) {
this.jdbcTemplateWithLimitedRows.setMaxRows(1000);
final List<Aggregate> aggregates = jdbcTemplate.query(
"SELECT * FROM volkszaehler.tobias_aggregate WHERE channel_id=? AND timestamp_start>=? AND timestamp_end<=? ORDER BY timestamp_start;",
(rs, rowNum) -> new Aggregate(rs.getInt("channel_id"), rs.getLong("timestamp_start"), rs.getLong("timestamp_end"),
rs.getLong("sum_positive"), rs.getLong("sum_negative")),
channelId ,timestampStart, timestampEnd);
long firstTimestamp = Long.MAX_VALUE;
long lastTimestamp = Long.MIN_VALUE;
long sumPos = 0;
long sumNeg = 0;
for (Aggregate ag : aggregates) {
if (ag.getTimestampStart() < firstTimestamp) {
firstTimestamp = ag.getTimestampStart();
}
if (ag.getTimestampEnd() > lastTimestamp) {
lastTimestamp = ag.getTimestampEnd();
}
sumPos += ag.getSumPositive();
sumNeg += ag.getSumNegative();
}
log.debug("sumPos: " + sumPos + " / sumNeg: " + sumNeg);
log.debug("timestampStart: " + timestampStart);
log.debug("timestampEnd: " + timestampEnd);
log.debug("firstTimestamp: " + firstTimestamp);
log.debug("lastTimestamp: " + lastTimestamp);
if (timestampStart < firstTimestamp ) {
final Sums startSums = getSums(timestampStart, firstTimestamp, channelId);
sumPos += startSums.getSumPositive();
sumNeg += startSums.getSumNegative();
log.debug("Start: " + startSums.toString());
}
if (timestampEnd > lastTimestamp) {
final Sums endSums = getSums(lastTimestamp, timestampEnd, channelId);
sumPos += endSums.getSumPositive();
sumNeg += endSums.getSumNegative();
log.debug("End: " + endSums.toString());
}
return new Sums(sumPos, sumNeg);
}
@ -56,5 +90,33 @@ class VzRestController {
return aggregateRep.save(aggregate);
}
private Sums getSums(final long startTimestamp,
final long endTimestamp,
final int channelId) {
long currentTimestamp = startTimestamp;
long wattMillisecondsPos = 0;
long wattMillisecondsNeg = 0;
while (currentTimestamp < endTimestamp) {
final List<Data> datas = jdbcTemplateWithLimitedRows.query("SELECT * FROM volkszaehler.data WHERE channel_id=? AND timestamp>? ORDER BY timestamp;",
(rs, rowNum) -> new Data(rs.getInt("channel_id"), rs.getLong("timestamp"), rs.getDouble("value")),
channelId, currentTimestamp);
for (Data data : datas) {
if (currentTimestamp <= endTimestamp)
{
final long tsDiff = Math.min(data.getTimestamp() - currentTimestamp, endTimestamp - currentTimestamp);
currentTimestamp = data.getTimestamp();
if (data.getValue() > 0.0) {
wattMillisecondsPos += (data.getValue() * tsDiff);
}
if (data.getValue() < 0.0) {
wattMillisecondsNeg += (-data.getValue() * tsDiff);
}
}
}
}
return new Sums(wattMillisecondsPos/3600, wattMillisecondsNeg/3600);
}
}

View File

@ -40,6 +40,10 @@ public class Sums implements Serializable {
return false;
return true;
}
@Override
public String toString() {
return "Sums [sumPositive=" + sumPositive + ", sumNegative=" + sumNegative + "]";
}
}

View File

@ -49,4 +49,36 @@ public class Data {
@Column(name="value")
private double value;
public Data(int channelId, long timestamp, double value) {
super();
this.channelId = channelId;
this.timestamp = timestamp;
this.value = value;
}
public int getChannelId() {
return channelId;
}
public void setChannelId(int channelId) {
this.channelId = channelId;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
}