influxdb-client 2.2.0.pre.3845 → 2.2.0.pre.4244
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +27 -0
- data/lib/influxdb2/client/models/query.rb +15 -3
- data/lib/influxdb2/client/query_api.rb +14 -10
- data/test/influxdb/query_api_test.rb +51 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0dd61b35e7f884f78c34dc811322b5729752c29326b227cc490cefd51e74a8a
|
4
|
+
data.tar.gz: 67fa3056a29be235d4403f03af55fa2f13263935064886a88a9baaad5d513a51
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a67ea2c94cb86793664c997de39552c63ab9f6b0591368ac574788e00efb11ce6ea837f71e2e8e7dc596c213a62124512848fdd0b076b9581e16ecfb08694c63
|
7
|
+
data.tar.gz: fa8e865d6838ea8498ac4b76bc936d5712173b48e7bb815652a62f94fa233582206cd2bffd77f1689a8bbd7d03f30f55b2c4efff2bb06cf18fadcc0c99b87df4
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
## 2.2.0 [unreleased]
|
2
2
|
|
3
|
+
### Features
|
4
|
+
1. [#96](https://github.com/influxdata/influxdb-client-ruby/pull/96): Add support for Parameterized Queries
|
5
|
+
|
6
|
+
### Documentation
|
7
|
+
1. [#96](https://github.com/influxdata/influxdb-client-ruby/pull/96): Add Parameterized Queries example
|
8
|
+
|
3
9
|
## 2.1.0 [2021-10-22]
|
4
10
|
|
5
11
|
### Features
|
data/README.md
CHANGED
@@ -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
|
-
#
|
2
|
+
#InfluxDB OSS API Service
|
3
3
|
|
4
|
-
#
|
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
|
@@ -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.
|
4
|
+
version: 2.2.0.pre.4244
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jakub Bednar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-31 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.
|
192
|
+
rubygems_version: 3.2.32
|
193
193
|
signing_key:
|
194
194
|
specification_version: 4
|
195
195
|
summary: Ruby library for InfluxDB 2.
|