AnyEvent-InfluxDB
view release on metacpan or search on metacpan
# 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";
for my $value ( @{ $row->{values} || [] } ) {
print " * $_ = $value->{$_}\n" for keys %{ $value || {} };
}
}
Executes an select query on database "database" created from provided
arguments measurement "measurement", fields to select "fields", optional
"where" clause, grouped by "group_by" and empty values filled with
"fill", ordered by "order_by" with number of results limited to "limit"
with offset "offset". To limit number of returned series use "slimit"
with offset "soffset". If "into" parameter is provided the result of the
query will be copied to specified measurement. If "epoch" is provided
the returned "time" value will in Unix epoch format. Optional
"chunk_size" can be provided to override the default value of 10,000
datapoints.
The required "on_success" code reference is executed if request was
successful, otherwise executes the required "on_error" code reference.
Database Management
create_database
$cv = AE::cv;
$db->create_database(
# raw query
q => "CREATE DATABASE mydb WITH DURATION 7d REPLICATION 3 SHARD DURATION 30m NAME oneweek",
# or query created from arguments
database => "mydb",
# retention policy parameters
duration => '7d',
shard_duration => '30m',
replication => 3,
name => 'oneweek',
# callbacks
on_success => $cv,
on_error => sub {
$cv->croak("Failed to create database: @_");
}
);
$cv->recv;
Creates database specified by "database" argument.
If one of retention policy parameters is specified then the database
will be created with that retention policy as default - see "Retention
Policy Management" for more details.
The required "on_success" code reference is executed if request was
successful, otherwise executes the required "on_error" code reference.
drop_database
$cv = AE::cv;
$db->drop_database(
# raw query
q => "DROP DATABASE mydb",
# or query created from arguments
database => "mydb",
# callbacks
on_success => $cv,
on_error => sub {
$cv->croak("Failed to drop database: @_");
}
);
$cv->recv;
Drops database specified by "database" argument.
The required "on_success" code reference is executed if request was
successful, otherwise executes the required "on_error" code reference.
( run in 1.306 second using v1.01-cache-2.11-cpan-39bf76dae61 )