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