Skip to content

Commit

Permalink
main: use authoritive backends when getting number of contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Jul 10, 2023
1 parent 005138d commit 6c65ee7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/Thruk/Action/AddDefaults.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,36 @@ sub set_enabled_backends {
return($disabled_backends);
}

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

=head2 has_backends_set
has_backends_set($c)
returns 1 if user has set custom backends
=cut
sub has_backends_set {
my($c) = @_;
my $num_backends = scalar @{$c->db->get_peers()};
my $cookie_src = $c->stash->{'backend_cookie_src'} // 'thruk_backends';
if($num_backends <= 1) {
return;
}
if(defined $c->cookies($cookie_src)) {
return(1);
}
my $backend = $c->req->parameters->{'backend'} || $c->req->parameters->{'backends'};
if(defined $backend) {
return(1);
}
if(defined $ENV{'THRUK_BACKENDS'}) {
return(1);
}

return;
}

########################################
sub _set_disabled_by_section {
my($c, $backend, $disabled_backends) = @_;
Expand Down
19 changes: 19 additions & 0 deletions lib/Thruk/Controller/main.pm
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,36 @@ sub _get_contacts {
my($c) = @_;
$c->stats->profile(begin => "_get_contacts");

my $backends = Thruk::Action::AddDefaults::has_backends_set($c) ? undef : $c->db->authoritive_peer_keys();

my $data = $c->db->caching_query(
$c->config->{'var_path'}.'/caching_query.contact_names.cache',
'get_contacts',
{
columns => [qw/name/],
debug_hint => 'total contacts',
backends => $backends,
},
sub { return $_[0]->{'name'}; },
1,
);

# take shortcut if its only one backend
my $key;
if($backends && scalar @{$backends} == 1) {
$key = (keys %{$data})[0];
}
elsif(scalar keys %{$data} == 1) {
$key = (keys %{$data})[0];
}

if($key) {
my $num = scalar @{$data->{$key}};

$c->stats->profile(end => "_get_contacts");
return($num);
}

my $uniq = {};
for my $peer_key (sort keys %{$data}) {
for my $name (@{$data->{$peer_key}}) {
Expand Down

0 comments on commit 6c65ee7

Please sign in to comment.