influxdb-client 2.2.0.pre.3845 → 2.3.0.pre.4380

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0e228772c8d102ecf07e3179adde5114ba29e2a6e8e50e2e6e3f7b24afbf16f
4
- data.tar.gz: 5a266a027f3b789c68c65e3e21ddd551a307eff3747c65b47cf1f85e96da1f05
3
+ metadata.gz: ab20fd471926d6750ef912f5a9578839a6cd27b15fd01932f3a22cd59d8a53e5
4
+ data.tar.gz: f70d1ca2be69dec8f05b48e7caf9f1549d63b9a853807a0825d77379e85ddaa3
5
5
  SHA512:
6
- metadata.gz: 796c53b19fabae84db3d8333abc9d0246c794c80c63f8349c0464fba4a5d3576edb00e7c243806862f2e9859430ac82a236b983a91aa5cfdeb10fc5f90be671c
7
- data.tar.gz: 87722335447bf97002d60a1221927ab2dd8c06cfdeb64ab7e2f8f953bb5b6dc9006412b243b9b0e0a00263a2f2acfb18e91f96e7b7c4eca6789b5afb0070fd87
6
+ metadata.gz: e37e684b5ff7fa802086f9dc6234a8a394172b4e5f01935562bde2cbc226090590d17a713d3b85e79fcbf63915028269d42d593a02680080544dfc092fb3d76f
7
+ data.tar.gz: 6459cb3079dd81ef91144513351058a61827f50a3c12dd5f92e682537204223752daef92df94a7a5220639edb11399e45137ff126973da53cceb4fe442f54628
data/CHANGELOG.md CHANGED
@@ -1,4 +1,15 @@
1
- ## 2.2.0 [unreleased]
1
+ ## 2.3.0 [unreleased]
2
+
3
+ ## 2.2.0 [2022-02-18]
4
+
5
+ ### Features
6
+ 1. [#96](https://github.com/influxdata/influxdb-client-ruby/pull/96): Add support for Parameterized Queries
7
+
8
+ ### Bug Fixes
9
+ 1. [#97](https://github.com/influxdata/influxdb-client-ruby/pull/97): Add missing PermissionResources from Cloud API definition
10
+
11
+ ### Documentation
12
+ 1. [#96](https://github.com/influxdata/influxdb-client-ruby/pull/96): Add Parameterized Queries example
2
13
 
3
14
  ## 2.1.0 [2021-10-22]
4
15
 
data/README.md CHANGED
@@ -64,13 +64,13 @@ The client can be installed manually or with bundler.
64
64
  To install the client gem manually:
65
65
 
66
66
  ```
67
- gem install influxdb-client -v 2.1.0
67
+ gem install influxdb-client -v 2.2.0
68
68
  ```
69
69
 
70
70
  For management API:
71
71
 
72
72
  ```
73
- gem install influxdb-client-apis -v 2.1.0
73
+ gem install influxdb-client-apis -v 2.2.0
74
74
  ```
75
75
 
76
76
  ## Usage
@@ -152,6 +152,33 @@ query_api.query_stream(query: query).each do |record|
152
152
  end
153
153
  ```
154
154
 
155
+ #### Parameterized queries
156
+ InfluxDB Cloud supports [Parameterized Queries](https://docs.influxdata.com/influxdb/cloud/query-data/parameterized-queries/)
157
+ that let you dynamically change values in a query using the InfluxDB API. Parameterized queries make Flux queries more
158
+ reusable and can also be used to help prevent injection attacks.
159
+
160
+ InfluxDB Cloud inserts the params object into the Flux query as a Flux record named `params`. Use dot or bracket
161
+ notation to access parameters in the `params` record in your Flux query. Parameterized Flux queries support only `int`
162
+ , `float`, and `string` data types. To convert the supported data types into
163
+ other [Flux basic data types, use Flux type conversion functions](https://docs.influxdata.com/influxdb/cloud/query-data/parameterized-queries/#supported-parameter-data-types).
164
+
165
+ Parameterized query example:
166
+ > :warning: Parameterized Queries are supported only in InfluxDB Cloud, currently there is no support in InfluxDB OSS.
167
+
168
+ ```ruby
169
+ client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
170
+ bucket: 'my-bucket',
171
+ org: 'my-org')
172
+
173
+ query = 'from(bucket: params.bucketParam) |> range(start: duration(v: params.startParam))'
174
+ params = { 'bucketParam' => 'my-bucket', 'startParam' => '-1h' }
175
+
176
+ query_api = client.create_query_api
177
+ result = query_api.query(query: query, params: params)
178
+
179
+ result[0].records.each { |record| puts "#{record.time} #{record.measurement}: #{record.field} #{record.value}" }
180
+ ```
181
+
155
182
  ### Writing data
156
183
  The [WriteApi](https://github.com/influxdata/influxdb-client-ruby/blob/master/lib/influxdb2/client/write_api.rb) supports synchronous and batching writes into InfluxDB 2.0. In default api uses synchronous write. To enable batching you can use WriteOption.
157
184
 
@@ -1,7 +1,7 @@
1
1
  =begin
2
- #Influx OSS API Service
2
+ #InfluxDB OSS API Service
3
3
 
4
- #No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
4
+ #The InfluxDB v2 API provides a programmatic interface for all interactions with InfluxDB. Access the InfluxDB API using the `/api/v2/` endpoint.
5
5
 
6
6
  The version of the OpenAPI document: 2.0.0
7
7
 
@@ -24,6 +24,9 @@ module InfluxDB2
24
24
  # The type of query. Must be \"flux\".
25
25
  attr_reader :type
26
26
 
27
+ # Enumeration of key/value pairs that respresent parameters to be injected into query (can only specify either this field or extern and not both)
28
+ attr_accessor :params
29
+
27
30
  attr_accessor :dialect
28
31
 
29
32
  # Specifies the time that should be reported as \"now\" in the query. Default is the server's now time.
@@ -57,6 +60,7 @@ module InfluxDB2
57
60
  :'extern' => :'extern',
58
61
  :'query' => :'query',
59
62
  :'type' => :'type',
63
+ :'params' => :'params',
60
64
  :'dialect' => :'dialect',
61
65
  :'now' => :'now',
62
66
  }
@@ -68,6 +72,7 @@ module InfluxDB2
68
72
  :'extern' => :'File',
69
73
  :'query' => :'String',
70
74
  :'type' => :'String',
75
+ :'params' => :'Hash<String, Object>',
71
76
  :'dialect' => :'Dialect',
72
77
  :'now' => :'Time'
73
78
  }
@@ -106,6 +111,12 @@ module InfluxDB2
106
111
  self.type = attributes[:'type']
107
112
  end
108
113
 
114
+ if attributes.key?(:'params')
115
+ if (value = attributes[:'params']).is_a?(Hash)
116
+ self.params = value
117
+ end
118
+ end
119
+
109
120
  if attributes.key?(:'dialect')
110
121
  self.dialect = attributes[:'dialect']
111
122
  end
@@ -153,6 +164,7 @@ module InfluxDB2
153
164
  extern == o.extern &&
154
165
  query == o.query &&
155
166
  type == o.type &&
167
+ params == o.params &&
156
168
  dialect == o.dialect &&
157
169
  now == o.now
158
170
  end
@@ -166,7 +178,7 @@ module InfluxDB2
166
178
  # Calculates hash code according to all attributes.
167
179
  # @return [Integer] Hash code
168
180
  def hash
169
- [extern, query, type, dialect, now].hash
181
+ [extern, query, type, params, dialect, now].hash
170
182
  end
171
183
 
172
184
  # Builds the object from hash
@@ -36,16 +36,18 @@ module InfluxDB2
36
36
 
37
37
  # @param [Object] query the flux query to execute. The data could be represent by [String], [Query]
38
38
  # @param [String] org specifies the source organization
39
+ # @param [Enumerable] params represent key/value pairs parameters to be injected into query
39
40
  # @return [String] result of query
40
- def query_raw(query: nil, org: nil, dialect: DEFAULT_DIALECT)
41
- _post_query(query: query, org: org, dialect: dialect).read_body
41
+ def query_raw(query: nil, org: nil, dialect: DEFAULT_DIALECT, params: nil)
42
+ _post_query(query: query, org: org, dialect: dialect, params: params).read_body
42
43
  end
43
44
 
44
45
  # @param [Object] query the flux query to execute. The data could be represent by [String], [Query]
45
46
  # @param [String] org specifies the source organization
47
+ # @param [Enumerable] params represent key/value pairs parameters to be injected into query
46
48
  # @return [Array] list of FluxTables which are matched the query
47
- def query(query: nil, org: nil, dialect: DEFAULT_DIALECT)
48
- response = query_raw(query: query, org: org, dialect: dialect)
49
+ def query(query: nil, org: nil, dialect: DEFAULT_DIALECT, params: nil)
50
+ response = query_raw(query: query, org: org, dialect: dialect, params: params)
49
51
  parser = InfluxDB2::FluxCsvParser.new(response)
50
52
 
51
53
  parser.parse
@@ -54,20 +56,21 @@ module InfluxDB2
54
56
 
55
57
  # @param [Object] query the flux query to execute. The data could be represent by [String], [Query]
56
58
  # @param [String] org specifies the source organization
59
+ # @param [Enumerable] params represent key/value pairs parameters to be injected into query
57
60
  # @return stream of Flux Records
58
- def query_stream(query: nil, org: nil, dialect: DEFAULT_DIALECT)
59
- response = _post_query(query: query, org: org, dialect: dialect)
61
+ def query_stream(query: nil, org: nil, dialect: DEFAULT_DIALECT, params: nil)
62
+ response = _post_query(query: query, org: org, dialect: dialect, params: params)
60
63
 
61
64
  InfluxDB2::FluxCsvParser.new(response, stream: true)
62
65
  end
63
66
 
64
67
  private
65
68
 
66
- def _post_query(query: nil, org: nil, dialect: DEFAULT_DIALECT)
69
+ def _post_query(query: nil, org: nil, dialect: DEFAULT_DIALECT, params: nil)
67
70
  org_param = org || @options[:org]
68
71
  _check('org', org_param)
69
72
 
70
- payload = _generate_payload(query, dialect)
73
+ payload = _generate_payload(query: query, dialect: dialect, params: params)
71
74
  return nil if payload.nil?
72
75
 
73
76
  uri = _parse_uri('/api/v2/query')
@@ -76,16 +79,17 @@ module InfluxDB2
76
79
  _post_json(payload.to_body.to_json, uri)
77
80
  end
78
81
 
79
- def _generate_payload(query, dialect)
82
+ def _generate_payload(query: nil, dialect: nil, params: nil)
80
83
  if query.nil?
81
84
  nil
82
85
  elsif query.is_a?(Query)
86
+ query.params = params unless params.nil?
83
87
  query
84
88
  elsif query.is_a?(String)
85
89
  if query.empty?
86
90
  nil
87
91
  else
88
- Query.new(query: query, dialect: dialect, type: nil)
92
+ Query.new(query: query, dialect: dialect, type: nil, params: params)
89
93
  end
90
94
  end
91
95
  end
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module InfluxDB2
22
- VERSION = '2.2.0'.freeze
22
+ VERSION = '2.3.0'.freeze
23
23
  end
@@ -49,6 +49,27 @@ class QueryApiTest < MiniTest::Test
49
49
  assert_equal result, SUCCESS_DATA
50
50
  end
51
51
 
52
+ def test_parameterized_query_raw
53
+ body = '{"query":"from(bucket: params.bucketParam) |> range(start: duration(v: params.startParam)) |> last()",' \
54
+ '"params":{"bucketParam":"my-bucket","startParam":"1970-01-01T00:00:00.000000001Z"},' \
55
+ '"dialect":{"header":true,"delimiter":",","annotations":["datatype","group","default"],' \
56
+ '"commentPrefix":"#","dateTimeFormat":"RFC3339"}}'
57
+ stub_request(:post, 'http://localhost:8086/api/v2/query?org=my-org')
58
+ .with(body: body)
59
+ .to_return(body: SUCCESS_DATA)
60
+
61
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
62
+ bucket: 'my-bucket',
63
+ org: 'my-org',
64
+ use_ssl: false)
65
+
66
+ query = 'from(bucket: params.bucketParam) |> range(start: duration(v: params.startParam)) |> last()'
67
+ params = Hash['bucketParam' => 'my-bucket', 'startParam' => '1970-01-01T00:00:00.000000001Z']
68
+ result = client.create_query_api.query_raw(query: query, params: params)
69
+
70
+ assert_equal result, SUCCESS_DATA
71
+ end
72
+
52
73
  def test_query
53
74
  stub_request(:post, 'http://localhost:8086/api/v2/query?org=my-org')
54
75
  .to_return(body: SUCCESS_DATA)
@@ -73,6 +94,36 @@ class QueryApiTest < MiniTest::Test
73
94
  assert_equal 'free', record1.field
74
95
  end
75
96
 
97
+ def test_parameterized_query
98
+ body = '{"query":"from(bucket: params.bucketParam) |> range(start: duration(v: params.startParam)) |> last()",' \
99
+ '"params":{"bucketParam":"my-bucket","startParam":"1970-01-01T00:00:00.000000001Z"},' \
100
+ '"dialect":{"header":true,"delimiter":",","annotations":["datatype","group","default"],' \
101
+ '"commentPrefix":"#","dateTimeFormat":"RFC3339"}}'
102
+ stub_request(:post, 'http://localhost:8086/api/v2/query?org=my-org')
103
+ .with(body: body)
104
+ .to_return(body: SUCCESS_DATA)
105
+
106
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
107
+ bucket: 'my-bucket',
108
+ org: 'my-org',
109
+ use_ssl: false)
110
+
111
+ query = 'from(bucket: params.bucketParam) |> range(start: duration(v: params.startParam)) |> last()'
112
+ params = Hash['bucketParam' => 'my-bucket', 'startParam' => '1970-01-01T00:00:00.000000001Z']
113
+
114
+ result = client.create_query_api.query(query: query, params: params)
115
+
116
+ assert_equal 1, result.length
117
+ assert_equal 4, result[0].records.length
118
+
119
+ record1 = result[0].records[0]
120
+
121
+ assert_equal Time.parse('1970-01-01T00:00:10Z').to_datetime.rfc3339(9), record1.time
122
+ assert_equal 'mem', record1.measurement
123
+ assert_equal 10, record1.value
124
+ assert_equal 'free', record1.field
125
+ end
126
+
76
127
  def test_headers
77
128
  stub_request(:post, 'http://localhost:8086/api/v2/query?org=my-org')
78
129
  .to_return(body: SUCCESS_DATA)
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: 2.2.0.pre.3845
4
+ version: 2.3.0.pre.4380
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Bednar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-22 00:00:00.000000000 Z
11
+ date: 2022-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -189,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
189
  - !ruby/object:Gem::Version
190
190
  version: 1.3.1
191
191
  requirements: []
192
- rubygems_version: 3.2.22
192
+ rubygems_version: 3.2.32
193
193
  signing_key:
194
194
  specification_version: 4
195
195
  summary: Ruby library for InfluxDB 2.