Skip to content
This repository was archived by the owner on Feb 24, 2021. It is now read-only.

Commit 5a0efba

Browse files
committed
Allow a retention policy to be passed when writing points.
1 parent 17c506e commit 5a0efba

File tree

5 files changed

+93
-15
lines changed

5 files changed

+93
-15
lines changed

README.md

+53-7
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ Write data with time precision (precision can be set in 2 ways):
226226
``` ruby
227227
require 'influxdb'
228228

229-
username = 'foo'
230-
password = 'bar'
231-
database = 'site_development'
232-
name = 'foobar'
229+
username = 'foo'
230+
password = 'bar'
231+
database = 'site_development'
232+
name = 'foobar'
233233
time_precision = 's'
234234

235235
# either in the client initialization:
@@ -249,6 +249,31 @@ influxdb.write_point(name, data, time_precision)
249249

250250
```
251251

252+
Write data with a specific retention policy:
253+
254+
``` ruby
255+
require 'influxdb'
256+
257+
username = 'foo'
258+
password = 'bar'
259+
database = 'site_development'
260+
name = 'foobar'
261+
precision = 's'
262+
retention = '1h.cpu'
263+
264+
influxdb = InfluxDB::Client.new database,
265+
username: username,
266+
password: password
267+
268+
data = {
269+
values: { value: 0 },
270+
tags: { foo: 'bar', bar: 'baz' }
271+
timestamp: Time.now.to_i
272+
}
273+
274+
influxdb.write_point(name, data, precision, retention)
275+
```
276+
252277
Write multiple points in a batch (performance boost):
253278

254279
``` ruby
@@ -274,7 +299,29 @@ influxdb.write_points(data, precision)
274299

275300
```
276301

277-
Write asynchronously:
302+
Write multiple points in a batch with a specific retention policy:
303+
304+
``` ruby
305+
306+
data = [
307+
{
308+
series: 'cpu',
309+
tags: { host: 'server_1', regios: 'us' },
310+
values: {internal: 5, external: 0.453345}
311+
},
312+
{
313+
series: 'gpu',
314+
values: {value: 0.9999},
315+
}
316+
]
317+
318+
precision = 'm'
319+
retention = '1h.cpu'
320+
influxdb.write_points(data, precision, retention)
321+
322+
```
323+
324+
Write asynchronously (note that a retention policy cannot be specified for asynchronous writes):
278325

279326
``` ruby
280327
require 'influxdb'
@@ -283,7 +330,6 @@ username = 'foo'
283330
password = 'bar'
284331
database = 'site_development'
285332
name = 'foobar'
286-
time_precision = 's'
287333

288334
influxdb = InfluxDB::Client.new database,
289335
username: username,
@@ -299,7 +345,7 @@ data = {
299345
influxdb.write_point(name, data)
300346
```
301347

302-
Write data via UDP:
348+
Write data via UDP (note that a retention policy cannot be specified for UDP writes):
303349

304350
``` ruby
305351
require 'influxdb'

lib/influxdb/query/core.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,23 @@ def query(query, opts = {})
3737
# values: {value: 0.9999},
3838
# }
3939
# ])
40-
def write_points(data, precision = nil)
40+
def write_points(data, precision = nil, retention_policy = nil)
4141
data = data.is_a?(Array) ? data : [data]
4242
payload = generate_payload(data)
43-
writer.write(payload, precision)
43+
writer.write(payload, precision, retention_policy)
4444
end
4545

4646
# Example:
4747
# write_point('cpu', tags: {region: 'us'}, values: {internal: 60})
48-
def write_point(series, data, precision = nil)
49-
write_points(data.merge(series: series), precision)
48+
def write_point(series, data, precision = nil, retention_policy = nil)
49+
write_points(data.merge(series: series), precision, retention_policy)
5050
end
5151

52-
def write(data, precision)
52+
def write(data, precision, retention_policy = nil)
5353
precision ||= config.time_precision
54-
url = full_url("/write", db: config.database, precision: precision)
54+
params = { db: config.database, precision: precision }
55+
params[:rp] = retention_policy if retention_policy
56+
url = full_url("/write", params)
5557
post(url, data)
5658
end
5759

lib/influxdb/writer/async.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def initialize(client, config)
1212
@config = config
1313
end
1414

15-
def write(data, _precision = nil)
15+
def write(data, _precision = nil, _retention_policy = nil)
1616
data = data.is_a?(Array) ? data : [data]
1717
data.map { |p| worker.push(p) }
1818
end

lib/influxdb/writer/udp.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(client, config)
1313
socket.connect(host, port)
1414
end
1515

16-
def write(payload, _precision = nil)
16+
def write(payload, _precision = nil, _retention_policy = nil)
1717
socket.send(payload, 0)
1818
end
1919
end

spec/influxdb/cases/write_points_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,35 @@
143143
expect(subject.write_points(data, 'm')).to be_a(Net::HTTPOK)
144144
end
145145
end
146+
147+
context "with retention policy" do
148+
let(:data) do
149+
[{
150+
series: 'cpu',
151+
values: { temp: 88, value: 54 }
152+
},
153+
{
154+
series: 'gpu',
155+
values: { value: 0.5435345 }
156+
}]
157+
end
158+
159+
let(:body) do
160+
data.map do |point|
161+
InfluxDB::PointValue.new(point).dump
162+
end.join("\n")
163+
end
164+
165+
before do
166+
stub_request(:post, "http://influxdb.test:9999/write").with(
167+
query: { u: "username", p: "password", precision: 's', db: database, rp: 'rp_1_hour' },
168+
headers: { "Content-Type" => "application/octet-stream" },
169+
body: body
170+
)
171+
end
172+
it "should POST multiple points" do
173+
expect(subject.write_points(data, nil, 'rp_1_hour')).to be_a(Net::HTTPOK)
174+
end
175+
end
146176
end
147177
end

0 commit comments

Comments
 (0)