influxdb-client 1.0.0.pre.191 → 1.1.0.pre.459

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.
@@ -18,8 +18,11 @@
18
18
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
19
  # THE SOFTWARE.
20
20
 
21
+ require 'influxdb2/client/default_api'
21
22
  require 'influxdb2/client/version'
22
23
  require 'influxdb2/client/client'
23
24
  require 'influxdb2/client/influx_error'
24
25
  require 'influxdb2/client/write_api'
26
+ require 'influxdb2/client/query_api'
25
27
  require 'influxdb2/client/point'
28
+ require 'influxdb2/client/flux_table'
@@ -45,6 +45,7 @@ module InfluxDB2
45
45
  # @option options [bool] :use_ssl Turn on/off SSL for HTTP communication
46
46
  # the body line-protocol
47
47
  def initialize(url, token, options = nil)
48
+ @auto_closeable = []
48
49
  @options = options ? options.dup : {}
49
50
  @options[:url] = url if url.is_a? String
50
51
  @options[:token] = token if token.is_a? String
@@ -56,8 +57,17 @@ module InfluxDB2
56
57
  # Write time series data into InfluxDB thought WriteApi.
57
58
  #
58
59
  # @return [WriteApi] New instance of WriteApi.
59
- def create_write_api
60
- WriteApi.new(options: @options)
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)
61
71
  end
62
72
 
63
73
  # Close all connections into InfluxDB 2.
@@ -65,6 +75,7 @@ module InfluxDB2
65
75
  # @return [ true ] Always true.
66
76
  def close!
67
77
  @closed = true
78
+ @auto_closeable.each(&:close!)
68
79
  true
69
80
  end
70
81
  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
@@ -0,0 +1,99 @@
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
+ # This class represents the table structure of the Flux CSV Response.
23
+ # Ref: http://bit.ly/flux-spec#table
24
+ class FluxTable
25
+ def initialize
26
+ @columns = []
27
+ @records = []
28
+ end
29
+ attr_reader :columns, :records
30
+
31
+ # A table's group key is subset of the entire columns dataset that assigned to the table.
32
+ # As such, all records within a table will have the same values for each column that is part of the group key.
33
+ def group_key
34
+ columns = []
35
+
36
+ @columns.each do |column|
37
+ columns.push(column) if column.group
38
+ end
39
+
40
+ columns
41
+ end
42
+ end
43
+
44
+ # A record is a tuple of values. Each record in the table represents a single point in the series.
45
+ # Ref: http://bit.ly/flux-spec#record
46
+ class FluxRecord
47
+ # @param [Integer] table the index of table which contains the record
48
+ # @param [Hash] values tuple of values
49
+ def initialize(table, values: nil)
50
+ @table = table
51
+ @values = values || {}
52
+ end
53
+ attr_reader :table, :values
54
+ attr_writer :table
55
+
56
+ # @return [Time] the inclusive lower time bound of all records
57
+ def start
58
+ values['_start']
59
+ end
60
+
61
+ # @return [Time] the exclusive upper time bound of all records
62
+ def stop
63
+ values['_stop']
64
+ end
65
+
66
+ # @return [Time] the time of the record
67
+ def time
68
+ values['_time']
69
+ end
70
+
71
+ # @return [Object] the value of the record
72
+ def value
73
+ values['_value']
74
+ end
75
+
76
+ # @return [String] value with key "_field"
77
+ def field
78
+ values['_field']
79
+ end
80
+
81
+ # @return [String] value with key "_measurement"
82
+ def measurement
83
+ values['_measurement']
84
+ end
85
+ end
86
+
87
+ # This class represents a column header specification of FluxTable.
88
+ class FluxColumn
89
+ def initialize(index: nil, label: nil, data_type: nil, group: nil, default_value: nil)
90
+ @index = index
91
+ @label = label
92
+ @data_type = data_type
93
+ @group = group
94
+ @default_value = default_value
95
+ end
96
+ attr_reader :index, :label, :data_type, :group, :default_value
97
+ attr_writer :index, :label, :data_type, :group, :default_value
98
+ end
99
+ end