influxdb-client 1.2.0 → 1.3.0.pre.678

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0bef54ca002cca11fb29d73a4de5efaa081a800ebc6a284a7eca088bf7d257ea
4
- data.tar.gz: 1ca91c0b690ee38c10e89cc41a178f071e244b7b85126a0fadb52315c9eb5a45
3
+ metadata.gz: cfdd87f63df56ccaccfa5efa62a17363aa4f23ef5b5f4181fc818a8dd5b88878
4
+ data.tar.gz: '0139f72ececad7ec2a3dbd10686e769c92a626ca938ee091a967abfe91ac1452'
5
5
  SHA512:
6
- metadata.gz: 849abb961288944fb762ec38a8bb7fdc4b51be0941a03547e8e23360aee98aedf78018e98f75bb879497784bf75a6df3f5c80f164c2a9a453a466f5b8e890d7d
7
- data.tar.gz: 2f9a75a9913f0f719d4dce37b1dfa33527159c0435672473979562aba705f8bf1ce34e1be5bd27bfd5dd713af41e39cf194460859a3631d6b28ededb9d188588
6
+ metadata.gz: f0f1a96df880bd30f3ef945968ee7e0163e28507b2372f2e0c5daa2a7437570e49cd2fe183de9bd8eb0f002bf83272a814a562928d2a14c480a46456c4a46955
7
+ data.tar.gz: cbae27badcc80adc5b4bd4185ab02262ce941763da4727ae01366adc2579db4d697ea62131957a3abf088a56ac74bbc7a2b2c4349e48b942790101eabf6c4007
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 1.3.0 [unreleased]
2
+
3
+ ### Features
4
+ 1. [#32](https://github.com/influxdata/influxdb-client-ruby/pull/32): Checks the health of a running InfluxDB instance by querying the /health
5
+
1
6
  ## 1.2.0 [2020-03-13]
2
7
 
3
8
  ### Features
data/README.md CHANGED
@@ -231,6 +231,12 @@ The time range could be specified as:
231
231
  1. DateTime - `DateTime.rfc3339('2019-03-03T04:05:06+07:00')`
232
232
  1. Time - `Time.utc(2015, 10, 16, 8, 20, 15)`
233
233
 
234
+ ## Advanced Usage
235
+
236
+ ### Check the server status
237
+
238
+ Server availability can be checked using the `client.health` method. That is equivalent of the [influx ping](https://v2.docs.influxdata.com/v2.0/reference/cli/influx/ping/).
239
+
234
240
  ## Local tests
235
241
 
236
242
  ```
@@ -16,7 +16,7 @@ mkdir -p "${SCRIPT_PATH}"/../lib/influxdb2/client/models
16
16
  mv "${SCRIPT_PATH}"/../lib/influx_db2/models/* "${SCRIPT_PATH}"/../lib/influxdb2/client/models
17
17
 
18
18
  cd "${SCRIPT_PATH}"/../lib/influxdb2/client/models || exit
19
- rm -r $(ls | grep -v "\<dialect.rb\>\|\<query.rb\>\|\<delete_predicate_request.rb\>")
19
+ rm -r $(ls | grep -v "\<health_check.rb\>\|\<dialect.rb\>\|\<query.rb\>\|\<delete_predicate_request.rb\>")
20
20
 
21
21
  # Clean
22
22
  rmdir "${SCRIPT_PATH}"/../lib/influx_db2/models
@@ -25,5 +25,6 @@ require 'influxdb2/client/influx_error'
25
25
  require 'influxdb2/client/write_api'
26
26
  require 'influxdb2/client/query_api'
27
27
  require 'influxdb2/client/delete_api'
28
+ require 'influxdb2/client/health_api'
28
29
  require 'influxdb2/client/point'
29
30
  require 'influxdb2/client/flux_table'
@@ -77,6 +77,13 @@ module InfluxDB2
77
77
  DeleteApi.new(options: @options)
78
78
  end
79
79
 
80
+ # Get the health of an instance.
81
+ #
82
+ # @return [HealthCheck]
83
+ def health
84
+ HealthApi.new(options: @options).health
85
+ end
86
+
80
87
  # Close all connections into InfluxDB 2.
81
88
  #
82
89
  # @return [ true ] Always true.
@@ -45,6 +45,14 @@ module InfluxDB2
45
45
  end
46
46
 
47
47
  def _post(payload, uri, limit: @max_redirect_count, headers: {})
48
+ _request(payload, uri, limit: limit, headers: headers, request: Net::HTTP::Post)
49
+ end
50
+
51
+ def _get(uri, limit: @max_redirect_count, headers: {})
52
+ _request(nil, uri, limit: limit, headers: headers.merge('Accept' => 'application/json'), request: Net::HTTP::Get)
53
+ end
54
+
55
+ def _request(payload, uri, limit: @max_redirect_count, headers: {}, request: Net::HTTP::Post)
48
56
  raise InfluxError.from_message("Too many HTTP redirects. Exceeded limit: #{@max_redirect_count}") if limit.zero?
49
57
 
50
58
  http = Net::HTTP.new(uri.host, uri.port)
@@ -53,7 +61,7 @@ module InfluxDB2
53
61
  http.read_timeout = @options[:read_timeout] || DEFAULT_TIMEOUT
54
62
  http.use_ssl = @options[:use_ssl].nil? ? true : @options[:use_ssl]
55
63
 
56
- request = Net::HTTP::Post.new(uri.request_uri)
64
+ request = request.new(uri.request_uri)
57
65
  request['Authorization'] = "Token #{@options[:token]}"
58
66
  request['User-Agent'] = "influxdb-client-ruby/#{InfluxDB2::VERSION}"
59
67
  headers.each { |k, v| request[k] = v }
@@ -0,0 +1,49 @@
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_relative 'models/health_check'
21
+
22
+ module InfluxDB2
23
+ # The client of the InfluxDB 2.0 that implement Health HTTP API endpoint.
24
+ #
25
+ class HealthApi < DefaultApi
26
+ # @param [Hash] options The options to be used by the client.
27
+ def initialize(options:)
28
+ super(options: options)
29
+ end
30
+
31
+ # Get the health of an instance.
32
+ #
33
+ # @return [HealthCheck]
34
+ def health
35
+ uri = URI.parse(File.join(@options[:url], '/health'))
36
+ body = _get(uri).body
37
+ data = JSON.parse("[#{body}]", symbolize_names: true)[0]
38
+ HealthCheck.new.tap do |model|
39
+ model.build_from_hash data
40
+ end
41
+ rescue StandardError => e
42
+ HealthCheck.new.tap do |model|
43
+ model.name = 'influxdb'
44
+ model.status = 'fail'
45
+ model.message = e.message
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,256 @@
1
+ =begin
2
+ #Influx API Service
3
+
4
+ #No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
5
+
6
+ OpenAPI spec version: 0.1.0
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ OpenAPI Generator version: 3.3.4
10
+
11
+ =end
12
+
13
+ require 'date'
14
+
15
+ module InfluxDB2
16
+ class HealthCheck
17
+ attr_accessor :name
18
+
19
+ attr_accessor :message
20
+
21
+ attr_accessor :checks
22
+
23
+ attr_accessor :status
24
+
25
+ class EnumAttributeValidator
26
+ attr_reader :datatype
27
+ attr_reader :allowable_values
28
+
29
+ def initialize(datatype, allowable_values)
30
+ @allowable_values = allowable_values.map do |value|
31
+ case datatype.to_s
32
+ when /Integer/i
33
+ value.to_i
34
+ when /Float/i
35
+ value.to_f
36
+ else
37
+ value
38
+ end
39
+ end
40
+ end
41
+
42
+ def valid?(value)
43
+ !value || allowable_values.include?(value)
44
+ end
45
+ end
46
+
47
+ # Attribute mapping from ruby-style variable name to JSON key.
48
+ def self.attribute_map
49
+ {
50
+ :'name' => :'name',
51
+ :'message' => :'message',
52
+ :'checks' => :'checks',
53
+ :'status' => :'status'
54
+ }
55
+ end
56
+
57
+ # Attribute type mapping.
58
+ def self.openapi_types
59
+ {
60
+ :'name' => :'String',
61
+ :'message' => :'String',
62
+ :'checks' => :'Array<HealthCheck>',
63
+ :'status' => :'String'
64
+ }
65
+ end
66
+
67
+ # Initializes the object
68
+ # @param [Hash] attributes Model attributes in the form of hash
69
+ def initialize(attributes = {})
70
+ return unless attributes.is_a?(Hash)
71
+
72
+ # convert string to symbol for hash key
73
+ attributes = attributes.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
74
+
75
+ if attributes.has_key?(:'name')
76
+ self.name = attributes[:'name']
77
+ end
78
+
79
+ if attributes.has_key?(:'message')
80
+ self.message = attributes[:'message']
81
+ end
82
+
83
+ if attributes.has_key?(:'checks')
84
+ if (value = attributes[:'checks']).is_a?(Array)
85
+ self.checks = value
86
+ end
87
+ end
88
+
89
+ if attributes.has_key?(:'status')
90
+ self.status = attributes[:'status']
91
+ end
92
+ end
93
+
94
+ # Show invalid properties with the reasons. Usually used together with valid?
95
+ # @return Array for valid properties with the reasons
96
+ def list_invalid_properties
97
+ invalid_properties = Array.new
98
+ if @name.nil?
99
+ invalid_properties.push('invalid value for "name", name cannot be nil.')
100
+ end
101
+
102
+ if @status.nil?
103
+ invalid_properties.push('invalid value for "status", status cannot be nil.')
104
+ end
105
+
106
+ invalid_properties
107
+ end
108
+
109
+ # Check to see if the all the properties in the model are valid
110
+ # @return true if the model is valid
111
+ def valid?
112
+ return false if @name.nil?
113
+ return false if @status.nil?
114
+ status_validator = EnumAttributeValidator.new('String', ['pass', 'fail'])
115
+ return false unless status_validator.valid?(@status)
116
+ true
117
+ end
118
+
119
+ # Custom attribute writer method checking allowed values (enum).
120
+ # @param [Object] status Object to be assigned
121
+ def status=(status)
122
+ validator = EnumAttributeValidator.new('String', ['pass', 'fail'])
123
+ unless validator.valid?(status)
124
+ fail ArgumentError, 'invalid value for "status", must be one of #{validator.allowable_values}.'
125
+ end
126
+ @status = status
127
+ end
128
+
129
+ # Checks equality by comparing each attribute.
130
+ # @param [Object] Object to be compared
131
+ def ==(o)
132
+ return true if self.equal?(o)
133
+ self.class == o.class &&
134
+ name == o.name &&
135
+ message == o.message &&
136
+ checks == o.checks &&
137
+ status == o.status
138
+ end
139
+
140
+ # @see the `==` method
141
+ # @param [Object] Object to be compared
142
+ def eql?(o)
143
+ self == o
144
+ end
145
+
146
+ # Calculates hash code according to all attributes.
147
+ # @return [Fixnum] Hash code
148
+ def hash
149
+ [name, message, checks, status].hash
150
+ end
151
+
152
+ # Builds the object from hash
153
+ # @param [Hash] attributes Model attributes in the form of hash
154
+ # @return [Object] Returns the model itself
155
+ def build_from_hash(attributes)
156
+ return nil unless attributes.is_a?(Hash)
157
+ self.class.openapi_types.each_pair do |key, type|
158
+ if type =~ /\AArray<(.*)>/i
159
+ # check to ensure the input is an array given that the the attribute
160
+ # is documented as an array but the input is not
161
+ if attributes[self.class.attribute_map[key]].is_a?(Array)
162
+ self.send("#{key}=", attributes[self.class.attribute_map[key]].map { |v| _deserialize($1, v) })
163
+ end
164
+ elsif !attributes[self.class.attribute_map[key]].nil?
165
+ self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
166
+ end # or else data not found in attributes(hash), not an issue as the data can be optional
167
+ end
168
+
169
+ self
170
+ end
171
+
172
+ # Deserializes the data based on type
173
+ # @param string type Data type
174
+ # @param string value Value to be deserialized
175
+ # @return [Object] Deserialized data
176
+ def _deserialize(type, value)
177
+ case type.to_sym
178
+ when :DateTime
179
+ DateTime.parse(value)
180
+ when :Date
181
+ Date.parse(value)
182
+ when :String
183
+ value.to_s
184
+ when :Integer
185
+ value.to_i
186
+ when :Float
187
+ value.to_f
188
+ when :BOOLEAN
189
+ if value.to_s =~ /\A(true|t|yes|y|1)\z/i
190
+ true
191
+ else
192
+ false
193
+ end
194
+ when :Object
195
+ # generic object (usually a Hash), return directly
196
+ value
197
+ when /\AArray<(?<inner_type>.+)>\z/
198
+ inner_type = Regexp.last_match[:inner_type]
199
+ value.map { |v| _deserialize(inner_type, v) }
200
+ when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
201
+ k_type = Regexp.last_match[:k_type]
202
+ v_type = Regexp.last_match[:v_type]
203
+ {}.tap do |hash|
204
+ value.each do |k, v|
205
+ hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
206
+ end
207
+ end
208
+ else # model
209
+ temp_model = InfluxDB2.const_get(type).new
210
+ temp_model.build_from_hash(value)
211
+ end
212
+ end
213
+
214
+ # Returns the string representation of the object
215
+ # @return [String] String presentation of the object
216
+ def to_s
217
+ to_hash.to_s
218
+ end
219
+
220
+ # to_body is an alias to to_hash (backward compatibility)
221
+ # @return [Hash] Returns the object in the form of hash
222
+ def to_body
223
+ to_hash
224
+ end
225
+
226
+ # Returns the object in the form of hash
227
+ # @return [Hash] Returns the object in the form of hash
228
+ def to_hash
229
+ hash = {}
230
+ self.class.attribute_map.each_pair do |attr, param|
231
+ value = self.send(attr)
232
+ next if value.nil?
233
+ hash[param] = _to_hash(value)
234
+ end
235
+ hash
236
+ end
237
+
238
+ # Outputs non-array value in the form of hash
239
+ # For object, use to_hash. Otherwise, just return the value
240
+ # @param [Object] value Any valid value
241
+ # @return [Hash] Returns the value in the form of hash
242
+ def _to_hash(value)
243
+ if value.is_a?(Array)
244
+ value.compact.map { |v| _to_hash(v) }
245
+ elsif value.is_a?(Hash)
246
+ {}.tap do |hash|
247
+ value.each { |k, v| hash[k] = _to_hash(v) }
248
+ end
249
+ elsif value.respond_to? :to_hash
250
+ value.to_hash
251
+ else
252
+ value
253
+ end
254
+ end
255
+ end
256
+ end
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module InfluxDB2
22
- VERSION = '1.2.0'.freeze
22
+ VERSION = '1.3.0'.freeze
23
23
  end
@@ -21,6 +21,10 @@
21
21
  require 'test_helper'
22
22
 
23
23
  class ClientTest < Minitest::Test
24
+ def setup
25
+ WebMock.allow_net_connect!
26
+ end
27
+
24
28
  def test_defined_version_number
25
29
  refute_nil ::InfluxDB2::VERSION
26
30
  end
@@ -67,4 +71,22 @@ class ClientTest < Minitest::Test
67
71
  refute_nil write_api
68
72
  assert_instance_of InfluxDB2::WriteApi, write_api
69
73
  end
74
+
75
+ def test_health
76
+ client = InfluxDB2::Client.new('http://localhost:9999', 'my-token', use_ssl: false)
77
+
78
+ health = client.health
79
+ assert_equal 'ready for queries and writes', health.message
80
+ assert_equal 'influxdb', health.name
81
+ assert_equal 'pass', health.status
82
+ end
83
+
84
+ def test_health_not_running
85
+ client_not_running = InfluxDB2::Client.new('http://localhost:8099', 'my-token', use_ssl: false)
86
+ health = client_not_running.health
87
+
88
+ assert_match 'Failed to open TCP connection to localhost:8099', health.message
89
+ assert_equal 'influxdb', health.name
90
+ assert_equal 'fail', health.status
91
+ end
70
92
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: influxdb-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0.pre.678
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Bednar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-13 00:00:00.000000000 Z
11
+ date: 2020-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -152,9 +152,11 @@ files:
152
152
  - lib/influxdb2/client/delete_api.rb
153
153
  - lib/influxdb2/client/flux_csv_parser.rb
154
154
  - lib/influxdb2/client/flux_table.rb
155
+ - lib/influxdb2/client/health_api.rb
155
156
  - lib/influxdb2/client/influx_error.rb
156
157
  - lib/influxdb2/client/models/delete_predicate_request.rb
157
158
  - lib/influxdb2/client/models/dialect.rb
159
+ - lib/influxdb2/client/models/health_check.rb
158
160
  - lib/influxdb2/client/models/query.rb
159
161
  - lib/influxdb2/client/point.rb
160
162
  - lib/influxdb2/client/query_api.rb
@@ -191,9 +193,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
191
193
  version: 2.2.0
192
194
  required_rubygems_version: !ruby/object:Gem::Requirement
193
195
  requirements:
194
- - - ">="
196
+ - - ">"
195
197
  - !ruby/object:Gem::Version
196
- version: '0'
198
+ version: 1.3.1
197
199
  requirements: []
198
200
  rubygems_version: 3.0.3
199
201
  signing_key: