8c573cbc54829ad10000479620aaba13483836d1
[koha.git] / installer / data / mysql / backfill_statistics.pl
1 #!/usr/bin/perl
2
3 # Part of the Koha Library Software www.koha-community.org
4 # Licensed under the GPL.
5
6 use Modern::Perl;
7
8 # CPAN modules
9
10 # Koha modules
11 use C4::Context;
12
13 my $dbh = C4::Context->dbh;
14
15 sub get_counts() {
16         my $query = q(
17         SELECT
18         (SELECT count(*) FROM statistics WHERE branch="NO_LIBRARY"       ) AS NO_LIBRARY,
19         (SELECT count(*) FROM statistics WHERE branch             IS NULL) AS NULL_BRANCH,
20         (SELECT count(*) FROM statistics WHERE itemtype           IS NULL AND itemnumber IS NOT NULL) AS NULL_ITEMTYPE,
21         (SELECT count(*) FROM statistics WHERE borrowernumber     IS NULL) AS NULL_BORROWERNUMBER,
22         (SELECT count(*) FROM statistics                                 ) AS Total
23         );
24         my $sth = $dbh->prepare($query);
25         $sth->execute;
26         return $sth->fetchrow_hashref;
27 }
28
29 sub itemnumber_array() {
30         my $query = q(
31                 SELECT DISTINCT itemnumber
32                 FROM statistics
33                 WHERE itemtype IS NULL
34                 AND itemnumber IS NOT NULL
35         );
36         my $sth = $dbh->prepare($query);
37         $sth->execute;
38         my @itemnumbers = map {shift @$_} @{$sth->fetchall_arrayref};
39         return @itemnumbers;
40 }
41 sub null_borrower_lines() {
42         my $query = "SELECT * FROM statistics WHERE borrowernumber IS NULL";
43         my $sth = $dbh->prepare($query);
44         $sth->execute;
45         print "Number of lines with NULL_BORROWERNUMBER: ", scalar($sth->rows), "\n";
46         return $sth->fetchall_arrayref({});
47 }
48
49 sub show_counts() {
50         print "\nThe following counts represent the number of (potential) errors in your statistics table:\n";
51         my $counts = get_counts;
52         foreach (sort keys %$counts) {
53                 $_ eq 'Total' and next; 
54                 $counts->{Error_Total} += $counts->{$_};
55                 print sprintf("%30s : %3d \n",$_ ,$counts->{$_});
56         }
57         print sprintf("%30s : %3d (potential) errors in %d lines\n",'Error_Total',$counts->{Error_Total}, $counts->{'Total'});
58 }
59
60 ##### MAIN #####
61 print "This operation may take a while.\n";
62 (scalar @ARGV) or show_counts;
63 print "\nAttempting to populate missing data.\n";
64
65 my (@itemnumbers) = (scalar @ARGV) ? @ARGV : &itemnumber_array;
66 print "Number of distinct itemnumbers paired with NULL_ITEMTYPE: ", scalar(@itemnumbers), "\n";
67
68 my $query = "UPDATE statistics SET itemtype = ? WHERE itemnumber = ?";
69 my $update = $dbh->prepare($query);
70 foreach (@itemnumbers) {
71     my $item = Koha::Items->find($_);
72     unless ($item) {
73                 print STDERR "\tNo item found for itemnumber $_\n"; 
74                 next;
75         }
76     my $itemtype = $item->effective_itemtype;
77     $update->execute($itemtype,$_) or warn "Error in UPDATE execution";
78     printf "\titemnumber %5d : %7s  (%s rows)\n", $_, $itemtype, $update->rows;
79 }
80
81 my $old_issues = $dbh->prepare("SELECT * FROM old_issues WHERE timestamp = ? AND itemnumber = ?");
82 my     $issues = $dbh->prepare("SELECT * FROM     issues WHERE timestamp = ? AND itemnumber = ?");
83 $update = $dbh->prepare("UPDATE statistics SET borrowernumber = ? WHERE datetime = ? AND itemnumber = ?");
84 my $nullborrs = null_borrower_lines;
85 foreach (@$nullborrs) {
86         $old_issues->execute($_->{datetime},$_->{itemnumber});
87         my $issue;
88         if ($old_issues->rows != 1) {
89                 print STDERR "Warning!  Unexpected number of matches from old_issues: ",$old_issues->rows;
90                 $issues->execute($_->{datetime},$_->{itemnumber});
91                 if ($issues->rows != 1) {
92                         print STDERR ", from issues: ",$issues->rows,"\tskipping this record\n";
93                         next;
94                 }
95                 print STDERR "\n";
96                 $issue = $issues->fetchrow_hashref;
97         } else {
98                 $issue = $old_issues->fetchrow_hashref;
99         }
100         printf "\titemnumber: %5d at %20s -- borrowernumber: %5d\n", $_->{itemnumber}, $_->{datetime}, $issue->{borrowernumber};
101         $update->execute($issue->{borrowernumber},$_->{datetime},$_->{itemnumber});
102 }
103
104 print "\nOperations complete.\n";
105 (scalar @ARGV) or show_counts;
106