-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement simple InfluxDB client (#19)
* 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
1 parent
34640a7
commit b805885
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |