Aktueller Stand
This commit is contained in:
parent
92f0be4761
commit
2b2dbf5e22
@ -27,17 +27,20 @@ public class FillAggregateTableMain {
|
||||
"vz",
|
||||
getPassword())) {
|
||||
long startTimeStamp = getTimestamp("2022-06-01T00:00:00");
|
||||
final long finalEndTimeStamp = getTimestamp("2025-02-24T00:00:00");
|
||||
final long finalEndTimeStamp = getTimestamp("2025-03-01T00:00:00");
|
||||
|
||||
long endTimeStamp = startTimeStamp + (24*60*60*1000);
|
||||
long endTimeStamp = startTimeStamp + (60*60*1000);
|
||||
while (endTimeStamp < finalEndTimeStamp) {
|
||||
if (startTimeStamp % 86400000 == 0) {
|
||||
System.out.println(DTF.format(Instant.ofEpochMilli(startTimeStamp)));
|
||||
}
|
||||
for (int channelId : CHANNELS_TO_USE) {
|
||||
System.out.print(DTF.format(Instant.ofEpochMilli(startTimeStamp)) + ": " + channelId + ": ");
|
||||
// System.out.print(DTF.format(Instant.ofEpochMilli(startTimeStamp)) + ": " + channelId + ": ");
|
||||
final long[] values = getValues(con, startTimeStamp, endTimeStamp, channelId);
|
||||
saveValues(con, startTimeStamp, endTimeStamp, channelId, values);
|
||||
}
|
||||
startTimeStamp += 24*60*60*1000;
|
||||
endTimeStamp += 24*60*60*1000;
|
||||
startTimeStamp += 60*60*1000;
|
||||
endTimeStamp += 60*60*1000;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -80,12 +83,13 @@ public class FillAggregateTableMain {
|
||||
|
||||
int numberOfRows = 0;
|
||||
while (currentTimestamp < endTimestamp) {
|
||||
try (final PreparedStatement stmt = con.prepareStatement("SELECT * FROM volkszaehler.data WHERE channel_id=? AND timestamp>? AND timestamp<=? ORDER BY timestamp;")) {
|
||||
System.out.print("#");
|
||||
stmt.setMaxRows(5000);
|
||||
try (final PreparedStatement stmt = con.prepareStatement("SELECT * FROM volkszaehler.data WHERE channel_id=? AND timestamp>? ORDER BY timestamp;")) {
|
||||
// System.out.print("#");
|
||||
stmt.setMaxRows(500);
|
||||
stmt.setFetchSize(100);
|
||||
stmt.setInt(1, channelId);
|
||||
stmt.setLong(2, currentTimestamp);
|
||||
stmt.setLong(3, currentTimestamp + (864000000));
|
||||
// stmt.setLong(3, currentTimestamp + (864000000));
|
||||
try (final ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next() && currentTimestamp <= endTimestamp) {
|
||||
numberOfRows++;
|
||||
@ -102,7 +106,7 @@ public class FillAggregateTableMain {
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println(" " + numberOfRows);
|
||||
// System.out.println(" " + numberOfRows);
|
||||
return new long[] {wattMillisecondsPos/3600, wattMillisecondsNeg/3600};
|
||||
|
||||
}
|
||||
|
||||
163
src/main/java/info/peper/vz/rest/FillAggregateTableMain2.java
Normal file
163
src/main/java/info/peper/vz/rest/FillAggregateTableMain2.java
Normal file
@ -0,0 +1,163 @@
|
||||
package info.peper.vz.rest;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class FillAggregateTableMain2 {
|
||||
|
||||
private static final DateTimeFormatter DTF = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault());
|
||||
private static final int[] CHANNELS_TO_USE = new int[] {1, 4, };
|
||||
private static final List<AggregateToSave> SAVELIST = new LinkedList<AggregateToSave>();
|
||||
|
||||
private static class AggregateToSave {
|
||||
private final long startTs;
|
||||
private final long endTs;
|
||||
private final long valuePos;
|
||||
private final long valueNeg;
|
||||
private final int channelId;
|
||||
|
||||
private AggregateToSave(long startTs, long endTs, long valuePos, long valueNeg, int channelId) {
|
||||
super();
|
||||
this.startTs = startTs;
|
||||
this.endTs = endTs;
|
||||
this.valuePos = valuePos;
|
||||
this.valueNeg = valueNeg;
|
||||
this.channelId = channelId;
|
||||
}
|
||||
|
||||
private long getStartTs() {
|
||||
return startTs;
|
||||
}
|
||||
|
||||
private long getEndTs() {
|
||||
return endTs;
|
||||
}
|
||||
|
||||
private long getValuePos() {
|
||||
return valuePos;
|
||||
}
|
||||
|
||||
private long getValueNeg() {
|
||||
return valueNeg;
|
||||
}
|
||||
|
||||
private int getChannelId() {
|
||||
return channelId;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
try (final Connection con = DriverManager.getConnection(
|
||||
"jdbc:mariadb://mariadb.fritz.box/volkszaehler",
|
||||
"vz",
|
||||
getPassword())) {
|
||||
long startTimeStamp = getTimestamp("2022-05-20T09:00:00");
|
||||
final long finalEndTimeStamp = getTimestamp("2025-03-01T00:00:00");
|
||||
for (int channelId : CHANNELS_TO_USE) {
|
||||
processChannel(startTimeStamp, finalEndTimeStamp, 60*1000, channelId, con);
|
||||
}
|
||||
System.out.println("Saving...");
|
||||
for (AggregateToSave ats : SAVELIST) {
|
||||
saveValues(con, ats.startTs, ats.endTs, ats.channelId, new long[] {ats.valuePos, ats.valueNeg});
|
||||
System.out.println("####");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void processChannel(final long startTimestamp,
|
||||
final long endTimestamp,
|
||||
final long interval,
|
||||
final int channelId,
|
||||
final Connection con) throws SQLException {
|
||||
long currentTimestamp = startTimestamp;
|
||||
long intervalStartTimestamp = startTimestamp;
|
||||
long intervalEndTimestamp = startTimestamp + interval;
|
||||
long intervalPos = 0;
|
||||
long intervalNeg = 0;
|
||||
try (final PreparedStatement stmt = con.prepareStatement("SELECT * FROM volkszaehler.data WHERE channel_id=? AND timestamp>? ORDER BY timestamp;")) {
|
||||
stmt.setInt(1, channelId);
|
||||
stmt.setLong(2, startTimestamp);
|
||||
stmt.setFetchSize(1000);
|
||||
boolean noNextRecord = false;
|
||||
try (final ResultSet rs = stmt.executeQuery()) {
|
||||
while (noNextRecord || (rs.next() && currentTimestamp <= endTimestamp)) {
|
||||
noNextRecord = false;
|
||||
final long timestamp = rs.getLong("timestamp");
|
||||
final long value = rs.getLong("value");
|
||||
final long tsDiff = Math.min(timestamp - currentTimestamp, intervalEndTimestamp - currentTimestamp);
|
||||
currentTimestamp = timestamp;
|
||||
if (value > 0) {
|
||||
intervalPos += (value * tsDiff);
|
||||
} else if (value < 0) {
|
||||
intervalNeg += (-value * tsDiff);
|
||||
}
|
||||
if (timestamp >= intervalEndTimestamp) {
|
||||
// System.out.println(
|
||||
// DTF.format(Instant.ofEpochMilli(intervalStartTimestamp)) + " - " +
|
||||
// DTF.format(Instant.ofEpochMilli(intervalEndTimestamp)) + " / " +
|
||||
// (intervalPos/3600) + " / " +
|
||||
// (intervalNeg/3600));
|
||||
SAVELIST.add(new AggregateToSave(intervalStartTimestamp, intervalEndTimestamp, intervalPos/3600, intervalNeg/3600, channelId));
|
||||
intervalStartTimestamp += interval;
|
||||
intervalEndTimestamp += interval;
|
||||
currentTimestamp = intervalStartTimestamp;
|
||||
intervalPos = 0;
|
||||
intervalNeg = 0;
|
||||
noNextRecord = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String getPassword() throws IOException {
|
||||
try (final InputStream is = new FileInputStream("db-password.txt")) {
|
||||
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
|
||||
return bufferedReader.readLine();
|
||||
}
|
||||
}
|
||||
|
||||
private static long getTimestamp(final String dateTime) {
|
||||
final TemporalAccessor tempAccessor = DTF.parse(dateTime);
|
||||
final Instant instant = Instant.from(tempAccessor);
|
||||
return Instant.EPOCH.until(instant, ChronoUnit.MILLIS);
|
||||
}
|
||||
|
||||
private static void saveValues(final Connection con,
|
||||
final long startTimestamp,
|
||||
final long endTimestamp,
|
||||
final int channelId,
|
||||
final long[] values) throws SQLException {
|
||||
try (final PreparedStatement stmt = con.prepareStatement("INSERT INTO tobias_aggregate (channel_id, timestamp_start, timestamp_end, sum_positive, sum_negative) VALUES (?, ?, ?, ?, ?)")) {
|
||||
stmt.setInt(1, channelId);
|
||||
stmt.setLong(2, startTimestamp);
|
||||
stmt.setLong(3, endTimestamp);
|
||||
stmt.setLong(4, values[0]);
|
||||
stmt.setLong(5, values[1]);
|
||||
stmt.execute();
|
||||
}
|
||||
}
|
||||
|
||||
private static void saveValues(final Connection con) throws SQLException {
|
||||
try (final PreparedStatement stmt = con.prepareStatement("INSERT INTO tobias_aggregate (channel_id, timestamp_start, timestamp_end, sum_positive, sum_negative) VALUES (?, ?, ?, ?, ?)")) {
|
||||
for (AggregateToSave ats : SAVELIST) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -20,42 +20,24 @@ import java.time.temporal.TemporalAccessor;
|
||||
public class ReadDbMain {
|
||||
|
||||
private static final DateTimeFormatter DTF = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault());
|
||||
private static final int[] CHANNELS_TO_USE = new int[] {1, 4, };
|
||||
private static final int[] CHANNELS_TO_USE = new int[] {4};
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
try (final Connection con = DriverManager.getConnection(
|
||||
"jdbc:mariadb://mariadb.fritz.box/volkszaehler",
|
||||
"vz",
|
||||
getPassword());
|
||||
final Statement stmt = con.createStatement();) {
|
||||
stmt.setFetchSize(10);
|
||||
stmt.setMaxRows(24*60*60);
|
||||
try (final ResultSet rs = stmt.executeQuery("SELECT * FROM data WHERE channel_id=1 AND timestamp>1656021600000");) {
|
||||
for (int i = 0; i < 50000; i++) {
|
||||
if (rs.next()) {
|
||||
rs.getDouble("value");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void oldMain(String[] args) throws Exception {
|
||||
try (final Connection con = DriverManager.getConnection(
|
||||
"jdbc:mariadb://mariadb.fritz.box/volkszaehler",
|
||||
"vz",
|
||||
getPassword())) {
|
||||
long startTimeStamp = getTimestamp("2022-06-01T00:00:00");
|
||||
long endTimeStamp = startTimeStamp + (24*60*60*1000);
|
||||
final long finalEndTimeStamp = getTimestamp("2025-02-24T00:00:00");
|
||||
long startTimeStamp = getTimestamp("2025-02-28T18:00:00");
|
||||
long endTimeStamp = startTimeStamp + (60*60*1000);
|
||||
final long finalEndTimeStamp = getTimestamp("2025-03-01T18:00:00");
|
||||
while (endTimeStamp < finalEndTimeStamp) {
|
||||
for (int channelId : CHANNELS_TO_USE) {
|
||||
final long[] values = getValues(con, startTimeStamp, endTimeStamp, channelId);
|
||||
saveValues(con, startTimeStamp, endTimeStamp, channelId, values);
|
||||
System.out.println(DTF.format(Instant.ofEpochMilli(startTimeStamp)) + ": " + channelId);
|
||||
// saveValues(con, startTimeStamp, endTimeStamp, channelId, values);
|
||||
System.out.println("{x: " + startTimeStamp + ", y: " + (values[0]*60/1000) + "},");
|
||||
}
|
||||
startTimeStamp += 24*60*60*1000;
|
||||
endTimeStamp += 24*60*60*1000;
|
||||
startTimeStamp += 60*60*1000;
|
||||
endTimeStamp += 60*60*1000;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -97,8 +79,8 @@ public class ReadDbMain {
|
||||
long wattMillisecondsNeg = 0;
|
||||
|
||||
while (currentTimestamp < endTimestamp) {
|
||||
try (final PreparedStatement stmt = con.prepareStatement("SELECT * FROM volkszaehler.data WHERE channel_id=? AND timestamp>?ORDER BY timestamp;")) {
|
||||
stmt.setMaxRows(10000);
|
||||
try (final PreparedStatement stmt = con.prepareStatement("SELECT * FROM volkszaehler.data WHERE channel_id=? AND timestamp>? ORDER BY timestamp;")) {
|
||||
stmt.setMaxRows(1000);
|
||||
stmt.setInt(1, channelId);
|
||||
stmt.setLong(2, currentTimestamp);
|
||||
try (final ResultSet rs = stmt.executeQuery()) {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user