Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
j.hoerdt
sensor2graph
Commits
df9f1688
Commit
df9f1688
authored
Dec 07, 2021
by
neop
Browse files
parse timestamps in many forms
parent
ca733824
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/sensor2graph/DayUploader.java
View file @
df9f1688
...
...
@@ -84,9 +84,9 @@ class DayUploader {
csv
.
lines
()
.
map
(
line
->
line
.
split
(
";"
))
.
map
(
data
->
{
.
forEach
(
data
->
{
var
point
=
Point
.
measurement
(
"sensor"
)
.
time
(
Instant
.
pars
e
(
data
[
index_of_field
.
get
(
"timestamp"
)]
+
"Z"
),
WritePrecision
.
S
);
.
time
(
Util
.
parseAnyDateTim
e
(
data
[
index_of_field
.
get
(
"timestamp"
)]),
WritePrecision
.
N
S
);
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
();
...
...
src/main/java/sensor2graph/Main.java
View file @
df9f1688
...
...
@@ -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: -
6
00d1h, stop: -
600
d)");
System
.
out
.
println
(
"starting influx query"
);
var
tables
=
influxDBClient
.
getQueryApi
().
query
(
"from(bucket:\"
bucket0
\") |> range(start: -
100
00d1h, stop: -
1
d)"
);
//
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
();
...
...
src/main/java/sensor2graph/Util.java
View file @
df9f1688
...
...
@@ -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 {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment