Skip to content

Commit

Permalink
Implement simple InfluxDB client (#19)
Browse files Browse the repository at this point in the history
* Implement simple InfluxDB client

* Put InfluxDBClient inside a module, rename it to be consistent with Influx API

* Rename influx.jl to influxdb.jl

---------

Co-authored-by: Suvayu Ali <[email protected]>
  • Loading branch information
wcoenraads and suvayu authored Apr 8, 2024
1 parent 34640a7 commit b805885
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/TulipaIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module TulipaIO

include("exceptions.jl")

# InfluxDB client
include("influx.jl")
# ESDL JSON parser
include("parsers.jl")

Expand Down
49 changes: 49 additions & 0 deletions src/influxdb.jl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b805885

Please sign in to comment.