AnyEvent-InfluxDB
view release on metacpan or search on metacpan
);
my $version = $cv->recv;
Checks the leader of the cluster to ensure that the leader is available
and ready. The optional parameter "wait_for_leader" specifies the number
of seconds to wait before returning a response.
The required "on_success" code reference is executed if request was
successful with the value of "X-Influxdb-Version" response header as
argument, otherwise executes the required "on_error" code reference with
the value of "Reason" response header as argument.
Managing Data
write
$cv = AE::cv;
$db->write(
database => 'mydb',
precision => 's',
rp => 'last_day',
consistency => 'quorum',
data => [
# line protocol formatted
'cpu_load,host=server02,region=eu-east sensor="top",value=0.64 1456097956',
# or as a hash
{
measurement => 'cpu_load',
tags => {
host => 'server02',
region => 'eu-east',
},
fields => {
value => '0.64',
sensor => q{"top"},
},
time => time()
}
],
on_success => $cv,
on_error => sub {
$cv->croak("Failed to write data: @_");
}
);
$cv->recv;
Writes time-series data "data" to database "database" with optional
parameters: retention policy "rp", time precision "precision" and
consistency "consistency".
The required "on_success" code reference is executed if request was
successful, otherwise executes the required "on_error" code reference.
The "data" can be specified as single scalar value or hash reference
with required keys "measurement" and "fields" and optional "tags" and
"time". Both can be also mixed and matched within an array reference.
Scalar values are expected to be formatted using InfluxDB line protocol.
All special characters need to be escaped. In that case you might want
to use InfluxDB::LineProtocol:
use InfluxDB::LineProtocol qw(dataline);
...
$db->write(
database => 'mydb',
precision => 'n',
data => [
dataline('CPU Load', 0.64, { "Region of the World" => "Eastern Europe", codename => "eu-east" }, 1437868012260500137)
# which translates to
'CPU\ Load,Region\ of\ the\ World=Eastern\ Europe,codename=eu-east value=0.64 1437868012260500137',
],
...
);
Querying Data
select
$cv = AE::cv;
$db->select(
database => 'mydb',
# return time in Unix epoch format
epoch => "s",
# raw query
q => "SELECT count(value) FROM cpu_load"
." WHERE region = 'eu-east' AND time > now() - 14d"
." GROUP BY time(1d) fill(none)"
." ORDER BY time DESC"
." LIMIT 10 OFFSET 3",
# or query created from arguments
fields => 'count(value)',
measurement => 'cpu_load',
where => "region = 'eu-east' AND time > now() - 14d",
group_by => 'time(1d)',
fill => 'none',
order_by => 'time DESC',
limit => 10,
offset => 3,
# downsample result to another database, retention policy and measurement
into => 'otherdb."default".cpu_load_per5m',
# callbacks
on_success => $cv,
on_error => sub {
$cv->croak("Failed to select data: @_");
}
);
my $results = $cv->recv;
for my $row ( @{ $results } ) {
print "Measurement: $row->{name}\n";
print "Values:\n";
( run in 1.946 second using v1.01-cache-2.11-cpan-39bf76dae61 )