influxdb 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +41 -0
  4. data/.travis.yml +3 -2
  5. data/Gemfile +7 -1
  6. data/README.md +218 -102
  7. data/Rakefile +2 -6
  8. data/lib/influxdb.rb +15 -5
  9. data/lib/influxdb/client.rb +38 -433
  10. data/lib/influxdb/client/http.rb +123 -0
  11. data/lib/influxdb/config.rb +66 -0
  12. data/lib/influxdb/errors.rb +8 -2
  13. data/lib/influxdb/{logger.rb → logging.rb} +6 -5
  14. data/lib/influxdb/max_queue.rb +2 -1
  15. data/lib/influxdb/point_value.rb +27 -25
  16. data/lib/influxdb/query/cluster.rb +17 -0
  17. data/lib/influxdb/query/continuous_query.rb +22 -0
  18. data/lib/influxdb/query/core.rb +110 -0
  19. data/lib/influxdb/query/database.rb +21 -0
  20. data/lib/influxdb/query/retention_policy.rb +26 -0
  21. data/lib/influxdb/query/user.rb +41 -0
  22. data/lib/influxdb/version.rb +2 -2
  23. data/lib/influxdb/writer/async.rb +115 -0
  24. data/lib/influxdb/writer/udp.rb +21 -0
  25. data/spec/influxdb/cases/async_client_spec.rb +33 -0
  26. data/spec/influxdb/cases/query_cluster_spec.rb +65 -0
  27. data/spec/influxdb/cases/query_continuous_query_spec.rb +82 -0
  28. data/spec/influxdb/cases/query_core.rb +34 -0
  29. data/spec/influxdb/cases/query_database_spec.rb +58 -0
  30. data/spec/influxdb/cases/query_retention_policy_spec.rb +84 -0
  31. data/spec/influxdb/cases/query_series_spec.rb +50 -0
  32. data/spec/influxdb/cases/query_shard_space_spec.rb +105 -0
  33. data/spec/influxdb/cases/query_shard_spec.rb +43 -0
  34. data/spec/influxdb/cases/query_user_spec.rb +127 -0
  35. data/spec/influxdb/cases/querying_spec.rb +149 -0
  36. data/spec/influxdb/cases/retry_requests_spec.rb +102 -0
  37. data/spec/influxdb/cases/udp_client_spec.rb +21 -0
  38. data/spec/influxdb/cases/write_points_spec.rb +140 -0
  39. data/spec/influxdb/client_spec.rb +37 -810
  40. data/spec/influxdb/config_spec.rb +118 -0
  41. data/spec/influxdb/{logger_spec.rb → logging_spec.rb} +4 -8
  42. data/spec/influxdb/max_queue_spec.rb +29 -0
  43. data/spec/influxdb/point_value_spec.rb +81 -14
  44. data/spec/influxdb/worker_spec.rb +8 -11
  45. data/spec/spec_helper.rb +7 -10
  46. metadata +65 -30
  47. data/lib/influxdb/udp_client.rb +0 -16
  48. data/lib/influxdb/worker.rb +0 -80
  49. data/spec/influxdb/udp_client_spec.rb +0 -33
  50. data/spec/influxdb_spec.rb +0 -4
  51. data/spec/max_queue_spec.rb +0 -32
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+ require "json"
3
+
4
+ describe InfluxDB::Client do
5
+ let(:subject) do
6
+ described_class.new(
7
+ "database",
8
+ {
9
+ host: "influxdb.test",
10
+ port: 9999,
11
+ username: "username",
12
+ password: "password",
13
+ time_precision: "s"
14
+ }.merge(args)
15
+ )
16
+ end
17
+
18
+ let(:args) { {} }
19
+
20
+ describe "#create_database" do
21
+ before do
22
+ stub_request(:get, "http://influxdb.test:9999/query").with(
23
+ query: { u: "username", p: "password", q: "CREATE DATABASE foo" }
24
+ )
25
+ end
26
+
27
+ it "should GET to create a new database" do
28
+ expect(subject.create_database("foo")).to be_a(Net::HTTPOK)
29
+ end
30
+ end
31
+
32
+ describe "#delete_database" do
33
+ before do
34
+ stub_request(:get, "http://influxdb.test:9999/query").with(
35
+ query: { u: "username", p: "password", q: "DROP DATABASE foo" }
36
+ )
37
+ end
38
+
39
+ it "should GET to remove a database" do
40
+ expect(subject.delete_database("foo")).to be_a(Net::HTTPOK)
41
+ end
42
+ end
43
+
44
+ describe "#list_databases" do
45
+ let(:response) { { "results" => [{ "series" => [{ "name" => "databases", "columns" => ["name"], "values" => [["foobar"]] }] }] } }
46
+ let(:expected_result) { [{ "name" => "foobar" }] }
47
+
48
+ before do
49
+ stub_request(:get, "http://influxdb.test:9999/query").with(
50
+ query: { u: "username", p: "password", q: "SHOW DATABASES" }
51
+ ).to_return(body: JSON.generate(response), status: 200)
52
+ end
53
+
54
+ it "should GET a list of databases" do
55
+ expect(subject.list_databases).to eq(expected_result)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,84 @@
1
+ require "spec_helper"
2
+ require "json"
3
+
4
+ describe InfluxDB::Client do
5
+ let(:subject) do
6
+ described_class.new(
7
+ "database",
8
+ {
9
+ host: "influxdb.test",
10
+ port: 9999,
11
+ username: "username",
12
+ password: "password",
13
+ time_precision: "s"
14
+ }.merge(args)
15
+ )
16
+ end
17
+
18
+ let(:args) { {} }
19
+
20
+ describe "#list_retention_policies" do
21
+ let(:response) { { "results" => [{ "series" => [{ "columns" => %w(name duration replicaN default), "values" => [["default", "0", 1, true], ["another", "1", 2, false]] }] }] } }
22
+ let(:expected_result) { [{ "name" => "default", "duration" => "0", "replicaN" => 1, "default" => true }, { "name" => "another", "duration" => "1", "replicaN" => 2, "default" => false }] }
23
+
24
+ before do
25
+ stub_request(:get, "http://influxdb.test:9999/query").with(
26
+ query: { u: "username", p: "password", q: "SHOW RETENTION POLICIES \"database\"" }
27
+ ).to_return(body: JSON.generate(response), status: 200)
28
+ end
29
+
30
+ it "should GET a list of retention policies" do
31
+ expect(subject.list_retention_policies('database')).to eq(expected_result)
32
+ end
33
+ end
34
+
35
+ describe "#create_retention_policy" do
36
+ context "default" do
37
+ before do
38
+ stub_request(:get, "http://influxdb.test:9999/query")
39
+ .with(
40
+ query:
41
+ {
42
+ u: "username",
43
+ p: "password",
44
+ q: "CREATE RETENTION POLICY \"1h.cpu\" ON foo DURATION 1h REPLICATION 2 DEFAULT"
45
+ }
46
+ )
47
+ end
48
+
49
+ it "should GET to create a new database" do
50
+ expect(subject.create_retention_policy('1h.cpu', 'foo', '1h', 2, true)).to be_a(Net::HTTPOK)
51
+ end
52
+ end
53
+
54
+ context "non-default" do
55
+ before do
56
+ stub_request(:get, "http://influxdb.test:9999/query")
57
+ .with(
58
+ query:
59
+ {
60
+ u: "username",
61
+ p: "password",
62
+ q: "CREATE RETENTION POLICY \"1h.cpu\" ON foo DURATION 1h REPLICATION 2"
63
+ }
64
+ )
65
+ end
66
+
67
+ it "should GET to create a new database" do
68
+ expect(subject.create_retention_policy('1h.cpu', 'foo', '1h', 2)).to be_a(Net::HTTPOK)
69
+ end
70
+ end
71
+ end
72
+
73
+ describe "#delete_retention_policy" do
74
+ before do
75
+ stub_request(:get, "http://influxdb.test:9999/query").with(
76
+ query: { u: "username", p: "password", q: "DROP RETENTION POLICY \"1h.cpu\" ON foo" }
77
+ )
78
+ end
79
+
80
+ it "should GET to remove a database" do
81
+ expect(subject.delete_retention_policy('1h.cpu', 'foo')).to be_a(Net::HTTPOK)
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+ require "json"
3
+
4
+ describe InfluxDB::Client do
5
+ let(:subject) do
6
+ described_class.new(
7
+ "database",
8
+ {
9
+ host: "influxdb.test",
10
+ port: 9999,
11
+ username: "username",
12
+ password: "password",
13
+ time_precision: "s"
14
+ }.merge(args)
15
+ )
16
+ end
17
+
18
+ let(:args) { {} }
19
+
20
+ ### TODO ###
21
+
22
+ # describe "DELETE #delete_series" do
23
+ # it "removes a series" do
24
+ # stub_request(:delete, "http://influxdb.test:9999/db/database/series/foo").with(
25
+ # query: { u: "username", p: "password" }
26
+ # )
27
+
28
+ # expect(subject.delete_series("foo")).to be_a(Net::HTTPOK)
29
+ # end
30
+ # end
31
+
32
+ # describe "GET #list_series" do
33
+ # it "returns a list of all series names" do
34
+ # data = [
35
+ # { "name" => "list_series_result",
36
+ # "columns" => %w(time name),
37
+ # "points" => [[0, 'a'], [0, 'b']]
38
+ # }
39
+ # ]
40
+
41
+ # stub_request(:get, "http://influxdb.test:9999/db/database/series").with(
42
+ # query: { u: "username", p: "password", q: "list series", time_precision: "s" }
43
+ # ).to_return(
44
+ # body: JSON.generate(data)
45
+ # )
46
+
47
+ # expect(subject.list_series).to eq %w(a b)
48
+ # end
49
+ # end
50
+ end
@@ -0,0 +1,105 @@
1
+ # TODO: support 0.9.x
2
+
3
+ # require "spec_helper"
4
+ # require "json"
5
+
6
+ # describe InfluxDB::Client do
7
+ # let(:subject) do
8
+ # described_class.new(
9
+ # "database",
10
+ # {
11
+ # host: "influxdb.test",
12
+ # port: 9999,
13
+ # username: "username",
14
+ # password: "password",
15
+ # time_precision: "s"
16
+ # }.merge(args)
17
+ # )
18
+ # end
19
+
20
+ # let(:args) { {} }
21
+
22
+ # let(:url) { "http://influxdb.test:9999/cluster/shard_spaces" }
23
+ # let(:req_query) { { u: "username", p: "password" } }
24
+ # let(:req_body) { nil }
25
+ # let(:request_params) { { query: req_query, body: req_body } }
26
+ # let(:response) { { body: JSON.generate(shard_spaces), status: 200 } }
27
+ # let(:shard_spaces) { [subject.default_shard_space_options.merge("database" => "foo")] }
28
+
29
+ # context "GET methods" do
30
+ # before { stub_request(:get, url).with(request_params).to_return(response) }
31
+
32
+ # describe "GET #list_shard_spaces" do
33
+ # it 'returns OK' do
34
+ # expect(subject.list_shard_spaces).to eq shard_spaces
35
+ # end
36
+ # end
37
+
38
+ # describe "GET #shard_space_info" do
39
+ # context "non-empty list" do
40
+ # it "returns shard space info" do
41
+ # expect(subject.shard_space_info('foo', 'default')).to eq shard_spaces.first
42
+ # end
43
+ # end
44
+
45
+ # context "returns an empty list" do
46
+ # let(:shard_spaces) { [] }
47
+
48
+ # it "returns no shard space" do
49
+ # expect(subject.shard_space_info('foo', 'default')).to be_nil
50
+ # end
51
+ # end
52
+ # end
53
+ # end
54
+
55
+ # describe "POST #create_shard_space" do
56
+ # let(:url) { "http://influxdb.test:9999/cluster/shard_spaces/foo" }
57
+ # let(:req_body) { subject.default_shard_space_options }
58
+ # let(:response) { { status: 200 } }
59
+
60
+ # before { stub_request(:post, url).with(request_params).to_return(response) }
61
+
62
+ # it 'returns OK' do
63
+ # expect(subject.create_shard_space("foo", subject.default_shard_space_options))
64
+ # .to be_a(Net::HTTPOK)
65
+ # end
66
+ # end
67
+
68
+ # describe "DELETE #delete_shard_space" do
69
+ # let(:url) { "http://influxdb.test:9999/cluster/shard_spaces/foo/default" }
70
+ # let(:response) { { status: 200 } }
71
+ # before { stub_request(:delete, url).with(request_params).to_return(response) }
72
+
73
+ # it 'returns OK' do
74
+ # expect(subject.delete_shard_space("foo", "default")).to be_a(Net::HTTPOK)
75
+ # end
76
+ # end
77
+
78
+ # describe "#update_shard_space" do
79
+ # let(:post_url) { "http://influxdb.test:9999/cluster/shard_spaces/foo/default" }
80
+ # let(:post_request_params) do
81
+ # {
82
+ # query: req_query,
83
+ # body: subject.default_shard_space_options.merge("shardDuration" => "30d")
84
+ # }
85
+ # end
86
+
87
+ # it 'gets the shard space and updates the shard space' do
88
+ # stub_request(:get, url).with(request_params).to_return(response)
89
+ # stub_request(:post, post_url).with(post_request_params)
90
+
91
+ # expect(subject.update_shard_space("foo", "default", "shardDuration" => "30d")).to be_a(Net::HTTPOK)
92
+ # end
93
+ # end
94
+
95
+ # describe "POST #configure_database" do
96
+ # let(:url) { "http://influxdb.test:9999/cluster/database_configs/foo" }
97
+ # let(:req_body) { subject.default_database_configuration }
98
+
99
+ # before { stub_request(:post, url).with(request_params) }
100
+
101
+ # it "returns OK" do
102
+ # expect(subject.configure_database("foo")).to be_a(Net::HTTPOK)
103
+ # end
104
+ # end
105
+ # end
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+ require "json"
3
+
4
+ describe InfluxDB::Client do
5
+ let(:subject) do
6
+ described_class.new(
7
+ "database",
8
+ {
9
+ host: "influxdb.test",
10
+ port: 9999,
11
+ username: "username",
12
+ password: "password",
13
+ time_precision: "s"
14
+ }.merge(args)
15
+ )
16
+ end
17
+
18
+ let(:args) { {} }
19
+
20
+ ### TODO ###
21
+
22
+ # describe "GET #list_shards" do
23
+ # it "returns a list of shards" do
24
+ # shard_list = { "longTerm" => [], "shortTerm" => [] }
25
+ # stub_request(:get, "http://influxdb.test:9999/cluster/shards").with(
26
+ # query: { u: "username", p: "password" }
27
+ # ).to_return(body: JSON.generate(shard_list, status: 200))
28
+
29
+ # expect(subject.list_shards).to eq shard_list
30
+ # end
31
+ # end
32
+
33
+ # describe "DELETE #delete_shard" do
34
+ # it "removes shard by id" do
35
+ # shard_id = 1
36
+ # stub_request(:delete, "http://influxdb.test:9999/cluster/shards/#{shard_id}").with(
37
+ # query: { u: "username", p: "password" }
38
+ # )
39
+
40
+ # expect(subject.delete_shard(shard_id, [1, 2])).to be_a(Net::HTTPOK)
41
+ # end
42
+ # end
43
+ end
@@ -0,0 +1,127 @@
1
+ require "spec_helper"
2
+ require "json"
3
+
4
+ describe InfluxDB::Client do
5
+ let(:subject) do
6
+ described_class.new(
7
+ "database",
8
+ {
9
+ host: "influxdb.test",
10
+ port: 9999,
11
+ username: "username",
12
+ password: "password",
13
+ time_precision: "s"
14
+ }.merge(args)
15
+ )
16
+ end
17
+
18
+ let(:args) { {} }
19
+
20
+ describe "#update user password" do
21
+ let(:user) { 'useruser' }
22
+ let(:pass) { 'passpass' }
23
+ let(:query) { "SET PASSWORD FOR #{user} = '#{pass}'" }
24
+
25
+ before do
26
+ stub_request(:get, "http://influxdb.test:9999/query").with(
27
+ query: { u: "username", p: "password", q: query }
28
+ )
29
+ end
30
+
31
+ it "should GET to update user password" do
32
+ expect(subject.update_user_password(user, pass)).to be_a(Net::HTTPOK)
33
+ end
34
+ end
35
+
36
+ describe "#grant_user_privileges" do
37
+ let(:user) { 'useruser' }
38
+ let(:perm) { :write }
39
+ let(:db) { 'foo' }
40
+ let(:query) { "GRANT #{perm.to_s.upcase} ON #{db} TO #{user}" }
41
+
42
+ before do
43
+ stub_request(:get, "http://influxdb.test:9999/query").with(
44
+ query: { u: "username", p: "password", q: query }
45
+ )
46
+ end
47
+
48
+ it "should GET to grant privileges for a user on a database" do
49
+ expect(subject.grant_user_privileges(user, db, perm)).to be_a(Net::HTTPOK)
50
+ end
51
+ end
52
+
53
+ describe "#revoke_user_privileges" do
54
+ let(:user) { 'useruser' }
55
+ let(:perm) { :write }
56
+ let(:db) { 'foo' }
57
+ let(:query) { "REVOKE #{perm.to_s.upcase} ON #{db} FROM #{user}" }
58
+
59
+ before do
60
+ stub_request(:get, "http://influxdb.test:9999/query").with(
61
+ query: { u: "username", p: "password", q: query }
62
+ )
63
+ end
64
+
65
+ it "should GET to revoke privileges from a user on a database" do
66
+ expect(subject.revoke_user_privileges(user, db, perm)).to be_a(Net::HTTPOK)
67
+ end
68
+ end
69
+
70
+ describe "#create_database_user" do
71
+ let(:user) { 'useruser' }
72
+ let(:pass) { 'passpass' }
73
+ let(:db) { 'foo' }
74
+ let(:query) { "CREATE user #{user} WITH PASSWORD '#{pass}'; GRANT ALL ON #{db} TO #{user}" }
75
+
76
+ before do
77
+ stub_request(:get, "http://influxdb.test:9999/query").with(
78
+ query: { u: "username", p: "password", q: query }
79
+ )
80
+ end
81
+
82
+ context "without specifying permissions" do
83
+ it "should GET to create a new database user with all permissions" do
84
+ expect(subject.create_database_user(db, user, pass)).to be_a(Net::HTTPOK)
85
+ end
86
+ end
87
+
88
+ context "with passing permission as argument" do
89
+ let(:permission) { :read }
90
+ let(:query) { "CREATE user #{user} WITH PASSWORD '#{pass}'; GRANT #{permission.to_s.upcase} ON #{db} TO #{user}" }
91
+
92
+ it "should GET to create a new database user with permission set" do
93
+ expect(subject.create_database_user(db, user, pass, permissions: permission)).to be_a(Net::HTTPOK)
94
+ end
95
+ end
96
+ end
97
+
98
+ describe "#delete_user" do
99
+ let(:user) { 'useruser' }
100
+ let(:query) { "DROP USER #{user}" }
101
+
102
+ before do
103
+ stub_request(:get, "http://influxdb.test:9999/query").with(
104
+ query: { u: "username", p: "password", q: query }
105
+ )
106
+ end
107
+
108
+ it "should GET to delete a user" do
109
+ expect(subject.delete_user(user)).to be_a(Net::HTTPOK)
110
+ end
111
+ end
112
+
113
+ describe "#list_users" do
114
+ let(:response) { { "results" => [{ "series" => [{ "columns" => %w(user admin), "values" => [["dbadmin", true], ["foobar", false]] }] }] } }
115
+ let(:expected_result) { [{ "username" => "dbadmin", "admin" => true }, { "username" => "foobar", "admin" => false }] }
116
+
117
+ before do
118
+ stub_request(:get, "http://influxdb.test:9999/query").with(
119
+ query: { u: "username", p: "password", q: "SHOW USERS" }
120
+ ).to_return(body: JSON.generate(response, status: 200))
121
+ end
122
+
123
+ it "should GET a list of database users" do
124
+ expect(subject.list_users).to eq(expected_result)
125
+ end
126
+ end
127
+ end