Skip to content

Commit

Permalink
rest: improve performance
Browse files Browse the repository at this point in the history
ex.: on count(*) queries and queries on performance data or custom variables.
Do not fetch all columns in those cases.
  • Loading branch information
sni committed Apr 29, 2024
1 parent 0f4af7d commit 1b2c719
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
49 changes: 45 additions & 4 deletions lib/Thruk/Controller/rest_v1.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,31 @@ sub _livestatus_options {

# try to reduce the number of requested columns
if($type) {
my $columns = get_request_columns($c, NAMES) || [];
my $columns = [];
for my $col (@{get_request_columns($c, STRUCT) || []}) {
if($col->{'orig'} eq 'count(*)') {
# find a suitable column without requesting *
if($type eq 'hosts') {
push @{$columns}, 'name'; next;
}
elsif($type eq 'services') {
push @{$columns}, 'description'; next;
}
elsif($type eq 'contacts') {
push @{$columns}, 'name'; next;
}
elsif($type eq 'logs') {
push @{$columns}, 'class'; next;
}
elsif($type eq 'comments') {
push @{$columns}, 'id'; next;
}
elsif($type eq 'downtimes') {
push @{$columns}, 'id'; next;
}
}
push @{$columns}, $col->{'column'};
}
if(scalar @{$columns} > 0) {
push @{$columns}, @{get_filter_columns($c)};
$columns = Thruk::Base::array_uniq($columns);
Expand Down Expand Up @@ -1172,16 +1196,33 @@ sub _livestatus_options {
# if all requested columns are default columns, we can pass the columns to livestatus
my $found = 1;
for my $col (@{$columns}) {
$col = "comments_with_info" if $col eq 'comments_info';
$col = "downtimes_with_info" if $col eq 'downtimes_info';
$col = "custom_variables" if $col =~ m/^_/mx;
$col = "comments_with_info" if $col eq 'comments_info';
$col = "downtimes_with_info" if $col eq 'downtimes_info';
if($col =~ m/^_/mx) {
$col = 'custom_variable_values';
push @{$columns}, 'custom_variable_names' if ! grep { $_ eq 'custom_variable_names' } @{$columns};
if($type eq 'services') {
push @{$columns}, 'host_custom_variable_values' if ! grep { $_ eq 'host_custom_variable_values' } @{$columns};
push @{$columns}, 'host_custom_variable_names' if ! grep { $_ eq 'host_custom_variable_names' } @{$columns};
}
}
if($col eq '*') {
$found = 0;
last;
}
if(!$ref_columns->{$col} && $ref_columns->{'perf_data'}) {
# add perf data column if available, since this is the only column (along with custom vars) which gets expanded later
$col = "perf_data" if ! grep { $_ eq 'perf_data' } @{$columns};
}

if(!$ref_columns->{$col}) {
_debug("unknown column requested: %s, need to fetch all columns", $col);
$found = 0;
last;
}
}
if($found) {
_debug("all request columns found: %s", join(', ', @{$columns}));
$options->{'columns'} = $columns;
}
}
Expand Down
24 changes: 23 additions & 1 deletion t/scenarios/rest_api/t/local/rest_api_misc.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ BEGIN {
require TestUtils;
import TestUtils;
}
plan tests => 57;
plan tests => 70;

###########################################################
# test thruks script path
Expand Down Expand Up @@ -88,3 +88,25 @@ $ENV{'THRUK_TEST_AUTH_USER'} = "omdadmin";
}

###########################################################
# requested columns
{
# do not request all columns for a count(*)
TestUtils::test_command({
cmd => '/usr/bin/env thruk r -vv \'/services?columns=count(*):total,state\'',
errlike => ['/all request columns found: description, state/', '/"total"/'],
});

# do not request all columns for performance data
TestUtils::test_command({
cmd => '/usr/bin/env thruk r -vv \'/hosts?columns=name,rta\'',
errlike => ['/all request columns found: name, perf_data/', '/"rta"/'],
});

# do not request all columns for custom variables
TestUtils::test_command({
cmd => '/usr/bin/env thruk r -vv \'/hosts?columns=name,_SITE\'',
errlike => ['/all request columns found: name, custom_variable_values, custom_variable_names/', '/"_SITE"/', '/: null,/'],
});
}

###########################################################

0 comments on commit 1b2c719

Please sign in to comment.