influxdb-client 2.1.0 → 2.2.0

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: be3ac6c581d2ec4f1bea7183b78bc8d189510a9815cd54483e09611ca65402bd
4
- data.tar.gz: 5c18d34011098b03d9226188d5663a1c16b2b02348931409bf7fb5fb832dbb10
3
+ metadata.gz: 5d6f5f89f9daa086decb93207d92e031202d6b710dc727c4ae57bc46944be578
4
+ data.tar.gz: 437d834ee65767d564211e114172cef9b2e256b3a9d523c9f649c5c35869062f
5
5
  SHA512:
6
- metadata.gz: 5e998c044335d66a56dcf9b25e53fbdbac305307458a456a451c517698e2e6f1295ea44bb11142fbbad6e42856e1a0e5fa1f0c6105cc3a9711a1efdc30ebfe8f
7
- data.tar.gz: 58626af8588b716b4b4edba032c0a5d28b8a5645080ad0cd2ba39ded13d529025c6029aee5ac66e687a6143c6c4795d356730f74fbc78f78d551299745194217
6
+ metadata.gz: caafd6227b5159448a486952e0943792aafced212a489771516da8a0ef3a0e9b6df14915202bfedb2c254c1c6887dcfa014c0712589060612799b3674483cef0
7
+ data.tar.gz: 588b2b3df0f49fa89db5d3e8ef30d946178c75d01a5e7f3346ca818edc58f986ffc35484981d24ef4104cc919eb0b9b21c68cf0c2e366880b47f3fec2d2f1039
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ## 2.2.0 [2022-02-18]
2
+
3
+ ### Features
4
+ 1. [#96](https://github.com/influxdata/influxdb-client-ruby/pull/96): Add support for Parameterized Queries
5
+
6
+ ### Bug Fixes
7
+ 1. [#97](https://github.com/influxdata/influxdb-client-ruby/pull/97): Add missing PermissionResources from Cloud API definition
8
+
9
+ ### Documentation
10
+ 1. [#96](https://github.com/influxdata/influxdb-client-ruby/pull/96): Add Parameterized Queries example
11
+
1
12
  ## 2.1.0 [2021-10-22]
2
13
 
3
14
  ### Features
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.1.0'.freeze
22
+ VERSION = '2.2.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.1.0
4
+ version: 2.2.0
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