Bug 20600: Add filtering of ILL requests in list
[koha.git] / misc / cronjobs / update_totalissues.pl
blobaffa2a7e3457542b5057605510d84077ec8febaf
1 #!/usr/bin/perl
3 # Copyright 2012 C & P Bibliography Services
5 # This file is part of Koha.
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.
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.
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>.
20 use strict;
21 use warnings;
23 BEGIN {
25 # find Koha's Perl modules
26 # test carefully before changing this
27 use FindBin;
28 eval { require "$FindBin::Bin/../kohalib.pl" };
31 use Getopt::Long;
32 use Pod::Usage;
33 use C4::Context;
34 use C4::Biblio;
35 use C4::Log;
36 use DateTime;
37 use DateTime::Format::MySQL;
38 use Time::HiRes qw/time/;
39 use POSIX qw/strftime ceil/;
41 sub usage {
42 pod2usage( -verbose => 2 );
43 exit;
46 $| = 1;
48 # command-line parameters
49 my $verbose = 0;
50 my $test_only = 0;
51 my $want_help = 0;
52 my $since;
53 my $interval;
54 my $usestats = 0;
55 my $useitems = 0;
56 my $incremental = 0;
57 my $commit = 100;
58 my $unit;
60 my $result = GetOptions(
61 'v|verbose' => \$verbose,
62 't|test' => \$test_only,
63 's|since=s' => \$since,
64 'i|interval=s' => \$interval,
65 'use-stats' => \$usestats,
66 'use-items' => \$useitems,
67 'incremental' => \$incremental,
68 'c|commit=i' => \$commit,
69 'h|help' => \$want_help
72 binmode( STDOUT, ":utf8" );
74 if ( defined $since && defined $interval ) {
75 print "The --since and --interval options are mutually exclusive.\n\n";
76 $want_help = 1;
79 if ( $useitems && $incremental ) {
80 print
81 "The --use-items and --incremental options are mutually exclusive.\n\n";
82 $want_help = 1;
85 if ( $incremental && !( defined $since || defined $interval ) ) {
86 $interval = '24h';
89 unless ( $usestats || $useitems ) {
90 print "You must specify either --use-stats and/or --use-items.\n\n";
91 $want_help = 1;
94 if ( not $result or $want_help ) {
95 usage();
98 cronlogaction();
100 my $dbh = C4::Context->dbh;
101 $dbh->{AutoCommit} = 0;
103 my $num_bibs_processed = 0;
104 my $num_bibs_error = 0;
106 my $starttime = time();
108 process_items() if $useitems;
109 process_stats() if $usestats;
111 report();
113 exit 0;
115 sub process_items {
116 my $query =
117 "SELECT items.biblionumber, SUM(items.issues) FROM items GROUP BY items.biblionumber;";
118 process_query($query);
121 sub process_stats {
122 if ($interval) {
123 my $dt = DateTime->now;
125 my %units = (
126 h => 'hours',
127 d => 'days',
128 w => 'weeks',
129 m => 'months',
130 y => 'years'
133 $interval =~ m/([0-9]*)([hdwmy]?)$/;
134 $unit = $2 || 'd';
135 $since = DateTime::Format::MySQL->format_datetime(
136 $dt->subtract( $units{$unit} => $1 ) );
138 my $limit = '';
139 $limit = " AND statistics.datetime >= ?" if ( $interval || $since );
141 my $query =
142 "SELECT biblio.biblionumber, COUNT(statistics.itemnumber) FROM biblio LEFT JOIN items ON (biblio.biblionumber=items.biblionumber) LEFT JOIN statistics ON (items.itemnumber=statistics.itemnumber) WHERE statistics.type = 'issue' $limit GROUP BY biblio.biblionumber;";
143 process_query( $query, $limit );
145 unless ($incremental) {
146 $query =
147 "SELECT biblio.biblionumber, 0 FROM biblio LEFT JOIN items ON (biblio.biblionumber=items.biblionumber) LEFT JOIN statistics ON (items.itemnumber=statistics.itemnumber) WHERE statistics.itemnumber IS NULL GROUP BY biblio.biblionumber;";
148 process_query( $query, '' );
150 $query =
151 "SELECT biblio.biblionumber, 0 FROM biblio LEFT JOIN items ON (biblio.biblionumber=items.biblionumber) WHERE items.itemnumber IS NULL GROUP BY biblio.biblionumber;";
152 process_query( $query, '' );
155 $dbh->commit();
158 sub process_query {
159 my $query = shift;
160 my $uselimit = shift;
161 my $sth = $dbh->prepare($query);
163 if ( $since && $uselimit ) {
164 $sth->execute($since);
166 else {
167 $sth->execute();
170 while ( my ( $biblionumber, $totalissues ) = $sth->fetchrow_array() ) {
171 $num_bibs_processed++;
172 $totalissues = 0 unless $totalissues;
173 print "Processing bib $biblionumber ($totalissues issues)\n"
174 if $verbose;
175 if ( not $test_only ) {
176 my $ret;
177 if ( $incremental && $totalissues > 0 ) {
178 $ret = UpdateTotalIssues( $biblionumber, $totalissues );
180 else {
181 $ret = UpdateTotalIssues( $biblionumber, 0, $totalissues );
183 unless ($ret) {
184 print "Error while processing bib $biblionumber\n" if $verbose;
185 $num_bibs_error++;
188 if ( not $test_only and ( $num_bibs_processed % $commit ) == 0 ) {
189 print_progress_and_commit($num_bibs_processed);
193 $dbh->commit();
196 sub report {
197 my $endtime = time();
198 my $totaltime = ceil( ( $endtime - $starttime ) * 1000 );
199 $starttime = strftime( '%D %T', localtime($starttime) );
200 $endtime = strftime( '%D %T', localtime($endtime) );
202 my $summary = <<_SUMMARY_;
204 Update total issues count script report
205 =======================================================
206 Run started at: $starttime
207 Run ended at: $endtime
208 Total run time: $totaltime ms
209 Number of bibs modified: $num_bibs_processed
210 Number of bibs with error: $num_bibs_error
211 _SUMMARY_
212 $summary .= "\n**** Ran in test mode only ****\n" if $test_only;
213 print $summary;
216 sub print_progress_and_commit {
217 my $recs = shift;
218 $dbh->commit();
219 print "... processed $recs records\n";
222 =head1 NAME
224 update_totalissues.pl
226 =head1 SYNOPSIS
228 update_totalissues.pl --use-stats
229 update_totalissues.pl --use-items
230 update_totalissues.pl --commit=1000
231 update_totalissues.pl --since='2012-01-01'
232 update_totalissues.pl --interval=30d
234 =head1 DESCRIPTION
236 This batch job populates bibliographic records' total issues count based
237 on historical issue statistics.
239 =over 8
241 =item B<--help>
243 Prints this help
245 =item B<-v|--verbose>
247 Provide verbose log information (list every bib modified).
249 =item B<--use-stats>
251 Use the data in the statistics table for populating total issues.
253 =item B<--use-items>
255 Use items.issues data for populating total issues. Note that issues
256 data from the items table does not respect the --since or --interval
257 options, by definition. Also note that if both --use-stats and
258 --use-items are specified, the count of biblios processed will be
259 misleading.
261 =item B<-s|--since=DATE>
263 Only process issues recorded in the statistics table since DATE.
265 =item B<-i|--interval=S>
267 Only process issues recorded in the statistics table in the last N
268 units of time. The interval should consist of a number with a one-letter
269 unit suffix. The valid suffixes are h (hours), d (days), w (weeks),
270 m (months), and y (years). The default unit is days.
272 =item B<--incremental>
274 Add the number of issues found in the statistics table to the existing
275 total issues count. Intended so that this script can be used as a cron
276 job to update popularity information during low-usage periods. If neither
277 --since or --interval are specified, incremental mode will default to
278 processing the last twenty-four hours.
280 =item B<--commit=N>
282 Commit the results to the database after every N records are processed.
284 =item B<--test>
286 Only test the popularity population script.
288 =back
290 =head1 WARNING
292 If the time on your database server does not match the time on your Koha
293 server you will need to take that into account, and probably use the
294 --since argument instead of the --interval argument for incremental
295 updating.
297 =head1 CREDITS
299 This patch to Koha was sponsored by the Arcadia Public Library and the
300 Arcadia Public Library Foundation in honor of Jackie Faust-Moreno, late
301 director of the Arcadia Public Library.
303 =head1 AUTHOR
305 Jared Camins-Esakov <jcamins AT cpbibliography DOT com>
307 =cut