Skip to content

Commit

Permalink
Merge pull request #3 from aca-labs/master
Browse files Browse the repository at this point in the history
update to work with InfluxDB 1.5 and crystal 0.25.1
  • Loading branch information
stakach authored Aug 4, 2018
2 parents d303af5 + f16f3f2 commit ab84c1f
Show file tree
Hide file tree
Showing 23 changed files with 78 additions and 105 deletions.
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/doc/
/libs/
/.crystal/
/.shards/
doc
lib
.crystal
.shards


# Libraries don't need dependency lock
# Dependencies will be locked in application that uses them
/shard.lock
shard.lock

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ db.write "your_series", InfluxDB::Fields{:a_field => 100, :value => 10000},
tags: InfluxDB::Tags{:region => "us"}, timestamp: Time.now
```

Write a point synchronously
Write a point asynchronously

```crystal
db.write "your_series", 10, sync: true # => true or false depending on the response
spawn { db.write "your_series", 10 }
```

## Contributing
Expand Down
1 change: 0 additions & 1 deletion examples/basic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ blah = influx.databases["blah"]
blah.select.from("many_series").each do |res|
p res
end

11 changes: 2 additions & 9 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
name: influxdb
version: 0.1.1
version: 0.2.0

authors:
- Jerome Gravel-Niquet <[email protected]>
- Stephen von Takach <[email protected]>

license: MIT

development_dependencies:
webmock:
github: manastech/webmock.cr
branch: master
spec2:
github: waterlink/spec2.cr
version: ~> 0.7.1
14 changes: 2 additions & 12 deletions spec/influxdb/database_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require "../spec_helper"

describe InfluxDB::Database do

describe "#drop" do
it "drops the database" do
db = dbs.create("test_hello")
Expand All @@ -11,7 +10,6 @@ describe InfluxDB::Database do
end

describe "#select" do

it "returns a Query instance" do
db.select.should be_a(InfluxDB::Query)
end
Expand All @@ -20,11 +18,9 @@ describe InfluxDB::Database do
q = db.select("hello")
q.fields.should eq("hello")
end

end

describe "#write" do

it "with PointValue" do
pv = InfluxDB::PointValue.new("some_series", InfluxDB::Fields{:value => 10})
db.write(pv).should eq(true)
Expand All @@ -39,17 +35,11 @@ describe InfluxDB::Database do
end

it "with many values" do
db.write do |points|
db.write { |points|
points.write "many_series", InfluxDB::Fields{:value => 1}, tags: {:from_block => "yes"}
points.write "many_series", InfluxDB::Fields{:value => 11}, tags: {:from_block => "yes", :second => "ah"}
points.write "many_series", InfluxDB::Fields{:value => 111}, tags: {:from_block => "yes", :third => "oh"}
end.should eq(true)
}.should eq(true)
end

it "synchronized" do
db.write("some_series", InfluxDB::Fields{:value => 10}, sync: true).should eq(true)
end

end

end
2 changes: 1 addition & 1 deletion spec/influxdb/databases_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ describe InfluxDB::Databases do
end
end
end
end
end
5 changes: 1 addition & 4 deletions spec/influxdb/query_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
require "../spec_helper"

describe InfluxDB::Query do



end
end
33 changes: 23 additions & 10 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
require "webmock"
require "../src/influxdb"
require "spec"

WebMock.allow_net_connect = true

Spec.before_each do
db.drop
dbs.create "test"
end

class Runtime
@dbs : InfluxDB::Databases?
@db : InfluxDB::Database?
@db : InfluxDB::Database?

def client
@client ||= InfluxDB::Client.new
end

def client; @client ||= InfluxDB::Client.new; end
def dbs; (@dbs ||= client.databases).not_nil!; end
def db; (@db ||= dbs["test"]).not_nil!; end
def dbs
(@dbs ||= client.databases).not_nil!
end

def db
(@db ||= dbs["test"]).not_nil!
end

INSTANCE = new
end

def client; Runtime::INSTANCE.client; end
def dbs; Runtime::INSTANCE.dbs; end
def db; Runtime::INSTANCE.db; end
def client
Runtime::INSTANCE.client
end

def dbs
Runtime::INSTANCE.dbs
end

def db
Runtime::INSTANCE.db
end
3 changes: 1 addition & 2 deletions src/influxdb.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ require "./influxdb/*"

module InfluxDB
alias Key = String | Symbol
alias Value = Int32 | Int64 | Float32 | Float64 | String
alias Value = Int32 | Int64 | Float32 | Float64 | String | Bool
alias Tags = Hash(Key, Value)
alias Fields = Hash(Key, Value)
# TODO Put your code here
end
6 changes: 3 additions & 3 deletions src/influxdb/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ module InfluxDB
include Databases
include Users

DEFAULT_URL = "http://localhost:8086"
DEFAULT_URL = "http://localhost:8086"
DEFAULT_USERNAME = "root"
DEFAULT_PASSWORD = "root"

forward_missing_to client

@client : HTTP::Client?

def initialize(url = DEFAULT_URL, @username = DEFAULT_USERNAME, @password = DEFAULT_PASSWORD)
@url = URI.parse(url)
end

def host
@url.host
end

def port
@url.port
end
Expand All @@ -46,6 +47,5 @@ module InfluxDB
raise Exception.new("InfluxDB error: #{err_msg}")
end
end

end
end
4 changes: 1 addition & 3 deletions src/influxdb/client/databases.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module InfluxDB
class Client
module Databases

def databases
InfluxDB::Databases.new(self)
end

end
end
end
end
2 changes: 1 addition & 1 deletion src/influxdb/client/exception.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ module InfluxDB
class Exception < ::Exception
end
end
end
end
2 changes: 1 addition & 1 deletion src/influxdb/client/exceptions/unauthorized.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ module InfluxDB
end
end
end
end
end
4 changes: 1 addition & 3 deletions src/influxdb/client/users.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module InfluxDB
class Client
module Users

def users(db = "")
InfluxDB::Users.new(self, db)
end

end
end
end
end
52 changes: 25 additions & 27 deletions src/influxdb/database.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,57 @@ module InfluxDB
# end

def initialize(@client : Client, @name : String)
@mutex = Mutex.new
end

# https://docs.influxdata.com/influxdb/latest/query_language/database_management/#delete-a-database-with-drop-database
def drop
query "DROP DATABASE IF EXISTS #{name}"
query "DROP DATABASE #{name}"
true
end

def select(fields = "*")
Query.new(@client, name).select(fields)
end

def write(point_value : PointValue, sync = false)
write [point_value], sync: sync
def write(point_value : PointValue)
write [point_value]
end

def write(point_values : Array(PointValue), sync = false)
body = String.build do |str|
def write(point_values : Array(PointValue))
body = String.build { |str|
point_values.each_with_index do |pv, i|
pv.to_s(str)
str << "\n" unless i == point_values.size - 1
end
end.strip

if sync
send_write(body).status_code == 204
else
spawn { send_write(body) }
true
end
}.strip

send_write(body).status_code == 204
end

private def send_write(body)
@client.post "/write?db=#{name}",
HTTP::Headers{
"Content-Type" => "application/octet-stream",
"User-Agent" => "influxdb.cr v#{InfluxDB::VERSION}"
},
body
@mutex.synchronize do
@client.post "/write?db=#{name}&precision=ms",
HTTP::Headers{
"Content-Type" => "application/octet-stream",
},
body
end
end

def write(series : String, fields : Fields, tags = Tags.new, timestamp : Time? = nil, sync = false)
timestamp = Time.now if sync == false && timestamp.nil?
write PointValue.new(series, tags: tags, fields: fields, timestamp: timestamp), sync: sync
def write(series : String, fields : Fields, tags = Tags.new, timestamp : Time? = nil)
timestamp = Time.now if timestamp.nil?
write PointValue.new(series, tags: tags, fields: fields, timestamp: timestamp)
end

def write(series : String, value : Value, tags = Tags.new, timestamp : Time? = nil, sync = false)
write series, Fields{:value => value}, tags: tags, timestamp: timestamp, sync: sync
def write(series : String, value : Value, tags = Tags.new, timestamp : Time? = nil)
write series, Fields{:value => value}, tags: tags, timestamp: timestamp
end

def write(sync = false)
pw = PointsWriter.new(sync: sync)
def write
pw = PointsWriter.new
yield pw
write pw.points, sync: sync
write pw.points
end
end
end
4 changes: 1 addition & 3 deletions src/influxdb/databases.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module InfluxDB
struct Databases

include Enumerable(Database)
delegate :query, to: @client

Expand All @@ -19,7 +18,7 @@ module InfluxDB
def all
res = query("SHOW DATABASES")
dbs = [] of Database
res[0]["series"][0]["values"].each do |arr|
res[0]["series"][0]["values"].as_a.each do |arr|
dbs << Database.new(@client, arr[0].as_s)
end
dbs
Expand All @@ -28,6 +27,5 @@ module InfluxDB
def each
all.each { |db| yield db }
end

end
end
12 changes: 5 additions & 7 deletions src/influxdb/point_value.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module InfluxDB
struct PointValue

getter :series, :tags, :fields, :timestamp

def initialize(
Expand Down Expand Up @@ -40,7 +39,7 @@ module InfluxDB
end

private def map(h, quote_escape)
h.map do |k,v|
h.map do |k, v|
key = escape_key(k)
val = v.is_a?(String) ? escape_value(v, quote_escape) : v
"#{key}=#{val}"
Expand All @@ -52,13 +51,12 @@ module InfluxDB
end

private def escape_value(value, quote_escape)
val = value.
gsub(/\s/, "\ ").
gsub(",", "\,").
gsub("\"", "\\\"")
val = value
.gsub(/\s/, "\ ")
.gsub(",", "\,")
.gsub("\"", "\\\"")
val = %("#{val}") if quote_escape
val
end

end
end
Loading

0 comments on commit ab84c1f

Please sign in to comment.