influxdb 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5317b3f282142902370f70efe284c0b3bc26fd33
4
- data.tar.gz: 7c8d66553e69ca83a7c7bd195c15b720ce566db4
3
+ metadata.gz: e4c1d9fba941cead2557e6067144733298d81599
4
+ data.tar.gz: 4859b9674377a0c4cd613f79b4db5488179523ca
5
5
  SHA512:
6
- metadata.gz: 4aa6353445b0e3741e9618389f96db0494c0bca4d9287275db4eb09abbc092adea05fd294800f7a3b06fd80402fdf58fdebbc9174a493c0abb379841b7a7c307
7
- data.tar.gz: 6d04fd6f05351872f029af105b2c7a183d136ab76be4580339e97bb9d58c5ff37d48c1a81e54e1ece16a4ad58a13ff660a064d275305aa70520fccbf61bd6fcf
6
+ metadata.gz: 65633c47e35680828609e2ed6b84dc74f72c506b3c58271db3aa69b18debccd2a5b3e0bace93eefbaa7fe1a0965a28f2142abb4eb5af072fa5833e2b3f15cf5e
7
+ data.tar.gz: 41492fac4c940702fbe15d2c17bf3c3c6afd54b70d3777b09eeab563b8ba9bde13a2ee1885fe1569a13fd91952877520544addf5b5ed1184f569cbef4de2a393
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
+
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  influxdb-ruby
2
2
  =============
3
3
 
4
- ![InfluxDB Build Status](https://travis-ci.org/influxdb/influxdb-ruby.png)
4
+ [![Build Status](https://travis-ci.org/influxdb/influxdb-ruby.png?branch=master)](https://travis-ci.org/influxdb/influxdb-ruby)
5
5
 
6
6
  Ruby client library for [InfluxDB](http://influxdb.org/).
7
7
 
data/influxdb.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_runtime_dependency "json"
22
+ spec.add_runtime_dependency "addressable"
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.3"
24
25
  spec.add_development_dependency "rake"
@@ -1,5 +1,6 @@
1
1
  require 'net/http'
2
2
  require 'json'
3
+ require 'addressable/uri'
3
4
 
4
5
  module InfluxDB
5
6
  class Client
@@ -69,15 +70,44 @@ module InfluxDB
69
70
  url = "/db/#{@database}/series?u=#{@username}&p=#{@password}"
70
71
  payload = {:name => name, :points => [], :columns => []}
71
72
 
72
- point = []
73
- data.each_pair do |k,v|
74
- payload[:columns].push k.to_s
75
- point.push v
73
+ data = data.is_a?(Array) ? data : [data]
74
+ columns = data.reduce(:merge).keys
75
+ payload[:columns] = columns
76
+
77
+ data.each do |p|
78
+ point = []
79
+ columns.each { |c| point << p[c] }
80
+ payload[:points].push point
76
81
  end
77
82
 
78
- payload[:points].push point
79
83
  data = JSON.generate([payload])
80
84
  response = http.request(Net::HTTP::Post.new(url), data)
81
85
  end
86
+
87
+ def query query
88
+ http = Net::HTTP.new(@host, @port)
89
+ url = "/db/#{@database}/query?u=#{@username}&p=#{@password}&q=#{query}"
90
+ url = Addressable::URI.encode url
91
+ response = http.request(Net::HTTP::Get.new(url))
92
+ series = JSON.parse(response.body)
93
+
94
+ if block_given?
95
+ series.each { |s| yield s['name'], denormalize_series(s) }
96
+ else
97
+ series.reduce({}) do |col, s|
98
+ name = s['name']
99
+ denormalized_series = denormalize_series s
100
+ col[name] = denormalized_series
101
+ col
102
+ end
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def denormalize_series series
109
+ columns = series['columns']
110
+ series['values'].map { |point| Hash[columns.zip(point)]}
111
+ end
82
112
  end
83
113
  end
@@ -1,3 +1,3 @@
1
1
  module InfluxDB
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -91,5 +91,68 @@ describe InfluxDB::Client do
91
91
 
92
92
  @influxdb.write_point("seriez", data).should be_a(Net::HTTPOK)
93
93
  end
94
+
95
+ it "should POST multiple points" do
96
+ body = [{
97
+ :name => "seriez",
98
+ :points => [["juan", 87], ["shahid", 99]],
99
+ :columns => ["name", "age"]
100
+ }]
101
+
102
+ stub_request(:post, "http://influxdb.test:9999/db/database/series").with(
103
+ :query => {:u => "username", :p => "password"},
104
+ :body => JSON.generate(body)
105
+ )
106
+
107
+ data = [{:name => "juan", :age => 87}, { :name => "shahid", :age => 99}]
108
+
109
+ @influxdb.write_point("seriez", data).should be_a(Net::HTTPOK)
110
+ end
111
+
112
+ it "should POST multiple points with missing columns" do
113
+ body = [{
114
+ :name => "seriez",
115
+ :points => [["juan", 87], ["shahid", nil]],
116
+ :columns => ["name", "age"]
117
+ }]
118
+
119
+ stub_request(:post, "http://influxdb.test:9999/db/database/series").with(
120
+ :query => {:u => "username", :p => "password"},
121
+ :body => JSON.generate(body)
122
+ )
123
+
124
+ data = [{:name => "juan", :age => 87}, { :name => "shahid"}]
125
+
126
+ @influxdb.write_point("seriez", data).should be_a(Net::HTTPOK)
127
+ end
128
+ end
129
+
130
+ describe "#execute_queries" do
131
+ before(:each) do
132
+ data = [{ :name => "foo", :columns => ["name", "age"], :values => [["shahid", 99],["dix", 50]]}]
133
+
134
+ stub_request(:get, "http://influxdb.test:9999/db/database/query").with(
135
+ :query => { :q => "select * from foo", :u => "username", :p => "password"}
136
+ ).to_return(
137
+ :body => JSON.generate(data)
138
+ )
139
+ end
140
+
141
+ expected_series = { 'foo' => [{"name" => "shahid", "age" => 99}, {"name" => "dix", "age" => 50}]}
142
+
143
+ it 'can execute a query with a block' do
144
+ series = { }
145
+
146
+ @influxdb.query "select * from foo" do |name, points|
147
+ series[name] = points
148
+ end
149
+
150
+ series.should ==(expected_series)
151
+ end
152
+
153
+ it 'can execute a query without a block' do
154
+ series = @influxdb.query 'select * from foo'
155
+ series.should ==(expected_series)
156
+ end
94
157
  end
95
158
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: influxdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Todd Persen
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: addressable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement