View | Details | Raw Unified | Return to bug 36093
Collapse All | Expand All

(-)a/Koha/REST/V1/ERM/EUsage/CustomReports.pm (-24 / +28 lines)
Lines 50-56 sub monthly_report { Link Here
50
    return try {
50
    return try {
51
51
52
        my $json = JSON->new;
52
        my $json = JSON->new;
53
        my $args = $json->decode($c->param('q'));
53
        my $args = $json->decode( $c->param('q') );
54
        my @query_params_array;
54
        my @query_params_array;
55
55
56
        if ( ref($args) eq 'ARRAY' ) {
56
        if ( ref($args) eq 'ARRAY' ) {
Lines 66-73 sub monthly_report { Link Here
66
66
67
        my $usage_data_providers =
67
        my $usage_data_providers =
68
            Koha::ERM::EUsage::UsageDataProviders->search( {}, {} )->unblessed;
68
            Koha::ERM::EUsage::UsageDataProviders->search( {}, {} )->unblessed;
69
        my $metric_types =
69
        my $metric_types = $query_params_array[0]->{'erm_usage_muses.metric_type'};
70
            $query_params_array[0]->{'erm_usage_muses.metric_type'};
71
        my $access_types =
70
        my $access_types =
72
              $query_params_array[0]->{'erm_usage_muses.access_type'}
71
              $query_params_array[0]->{'erm_usage_muses.access_type'}
73
            ? $query_params_array[0]->{'erm_usage_muses.access_type'}
72
            ? $query_params_array[0]->{'erm_usage_muses.access_type'}
Lines 213-220 sub metric_types_report { Link Here
213
            push @{$data}, $missing_result if $missing_result;
212
            push @{$data}, $missing_result if $missing_result;
214
        }
213
        }
215
214
216
        my @metric_types = ('metric_types_report'); # Dummy value to ensure the loop triggers at least once in _create_report_rows
215
        my @metric_types =
217
        my $report_data  = _get_report_data(
216
            ('metric_types_report');    # Dummy value to ensure the loop triggers at least once in _create_report_rows
217
        my $report_data = _get_report_data(
218
            {
218
            {
219
                data_type            => $data_type,
219
                data_type            => $data_type,
220
                data                 => $data,
220
                data                 => $data,
Lines 257-267 sub provider_rollup_report { Link Here
257
        my $usage_data_providers_set = Koha::ERM::EUsage::UsageDataProviders->new;
257
        my $usage_data_providers_set = Koha::ERM::EUsage::UsageDataProviders->new;
258
        my $usage_data_providers     = $c->objects->search($usage_data_providers_set);
258
        my $usage_data_providers     = $c->objects->search($usage_data_providers_set);
259
259
260
        my $data_type = $c->param('data_type');
260
        my $data_type    = $c->param('data_type');
261
        my $key       = 'erm_usage_' . $data_type . 's';
261
        my $key          = 'erm_usage_' . $data_type . 's';
262
        my $metric_types =
262
        my $metric_types = $query_params_array[0]->{ $key . '.erm_usage_muses.metric_type' };
263
            $query_params_array[0]->{ $key . '.erm_usage_muses.metric_type' };
263
264
            
265
        my @usage_data_provider_report_data;
264
        my @usage_data_provider_report_data;
266
265
267
        for my $usage_data_provider ( @{$usage_data_providers} ) {
266
        for my $usage_data_provider ( @{$usage_data_providers} ) {
Lines 276-282 sub provider_rollup_report { Link Here
276
                        grep { $metric_type eq $_->{metric_type} } @$statistics;
275
                        grep { $metric_type eq $_->{metric_type} } @$statistics;
277
                    my @usage_counts =
276
                    my @usage_counts =
278
                        map { $_->{usage_count} } @filtered_statistics;
277
                        map { $_->{usage_count} } @filtered_statistics;
279
                    my $sum = scalar(@usage_counts) > 0
278
                    my $sum =
279
                        scalar(@usage_counts) > 0
280
                        ? _get_usage_total( \@usage_counts )
280
                        ? _get_usage_total( \@usage_counts )
281
                        : 0;
281
                        : 0;
282
282
Lines 299-305 sub provider_rollup_report { Link Here
299
                    map { $_->{usage_total} } @filtered_object_data;
299
                    map { $_->{usage_total} } @filtered_object_data;
300
                my $provider_rollup_total =
300
                my $provider_rollup_total =
301
                    scalar(@data_object_usage_totals) > 0
301
                    scalar(@data_object_usage_totals) > 0
302
                    ? _get_usage_total(\@data_object_usage_totals)
302
                    ? _get_usage_total( \@data_object_usage_totals )
303
                    : 0;
303
                    : 0;
304
304
305
                my %usage_data_provider_hash = (
305
                my %usage_data_provider_hash = (
Lines 405-417 sub _get_result_with_no_statistics { Link Here
405
    if ( !$check_result ) {
405
    if ( !$check_result ) {
406
        my $missing_result   = _get_missing_data( $data_type, $id );
406
        my $missing_result   = _get_missing_data( $data_type, $id );
407
        my @blank_statistics = ();
407
        my @blank_statistics = ();
408
        return _get_object_hash({
408
        return _get_object_hash(
409
            data_type   => $data_type,
409
            {
410
            data_object => $missing_result,
410
                data_type   => $data_type,
411
            statistics  => \@blank_statistics,
411
                data_object => $missing_result,
412
            provider    => '',
412
                statistics  => \@blank_statistics,
413
            metric_type => '',
413
                provider    => '',
414
        });
414
                metric_type => '',
415
            }
416
        );
415
    }
417
    }
416
    return 0;
418
    return 0;
417
}
419
}
Lines 623-629 sub _create_report_rows { Link Here
623
                    grep { $metric_type eq $_->{metric_type} } @stats_by_access_type;
625
                    grep { $metric_type eq $_->{metric_type} } @stats_by_access_type;
624
                my @usage_counts =
626
                my @usage_counts =
625
                    map { $_->{usage_count} } @stats_by_metric_type;
627
                    map { $_->{usage_count} } @stats_by_metric_type;
626
                my $sum = $period eq 'monthly' && scalar(@usage_counts) > 0
628
                my $sum =
629
                    $period eq 'monthly' && scalar(@usage_counts) > 0
627
                    ? _get_usage_total( \@usage_counts )
630
                    ? _get_usage_total( \@usage_counts )
628
                    : 0;
631
                    : 0;
629
632
Lines 652-659 sub _create_report_rows { Link Here
652
                : @usage_stats;
655
                : @usage_stats;
653
            my @usage_counts =
656
            my @usage_counts =
654
                map { $_->{usage_count} } @stats_by_metric_type;
657
                map { $_->{usage_count} } @stats_by_metric_type;
655
            my $sum = $period eq 'monthly' && scalar(@usage_counts) > 0
658
            my $sum =
656
                ? _get_usage_total(\@usage_counts)
659
                $period eq 'monthly' && scalar(@usage_counts) > 0
660
                ? _get_usage_total( \@usage_counts )
657
                : 0;
661
                : 0;
658
662
659
            my $data_object_hash = _get_object_hash(
663
            my $data_object_hash = _get_object_hash(
Lines 681-694 A method for summing the usage counts for a data object Link Here
681
=cut
685
=cut
682
686
683
sub _get_usage_total {
687
sub _get_usage_total {
684
    my ( $statistics ) = @_;
688
    my ($statistics) = @_;
685
689
686
    my $sum = 0;
690
    my $sum = 0;
687
    foreach my $statistic (@$statistics) {
691
    foreach my $statistic (@$statistics) {
688
        $sum += $statistic;
692
        $sum += $statistic;
689
    }
693
    }
690
694
691
    return $sum
695
    return $sum;
692
}
696
}
693
697
694
1;
698
1;
(-)a/t/db_dependent/api/v1/erm_custom_reports.t (-2 / +77 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 3;
22
use Test::More tests => 4;
23
use Test::Mojo;
23
use Test::Mojo;
24
24
25
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
Lines 335-337 subtest "metric_types_report" => sub { Link Here
335
335
336
    $schema->storage->txn_rollback;
336
    $schema->storage->txn_rollback;
337
};
337
};
338
- 
338
339
subtest "provider_rollup_report" => sub {
340
    plan tests => 9;
341
342
    $schema->storage->txn_begin;
343
344
    Koha::ERM::EUsage::MonthlyUsages->search->delete;
345
346
    # Run a harvest to populate the database with data
347
    my $usage_data_provider = $builder->build_object( { class => 'Koha::ERM::EUsage::UsageDataProviders' } );
348
    my $counter_file        = $sushi_counter_TR_J1->get_COUNTER_from_SUSHI;
349
350
    $usage_data_provider->counter_files(
351
        [
352
            {
353
                usage_data_provider_id => $usage_data_provider->erm_usage_data_provider_id,
354
                file_content           => $counter_file,
355
                date_uploaded          => POSIX::strftime( "%Y%m%d%H%M%S", localtime ),
356
                filename               => "Test_TR_J1",
357
            }
358
        ]
359
    );
360
361
    my $librarian = $builder->build_object(
362
        {
363
            class => 'Koha::Patrons',
364
            value => { flags => 2**28 }
365
        }
366
    );
367
    my $password = 'thePassword123';
368
    $librarian->set_password( { password => $password, skip_validation => 1 } );
369
    my $userid = $librarian->userid;
370
371
    my $patron = $builder->build_object(
372
        {
373
            class => 'Koha::Patrons',
374
            value => { flags => 0 }
375
        }
376
    );
377
378
    $patron->set_password( { password => $password, skip_validation => 1 } );
379
    my $unauth_userid = $patron->userid;
380
381
    # Unauthorized access
382
    $t->get_ok("//$unauth_userid:$password@/api/v1/erm/eUsage/provider_rollup_report/title")->status_is(403);
383
384
    # Authorised access
385
    my $query_string_with_no_results = 'q=[
386
        {
387
            "erm_usage_titles.erm_usage_muses.year":2023,
388
            "erm_usage_titles.erm_usage_muses.report_type":"TR_J1",
389
            "erm_usage_titles.erm_usage_muses.month":[1,2,3,4,5,6,7,8,9,10,11,12],
390
            "erm_usage_titles.erm_usage_muses.metric_type":["Total_Item_Requests","Unique_Item_Requests"]
391
        }
392
    ]';
393
394
    $t->get_ok( "//$userid:$password@/api/v1/erm/eUsage/provider_rollup_report/title?$query_string_with_no_results" =>
395
            { 'x-koha-embed' => 'erm_usage_titles.erm_usage_muses' } )->status_is(200)->json_is( [] );
396
397
    my $query_string = 'q=[
398
        {
399
            "erm_usage_titles.erm_usage_muses.year":2022,
400
            "erm_usage_titles.erm_usage_muses.report_type":"TR_J1",
401
            "erm_usage_titles.erm_usage_muses.month":[8],
402
            "erm_usage_titles.erm_usage_muses.metric_type":["Total_Item_Requests","Unique_Item_Requests"]
403
        }
404
    ]';
405
    my $expected_results1 = 2;    # One provider repeated once for each metric type
406
407
    $t->get_ok( "//$userid:$password@/api/v1/erm/eUsage/provider_rollup_report/title?$query_string" =>
408
            { 'x-koha-embed' => 'erm_usage_titles.erm_usage_muses' } )->status_is(200)
409
        ->json_is( '/' . ( $expected_results1 - 2 ) . '/provider_rollup_total' => 2, 'Total is correct' )
410
        ->json_is( '/' . ( $expected_results1 - 1 ) . '/provider_rollup_total' => 1, 'Total is correct' );
411
412
    $schema->storage->txn_rollback;
413
};

Return to bug 36093