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,53 @@
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
+ lib = File.expand_path('lib', __dir__)
22
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
23
+ require 'influxdb2/client/version'
24
+
25
+ Gem::Specification.new do |spec|
26
+ spec.name = 'influxdb-client'
27
+ spec.version = ENV['CIRCLE_BUILD_NUM'] ? "#{InfluxDB2::VERSION}-#{ENV['CIRCLE_BUILD_NUM']}" : InfluxDB2::VERSION
28
+ spec.authors = ['Jakub Bednar']
29
+ spec.email = ['jakub.bednar@gmail.com']
30
+
31
+ spec.summary = 'Ruby library for InfluxDB 2.'
32
+ spec.description = 'This is the official Ruby library for InfluxDB 2.'
33
+ spec.homepage = 'https://github.com/influxdata/influxdb-client-ruby'
34
+ spec.license = 'MIT'
35
+
36
+ spec.metadata['homepage_uri'] = spec.homepage
37
+ spec.metadata['source_code_uri'] = 'https://github.com/influxdata/influxdb-client-ruby'
38
+ spec.metadata['changelog_uri'] = 'https://raw.githubusercontent.com/influxdata/influxdb-client-ruby/master/CHANGELOG.md'
39
+
40
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
41
+ spec.test_files = spec.files.grep(%r{^(test|spec|features|smoke)/})
42
+ spec.require_paths = ['lib']
43
+ spec.required_ruby_version = '>= 2.2.0'
44
+
45
+ spec.add_development_dependency 'bundler', '~> 2.0'
46
+ spec.add_development_dependency 'codecov', '~> 0.1.16'
47
+ spec.add_development_dependency 'minitest', '~> 5.0'
48
+ spec.add_development_dependency 'minitest-reporters', '~> 1.4'
49
+ spec.add_development_dependency 'rake', '~> 10.0'
50
+ spec.add_development_dependency 'rubocop', '~> 0.66.0'
51
+ spec.add_development_dependency 'simplecov', '~> 0.17.1'
52
+ spec.add_development_dependency 'webmock', '~> 3.7'
53
+ end
@@ -0,0 +1,28 @@
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 'influxdb2/client/default_api'
22
+ require 'influxdb2/client/version'
23
+ require 'influxdb2/client/client'
24
+ require 'influxdb2/client/influx_error'
25
+ require 'influxdb2/client/write_api'
26
+ require 'influxdb2/client/query_api'
27
+ require 'influxdb2/client/point'
28
+ require 'influxdb2/client/flux_table'
@@ -0,0 +1,82 @@
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
+ require 'net/http'
21
+
22
+ module InfluxDB2
23
+ # The client is the entry point to HTTP API defined
24
+ # in https://github.com/influxdata/influxdb/blob/master/http/swagger.yml.
25
+ class Client
26
+ # @return [ Hash ] options The configuration options.
27
+ attr_reader :options
28
+
29
+ # Instantiate a new InfluxDB client.
30
+ #
31
+ # @example Instantiate a client.
32
+ # InfluxDBClient::Client.new(url: 'https://localhost:9999', token: 'my-token')
33
+ #
34
+ # @param [Hash] options The options to be used by the client.
35
+ # @param [String] url InfluxDB URL to connect to (ex. https://localhost:9999).
36
+ # @param [String] token Access Token used for authenticating/authorizing the InfluxDB request sent by client.
37
+ #
38
+ # @option options [String] :bucket the default destination bucket for writes
39
+ # @option options [String] :org the default organization bucket for writes
40
+ # @option options [WritePrecision] :precision the default precision for the unix timestamps within
41
+ # @option options [Integer] :open_timeout Number of seconds to wait for the connection to open
42
+ # @option options [Integer] :write_timeout Number of seconds to wait for one block of data to be written
43
+ # @option options [Integer] :read_timeout Number of seconds to wait for one block of data to be read
44
+ # @option options [Integer] :max_redirect_count Maximal number of followed HTTP redirects
45
+ # @option options [bool] :use_ssl Turn on/off SSL for HTTP communication
46
+ # the body line-protocol
47
+ def initialize(url, token, options = nil)
48
+ @auto_closeable = []
49
+ @options = options ? options.dup : {}
50
+ @options[:url] = url if url.is_a? String
51
+ @options[:token] = token if token.is_a? String
52
+ @closed = false
53
+
54
+ at_exit { close! }
55
+ end
56
+
57
+ # Write time series data into InfluxDB thought WriteApi.
58
+ #
59
+ # @return [WriteApi] New instance of WriteApi.
60
+ def create_write_api(write_options: InfluxDB2::SYNCHRONOUS)
61
+ write_api = WriteApi.new(options: @options, write_options: write_options)
62
+ @auto_closeable.push(write_api)
63
+ write_api
64
+ end
65
+
66
+ # Get the Query client.
67
+ #
68
+ # @return [QueryApi] New instance of QueryApi.
69
+ def create_query_api
70
+ QueryApi.new(options: @options)
71
+ end
72
+
73
+ # Close all connections into InfluxDB 2.
74
+ #
75
+ # @return [ true ] Always true.
76
+ def close!
77
+ @closed = true
78
+ @auto_closeable.each(&:close!)
79
+ true
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,68 @@
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
+ module InfluxDB2
22
+ # default api
23
+ class DefaultApi
24
+ DEFAULT_TIMEOUT = 10
25
+ DEFAULT_REDIRECT_COUNT = 10
26
+
27
+ # @param [Hash] options The options to be used by the client.
28
+ def initialize(options:)
29
+ @options = options
30
+ @max_redirect_count = @options[:max_redirect_count] || DEFAULT_REDIRECT_COUNT
31
+ end
32
+
33
+ private
34
+
35
+ def _post(payload, uri, limit = @max_redirect_count)
36
+ raise InfluxError.from_message("Too many HTTP redirects. Exceeded limit: #{@max_redirect_count}") if limit.zero?
37
+
38
+ http = Net::HTTP.new(uri.host, uri.port)
39
+ http.open_timeout = @options[:open_timeout] || DEFAULT_TIMEOUT
40
+ http.write_timeout = @options[:write_timeout] || DEFAULT_TIMEOUT if Net::HTTP.method_defined? :write_timeout
41
+ http.read_timeout = @options[:read_timeout] || DEFAULT_TIMEOUT
42
+ http.use_ssl = @options[:use_ssl].nil? ? true : @options[:use_ssl]
43
+
44
+ request = Net::HTTP::Post.new(uri.request_uri)
45
+ request['Authorization'] = "Token #{@options[:token]}"
46
+ request.body = payload
47
+
48
+ begin
49
+ response = http.request(request)
50
+ case response
51
+ when Net::HTTPSuccess then
52
+ response
53
+ when Net::HTTPRedirection then
54
+ location = response['location']
55
+ _post(payload, URI.parse(location), limit - 1)
56
+ else
57
+ raise InfluxError.from_response(response)
58
+ end
59
+ ensure
60
+ http.finish if http.started?
61
+ end
62
+ end
63
+
64
+ def _check(key, value)
65
+ raise ArgumentError, "The '#{key}' should be defined as argument or default option: #{@options}" if value.nil?
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,246 @@
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
+ require 'csv'
21
+ require 'base64'
22
+
23
+ module InfluxDB2
24
+ # This class represents Flux query error
25
+ #
26
+ class FluxQueryError < StandardError
27
+ def initialize(message, reference)
28
+ super(message)
29
+ @reference = reference
30
+ end
31
+
32
+ attr_reader :reference
33
+ end
34
+
35
+ # This class represents Flux query error
36
+ #
37
+ class FluxCsvParserError < StandardError
38
+ def initialize(message)
39
+ super(message)
40
+ end
41
+ end
42
+
43
+ # This class us used to construct FluxResult from CSV.
44
+ #
45
+ class FluxCsvParser
46
+ include Enumerable
47
+ def initialize(response, stream: false)
48
+ @response = response
49
+ @stream = stream
50
+ @tables = {}
51
+
52
+ @table_index = 0
53
+ @start_new_table = false
54
+ @table = nil
55
+ @parsing_state_error = false
56
+
57
+ @closed = false
58
+ end
59
+
60
+ attr_reader :tables, :closed
61
+
62
+ def parse
63
+ @csv_file = CSV.new(@response.instance_of?(Net::HTTPOK) ? @response.body : @response)
64
+
65
+ while (csv = @csv_file.shift)
66
+ # Response has HTTP status ok, but response is error.
67
+ next if csv.empty?
68
+
69
+ if csv[1] == 'error' && csv[2] == 'reference'
70
+ @parsing_state_error = true
71
+ next
72
+ end
73
+
74
+ # Throw InfluxException with error response
75
+ if @parsing_state_error
76
+ error = csv[1]
77
+ reference_value = csv[2]
78
+ raise FluxQueryError.new(error, reference_value.nil? || reference_value.empty? ? 0 : reference_value.to_i)
79
+ end
80
+
81
+ result = _parse_line(csv)
82
+
83
+ yield result if @stream && result.instance_of?(InfluxDB2::FluxRecord)
84
+ end
85
+
86
+ self
87
+ end
88
+
89
+ def each
90
+ return enum_for(:each) unless block_given?
91
+
92
+ parse do |record|
93
+ yield record
94
+ end
95
+
96
+ self
97
+ ensure
98
+ _close_connection
99
+ end
100
+
101
+ private
102
+
103
+ def _parse_line(csv)
104
+ token = csv[0]
105
+
106
+ # start new table
107
+ if token == '#datatype'
108
+ # Return already parsed DataFrame
109
+ @start_new_table = true
110
+ @table = InfluxDB2::FluxTable.new
111
+
112
+ @tables[@table_index] = @table unless @stream
113
+
114
+ @table_index += 1
115
+ elsif @table.nil?
116
+ raise FluxCsvParserError, 'Unable to parse CSV response. FluxTable definition was not found.'
117
+ end
118
+
119
+ # # datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string,string,string
120
+ if token == '#datatype'
121
+ _add_data_types(@table, csv)
122
+
123
+ elsif token == '#group'
124
+ _add_groups(@table, csv)
125
+
126
+ elsif token == '#default'
127
+ _add_default_empty_values(@table, csv)
128
+ else
129
+ _parse_values(csv)
130
+ end
131
+ end
132
+
133
+ def _add_data_types(table, data_types)
134
+ (1..data_types.length - 1).each do |index|
135
+ column_def = InfluxDB2::FluxColumn.new(index: index - 1, data_type: data_types[index])
136
+ table.columns.push(column_def)
137
+ end
138
+ end
139
+
140
+ def _add_groups(table, csv)
141
+ i = 1
142
+
143
+ table.columns.each do |column|
144
+ column.group = csv[i] == 'true'
145
+ i += 1
146
+ end
147
+ end
148
+
149
+ def _add_default_empty_values(table, default_values)
150
+ i = 1
151
+
152
+ table.columns.each do |column|
153
+ column.default_value = default_values[i]
154
+ i += 1
155
+ end
156
+ end
157
+
158
+ def _add_column_names_and_tags(table, csv)
159
+ i = 1
160
+
161
+ table.columns.each do |column|
162
+ column.label = csv[i]
163
+ i += 1
164
+ end
165
+ end
166
+
167
+ def _parse_values(csv)
168
+ # parse column names
169
+ if @start_new_table
170
+ _add_column_names_and_tags(@table, csv)
171
+ @start_new_table = false
172
+ return
173
+ end
174
+
175
+ @current_index = csv[2].to_i
176
+
177
+ if @current_index > (@table_index - 1)
178
+ # create new table with previous column headers settings
179
+ @flux_columns = @table.columns
180
+ @table = InfluxDB2::FluxTable.new
181
+
182
+ @flux_columns.each do |column|
183
+ @table.columns.push(column)
184
+ end
185
+
186
+ @tables[@table_index] = @table unless @stream
187
+ @table_index += 1
188
+ end
189
+
190
+ flux_record = _parse_record(@table_index - 1, @table, csv)
191
+
192
+ if @stream
193
+ flux_record
194
+ else
195
+ @tables[@table_index - 1].records.push(flux_record)
196
+ end
197
+ end
198
+
199
+ def _parse_record(table_index, table, csv)
200
+ record = InfluxDB2::FluxRecord.new(table_index)
201
+
202
+ table.columns.each do |flux_column|
203
+ column_name = flux_column.label
204
+ str_val = csv[flux_column.index + 1]
205
+ record.values[column_name] = _to_value(str_val, flux_column)
206
+ end
207
+
208
+ record
209
+ end
210
+
211
+ def _to_value(str_val, column)
212
+ if str_val.nil? || str_val.empty?
213
+ default_value = column.default_value
214
+
215
+ return nil if default_value.nil? || default_value.empty?
216
+
217
+ _to_value(default_value, column)
218
+ end
219
+
220
+ case column.data_type
221
+ when 'boolean'
222
+ if str_val.nil? || str_val.empty?
223
+ true
224
+ else
225
+ str_val.casecmp('true').zero?
226
+ end
227
+ when 'unsignedLong', 'long', 'duration'
228
+ str_val.to_i
229
+ when 'double'
230
+ str_val.to_f
231
+ when 'base64Binary'
232
+ Base64.decode64(str_val)
233
+ when 'dateTime:RFC3339', 'dateTime:RFC3339Nano'
234
+ Time.parse(str_val).to_datetime.rfc3339
235
+ else
236
+ str_val
237
+ end
238
+ end
239
+
240
+ def _close_connection
241
+ # Close CSV Parser
242
+ @csv_file.close
243
+ @closed = true
244
+ end
245
+ end
246
+ end