From b94baff3d3b9d5946d1dbeab8a253ebf0a6c88d2 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Mon, 29 Jul 2024 11:23:51 +0000 Subject: [PATCH] Bug 37509: Check elasticsearch info for both 'about' and 'sysinfo' tabs 1) Notice 'Elasticsearch:' entry at http://localhost:8081/cgi-bin/koha/about.pl?tab=about is empty 2) Apply patch + restart plack 3) Repeat step 1) Notice it now shows elasticsearch info as expected. 4) Verify that Elasticsearch warning http://localhost:8081/cgi-bin/koha/about.pl?tab=sysinfo is the same before and applying patch. --- about.pl | 157 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 80 insertions(+), 77 deletions(-) diff --git a/about.pl b/about.pl index 3dff63118d..6233d8be23 100755 --- a/about.pl +++ b/about.pl @@ -179,6 +179,7 @@ if ( $tab eq 'about' ) { } message_broker_check($template); + elasticsearch_check($template); $template->param( effective_caching_method => $effective_caching_method, @@ -400,83 +401,7 @@ if($tab eq 'sysinfo') { $template->param( warnXSLT => \@warnXSLT ) if @warnXSLT; } - if ( C4::Context->preference('SearchEngine') eq 'Elasticsearch' ) { - # Check ES configuration health and runtime status - - my $es_status; - my $es_config_error; - my $es_running = 1; - my $es_has_missing = 0; - - my $es_conf; - try { - $es_conf = Koha::SearchEngine::Elasticsearch::_read_configuration(); - } - catch { - if ( ref($_) eq 'Koha::Exceptions::Config::MissingEntry' ) { - $template->param( elasticsearch_fatal_config_error => $_->message ); - $es_config_error = 1; - } - }; - if ( !$es_config_error ) { - - my $biblios_index_name = $es_conf->{index_name} . "_" . $Koha::SearchEngine::BIBLIOS_INDEX; - my $authorities_index_name = $es_conf->{index_name} . "_" . $Koha::SearchEngine::AUTHORITIES_INDEX; - - my @indexes = ($biblios_index_name, $authorities_index_name); - # TODO: When new indexes get added, we could have other ways to - # fetch the list of available indexes (e.g. plugins, etc) - $es_status->{nodes} = $es_conf->{nodes}; - my $es = Search::Elasticsearch->new( $es_conf ); - my $es_status->{version} = $es->info->{version}->{number}; - - foreach my $index ( @indexes ) { - my $index_count; - try { - $index_count = $es->indices->stats( index => $index ) - ->{_all}{primaries}{docs}{count}; - } - catch { - if ( ref($_) eq 'Search::Elasticsearch::Error::Missing' ) { - push @{ $es_status->{errors} }, "Index not found ($index)"; - $index_count = -1; - } - elsif ( ref($_) eq 'Search::Elasticsearch::Error::NoNodes' ) { - $es_running = 0; - } - else { - # TODO: when time comes, we will cover more use cases - die $_; - } - }; - - my $db_count = -1; - my $missing_count = 0; - if ( $index eq $biblios_index_name ) { - $db_count = Koha::Biblios->search->count; - } elsif ( $index eq $authorities_index_name ) { - $db_count = Koha::Authorities->search->count; - } - if ( $db_count != -1 && $index_count != -1 ) { - $missing_count = $db_count - $index_count; - $es_has_missing = 1 if $missing_count > 0; - } - push @{ $es_status->{indexes} }, - { - index_name => $index, - index_count => $index_count, - db_count => $db_count, - missing_count => $missing_count, - }; - } - $es_status->{running} = $es_running; - - $template->param( - elasticsearch_status => $es_status, - elasticsearch_has_missing => $es_has_missing, - ); - } - } + elasticsearch_check( $template ); if ( C4::Context->preference('RESTOAuth2ClientCredentials') ) { # Do we have the required deps? @@ -899,6 +824,84 @@ if ( $tab eq 'history' ) { } } +sub elasticsearch_check { + my $template = shift; + + if ( C4::Context->preference('SearchEngine') eq 'Elasticsearch' ) { + + my $es_status; + my $es_config_error; + my $es_running = 1; + my $es_has_missing = 0; + + my $es_conf; + try { + $es_conf = Koha::SearchEngine::Elasticsearch::_read_configuration(); + } catch { + if ( ref($_) eq 'Koha::Exceptions::Config::MissingEntry' ) { + $template->param( elasticsearch_fatal_config_error => $_->message ); + $es_config_error = 1; + } + }; + if ( !$es_config_error ) { + + my $biblios_index_name = $es_conf->{index_name} . "_" . $Koha::SearchEngine::BIBLIOS_INDEX; + my $authorities_index_name = $es_conf->{index_name} . "_" . $Koha::SearchEngine::AUTHORITIES_INDEX; + + my @indexes = ( $biblios_index_name, $authorities_index_name ); + + # TODO: When new indexes get added, we could have other ways to + # fetch the list of available indexes (e.g. plugins, etc) + $es_status->{nodes} = $es_conf->{nodes}; + my $es = Search::Elasticsearch->new($es_conf); + my $es_status->{version} = $es->info->{version}->{number}; + + foreach my $index (@indexes) { + my $index_count; + try { + $index_count = $es->indices->stats( index => $index )->{_all}{primaries}{docs}{count}; + } catch { + if ( ref($_) eq 'Search::Elasticsearch::Error::Missing' ) { + push @{ $es_status->{errors} }, "Index not found ($index)"; + $index_count = -1; + } elsif ( ref($_) eq 'Search::Elasticsearch::Error::NoNodes' ) { + $es_running = 0; + } else { + + # TODO: when time comes, we will cover more use cases + die $_; + } + }; + + my $db_count = -1; + my $missing_count = 0; + if ( $index eq $biblios_index_name ) { + $db_count = Koha::Biblios->search->count; + } elsif ( $index eq $authorities_index_name ) { + $db_count = Koha::Authorities->search->count; + } + if ( $db_count != -1 && $index_count != -1 ) { + $missing_count = $db_count - $index_count; + $es_has_missing = 1 if $missing_count > 0; + } + push @{ $es_status->{indexes} }, + { + index_name => $index, + index_count => $index_count, + db_count => $db_count, + missing_count => $missing_count, + }; + } + $es_status->{running} = $es_running; + + $template->param( + elasticsearch_status => $es_status, + elasticsearch_has_missing => $es_has_missing, + ); + } + } +} + sub message_broker_check { my $template = shift; { -- 2.39.2