Skip to content

Commit

Permalink
status: fix query optimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Nov 15, 2024
1 parent 03c159a commit 286c763
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/Thruk/Utils/Status.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3270,13 +3270,19 @@ sub _improve_filter {
last;
}
my $missed = 0;
# encode first filter and compare to all of them
my $enc = $json->encode($filter->{$key}->[0]->{'-and'}->[0]);
for my $f (@{$filter->{$key}}) {
my $enc2 = $json->encode($f->{'-and'}->[0]);
if($enc2 ne $enc) {
$missed = 1;
last;
}
# must have at least one filter left
if(scalar @{$f->{'-and'}} <= 1) {
$missed = 1;
last;
}
}
# found identical filter
if(!$missed) {
Expand Down
83 changes: 81 additions & 2 deletions t/108-thruk_utils_status.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use strict;
use Test::More;
use utf8;

plan tests => 57;
plan tests => 59;

BEGIN {
use lib('t');
Expand Down Expand Up @@ -245,4 +245,83 @@ for my $b (@{$broken}) {
my $err = $@;
like($err, $b->[1], "query failed to parse");
is($f, undef, "no filter returned");
}
}

################################################################################
# test query optimizer
{
my $filter = {
'-or' => [
{
'-and' => [
{ 'host_groups' => { '>=' => 'hostgroup_01' } },
{
'-or' => [
'plugin_output', { '~~' => 'checked' },
'long_plugin_output', { '~~' => 'checked' }
]
}
]
}, {
'-and' => [
{
'host_groups' => { '>=' => 'hostgroup_01' }
},
{
'-or' => [
'plugin_output', { '~~' => 'checked' },
'long_plugin_output', { '~~' => 'checked' }
]
},
{
'-and' => [
'plugin_output', { '!~~' => 'random' },
'long_plugin_output', { '!~~' => 'random' }
]
}
]
}
]
};
my $exp = {
'-and' => [
{ '-and' => [ { 'host_groups' => { '>=' => 'hostgroup_01' } } ] },
{
'-or' => [
{
'-and' => [
{
'-or' => [
'plugin_output', { '~~' => 'checked' },
'long_plugin_output', { '~~' => 'checked' }
]
}
]
},
{
'-and' => [
{
'-or' => [
'plugin_output', { '~~' => 'checked' },
'long_plugin_output', { '~~' => 'checked' }
]
},
{
'-and' => [
'plugin_output', { '!~~' => 'random' },
'long_plugin_output', { '!~~' => 'random' }
]
}
]
}
]
}
]
};
my $json = Cpanel::JSON::XS->new->utf8->canonical;
my $enc = $json->encode($filter);
my $optimized = Thruk::Utils::Status::_improve_filter($filter);
my $enc2 = $json->encode($optimized);
ok($enc ne $enc2, "query can be optimized");
is_deeply($optimized, $exp, "optimized query is correct");
};

0 comments on commit 286c763

Please sign in to comment.