diff --git a/Project.toml b/Project.toml index b5bc498..fb35268 100644 --- a/Project.toml +++ b/Project.toml @@ -6,7 +6,9 @@ version = "0.1.0" [deps] CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" +Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" DuckDB = "d2f5444f-75bc-4fdf-ac35-56f514c445e1" +HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3" JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" diff --git a/src/TulipaIO.jl b/src/TulipaIO.jl index 70007aa..9a58b07 100644 --- a/src/TulipaIO.jl +++ b/src/TulipaIO.jl @@ -2,6 +2,8 @@ module TulipaIO include("exceptions.jl") +# InfluxDB client +include("influx.jl") # ESDL JSON parser include("parsers.jl") diff --git a/src/influxdb.jl b/src/influxdb.jl new file mode 100644 index 0000000..25e172c --- /dev/null +++ b/src/influxdb.jl @@ -0,0 +1,49 @@ +module InfluxDB + +import JSON3 +import HTTP +import DataFrames as DF +import Dates: DateTime + +# NOTE: this doesn't actually do anything smart like batching +# or keeping an open connection, it just remembers the connection +# details +struct InfluxDBClient + host::String + database::String + port::Int + path::String + username::String + password::String +end + +InfluxDBClient(host::String, database::String) = + InfluxDBClient(host, database, 8086, "query", "", "") + +function query( + client::InfluxDBClient, + measurement::String, + time_range_start::DateTime, + time_range_end::DateTime, +) + # NOTE: the query is not escaped, so no untrusted input should be accepted here + db_query = "SELECT time, value FROM \"$measurement\" WHERE time >= $time_range_start AND time <= $time_range_end" + url_params = ["db" => client.database, "q" => db_query] + uri = HTTP.URI(; + scheme = "http", + host = client.host, + path = client.path, + port = client.port, + query = url_params, + ) + + response = HTTP.get(uri) + parsed = JSON3.read(response.body) + + rows = parsed["results"][1]["series"][1]["values"] + columns = [[x[1] for x in rows], [x[2] for x in rows]] + df = DF.DataFrame(columns, parsed["results"][1]["series"][1]["columns"]) + return df +end + +end