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