Bug 35496: Open article requests tab by default on opac-user.pl after placing an...
[koha.git] / misc / cronjobs / erm_run_harvester.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2022 PTFS Europe
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use Getopt::Long qw( GetOptions );
22 use Koha::DateUtils qw( dt_from_string );
23 use POSIX;
24
25 use Koha::Script;
26 use Koha::ERM::EUsage::UsageDataProviders;
27
28 # Command line option values
29 my $get_help   = 0;
30 my $begin_date = 0;
31 my $end_date   = 0;
32 my $dry_run    = 0;
33 my $debug      = 0;
34
35 my $options = GetOptions(
36     'h|help'       => \$get_help,
37     'begin-date=s' => \$begin_date,
38     'end-date=s'   => \$end_date,
39     'dry-run'      => \$dry_run,
40     'debug'        => \$debug
41 );
42
43 if ($get_help) {
44     get_help();
45     exit 1;
46 }
47
48 my $udproviders = Koha::ERM::EUsage::UsageDataProviders->search( { active => 1 } );
49 unless ( scalar @{ $udproviders->as_list() } ) {
50     die "ERROR: No usage data providers found.";
51 }
52
53 unless ($begin_date) {
54     die "ERROR: Please specify a begin-date";
55 }
56
57 debug_msg("Dry run: Harvests will not be enqueued") if $dry_run;
58 while ( my $udprovider = $udproviders->next ) {
59     debug_msg(
60         sprintf(
61             "Processing usage data provider #%d - %s", $udprovider->erm_usage_data_provider_id, $udprovider->name
62         )
63     );
64
65     my $harvest_begin_date = dt_from_string($begin_date);
66     my $harvest_end_date   = dt_from_string($end_date) || dt_from_string();
67
68     if ( $harvest_begin_date > $harvest_end_date ) {
69         die sprintf(
70             "ERROR: begin-date must be earlier than end-date. Begin is %s and end date is %s",
71             $harvest_begin_date->ymd,
72             $harvest_end_date->ymd,
73         );
74     }
75
76     if ( !$dry_run ) {
77         my $job_ids = $udprovider->enqueue_sushi_harvest_jobs(
78             {
79                 begin_date => $harvest_begin_date->ymd,
80                 end_date   => $harvest_end_date->ymd
81             }
82         );
83         my $report_type_jobs = join ", ", map { $_->{report_type} . ': Job ID #' . $_->{job_id} } @{$job_ids};
84
85         debug_msg(
86             sprintf(
87                 " - Harvest job enqueued (yyyy-mm-dd):\n - Begin date: %s \n - End date: %s \n - %s",
88                 $harvest_begin_date->ymd,
89                 $harvest_end_date->ymd,
90                 $report_type_jobs
91             )
92         );
93     }
94
95 }
96
97 sub debug_msg {
98     my ($msg) = @_;
99
100     if ( !$debug ) {
101         return;
102     }
103
104     if ( ref $msg eq 'HASH' ) {
105         use Data::Dumper;
106         $msg = Dumper $msg;
107     }
108     print STDERR "$msg\n";
109 }
110
111 sub get_help {
112     print <<"HELP";
113 $0: Run a SUSHI harvesting for a ERM usage data provider
114
115 This script will run the SUSHI harvesting for usage data providers
116
117 Parameters:
118     --help or -h                         get help
119     --begin-date                         begin date for the harvest in yyyy-mm-dd format (e.g.: '2023-08-21')
120     --end-date                           end date for the harvest in yyyy-mm-dd format (e.g.: '2023-08-21')
121     --dry-run                            only produce a run report, without actually doing anything permanent
122     --debug                              print additional debugging info during run
123
124 Usage example:
125 ./misc/cronjobs/erm_run_harvester.pl --begin-date 2023-06-21 --debug
126
127 HELP
128 }