influxdb-client 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +157 -0
  3. data/.circleci/setup-rubygems.sh +3 -0
  4. data/.codecov.yml +3 -0
  5. data/.github/PULL_REQUEST_TEMPLATE +8 -0
  6. data/.gitignore +16 -0
  7. data/.rubocop.yml +38 -0
  8. data/CHANGELOG.md +12 -0
  9. data/Gemfile +24 -0
  10. data/LICENSE +21 -0
  11. data/README.md +226 -0
  12. data/Rakefile +37 -0
  13. data/bin/generate-sources.sh +30 -0
  14. data/bin/influxdb-onboarding.sh +39 -0
  15. data/bin/influxdb-restart.sh +60 -0
  16. data/bin/pom.xml +34 -0
  17. data/bin/swagger.yml +9867 -0
  18. data/influxdb-client.gemspec +53 -0
  19. data/lib/influxdb2/client.rb +28 -0
  20. data/lib/influxdb2/client/client.rb +82 -0
  21. data/lib/influxdb2/client/default_api.rb +68 -0
  22. data/lib/influxdb2/client/flux_csv_parser.rb +246 -0
  23. data/lib/influxdb2/client/flux_table.rb +99 -0
  24. data/lib/influxdb2/client/influx_error.rb +27 -0
  25. data/lib/influxdb2/client/models/dialect.rb +317 -0
  26. data/lib/influxdb2/client/models/query.rb +284 -0
  27. data/lib/influxdb2/client/point.rb +215 -0
  28. data/lib/influxdb2/client/query_api.rb +93 -0
  29. data/lib/influxdb2/client/version.rb +23 -0
  30. data/lib/influxdb2/client/worker.rb +89 -0
  31. data/lib/influxdb2/client/write_api.rb +219 -0
  32. data/test/influxdb/client_test.rb +70 -0
  33. data/test/influxdb/flux_csv_parser_test.rb +326 -0
  34. data/test/influxdb/point_test.rb +221 -0
  35. data/test/influxdb/query_api_integration_test.rb +58 -0
  36. data/test/influxdb/query_api_stream_test.rb +98 -0
  37. data/test/influxdb/query_api_test.rb +75 -0
  38. data/test/influxdb/write_api_batching_test.rb +153 -0
  39. data/test/influxdb/write_api_integration_test.rb +75 -0
  40. data/test/influxdb/write_api_test.rb +235 -0
  41. data/test/test_helper.rb +39 -0
  42. metadata +208 -0
