Skip to content

Commit

Permalink
Make gapfiller work
Browse files Browse the repository at this point in the history
  • Loading branch information
zeekoe committed Jan 29, 2024
1 parent 58e9f69 commit 7719778
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 21 deletions.
67 changes: 52 additions & 15 deletions src/main/java/com/github/zeekoe/bluebird/heatpump/Gapfiller.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,92 @@
import com.github.zeekoe.bluebird.heatpump.model.HeatpumpLog;
import com.github.zeekoe.bluebird.influx.PointMapper;
import com.github.zeekoe.bluebird.influx.RealInfluxConnection;
import com.github.zeekoe.bluebird.infrastructure.BluebirdProperties;
import com.github.zeekoe.bluebird.infrastructure.BluebirdProperty;
import org.influxdb.dto.Point;

import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

public class Gapfiller {
public static void main(String[] args) throws IOException, InterruptedException {
final HeatpumpLog[] heatpumpLogs = new Heatpump().doHeatpumpGapRequest(2*60);
fillTheGaps(heatpumpLogs);
private ZonedDateTime gapStartTime;
private ZonedDateTime nextFillTime;
private int timeWindow;

public Gapfiller() {
this.gapStartTime = ZonedDateTime.now().minusHours(2);
System.out.println("Gapfiller fresh start, set gap start time to " + gapStartTime);
nextFillTime = ZonedDateTime.now().plusSeconds(10);
setTimeWindow();

}

private void setTimeWindow() {
this.timeWindow = 10; // minimum of 10 minutes to be gentle for the server
try {
String timeWindowString = BluebirdProperties.property(BluebirdProperty.GAPFILLER_WINDOW_MINUTES);
int timeWindowInt = Integer.parseInt(timeWindowString);
if (timeWindowInt > 10) {
this.timeWindow = timeWindowInt;
}
} catch (Exception ignored) {
}
}
private static void fillTheGaps(HeatpumpLog[] heatpumplogs) {

public void checkAndRun() {
try {
if (ZonedDateTime.now().isAfter(nextFillTime)) {
HeatpumpLog[] heatpumpLogs = new Heatpump().doHeatpumpGapRequest(gapStartTime);
fillTheGaps(heatpumpLogs);
gapStartTime = nextFillTime;
nextFillTime = ZonedDateTime.now()
.plusMinutes(timeWindow)
.plusSeconds(new Random().nextInt(20));
System.out.println("Gapfiller next window: " + gapStartTime + " - " + nextFillTime);
}
} catch (Exception e) {
System.out.println("Gapfiller failed: " + e.getMessage());
}
}

private void fillTheGaps(HeatpumpLog[] heatpumplogs) {
if (heatpumplogs.length < 2) {
System.out.println("Please call this method for a bigger period");
return;
}
List<Point> pointsTodo = new ArrayList<>();
int doneCount = 0;

final ZonedDateTime oldestLog = Arrays.stream(heatpumplogs).min(Comparator.comparing(HeatpumpLog::getTimestamp))
ZonedDateTime oldestLog = Arrays.stream(heatpumplogs).min(Comparator.comparing(HeatpumpLog::getTimestamp))
.orElseThrow().getTimestamp().truncatedTo(ChronoUnit.SECONDS);
final ZonedDateTime newestLog = Arrays.stream(heatpumplogs).max(Comparator.comparing(HeatpumpLog::getTimestamp))
ZonedDateTime newestLog = Arrays.stream(heatpumplogs).max(Comparator.comparing(HeatpumpLog::getTimestamp))
.orElseThrow().getTimestamp().truncatedTo(ChronoUnit.SECONDS);

final RealInfluxConnection influxConnection = new RealInfluxConnection();
System.out.println("Filling gaps from " + oldestLog + " to " + newestLog);

final List<ZonedDateTime> influxedTimes = influxConnection.retrieveInfluxedTimesBetween(oldestLog, newestLog);
RealInfluxConnection influxConnection = new RealInfluxConnection();
List<ZonedDateTime> influxedTimes = influxConnection.retrieveInfluxedTimesBetween(oldestLog, newestLog);

for (HeatpumpLog heatpumplog : heatpumplogs) {
final boolean alreadyInfluxed = influxedTimes.contains(heatpumplog.getTimestamp().truncatedTo(ChronoUnit.SECONDS));
System.out.println(heatpumplog.getTimestamp() + " " + (alreadyInfluxed ? "Y" : "N"));
if (!alreadyInfluxed) {
final Point point = PointMapper.map(heatpumplog);
if (!influxedTimes.contains(heatpumplog.getTimestamp().truncatedTo(ChronoUnit.SECONDS))) {
Point point = PointMapper.map(heatpumplog);
pointsTodo.add(point);

} else {
doneCount++;
}
}
System.out.println("Todo: " + pointsTodo.size() + ", done: " + doneCount);
System.out.println("\nWill fill gaps: " + pointsTodo.size() + ", already logged: " + doneCount);
if (doneCount > 4) { // detect errors
for (Point point : pointsTodo) {
influxConnection.writePoint(point);
}
}
System.out.println("Done!");
System.out.println("Gapfiller done!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import static com.github.zeekoe.bluebird.infrastructure.BluebirdProperty.WEHEAT_LOG_URL;

public class Heatpump implements Runnable {

public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.registerModule(new JavaTimeModule());
private static final MyHttpClient httpClient = new MyHttpClient();
private final Gapfiller gapfiller = new Gapfiller();

private final Auth auth;
private final InfluxConnection influxConnection;
Expand All @@ -43,6 +43,7 @@ public void run() {
final HeatpumpLog heatpumpLog = doHeatpumpRequest();
System.out.print(heatpumpLog.gettRoom() + " ");
influx(heatpumpLog);
gapfiller.checkAndRun();
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
Expand All @@ -58,9 +59,9 @@ private HeatpumpLog doHeatpumpRequest() throws IOException, InterruptedException
return OBJECT_MAPPER.readValue(responseBody, HeatpumpLog.class);
}

public HeatpumpLog[] doHeatpumpGapRequest(int minutes) throws IOException, InterruptedException {
public HeatpumpLog[] doHeatpumpGapRequest(ZonedDateTime gapStartTime) throws IOException, InterruptedException {
final ZonedDateTime now = ZonedDateTime.now();
final String from = formatDateTime(now.minusMinutes(minutes));
final String from = formatDateTime(gapStartTime);
final String to = formatDateTime(now);

String url = property(WEHEAT_LOG_URL).replace("/latest","") + "/raw?startTime=" + from + "&endTime=" + to;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class BluebirdProperties {
private static final Properties properties;

static {
final String configFile = "/etc/bluebird.config";
String configFile = "/etc/bluebird.config";
properties = new Properties();
try {
properties.load(new FileInputStream(configFile));
Expand All @@ -18,6 +18,10 @@ public class BluebirdProperties {
}

public static String property(BluebirdProperty property) {
return properties.getProperty(property.getKey());
String value = properties.getProperty(property.getKey());
if (value == null || value.isEmpty()) {
return property.getDefaultValue();
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,27 @@ public enum BluebirdProperty {
INFLUXDB_DATABASE("influxdb.database"),
INFLUXDB_USERNAME("influxdb.username"),
INFLUXDB_PASSWORD("influxdb.password"),
INFLUXDB_MEASUREMENT("influxdb.bluebird.measurement");
INFLUXDB_MEASUREMENT("influxdb.bluebird.measurement"),

GAPFILLER_WINDOW_MINUTES("gapfiller.window.minutes", "10");

private final String key;
private final String defaultValue;

BluebirdProperty(String key) {
this.key = key;
this.defaultValue = "";
}
BluebirdProperty(String key, String defaultValue) {
this.key = key;
this.defaultValue = defaultValue;
}

public String getKey() {
return key;
}

public String getDefaultValue() {
return defaultValue;
}
}

0 comments on commit 7719778

Please sign in to comment.