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