Merge pull request #1702 from CPbN/influx

InfluxDB, allow to select custom field
This commit is contained in:
qgarnier 2019-11-14 10:29:15 +01:00 committed by GitHub
commit 8e02f9d4ae
2 changed files with 17 additions and 15 deletions

View File

@ -175,7 +175,7 @@ sub query {
my $data;
foreach my $query (@{$options{queries}}) {
my $results = $self->request(method => 'POST', url_path => '/query', post_param => ['q=' . $query]);
my $results = $self->request(method => 'POST', url_path => '/query?epoch=s', post_param => ['q=' . $query]);
if (defined($results->{results}[0]->{error})) {
$self->{output}->add_option_msg(short_msg => "API returns error '" . $results->{results}[0]->{error} . "'");
@ -196,22 +196,22 @@ sub compute {
my $points = 0;
foreach my $value (@{$options{values}}) {
$result = 0 if (!defined($result));
$result += $$value[1];
$result += $$value[$options{column}];
$points++;
}
$result /= $points;
} elsif ($options{aggregation} eq 'minimum') {
foreach my $value (@{$options{values}}) {
$result = $$value[1] if (!defined($result) || $$value[1] < $result);
$result = $$value[$options{column}] if (!defined($result) || $$value[$options{column}] < $result);
}
} elsif ($options{aggregation} eq 'maximum') {
foreach my $value (@{$options{values}}) {
$result = $$value[1] if (!defined($result) || $$value[1] > $result);
$result = $$value[$options{column}] if (!defined($result) || $$value[$options{column}] > $result);
}
} elsif ($options{aggregation} eq 'sum') {
foreach my $value (@{$options{values}}) {
$result = 0 if (!defined($result));
$result += $$value[1];
$result += $$value[$options{column}];
}
}

View File

@ -141,20 +141,22 @@ sub manage_selection {
$self->{queries_results} = {};
my (@results, @queries);
my $query_index = -1;
foreach my $label (keys %{$self->{queries}}) {
$query_index++;
@queries = ();
push @queries, $self->{queries}->{$label};
}
my $queries_results = [];
$queries_results = $options{custom}->query(queries => \@queries) if (scalar(@queries) > 0);
my $queries_results = $options{custom}->query(queries => \@queries);
foreach my $result (@{$queries_results}) {
next if (!defined($result->{tags}->{$self->{option_results}->{instance}}));
my $value;
$value = $options{custom}->compute(aggregation => $self->{option_results}->{aggregation}, values => $result->{values}) if (defined($result->{values}));
$self->{queries_results}->{$result->{tags}->{$self->{option_results}->{instance}}}->{instance} = $result->{tags}->{$self->{option_results}->{instance}};
$self->{queries_results}->{$result->{tags}->{$self->{option_results}->{instance}}}->{$result->{columns}[1]} = $value;
foreach my $result (@{$queries_results}) {
next if (!defined($result->{tags}->{$self->{option_results}->{instance}}));
my ($column_index) = grep { $result->{columns}[$_] eq $self->{custom_keys}[$query_index] } (0 .. @{$result->{columns}}-1);
my $value;
$value = $options{custom}->compute(aggregation => $self->{option_results}->{aggregation}, values => $result->{values}, column => $column_index) if (defined($result->{values}));
$self->{queries_results}->{$result->{tags}->{$self->{option_results}->{instance}}}->{instance} = $result->{tags}->{$self->{option_results}->{instance}};
$self->{queries_results}->{$result->{tags}->{$self->{option_results}->{instance}}}->{$result->{columns}[$column_index]} = $value;
}
}
if (scalar(keys %{$self->{queries_results}}) <= 0) {