AnyEvent-InfluxDB
view release on metacpan or search on metacpan
lib/AnyEvent/InfluxDB.pm view on Meta::CPAN
my $version = $cv->recv;
Checks the leader of the cluster to ensure that the leader is available and ready.
The optional parameter C<wait_for_leader> specifies the number of seconds to wait
before returning a response.
The required C<on_success> code reference is executed if request was successful
with the value of C<X-Influxdb-Version> response header as argument,
otherwise executes the required C<on_error> code reference with the value of
C<Reason> response header as argument.
=head2 Managing Data
=head3 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 C<data> to database C<database> with optional parameters:
retention policy C<rp>, time precision C<precision> and consistency C<consistency>.
The required C<on_success> code reference is executed if request was successful,
otherwise executes the required C<on_error> code reference.
The C<data> can be specified as single scalar value or hash reference with
required keys C<measurement> and C<fields> and optional C<tags> and C<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
L<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',
],
...
);
=head2 Querying Data
=head3 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 } ) {
( run in 2.917 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )