Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement simple InfluxDB client #19

Merged
merged 4 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions src/TulipaIO.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module TulipaIO

include("influx.jl")
include("parsers.jl")
include("pipeline.jl")

Expand Down
50 changes: 50 additions & 0 deletions src/influx.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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 InfluxClient
host::String
database::String
port::Int
path::String
username::String
password::String
InfluxClient(

Check warning on line 16 in src/influx.jl

View check run for this annotation

Codecov / codecov/patch

src/influx.jl#L16

Added line #L16 was not covered by tests
host::String,
database::String,
port::Int = 8086,
path::String = "query",
username::String = "",
password::String = "",
) = new(host, database, port, username, password, path)

Check warning on line 23 in src/influx.jl

View check run for this annotation

Codecov / codecov/patch

src/influx.jl#L23

Added line #L23 was not covered by tests
end
Copy link
Member

@suvayu suvayu Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this kind of convenience constructors can be inner. You should move this to outer, then you cannot use new. So, probably this:

Suggested change
InfluxClient(
host::String,
database::String,
port::Int = 8086,
path::String = "query",
username::String = "",
password::String = "",
) = new(host, database, port, username, password, path)
end
end
InfluxClient(
host::String,
database::String,
) = InfluxClient(host, database, 8086, "query", "", "")


function query(

Check warning on line 26 in src/influx.jl

View check run for this annotation

Codecov / codecov/patch

src/influx.jl#L26

Added line #L26 was not covered by tests
client::InfluxClient,
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(;

Check warning on line 35 in src/influx.jl

View check run for this annotation

Codecov / codecov/patch

src/influx.jl#L33-L35

Added lines #L33 - L35 were not covered by tests
scheme = "http",
host = client.host,
path = client.path,
port = client.port,
query = url_params,
)

response = HTTP.get(uri)
parsed = JSON3.read(response.body)

Check warning on line 44 in src/influx.jl

View check run for this annotation

Codecov / codecov/patch

src/influx.jl#L43-L44

Added lines #L43 - L44 were not covered by tests

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

Check warning on line 49 in src/influx.jl

View check run for this annotation

Codecov / codecov/patch

src/influx.jl#L46-L49

Added lines #L46 - L49 were not covered by tests
end
Loading