@@ -0,0 +1,58 @@
1
+ # The MIT License
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'test_helper'
22
+
23
+ class QueryApiIntegrationTest < MiniTest::Test
24
+ def setup
25
+ WebMock.allow_net_connect!
26
+
27
+ @client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
28
+ bucket: 'my-bucket',
29
+ org: 'my-org',
30
+ precision: InfluxDB2::WritePrecision::NANOSECOND,
31
+ use_ssl: false)
32
+ end
33
+
34
+ def test_query
35
+ now = Time.now.utc
36
+ measurement = 'h2o_query_' + now.to_i.to_s
37
+
38
+ @client.create_write_api.write(data: InfluxDB2::Point.new(name: measurement)
39
+ .add_tag('location', 'europe')
40
+ .add_field('level', 2)
41
+ .time(now, InfluxDB2::WritePrecision::NANOSECOND))
42
+
43
+ result = @client.create_query_api.query(query: 'from(bucket: "my-bucket") |> range(start: -1m, stop: now()) '\
44
+ "|> filter(fn: (r) => r._measurement == \"#{measurement}\")")
45
+
46
+ assert_equal 1, result.size
47
+
48
+ records = result[0].records
49
+ assert_equal 1, records.size
50
+
51
+ record = records[0]
52
+
53
+ assert_equal measurement, record.measurement
54
+ assert_equal 'europe', record.values['location']
55
+ assert_equal 2, record.value
56
+ assert_equal 'level', record.field
57
+ end
58
+ end
@@ -0,0 +1,98 @@
1
+ # The MIT License
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'test_helper'
22
+
23
+ class QueryApiStreamTest < MiniTest::Test
24
+ def setup
25
+ WebMock.allow_net_connect!
26
+
27
+ @client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
28
+ bucket: 'my-bucket',
29
+ org: 'my-org',
30
+ precision: InfluxDB2::WritePrecision::NANOSECOND,
31
+ use_ssl: false)
32
+ @now = Time.now.utc
33
+ end
34
+
35
+ def test_query_stream
36
+ measurement = 'h2o_query_stream' + @now.to_i.to_s + @now.nsec.to_s
37
+ _write(10, measurement: measurement)
38
+
39
+ query = 'from(bucket: "my-bucket") |> range(start: -1m, stop: now()) ' \
40
+ "|> filter(fn: (r) => r._measurement == \"#{measurement}\")"
41
+
42
+ count = 0
43
+ @client.create_query_api.query_stream(query: query).each do |record|
44
+ count += 1
45
+ assert_equal measurement, record.measurement
46
+ assert_equal 'europe', record.values['location']
47
+ assert_equal count, record.value
48
+ assert_equal 'level', record.field
49
+ end
50
+
51
+ assert_equal 10, count
52
+ end
53
+
54
+ def test_query_stream_break
55
+ measurement = 'h2o_query_stream_break' + @now.to_i.to_s + @now.nsec.to_s
56
+ _write(20, measurement: measurement)
57
+
58
+ query = 'from(bucket: "my-bucket") |> range(start: -1m, stop: now()) ' \
59
+ "|> filter(fn: (r) => r._measurement == \"#{measurement}\")"
60
+
61
+ records = []
62
+
63
+ parser = @client.create_query_api.query_stream(query: query)
64
+
65
+ assert_equal false, parser.closed
66
+
67
+ count = 0
68
+ parser.each do |record|
69
+ records.push(record)
70
+ count += 1
71
+
72
+ break if count >= 5
73
+ end
74
+
75
+ assert_equal 5, records.size
76
+ assert_equal true, parser.closed
77
+
78
+ # record 1
79
+ record = records[0]
80
+ assert_equal measurement, record.measurement
81
+ assert_equal 'europe', record.values['location']
82
+ assert_equal 1, record.value
83
+ assert_equal 'level', record.field
84
+ end
85
+
86
+ private
87
+
88
+ def _write(values, measurement:)
89
+ write_api = @client.create_write_api
90
+
91
+ (1..values).each do |value|
92
+ write_api.write(data: InfluxDB2::Point.new(name: measurement)
93
+ .add_tag('location', 'europe')
94
+ .add_field('level', value)
95
+ .time(@now - values + value, InfluxDB2::WritePrecision::NANOSECOND))
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,75 @@
1
+ # The MIT License
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'test_helper'
22
+
23
+ class QueryApiTest < MiniTest::Test
24
+ def setup
25
+ WebMock.disable_net_connect!
26
+ end
27
+
28
+ SUCCESS_DATA = '#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,' \
29
+ "long,string,string,string,string\n" \
30
+ "#group,false,false,false,false,false,false,false,false,false,true\n" + "#default,_result,,,,,,,,,\n" \
31
+ ",result,table,_start,_stop,_time,_value,_field,_measurement,host,region\n" \
32
+ ",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,west\n" \
33
+ ",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,20,free,mem,B,west\n" \
34
+ ",,0,1970-01-01T00:00:20Z,1970-01-01T00:00:30Z,1970-01-01T00:00:20Z,11,free,mem,A,west\n" \
35
+ ',,0,1970-01-01T00:00:20Z,1970-01-01T00:00:30Z,1970-01-01T00:00:20Z,22,free,mem,B,west'
36
+
37
+ def test_query_raw
38
+ stub_request(:post, 'http://localhost:9999/api/v2/query?org=my-org')
39
+ .to_return(body: SUCCESS_DATA)
40
+ client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
41
+ bucket: 'my-bucket',
42
+ org: 'my-org',
43
+ use_ssl: false)
44
+
45
+ bucket = 'my-bucket'
46
+ result = client.create_query_api.query_raw(query:
47
+ 'from(bucket:"' + bucket + '") |> range(start: 1970-01-01T00:00:00.000000001Z) |> last()')
48
+
49
+ assert_equal result, SUCCESS_DATA
50
+ end
51
+
52
+ def test_query
53
+ stub_request(:post, 'http://localhost:9999/api/v2/query?org=my-org')
54
+ .to_return(body: SUCCESS_DATA)
55
+
56
+ client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
57
+ bucket: 'my-bucket',
58
+ org: 'my-org',
59
+ use_ssl: false)
60
+
61
+ bucket = 'my-bucket'
62
+ result = client.create_query_api.query(query:
63
+ 'from(bucket:"' + bucket + '") |> range(start: 1970-01-01T00:00:00.000000001Z) |> last()')
64
+
65
+ assert_equal 1, result.length
66
+ assert_equal 4, result[0].records.length
67
+
68
+ record1 = result[0].records[0]
69
+
70
+ assert_equal Time.parse('1970-01-01T00:00:10Z').to_datetime.rfc3339, record1.time
71
+ assert_equal 'mem', record1.measurement
72
+ assert_equal 10, record1.value
73
+ assert_equal 'free', record1.field
74
+ end
75
+ end
@@ -0,0 +1,153 @@
1
+ # The MIT License
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'test_helper'
22
+
23
+ class WriteApiBatchingTest < MiniTest::Test
24
+ def setup
25
+ WebMock.disable_net_connect!
26
+
27
+ @write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
28
+ batch_size: 2, flush_interval: 5_000)
29
+ @client = InfluxDB2::Client.new('http://localhost:9999',
30
+ 'my-token',
31
+ bucket: 'my-bucket',
32
+ org: 'my-org',
33
+ precision: InfluxDB2::WritePrecision::NANOSECOND,
34
+ use_ssl: false)
35
+
36
+ @write_client = @client.create_write_api(write_options: @write_options)
37
+ end
38
+
39
+ def teardown
40
+ @client.close!
41
+
42
+ assert_equal true, @write_client.closed
43
+
44
+ WebMock.reset!
45
+ end
46
+
47
+ def test_batch_size
48
+ stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
49
+ .to_return(status: 204)
50
+
51
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=1.0 1')
52
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=2.0 2')
53
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=3.0 3')
54
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=4.0 4')
55
+
56
+ sleep(1)
57
+
58
+ request1 = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1\n" \
59
+ 'h2o_feet,location=coyote_creek level\\ water_level=2.0 2'
60
+ request2 = "h2o_feet,location=coyote_creek level\\ water_level=3.0 3\n" \
61
+ 'h2o_feet,location=coyote_creek level\\ water_level=4.0 4'
62
+
63
+ assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
64
+ times: 1, body: request1)
65
+ assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
66
+ times: 1, body: request2)
67
+ end
68
+
69
+ def test_batch_size_group_by
70
+ stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
71
+ .to_return(status: 204)
72
+ stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=s')
73
+ .to_return(status: 204)
74
+ stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org-a&precision=ns')
75
+ .to_return(status: 204)
76
+ stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket2&org=my-org-a&precision=ns')
77
+ .to_return(status: 204)
78
+
79
+ bucket = 'my-bucket'
80
+ bucket2 = 'my-bucket2'
81
+ org_a = 'my-org-a'
82
+
83
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=1.0 1', bucket: bucket, org: 'my-org')
84
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=2.0 2', bucket: bucket, org: 'my-org',
85
+ precision: InfluxDB2::WritePrecision::SECOND)
86
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=3.0 3', bucket: bucket, org: org_a)
87
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=4.0 4', bucket: bucket, org: org_a)
88
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=5.0 5', bucket: bucket2, org: org_a)
89
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=6.0 6', bucket: bucket, org: org_a)
90
+
91
+ sleep(1)
92
+
93
+ assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
94
+ times: 1, body: 'h2o_feet,location=coyote_creek level\\ water_level=1.0 1')
95
+ assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=s',
96
+ times: 1, body: 'h2o_feet,location=coyote_creek level\\ water_level=2.0 2')
97
+ assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org-a&precision=ns',
98
+ times: 1, body: "h2o_feet,location=coyote_creek level\\ water_level=3.0 3\n" \
99
+ 'h2o_feet,location=coyote_creek level\\ water_level=4.0 4')
100
+ assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket2&org=my-org-a&precision=ns',
101
+ times: 1, body: 'h2o_feet,location=coyote_creek level\\ water_level=5.0 5')
102
+ assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org-a&precision=ns',
103
+ times: 1, body: 'h2o_feet,location=coyote_creek level\\ water_level=6.0 6')
104
+ end
105
+
106
+ def test_flush_interval
107
+ stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
108
+ .to_return(status: 204)
109
+
110
+ request1 = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1\n" \
111
+ 'h2o_feet,location=coyote_creek level\\ water_level=2.0 2'
112
+ request2 = 'h2o_feet,location=coyote_creek level\\ water_level=3.0 3'
113
+
114
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=1.0 1')
115
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=2.0 2')
116
+
117
+ sleep(1)
118
+
119
+ assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
120
+ times: 1, body: request1)
121
+
122
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=3.0 3')
123
+
124
+ sleep(2)
125
+
126
+ assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
127
+ times: 0, body: request2)
128
+
129
+ sleep(3)
130
+
131
+ assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
132
+ times: 1, body: request2)
133
+ end
134
+
135
+ def test_flush_all_by_close_client
136
+ stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
137
+ .to_return(status: 204)
138
+
139
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=1.0 1')
140
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=2.0 2')
141
+ @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=3.0 3')
142
+
143
+ assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
144
+ times: 0, body: 'h2o_feet,location=coyote_creek level\\ water_level=3.0 3')
145
+
146
+ @client.close!
147
+
148
+ assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
149
+ times: 1, body: "h2o_feet,location=coyote_creek level\\ water_level=1.0 1\n" \
150
+ "h2o_feet,location=coyote_creek level\\ water_level=2.0 2\n" \
151
+ 'h2o_feet,location=coyote_creek level\\ water_level=3.0 3')
152
+ end
153
+ end
@@ -0,0 +1,75 @@
1
+ # The MIT License
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'test_helper'
22
+ require 'csv'
23
+
24
+ class WriteApiIntegrationTest < MiniTest::Test
25
+ def setup
26
+ WebMock.allow_net_connect!
27
+ end
28
+
29
+ def test_write_into_influx_db
30
+ client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
31
+ bucket: 'my-bucket',
32
+ org: 'my-org',
33
+ precision: InfluxDB2::WritePrecision::NANOSECOND,
34
+ use_ssl: false)
35
+
36
+ now = Time.now.utc
37
+
38
+ measurement = 'h2o_' + now.to_i.to_s
39
+ point = InfluxDB2::Point.new(name: measurement)
40
+ .add_tag('location', 'europe')
41
+ .add_field('level', 2)
42
+ .time(now, InfluxDB2::WritePrecision::NANOSECOND)
43
+
44
+ client.create_write_api.write(data: point)
45
+
46
+ csv = _query(measurement)
47
+
48
+ refute_nil csv
49
+ assert_equal measurement, csv[0]['_measurement']
50
+ assert_equal 'europe', csv[0]['location']
51
+ assert_equal '2', csv[0]['_value']
52
+ assert_equal 'level', csv[0]['_field']
53
+ end
54
+
55
+ private
56
+
57
+ def _query(measurement)
58
+ query = { 'query': 'from(bucket: "my-bucket") |> range(start: -15m, stop: now()) '\
59
+ "|> filter(fn: (r) => r._measurement == \"#{measurement}\")", 'type': 'flux' }
60
+
61
+ uri = URI.parse('http://localhost:9999/api/v2/query?org=my-org')
62
+ request = Net::HTTP::Post.new(uri.request_uri)
63
+ request['Authorization'] = 'Token my-token'
64
+ request.body = query.to_json
65
+
66
+ http = Net::HTTP.new(uri.host, uri.port)
67
+ begin
68
+ response = http.request(request)
69
+
70
+ CSV.parse(response.body, headers: true)
71
+ ensure
72
+ http.finish if http.started?
73
+ end
74
+ end
75
+ end