diff --git a/lib/SLUB/LZA/TA/Archivematica/Elasticsearch.pm b/lib/SLUB/LZA/TA/Archivematica/Elasticsearch.pm index 4c3f2dd5c5b8094b8d665c5bc4eb84a3200d6e61..a13ba3813d88801d278d677de94ea8d491b96d72 100644 --- a/lib/SLUB/LZA/TA/Archivematica/Elasticsearch.pm +++ b/lib/SLUB/LZA/TA/Archivematica/Elasticsearch.pm @@ -4,6 +4,7 @@ package SLUB::LZA::TA::Archivematica::Elasticsearch; # HINT: the server is v6.x.y of elasticsearch, therefore this code is using old client use v5.36; use namespace::autoclean -except => qr{import}; +use Data::Printer; use Search::Elasticsearch 6.81; # install with: cpanm Search::Elasticsearch~"<7.0" use Search::Elasticsearch::Client::6_0; @@ -33,7 +34,7 @@ sub version_elasticsearch($protocol, $host, $port) { return $info->{version}->{number}; } -sub query_elasticsearch($protocol, $host, $port, $index_name, $query_hash, $options= undef) { +sub query_elasticsearch($protocol, $host, $port, $index_name, $query_hash, $options=undef) { # HINT: filter_paths not well supported # next lines to deep copy hash, to avoid sideeffects in caller @@ -41,10 +42,19 @@ sub query_elasticsearch($protocol, $host, $port, $index_name, $query_hash, $opti foreach my $key (keys %{$query_hash} ) { $local_query_hash->{$key} = $query_hash->{$key} } - if ((defined $options) and ($options->{no_source} == 1)) { - $local_query_hash->{_source}="false"; # avoids transfering complete source tree in results - } else { - #$local_query_hash->{_source}->{excludes}="" + if (defined $options) { + if (exists $options->{no_source} and defined $options->{no_source} and $options->{no_source} == 1) { + $local_query_hash->{_source} = "false"; # avoids transfering complete source tree in results + } + else { + #$local_query_hash->{_source}->{excludes}="" + } + if (exists $options->{debug} and defined $options->{debug} and $options->{debug} == 1) { + say STDERR "query:"; + say STDERR "------------------------"; + say STDERR np($query_hash); + say STDERR "------------------------"; + } } my $e = _instantiate($protocol, $host, $port); @@ -52,11 +62,18 @@ sub query_elasticsearch($protocol, $host, $port, $index_name, $query_hash, $opti index => $index_name, body => $local_query_hash, #hashref ); + if (exists $options->{debug} and defined $options->{debug} and $options->{debug} == 1) { + say STDERR "response:"; + say STDERR "------------------------"; + say STDERR np($res); + say STDERR "------------------------"; + } return $res; } -sub query_elasticsearch_count($protocol, $host, $port, $index_name, $query_hash) { - my $res = query_elasticsearch($protocol, $host, $port, $index_name, $query_hash, {no_source => 1}); +sub query_elasticsearch_count($protocol, $host, $port, $index_name, $query_hash, $options=undef) { + $options->{ no_source } = 1; + my $res = query_elasticsearch($protocol, $host, $port, $index_name, $query_hash, $options); return $res->{hits}->{total}; } diff --git a/lib/SLUB/LZA/TA/Command/count.pm b/lib/SLUB/LZA/TA/Command/count.pm index f08fff20e5a439ef3092196bea45e7fd2d9b7b42..41e1987e2d75eaa503a0775a395dd226abb6c047 100644 --- a/lib/SLUB/LZA/TA/Command/count.pm +++ b/lib/SLUB/LZA/TA/Command/count.pm @@ -3,7 +3,6 @@ use SLUB::LZA::TA -command; use v5.36; use SLUB::LZA::TA::Archivematica::Elasticsearch; use SLUB::LZA::TA::Archivematica::Elasticsearch::PrepareQuery; -use Data::Printer; use Date::Calc qw(Date_to_Time); use namespace::autoclean -except => qr{SLUB::LZA::TA::.*}; @@ -122,19 +121,15 @@ sub validate_args { sub execute { my ($self, $opt, $args) = @_; my $aips_query = SLUB::LZA::TA::Archivematica::Elasticsearch::PrepareQuery::prepare_aip_query($opt); - if (exists $opt->{debug}) { - use Data::Printer; - say STDERR "query:"; - say STDERR "------------------------"; - say STDERR np( $aips_query); - say STDERR "------------------------"; - } my $response = SLUB::LZA::TA::Archivematica::Elasticsearch::query_elasticsearch_count( $SLUB::LZA::TA::config{elasticsearch_protocol}, $SLUB::LZA::TA::config{elasticsearch_host}, $SLUB::LZA::TA::config{elasticsearch_port}, 'aips', # indexname $aips_query, # query_hash ref + { + debug => $opt->{debug}, + } ); if ($opt->{verbose}) { say np($response); @@ -144,7 +139,6 @@ sub execute { return 1; } -no Data::Printer; no Date::Calc; 1; diff --git a/lib/SLUB/LZA/TA/Command/report.pm b/lib/SLUB/LZA/TA/Command/report.pm index eec1ced1df69ec7689dd8dde02e77462638198c9..ec335d4afd8868958603c24005b56cafcb9849bb 100644 --- a/lib/SLUB/LZA/TA/Command/report.pm +++ b/lib/SLUB/LZA/TA/Command/report.pm @@ -244,20 +244,15 @@ PAINLESS } }; $aips_query->{size}= 0; # only use aggregations - if (exists $opt->{debug}) { - use Data::Printer; - say STDERR "query:"; - say STDERR "------------------------"; - say STDERR np($aips_query); - say STDERR "------------------------"; - } - #p($aips_query); $aips_response = SLUB::LZA::TA::Archivematica::Elasticsearch::query_elasticsearch( $SLUB::LZA::TA::config{elasticsearch_protocol}, $SLUB::LZA::TA::config{elasticsearch_host}, $SLUB::LZA::TA::config{elasticsearch_port}, 'aips', # indexname $aips_query, # query_hash ref + { + debug => $opt->{debug}, + } ); $aips_response->{from_date}=$opt->{creationdate_epochs}->{from_string}; $aips_response->{to_date}=$opt->{creationdate_epochs}->{to_string}; @@ -267,20 +262,15 @@ PAINLESS sub get_ldp_projects { my ($self, $opt, $args) = @_; my $query = SLUB::LZA::TA::Archivematica::Elasticsearch::PrepareQuery::prepare_ldpprojects_query(@_); - if (exists $opt->{debug}) { - use Data::Printer; - say STDERR "query:"; - say STDERR "------------------------"; - say STDERR np($query); - say STDERR "------------------------"; - } - #p($aips_query); my $response = SLUB::LZA::TA::Archivematica::Elasticsearch::query_elasticsearch( $SLUB::LZA::TA::config{elasticsearch_protocol}, $SLUB::LZA::TA::config{elasticsearch_host}, $SLUB::LZA::TA::config{elasticsearch_port}, 'aips', # indexname $query, # query_hash ref + { + debug => $opt->{debug}, + } ); my %projects; foreach my $match (@{$response->{hits}->{hits}}) { @@ -307,20 +297,16 @@ sub get_filestypes_by_aips { $aips_query->{"_source"} = { "includes" => 'uuid' }; - if (exists $opt->{debug}) { - use Data::Printer; - say STDERR "query:"; - say STDERR "------------------------"; - say STDERR np($aips_query); - say STDERR "------------------------"; - } - #p($aips_query); + my $aips_response = SLUB::LZA::TA::Archivematica::Elasticsearch::query_elasticsearch( $SLUB::LZA::TA::config{elasticsearch_protocol}, $SLUB::LZA::TA::config{elasticsearch_host}, $SLUB::LZA::TA::config{elasticsearch_port}, 'aips', # indexname $aips_query, # query_hash ref + { + debug => $opt->{debug}, + } ); my @aips = map { $_->{_source}->{uuid} } @{ $aips_response->{hits}->{hits} }; my %results; @@ -379,14 +365,10 @@ sub get_filestypes_by_aips { $SLUB::LZA::TA::config{elasticsearch_port}, 'aipfiles', # indexname $files_query, # query_hash ref + { + debug => $opt->{debug}, + } ); - if (exists $opt->{debug}) { - use Data::Printer; - say "------------------------"; - say "query=",np( $files_query); - say "resp =",np($files_response); - say "------------------------"; - } foreach my $file_response (@{ $files_response->{hits}->{hits} }) { my $pronom_id = $file_response->{_source} ->{'METS'} @@ -612,7 +594,4 @@ RPTHEADER return 1; } - - - 1; diff --git a/lib/SLUB/LZA/TA/Command/search.pm b/lib/SLUB/LZA/TA/Command/search.pm index 46980de18cfc2f1c894ec50a2f83296d7dfc982d..0000976a48a8dcd7811bd38d67f76a35bb6fd537 100644 --- a/lib/SLUB/LZA/TA/Command/search.pm +++ b/lib/SLUB/LZA/TA/Command/search.pm @@ -185,13 +185,10 @@ sub find_aips_by_file_pronom_id { $SLUB::LZA::TA::config{elasticsearch_port}, 'aipfiles', # indexname $files_query, # query_hash ref + { + debug => $opt->{debug} + } ); - if (exists $opt->{debug}) { - use Data::Printer; - say "------------------------"; - say "query=",np( $files_query); - say "------------------------"; - } my %aips; #p($files_response->{hits}->{hits}); foreach my $match (@{$files_response->{hits}->{hits}}) { @@ -215,20 +212,15 @@ sub execute { # only index aips needed $aips_query = SLUB::LZA::TA::Archivematica::Elasticsearch::PrepareQuery::prepare_aip_query($opt); } - if (exists $opt->{debug}) { - use Data::Printer; - say STDERR "query:"; - say STDERR "------------------------"; - say STDERR np( $aips_query); - say STDERR "------------------------"; - } - #p($aips_query); $aips_response = SLUB::LZA::TA::Archivematica::Elasticsearch::query_elasticsearch( $SLUB::LZA::TA::config{elasticsearch_protocol}, $SLUB::LZA::TA::config{elasticsearch_host}, $SLUB::LZA::TA::config{elasticsearch_port}, 'aips', # indexname $aips_query, # query_hash ref + { + debug => $opt->{debug} + } ); #p($aips_response); SLUB::LZA::TA::Output::print_results($aips_response, $opt); diff --git a/t/03-report.t b/t/03-report.t index 57128f4a65f82bfa399143cf445a69acd19be86c..07d2d6dac72e7a8e798fc73a309e097a63c6974c 100644 --- a/t/03-report.t +++ b/t/03-report.t @@ -62,7 +62,7 @@ is($? >> 8, 0, 'ta-tool report, with ldp, exit status'); $testapp->run(args =>'report --output-as-csv --with-filetypes'); #say "STDOUT:",$testapp->stdout; #say "STDERR:",$testapp->stderr; -like($testapp->stdout, qr/^"set","subset","timespan_from","timespan_to","aip_state","filter","flavour","value"\n.*pronom/s, 'ta-tool report, with filetype, stdout'); +like($testapp->stdout, qr/^"set","subset","timespan_from","timespan_to","aip_state","filter","flavour","value"\n.*total/s, 'ta-tool report, with filetype, stdout'); like($testapp->stderr, qr/^reporting for period.*HINT:.*with-filetype/s, 'ta-tool report, with filetype, stderr'); is($? >> 8, 0, 'ta-tool report, with filetype, exit status');