adding kmig reporter
[migration-tools.git] / kmig.d / bin / mig-reporter
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use DBI;
7 use Data::Dumper;
8 use Getopt::Long;
9 use XML::LibXML;
10 use Env qw(
11     HOME MYSQL_HOST MYSQL_TCP_PORT MYSQL_USER MYSQL_DATABASE MYSQL_PW
12         MIGSCHEMA MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
13 );
14 use open ':encoding(utf8)';
15 use Cwd 'abs_path';
16 use Cwd qw(getcwd);
17 use FindBin;
18 my $mig_bin = "$FindBin::Bin/";
19 use lib "$FindBin::Bin/";
20 use KMig;
21
22 my $analyst = 'Equinox Open Library Initiative';
23 my $report_title;
24 my $reports_xml = 'stock_reports.xml';
25 my $tags = 'bibs.items.borrowers.circs.reserves.accounts.courses';
26 my $added_page_title;
27 my $added_page_file;
28 my $i = 0;
29 my $parser = XML::LibXML->new();
30 my $lines_per_page = 42;
31 my $koha_instance;
32 my $koha_conf_xml;
33
34 my $dbh = KMig::db_connect();
35
36 my $ret = GetOptions(
37     'instance:s'         => \$koha_instance,
38     'tags:s'             => \$tags,    
39     'reports_xml:s'      => \$reports_xml,
40     'analyst:s'          => \$analyst,
41     'added_page_file:s'  => \$added_page_file,
42     'added_page_title:s' => \$added_page_title,
43     'report_title:s'     => \$report_title,
44     'title:s'            => \$report_title
45 );
46
47 my $mig_path = abs_path($0);
48 $mig_path =~ s|[^/]+$||;
49 $reports_xml = find_xml($reports_xml,$mig_path);
50 if (!defined $reports_xml) { abort("Can not find xml reports file."); }
51 my $dom = $parser->parse_file($reports_xml);
52
53 abort('must supply a --title parameter') unless defined $report_title;
54 if (defined $added_page_title) { abort ('must specify --added_page_file') unless defined $added_page_file; }
55 if (defined $added_page_file) { abort ('must specify --added_page_title') unless defined $added_page_title; }
56
57 my $report_file = create_report_name($report_title);
58 $report_file = $MIGGITDIR . $report_file;
59 open(my $fh, '>', $report_file) or die "Could not open report file!";
60
61 write_title_page($report_title,$fh,$analyst);
62
63 if (defined $added_page_file and defined $added_page_title) { 
64     print $fh "<<<\n";
65     print $fh "== $added_page_title\n";
66     print "$added_page_file\t$added_page_title\n";
67     open(my $an,'<:encoding(UTF-8)', $added_page_file) or die "Could not open $added_page_file !";
68     while ( my $line = <$an> ) {
69         print $fh $line;
70     }
71     print $fh "\n";
72     close $an;
73 }
74
75 my @report_tags = split(/\./,$tags);
76 foreach my $t (@report_tags) {
77     print "\n\n=========== Starting to process tag $t\n";
78     print   "==========================================\n";
79     print_section_header(ucfirst($t),$fh);
80     my $linecount = $lines_per_page;
81     my $r;
82
83     my @report_names;
84
85     foreach my $report ($dom->findnodes('//report')) {
86         if (index($report->findvalue('./tag'),$t) != -1) {
87             push @report_names, $report->findvalue('./name');
88         }
89     }
90     
91     print Dumper(@report_names);
92
93     #only has one level of failover now but could change to array of hashes and loops
94     foreach my $rname (@report_names) {
95         print "\nchecking for $rname ... ";
96         my %report = find_report($dom,$t,$rname);
97         $r = print_query($fh,%report);
98     }
99 }
100 # end of main logic
101
102 print "\n";
103 close $fh;
104
105 sub find_xml {
106     my $reports_xml = shift;
107     my $mig_path = shift;
108
109     if ($reports_xml =~ m/\//) { return $reports_xml; }
110
111     my $mig_test_file =  $mig_path . '/../xml/' . $reports_xml;
112     my $working_test_dir = getcwd();
113     my $working_test_file = $working_test_dir . '/' . $reports_xml;
114
115     if (-e $mig_test_file) { return $mig_test_file; }
116     if (-e $working_test_file) { return $working_test_file; }
117
118     return undef;
119 }
120
121 sub find_report {
122     my $dom = shift;
123     my $tag = shift;
124     my $name = shift;
125     my %report;
126
127     foreach my $node ($dom->findnodes('//report')) {
128         if ($node->findvalue('./tag') =~ $tag and $node->findvalue('./name') eq $name) {
129             print "succeeded ... ";
130             %report = (
131                 name => $node->findvalue('./name'),
132                 report_title => $node->findvalue('./report_title'),
133                 query => $node->findvalue('./query'),
134                 heading => $node->findvalue('./heading'),
135                 tag => $node->findvalue('./tag'),
136                 note => $node->findvalue('./note'),
137             );
138             return %report;
139         }
140     }
141     print "failed ... ";
142     return %report = (
143         name => "eaten by grue"
144     );
145 }
146
147 sub print_section_header {
148     my $t = shift;
149     my $fh = shift;
150     $t =~ s/_/ /g;
151     #$t =~ s/(\w+)/\u$1/g;;
152     print $fh "<<<\n";
153     print $fh "== $t Reports\n";
154 }
155
156 sub create_report_name {
157     my $rt = shift;
158     my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
159     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
160     $year += 1900;
161     my $date = $year . '_' . $abbr[$mon] . '_' . $mday;
162     my $report_file = $rt . ' ' . $date . '.asciidoc';
163     $report_file =~ s/ /_/g;
164     return $report_file;
165 }
166
167 sub write_title_page {
168     my $rt = shift;
169     my $fh = shift;
170     my $a = shift;
171
172     my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
173     my $l = length($report_title);
174     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
175     $year += 1900;
176     print $fh "= $rt\n"; 
177     print $fh "$mday $abbr[$mon] $year\n";
178     print $fh "$a\n";
179     print $fh ":toc:\n";
180     print $fh "\n";
181 }
182
183 sub print_query {
184     my $fh = shift;
185     my %report = @_;
186     my $query = $report{query};
187     if (!defined $query) { print "No query defined, returning... \n"; return; }
188     print "$query\n";
189     my $sth = $dbh->prepare($query);
190     $sth->execute();
191
192     my $header_flag = 0;
193
194     while (my @row = $sth->fetchrow_array) {
195             if ($header_flag == 0) {
196                 print $fh "\n.*$report{report_title}*\n";
197                 print $fh "|===\n";
198                 my @h = split(/\./,$report{heading});
199                 my $h_length = @h;
200                 my $h_count = 1;
201                 while ($h_count <= $h_length) {
202                     print $fh "|$h[$h_count-1] ";
203                     $h_count++;
204                 }
205                 print $fh "\n";
206                 $header_flag = 1;
207             }
208             my $row_length = @row;
209             my $r = 1;
210             while ($r <= $row_length) {
211                 if (! defined $row[$r-1] ) {
212                     $row[$r-1] = 'none';
213                 }
214                 print $fh "|$row[$r-1] ";
215                 $r++;
216             }
217             print $fh "\n";
218         }
219     if ($header_flag == 1) { 
220         print $fh "|===\n\n"; 
221         print $fh $report{note};
222         print $fh "\n\n";
223     }
224     print "successfully wrote output for $report{name}.\n\n";
225 }
226
227 sub abort {
228     my $msg = shift;
229     print STDERR "$0: $msg", "\n";
230     print_usage();
231     exit 1;
232 }
233
234 sub print_usage {
235     print <<_USAGE_;
236
237   --tags            - period delimited these are the tags that it will 
238                       use to identify reports to run with (optional)
239   --report_title 
240
241 _USAGE_
242 }
243