Commit df9f1688 authored by neop's avatar neop
Browse files

parse timestamps in many forms

parent ca733824
......@@ -84,9 +84,9 @@ class DayUploader {
csv.lines()
.map(line -> line.split(";"))
.map(data -> {
.forEach(data -> {
var point = Point.measurement("sensor")
.time(Instant.parse(data[index_of_field.get("timestamp")] + "Z"), WritePrecision.S);
.time(Util.parseAnyDateTime(data[index_of_field.get("timestamp")]), WritePrecision.NS);
for (var tag_column : tag_columns) {
point.addTag(tag_column, (String) sensor.properties.get(tag_column));
......@@ -94,9 +94,9 @@ class DayUploader {
for (var field_column : field_columns) {
point.addField(field_column, data[index_of_field.get(field_column)]);
}
return point;
})
.forEach(point -> Main.influxdb_write_api.writePoint("bucket0", "openforecast", point));
Main.influxdb_write_api.writePoint(point);
});
}
Main.influxdb_write_api.flush();
......
......@@ -109,17 +109,17 @@ public class Main {
}
private static void run() throws Exception {
// System.out.println("starting influx query");
// var tables = influxDBClient.getQueryApi().query("from(bucket:\"openforecast\") |> range(start: -600d1h, stop: -600d)");
System.out.println("starting influx query");
var tables = influxDBClient.getQueryApi().query("from(bucket:\"bucket0\") |> range(start: -10000d1h, stop: -1d)");
// for (var fluxTable : tables) {
// System.out.println("table " + fluxTable);
// for (var record : fluxTable.getRecords()) {
// System.out.println(String.format("%s %s: %s %s", record.getTime(), record.getMeasurement(), record.getField(), record.getValue()));
// record.getValues().forEach((key, value) -> System.out.println(key + ":" + value));
// }
// }
// System.out.println("finished influx query");
for (var fluxTable : tables) {
System.out.println("table " + fluxTable);
for (var record : fluxTable.getRecords()) {
System.out.println(String.format("%s %s: %s %s", record.getTime(), record.getMeasurement(), record.getField(), record.getValue()));
record.getValues().forEach((key, value) -> System.out.println(key + ":" + value));
}
}
System.out.println("finished influx query");
try_uploading_failed_sensors();
upload_all_days();
......
......@@ -8,6 +8,8 @@ import java.util.stream.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.time.*;
import java.time.format.*;
public class Util {
private static int timeout_millis = 120000;
......@@ -63,6 +65,46 @@ public class Util {
}
Files.writeString(Path.of(fname), line + '\n', StandardOpenOption.APPEND);
}
public static Instant parseAnyDateTime(String str) {
// formatters for times directly convertible to Instant
for (var formatter : List.of(
DateTimeFormatter.ISO_INSTANT,
DateTimeFormatter.ISO_OFFSET_DATE_TIME
)) {
try {
return formatter.parse(str, Instant::from);
} catch (DateTimeParseException ex) {}
}
// formatters for local times assumed to be UTC
try {
return Instant.from(
DateTimeFormatter.ISO_LOCAL_DATE_TIME.parse(str, LocalDateTime::from)
.atOffset(ZoneOffset.UTC)
);
} catch (DateTimeParseException ex) {}
// save until Sa 20. Nov 18:46:40 CET 2286
var as_long = Long.parseLong(str);
switch (str.length()) {
case 10:
return Instant.ofEpochSecond(as_long);
case 13:
return Instant.ofEpochMilli(as_long);
case 16:
// may suffer from overlow
return Instant.ofEpochSecond(0, as_long * 1000);
case 19:
return Instant.ofEpochSecond(0, as_long);
}
throw new DateTimeException("what kind of timestamp is this supposed to be?: " + str);
}
/*
public static void main(String[] args) throws MalformedURLException, IOException {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment