influxdb-client 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.circleci/config.yml +157 -0
- data/.circleci/setup-rubygems.sh +3 -0
- data/.codecov.yml +3 -0
- data/.github/PULL_REQUEST_TEMPLATE +8 -0
- data/.gitignore +16 -0
- data/.rubocop.yml +38 -0
- data/CHANGELOG.md +27 -0
- data/Gemfile +24 -0
- data/LICENSE +21 -0
- data/README.md +248 -0
- data/Rakefile +37 -0
- data/bin/generate-sources.sh +23 -0
- data/bin/influxdb-onboarding.sh +39 -0
- data/bin/influxdb-restart.sh +60 -0
- data/bin/pom.xml +34 -0
- data/bin/swagger.yml +9867 -0
- data/influxdb-client.gemspec +53 -0
- data/lib/influxdb2/client.rb +29 -0
- data/lib/influxdb2/client/client.rb +89 -0
- data/lib/influxdb2/client/default_api.rb +87 -0
- data/lib/influxdb2/client/delete_api.rb +80 -0
- data/lib/influxdb2/client/flux_csv_parser.rb +251 -0
- data/lib/influxdb2/client/flux_table.rb +99 -0
- data/lib/influxdb2/client/influx_error.rb +31 -0
- data/lib/influxdb2/client/models/delete_predicate_request.rb +215 -0
- data/lib/influxdb2/client/models/dialect.rb +317 -0
- data/lib/influxdb2/client/models/query.rb +284 -0
- data/lib/influxdb2/client/point.rb +215 -0
- data/lib/influxdb2/client/query_api.rb +93 -0
- data/lib/influxdb2/client/version.rb +23 -0
- data/lib/influxdb2/client/worker.rb +115 -0
- data/lib/influxdb2/client/write_api.rb +243 -0
- data/test/influxdb/client_test.rb +70 -0
- data/test/influxdb/delete_api_integration_test.rb +100 -0
- data/test/influxdb/delete_api_test.rb +121 -0
- data/test/influxdb/flux_csv_parser_test.rb +401 -0
- data/test/influxdb/point_test.rb +221 -0
- data/test/influxdb/query_api_integration_test.rb +58 -0
- data/test/influxdb/query_api_stream_test.rb +98 -0
- data/test/influxdb/query_api_test.rb +96 -0
- data/test/influxdb/write_api_batching_test.rb +292 -0
- data/test/influxdb/write_api_integration_test.rb +76 -0
- data/test/influxdb/write_api_test.rb +275 -0
- data/test/test_helper.rb +39 -0
- metadata +214 -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', '>= 12.3.3'
|
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,29 @@
|
|
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/delete_api'
|
28
|
+
require 'influxdb2/client/point'
|
29
|
+
require 'influxdb2/client/flux_table'
|
@@ -0,0 +1,89 @@
|
|
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
|
+
# Get the Delete API to delete time series data from InfluxDB.
|
74
|
+
#
|
75
|
+
# @return [DeleteApi] New instance of DeleteApi.
|
76
|
+
def create_delete_api
|
77
|
+
DeleteApi.new(options: @options)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Close all connections into InfluxDB 2.
|
81
|
+
#
|
82
|
+
# @return [ true ] Always true.
|
83
|
+
def close!
|
84
|
+
@closed = true
|
85
|
+
@auto_closeable.each(&:close!)
|
86
|
+
true
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,87 @@
|
|
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
|
+
HEADER_CONTENT_TYPE = 'Content-Type'.freeze
|
28
|
+
|
29
|
+
# @param [Hash] options The options to be used by the client.
|
30
|
+
def initialize(options:)
|
31
|
+
@options = options
|
32
|
+
@max_redirect_count = @options[:max_redirect_count] || DEFAULT_REDIRECT_COUNT
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def _post_json(payload, uri, headers: {})
|
38
|
+
_check_arg_type(:headers, headers, Hash)
|
39
|
+
_post(payload, uri, headers: headers.merge(HEADER_CONTENT_TYPE => 'application/json'))
|
40
|
+
end
|
41
|
+
|
42
|
+
def _post_text(payload, uri, headers: {})
|
43
|
+
_check_arg_type(:headers, headers, Hash)
|
44
|
+
_post(payload, uri, headers: headers.merge(HEADER_CONTENT_TYPE => 'text/plain'))
|
45
|
+
end
|
46
|
+
|
47
|
+
def _post(payload, uri, limit: @max_redirect_count, headers: {})
|
48
|
+
raise InfluxError.from_message("Too many HTTP redirects. Exceeded limit: #{@max_redirect_count}") if limit.zero?
|
49
|
+
|
50
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
51
|
+
http.open_timeout = @options[:open_timeout] || DEFAULT_TIMEOUT
|
52
|
+
http.write_timeout = @options[:write_timeout] || DEFAULT_TIMEOUT if Net::HTTP.method_defined? :write_timeout
|
53
|
+
http.read_timeout = @options[:read_timeout] || DEFAULT_TIMEOUT
|
54
|
+
http.use_ssl = @options[:use_ssl].nil? ? true : @options[:use_ssl]
|
55
|
+
|
56
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
57
|
+
request['Authorization'] = "Token #{@options[:token]}"
|
58
|
+
request['User-Agent'] = "influxdb-client-ruby/#{InfluxDB2::VERSION}"
|
59
|
+
headers.each { |k, v| request[k] = v }
|
60
|
+
|
61
|
+
request.body = payload
|
62
|
+
|
63
|
+
begin
|
64
|
+
response = http.request(request)
|
65
|
+
case response
|
66
|
+
when Net::HTTPSuccess then
|
67
|
+
response
|
68
|
+
when Net::HTTPRedirection then
|
69
|
+
location = response['location']
|
70
|
+
_post(payload, URI.parse(location), limit: limit - 1, headers: headers)
|
71
|
+
else
|
72
|
+
raise InfluxError.from_response(response)
|
73
|
+
end
|
74
|
+
ensure
|
75
|
+
http.finish if http.started?
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def _check_arg_type(name, value, klass)
|
80
|
+
raise TypeError, "expected a #{klass.name} for #{name}; got #{value.class.name}" unless value.is_a?(klass)
|
81
|
+
end
|
82
|
+
|
83
|
+
def _check(key, value)
|
84
|
+
raise ArgumentError, "The '#{key}' should be defined as argument or default option: #{@options}" if value.nil?
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,80 @@
|
|
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_relative 'models/delete_predicate_request'
|
22
|
+
|
23
|
+
module InfluxDB2
|
24
|
+
# Delete time series data from InfluxDB
|
25
|
+
#
|
26
|
+
class DeleteApi < DefaultApi
|
27
|
+
# @param [Hash] options The options to be used by the client.
|
28
|
+
def initialize(options:)
|
29
|
+
super(options: options)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Delete time series data from InfluxDB.
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# delete('2019-02-03T04:05:06+07:00', '2019-04-03T04:05:06+07:00',
|
36
|
+
# predicate: 'key1="value1" AND key2="value"', bucket: 'my-bucket', org: 'my-org')
|
37
|
+
#
|
38
|
+
# @example
|
39
|
+
# delete(DateTime.rfc3339('2019-02-03T04:05:06+07:00'), DateTime.rfc3339('2019-03-03T04:05:06+07:00'),
|
40
|
+
# predicate: 'key1="value1" AND key2="value"', bucket: 'my-bucket', org: 'my-org')
|
41
|
+
#
|
42
|
+
# @param [Object] start Start time of interval to delete data.
|
43
|
+
# The start could be represent by [Time], [DateTime] or [String] formatted as RFC3339.
|
44
|
+
# @param [Object] stop Stop time of interval to delete data
|
45
|
+
# The stop could be represent by [Time], [DateTime] or [String] formatted as RFC3339.
|
46
|
+
# @param [String] predicate InfluxQL-like predicate Stop time of interval to delete data
|
47
|
+
# @param [String] bucket specifies the bucket to remove data from
|
48
|
+
# @param [String] org specifies the organization to remove data from
|
49
|
+
def delete(start, stop, predicate: nil, bucket: nil, org: nil)
|
50
|
+
delete_request = InfluxDB2::DeletePredicateRequest.new(start: _to_rfc3339(start), stop: _to_rfc3339(stop),
|
51
|
+
predicate: predicate)
|
52
|
+
|
53
|
+
_delete(delete_request, bucket: bucket, org: org)
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def _delete(delete_request, bucket: nil, org: nil)
|
59
|
+
bucket_param = bucket || @options[:bucket]
|
60
|
+
org_param = org || @options[:org]
|
61
|
+
_check('bucket', bucket_param)
|
62
|
+
_check('org', org_param)
|
63
|
+
|
64
|
+
uri = URI.parse(File.join(@options[:url], '/api/v2/delete'))
|
65
|
+
uri.query = URI.encode_www_form(org: org_param, bucket: bucket_param)
|
66
|
+
|
67
|
+
_post_json(delete_request.to_body.to_json, uri)
|
68
|
+
end
|
69
|
+
|
70
|
+
def _to_rfc3339(time)
|
71
|
+
if time.is_a?(String)
|
72
|
+
time
|
73
|
+
elsif time.is_a?(Time)
|
74
|
+
_to_rfc3339(time.to_datetime)
|
75
|
+
elsif time.is_a?(DateTime)
|
76
|
+
_to_rfc3339(time.rfc3339)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,251 @@
|
|
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
|
+
require 'time'
|
23
|
+
|
24
|
+
module InfluxDB2
|
25
|
+
# This class represents Flux query error
|
26
|
+
#
|
27
|
+
class FluxQueryError < StandardError
|
28
|
+
def initialize(message, reference)
|
29
|
+
super(message)
|
30
|
+
@reference = reference
|
31
|
+
end
|
32
|
+
|
33
|
+
attr_reader :reference
|
34
|
+
end
|
35
|
+
|
36
|
+
# This class represents Flux query error
|
37
|
+
#
|
38
|
+
class FluxCsvParserError < StandardError
|
39
|
+
def initialize(message)
|
40
|
+
super(message)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# This class us used to construct FluxResult from CSV.
|
45
|
+
#
|
46
|
+
class FluxCsvParser
|
47
|
+
include Enumerable
|
48
|
+
def initialize(response, stream: false)
|
49
|
+
@response = response
|
50
|
+
@stream = stream
|
51
|
+
@tables = {}
|
52
|
+
|
53
|
+
@table_index = 0
|
54
|
+
@table_id = -1
|
55
|
+
@start_new_table = false
|
56
|
+
@table = nil
|
57
|
+
@parsing_state_error = false
|
58
|
+
|
59
|
+
@closed = false
|
60
|
+
end
|
61
|
+
|
62
|
+
attr_reader :tables, :closed
|
63
|
+
|
64
|
+
def parse
|
65
|
+
@csv_file = CSV.new(@response.instance_of?(Net::HTTPOK) ? @response.body : @response)
|
66
|
+
|
67
|
+
while (csv = @csv_file.shift)
|
68
|
+
# Response has HTTP status ok, but response is error.
|
69
|
+
next if csv.empty?
|
70
|
+
|
71
|
+
if csv[1] == 'error' && csv[2] == 'reference'
|
72
|
+
@parsing_state_error = true
|
73
|
+
next
|
74
|
+
end
|
75
|
+
|
76
|
+
# Throw InfluxException with error response
|
77
|
+
if @parsing_state_error
|
78
|
+
error = csv[1]
|
79
|
+
reference_value = csv[2]
|
80
|
+
raise FluxQueryError.new(error, reference_value.nil? || reference_value.empty? ? 0 : reference_value.to_i)
|
81
|
+
end
|
82
|
+
|
83
|
+
result = _parse_line(csv)
|
84
|
+
|
85
|
+
yield result if @stream && result.instance_of?(InfluxDB2::FluxRecord)
|
86
|
+
end
|
87
|
+
|
88
|
+
self
|
89
|
+
end
|
90
|
+
|
91
|
+
def each
|
92
|
+
return enum_for(:each) unless block_given?
|
93
|
+
|
94
|
+
parse do |record|
|
95
|
+
yield record
|
96
|
+
end
|
97
|
+
|
98
|
+
self
|
99
|
+
ensure
|
100
|
+
_close_connection
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
def _parse_line(csv)
|
106
|
+
token = csv[0]
|
107
|
+
|
108
|
+
# start new table
|
109
|
+
if token == '#datatype'
|
110
|
+
# Return already parsed DataFrame
|
111
|
+
@start_new_table = true
|
112
|
+
@table = InfluxDB2::FluxTable.new
|
113
|
+
|
114
|
+
@tables[@table_index] = @table unless @stream
|
115
|
+
|
116
|
+
@table_index += 1
|
117
|
+
@table_id = -1
|
118
|
+
elsif @table.nil?
|
119
|
+
raise FluxCsvParserError, 'Unable to parse CSV response. FluxTable definition was not found.'
|
120
|
+
end
|
121
|
+
|
122
|
+
# # datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string,string,string
|
123
|
+
if token == '#datatype'
|
124
|
+
_add_data_types(@table, csv)
|
125
|
+
|
126
|
+
elsif token == '#group'
|
127
|
+
_add_groups(@table, csv)
|
128
|
+
|
129
|
+
elsif token == '#default'
|
130
|
+
_add_default_empty_values(@table, csv)
|
131
|
+
else
|
132
|
+
_parse_values(csv)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def _add_data_types(table, data_types)
|
137
|
+
(1..data_types.length - 1).each do |index|
|
138
|
+
column_def = InfluxDB2::FluxColumn.new(index: index - 1, data_type: data_types[index])
|
139
|
+
table.columns.push(column_def)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def _add_groups(table, csv)
|
144
|
+
i = 1
|
145
|
+
|
146
|
+
table.columns.each do |column|
|
147
|
+
column.group = csv[i] == 'true'
|
148
|
+
i += 1
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def _add_default_empty_values(table, default_values)
|
153
|
+
i = 1
|
154
|
+
|
155
|
+
table.columns.each do |column|
|
156
|
+
column.default_value = default_values[i]
|
157
|
+
i += 1
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def _add_column_names_and_tags(table, csv)
|
162
|
+
i = 1
|
163
|
+
|
164
|
+
table.columns.each do |column|
|
165
|
+
column.label = csv[i]
|
166
|
+
i += 1
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def _parse_values(csv)
|
171
|
+
# parse column names
|
172
|
+
if @start_new_table
|
173
|
+
_add_column_names_and_tags(@table, csv)
|
174
|
+
@start_new_table = false
|
175
|
+
return
|
176
|
+
end
|
177
|
+
|
178
|
+
current_id = csv[2].to_i
|
179
|
+
@table_id = current_id if @table_id == -1
|
180
|
+
|
181
|
+
if @table_id != current_id
|
182
|
+
# create new table with previous column headers settings
|
183
|
+
@flux_columns = @table.columns
|
184
|
+
@table = InfluxDB2::FluxTable.new
|
185
|
+
|
186
|
+
@flux_columns.each do |column|
|
187
|
+
@table.columns.push(column)
|
188
|
+
end
|
189
|
+
|
190
|
+
@tables[@table_index] = @table unless @stream
|
191
|
+
@table_index += 1
|
192
|
+
@table_id = current_id
|
193
|
+
end
|
194
|
+
|
195
|
+
flux_record = _parse_record(@table_index - 1, @table, csv)
|
196
|
+
|
197
|
+
if @stream
|
198
|
+
flux_record
|
199
|
+
else
|
200
|
+
@tables[@table_index - 1].records.push(flux_record)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def _parse_record(table_index, table, csv)
|
205
|
+
record = InfluxDB2::FluxRecord.new(table_index)
|
206
|
+
|
207
|
+
table.columns.each do |flux_column|
|
208
|
+
column_name = flux_column.label
|
209
|
+
str_val = csv[flux_column.index + 1]
|
210
|
+
record.values[column_name] = _to_value(str_val, flux_column)
|
211
|
+
end
|
212
|
+
|
213
|
+
record
|
214
|
+
end
|
215
|
+
|
216
|
+
def _to_value(str_val, column)
|
217
|
+
if str_val.nil? || str_val.empty?
|
218
|
+
default_value = column.default_value
|
219
|
+
|
220
|
+
return nil if default_value.nil? || default_value.empty?
|
221
|
+
|
222
|
+
_to_value(default_value, column)
|
223
|
+
end
|
224
|
+
|
225
|
+
case column.data_type
|
226
|
+
when 'boolean'
|
227
|
+
if str_val.nil? || str_val.empty?
|
228
|
+
true
|
229
|
+
else
|
230
|
+
str_val.casecmp('true').zero?
|
231
|
+
end
|
232
|
+
when 'unsignedLong', 'long', 'duration'
|
233
|
+
str_val.to_i
|
234
|
+
when 'double'
|
235
|
+
str_val.to_f
|
236
|
+
when 'base64Binary'
|
237
|
+
Base64.decode64(str_val)
|
238
|
+
when 'dateTime:RFC3339', 'dateTime:RFC3339Nano'
|
239
|
+
Time.parse(str_val).to_datetime.rfc3339
|
240
|
+
else
|
241
|
+
str_val
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
def _close_connection
|
246
|
+
# Close CSV Parser
|
247
|
+
@csv_file.close
|
248
|
+
@closed = true
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|