influxdb-client 2.1.0.pre.3812 → 2.2.0.pre.4317

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: 7a3fcf08b154394bbe7a5c20a3025e258d78696436ecef75c769e22ccfb3512e
4
- data.tar.gz: 49360f4aaf8995b26a460e5170d8c23959c1bc3610fe834a76f3e03479595820
3
+ metadata.gz: d9c8b5d2806c74100dfebe622d4ad32a317b35589285f59660dd2e33b5c7a6ad
4
+ data.tar.gz: ba1d16c3df99cb32805b739659fff918468ddd1551ec54a11d709536a2655d6e
5
5
  SHA512:
6
- metadata.gz: 960131b0ab69dfbcb7a907795b118500a299295211feec800727390fae8eabaafb5733bf031fb56b7388f8aea8385e24c57b651e08e70ac126604925b2248a47
7
- data.tar.gz: a15a699c569bf89689273217f7efe2ec9b53a354344b6714003e3ff9eb89aa210f1a3c6c875451a77c07be4bc6d97d799e8f7fea9e53d1a5e23a61eaf4b7fc4e
6
+ metadata.gz: 5cc8acc30fd2853e6fbcec229bef09db192cb3ac68e8efcbb3c785815b9560e9a8c30e9267ebdbe566756ab4625ba801df118273b34d7c640b2a0b8e75da7132
7
+ data.tar.gz: 624de4303580c11a210aa05b047bf16fc5c1d38f8262d34fab5783f81a7ec9c538a5872e8dc5a8167554fda1fc68efda16a213d0d29c78d8cd364b7e029b5b8b
data/CHANGELOG.md CHANGED
@@ -1,4 +1,15 @@
1
- ## 2.1.0 [unreleased]
1
+ ## 2.2.0 [unreleased]
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
+
12
+ ## 2.1.0 [2021-10-22]
2
13
 
3
14
  ### Features
4
15
  1. [#93](https://github.com/influxdata/influxdb-client-ruby/pull/93): Add `PingApi` to check status of OSS and Cloud instance
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.0.0
67
+ gem install influxdb-client -v 2.1.0
68
68
  ```
69
69
 
70
70
  For management API:
71
71
 
72
72
  ```
73
- gem install influxdb-client-apis -v 2.0.0
73
+ gem install influxdb-client-apis -v 2.1.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.pre.3812
4
+ version: 2.2.0.pre.4317
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-20 00:00:00.000000000 Z
11
+ date: 2022-02-01 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.