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