1 package Koha::REST::V1::ERM::UsageDataProviders;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::ERM::UsageDataProviders;
24 use Scalar::Util qw( blessed );
25 use Try::Tiny qw( catch try );
36 my $c = shift->openapi->valid_input or return;
39 my $usage_data_providers_set = Koha::ERM::UsageDataProviders->new;
40 my $usage_data_providers =
41 $c->objects->search($usage_data_providers_set);
42 if ( $c->validation->output->{"x-koha-embed"}[0]
43 && $c->validation->output->{"x-koha-embed"}[0] eq 'counter_files' )
45 foreach my $provider (@$usage_data_providers) {
46 my $title_dates = _get_earliest_and_latest_dates( 'TR',
47 $provider->{erm_usage_data_provider_id} );
48 $provider->{earliest_title} =
49 $title_dates->{earliest_date}
50 ? $title_dates->{earliest_date}
52 $provider->{latest_title} =
53 $title_dates->{latest_date}
54 ? $title_dates->{latest_date}
57 my $platform_dates = _get_earliest_and_latest_dates( 'PR',
58 $provider->{erm_usage_data_provider_id} );
59 $provider->{earliest_platform} =
60 $platform_dates->{earliest_date}
61 ? $platform_dates->{earliest_date}
63 $provider->{latest_platform} =
64 $platform_dates->{latest_date}
65 ? $platform_dates->{latest_date}
68 my $item_dates = _get_earliest_and_latest_dates( 'IR',
69 $provider->{erm_usage_data_provider_id} );
70 $provider->{earliest_item} =
71 $item_dates->{earliest_date}
72 ? $item_dates->{earliest_date}
74 $provider->{latest_item} =
75 $item_dates->{latest_date} ? $item_dates->{latest_date} : '';
77 my $database_dates = _get_earliest_and_latest_dates( 'DR',
78 $provider->{erm_usage_data_provider_id} );
79 $provider->{earliest_database} =
80 $database_dates->{earliest_date}
81 ? $database_dates->{earliest_date}
83 $provider->{latest_database} =
84 $database_dates->{latest_date}
85 ? $database_dates->{latest_date}
90 return $c->render( status => 200, openapi => $usage_data_providers );
93 $c->unhandled_exception($_);
100 Controller function that handles retrieving a single Koha::ERM::UsageDataProvider object
105 my $c = shift->openapi->valid_input or return;
108 my $usage_data_provider_id =
109 $c->validation->param('erm_usage_data_provider_id');
110 my $usage_data_provider =
111 $c->objects->find( Koha::ERM::UsageDataProviders->search,
112 $usage_data_provider_id );
114 unless ($usage_data_provider) {
117 openapi => { error => "Usage data provider not found" }
123 openapi => $usage_data_provider
127 $c->unhandled_exception($_);
133 Controller function that handles adding a new Koha::ERM::UsageDataProvider object
138 my $c = shift->openapi->valid_input or return;
141 Koha::Database->new->schema->txn_do(
144 my $body = $c->validation->param('body');
146 my $usage_data_provider =
147 Koha::ERM::UsageDataProvider->new_from_api($body)->store;
149 $c->res->headers->location( $c->req->url->to_string . '/'
150 . $usage_data_provider->erm_usage_data_provider_id );
153 openapi => $usage_data_provider->to_api
160 my $to_api_mapping = Koha::ERM::UsageDataProvider->new->to_api_mapping;
163 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
167 { error => $_->error, conflict => $_->duplicate_id }
170 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
175 . $to_api_mapping->{ $_->broken_fk }
180 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
185 . $to_api_mapping->{ $_->parameter }
190 elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) {
193 openapi => { error => $_->error }
198 $c->unhandled_exception($_);
204 Controller function that handles updating a Koha::ERM::UsageDataProvider object
209 my $c = shift->openapi->valid_input or return;
211 my $usage_data_provider_id =
212 $c->validation->param('erm_usage_data_provider_id');
213 my $usage_data_provider =
214 Koha::ERM::UsageDataProviders->find($usage_data_provider_id);
216 unless ($usage_data_provider) {
219 openapi => { error => "Usage data provider not found" }
224 Koha::Database->new->schema->txn_do(
227 my $body = $c->validation->param('body');
229 $usage_data_provider->set_from_api($body)->store;
231 $c->res->headers->location( $c->req->url->to_string . '/'
232 . $usage_data_provider->erm_usage_data_provider_id );
235 openapi => $usage_data_provider->to_api
241 my $to_api_mapping = Koha::ERM::UsageDataProvider->new->to_api_mapping;
244 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
249 . $to_api_mapping->{ $_->broken_fk }
254 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
259 . $to_api_mapping->{ $_->parameter }
264 elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) {
267 openapi => { error => $_->error }
272 $c->unhandled_exception($_);
281 my $c = shift->openapi->valid_input or return;
283 my $usage_data_provider_id =
284 $c->validation->param('erm_usage_data_provider_id');
285 my $usage_data_provider =
286 Koha::ERM::UsageDataProviders->find($usage_data_provider_id);
287 unless ($usage_data_provider) {
290 openapi => { error => "Usage data provider not found" }
295 $usage_data_provider->delete;
302 $c->unhandled_exception($_);
311 my $c = shift->openapi->valid_input or return;
313 my $udprovider = Koha::ERM::UsageDataProviders->find( $c->validation->param('erm_usage_data_provider_id') );
315 unless ($udprovider) {
318 openapi => { error => "Usage data provider not found" }
323 my $jobs = $udprovider->run;
327 openapi => { jobs => [ @{$jobs} ] }
331 $c->unhandled_exception($_);
335 =head3 test_connection
339 sub test_connection {
340 my $c = shift->openapi->valid_input or return;
342 my $udprovider = Koha::ERM::UsageDataProviders->find( $c->validation->param('erm_usage_data_provider_id') );
344 unless ($udprovider) {
347 openapi => { error => "Usage data provider not found" }
351 my $service_active = $udprovider->test_connection;
354 openapi => $service_active
358 $c->unhandled_exception($_);
362 =head3 providers_report
366 sub providers_report {
367 my $c = shift->openapi->valid_input or return;
371 my $args = $c->validation->output;
373 my $usage_data_providers_set = Koha::ERM::UsageDataProviders->new;
374 my $usage_data_providers = $c->objects->search( $usage_data_providers_set );
376 my @query_params_array;
377 my $json = JSON->new;
379 if ( ref( $args->{q} ) eq 'ARRAY' ) {
380 foreach my $q ( @{ $args->{q} } ) {
381 push @query_params_array, $json->decode($q)
386 my $metric_types = $query_params_array[0][0]->{'erm_usage_titles.erm_usage_muses.metric_type'};
388 my @usage_data_provider_report_data;
390 for my $usage_data_provider ( @{ $usage_data_providers } ) {
392 # Split usage_data_providers into metric_types i.e. one table row per metric_type
393 for my $metric_type ( @$metric_types ) {
394 my @filtered_title_data;
396 for my $title ( @{ $usage_data_provider->{'erm_usage_titles'} }) {
397 my $statistics = $title->{'erm_usage_muses'};
398 my @filtered_statistics = grep { $metric_type eq $_->{metric_type} } @$statistics;
401 usage_data_provider_id => $title->{usage_data_provider_id},
402 title_id => $title->{title_id},
403 title => $title->{title},
404 erm_usage_muses => \@filtered_statistics,
405 online_issn => $title->{online_issn},
406 print_issn => $title->{print_issn},
407 title_doi => $title->{title_doi},
408 title_uri => $title->{title_uri},
409 metric_type => $metric_type,
410 publisher => $title->{publisher},
411 publisher_id => $title->{publisher_id},
414 push @filtered_title_data, \%title_hash;
418 my %usage_data_provider_hash = (
419 erm_usage_data_provider_id => $usage_data_provider->{erm_usage_data_provider_id},
420 erm_usage_titles => \@filtered_title_data,
421 aggregator => $usage_data_provider->{aggregator},
422 api_key => $usage_data_provider->{api_key},
423 begin_date => $usage_data_provider->{begin_date},
424 customer_id => $usage_data_provider->{customer_id},
425 description => $usage_data_provider->{description},
426 end_date => $usage_data_provider->{end_date},
427 method => $usage_data_provider->{method},
428 name => $usage_data_provider->{name},
429 report_release => $usage_data_provider->{report_release},
430 report_types => $usage_data_provider->{report_types},
431 requestor_email => $usage_data_provider->{requestor_email},
432 requestor_id => $usage_data_provider->{requestor_id},
433 requestor_name => $usage_data_provider->{requestor_name},
434 service_type => $usage_data_provider->{service_type},
435 service_url => $usage_data_provider->{service_url},
436 metric_type => $metric_type
439 push @usage_data_provider_report_data, \%usage_data_provider_hash;
443 return $c->render( status => 200, openapi => \@usage_data_provider_report_data );
446 $c->unhandled_exception($_);