Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
Permalink
Browse files
Allow a retention policy to be passed when writing points.
  • Loading branch information
recurser committed Oct 27, 2015
1 parent 17c506e commit 5a0efba
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 15 deletions.
@@ -226,10 +226,10 @@ Write data with time precision (precision can be set in 2 ways):
``` ruby
require 'influxdb'
username = 'foo'
password = 'bar'
database = 'site_development'
name = 'foobar'
username = 'foo'
password = 'bar'
database = 'site_development'
name = 'foobar'
time_precision = 's'
# either in the client initialization:
@@ -249,6 +249,31 @@ influxdb.write_point(name, data, time_precision)
```

Write data with a specific retention policy:

``` ruby
require 'influxdb'
username = 'foo'
password = 'bar'
database = 'site_development'
name = 'foobar'
precision = 's'
retention = '1h.cpu'
influxdb = InfluxDB::Client.new database,
username: username,
password: password
data = {
values: { value: 0 },
tags: { foo: 'bar', bar: 'baz' }
timestamp: Time.now.to_i
}
influxdb.write_point(name, data, precision, retention)
```

Write multiple points in a batch (performance boost):

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

Write asynchronously:
Write multiple points in a batch with a specific retention policy:

``` ruby
data = [
{
series: 'cpu',
tags: { host: 'server_1', regios: 'us' },
values: {internal: 5, external: 0.453345}
},
{
series: 'gpu',
values: {value: 0.9999},
}
]
precision = 'm'
retention = '1h.cpu'
influxdb.write_points(data, precision, retention)
```

Write asynchronously (note that a retention policy cannot be specified for asynchronous writes):

``` ruby
require 'influxdb'
@@ -283,7 +330,6 @@ username = 'foo'
password = 'bar'
database = 'site_development'
name = 'foobar'
time_precision = 's'
influxdb = InfluxDB::Client.new database,
username: username,
@@ -299,7 +345,7 @@ data = {
influxdb.write_point(name, data)
```

Write data via UDP:
Write data via UDP (note that a retention policy cannot be specified for UDP writes):

``` ruby
require 'influxdb'
@@ -37,21 +37,23 @@ def query(query, opts = {})
# values: {value: 0.9999},
# }
# ])
def write_points(data, precision = nil)
def write_points(data, precision = nil, retention_policy = nil)
data = data.is_a?(Array) ? data : [data]
payload = generate_payload(data)
writer.write(payload, precision)
writer.write(payload, precision, retention_policy)
end

# Example:
# write_point('cpu', tags: {region: 'us'}, values: {internal: 60})
def write_point(series, data, precision = nil)
write_points(data.merge(series: series), precision)
def write_point(series, data, precision = nil, retention_policy = nil)
write_points(data.merge(series: series), precision, retention_policy)
end

def write(data, precision)
def write(data, precision, retention_policy = nil)
precision ||= config.time_precision
url = full_url("/write", db: config.database, precision: precision)
params = { db: config.database, precision: precision }
params[:rp] = retention_policy if retention_policy
url = full_url("/write", params)
post(url, data)
end

@@ -12,7 +12,7 @@ def initialize(client, config)
@config = config
end

def write(data, _precision = nil)
def write(data, _precision = nil, _retention_policy = nil)
data = data.is_a?(Array) ? data : [data]
data.map { |p| worker.push(p) }
end
@@ -13,7 +13,7 @@ def initialize(client, config)
socket.connect(host, port)
end

def write(payload, _precision = nil)
def write(payload, _precision = nil, _retention_policy = nil)
socket.send(payload, 0)
end
end
@@ -143,5 +143,35 @@
expect(subject.write_points(data, 'm')).to be_a(Net::HTTPOK)
end
end

context "with retention policy" do
let(:data) do
[{
series: 'cpu',
values: { temp: 88, value: 54 }
},
{
series: 'gpu',
values: { value: 0.5435345 }
}]
end

let(:body) do
data.map do |point|
InfluxDB::PointValue.new(point).dump
end.join("\n")
end

before do
stub_request(:post, "http://influxdb.test:9999/write").with(
query: { u: "username", p: "password", precision: 's', db: database, rp: 'rp_1_hour' },
headers: { "Content-Type" => "application/octet-stream" },
body: body
)
end
it "should POST multiple points" do
expect(subject.write_points(data, nil, 'rp_1_hour')).to be_a(Net::HTTPOK)
end
end
end
end

0 comments on commit 5a0efba

Please sign in to comment.