initial working branch for mig reporter
[migration-tools.git] / mig-bin / mig-reporter
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use DBI;
7 use Data::Dumper;
8 use XML::LibXML;
9 use Env qw(
10     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
11     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
12 );
13 use Pod::Usage;
14 use Switch;
15 use Cwd 'abs_path';
16 use FindBin;
17 my $mig_bin = "$FindBin::Bin/";
18 use lib "$FindBin::Bin/";
19 use Mig;
20 use open ':encoding(utf8)';
21
22 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
23
24 my $analyst;
25 my $next_arg_is_analyst;
26 my $report_title;
27 my $next_arg_is_report_title;
28 my $reports_xml;
29 my $next_arg_is_reports_xml;
30 my $tags;
31 my $next_arg_is_tags;
32 my $added_page_title;
33 my $next_arg_is_added_page_title;
34 my $added_page_file;
35 my $next_arg_is_added_page_file;
36 my $i = 0;
37 my $parser = XML::LibXML->new();
38 my $lines_per_page = 42;
39
40 foreach my $arg (@ARGV) {
41     if ($arg eq '--report_title') {
42         $next_arg_is_report_title = 1;
43         next;
44     }
45     if ($next_arg_is_report_title) {
46         $report_title = $arg;
47         $next_arg_is_report_title = 0;
48         next;
49     }
50     if ($arg eq '--analyst') {
51         $next_arg_is_analyst = 1;
52         next;
53     }
54     if ($next_arg_is_analyst) {
55         $analyst = $arg;
56         $next_arg_is_analyst = 0;
57         next;
58     }
59     if ($arg eq '--reports_xml') {
60         $next_arg_is_reports_xml = 1;
61         next;
62     }
63     if ($next_arg_is_reports_xml) {
64         $reports_xml = $arg;
65         $next_arg_is_reports_xml = 0;
66         next;
67     }
68     if ($arg eq '--tags') {
69         $next_arg_is_tags = 1;
70         next;
71     }
72     if ($next_arg_is_tags) {
73         $tags = $arg;
74         $next_arg_is_tags = 0;
75         next;
76     }
77     if ($arg eq '--added_page_title') {
78         $next_arg_is_added_page_title = 1;
79         next;
80     }
81     if ($next_arg_is_added_page_title) {
82         $added_page_title = $arg;
83         $next_arg_is_added_page_title = 0;
84         next;
85     }
86     if ($arg eq '--added_page_file') {
87         $next_arg_is_added_page_file = 1;
88         next;
89     }
90     if ($next_arg_is_added_page_file) {
91         $added_page_file = $arg;
92         $next_arg_is_added_page_file = 0;
93         next;
94     }
95 }
96
97 if (!defined $tags) {$tags = 'Circs.Holds.Actors.Bibs.Assets.Money'};
98 if (!defined $analyst) { abort('--analyst must be supplied'); }
99 if (!defined $report_title) { abort('--report_title must be supplied'); }
100
101 my $mig_path = abs_path($0);
102 $mig_path =~ s|[^/]+$||;
103 if (!defined $reports_xml) { $reports_xml = $mig_path . '../mig-xml/evergreen_staged_report.xml'; }
104 print "$reports_xml \n";
105 my $dom = $parser->parse_file($reports_xml);
106
107 if (defined $added_page_file or defined $added_page_title) {
108     abort('must specify --added_page_file and --added_page_title') unless defined $added_page_file and defined $added_page_title;
109     }
110 if ($MIGSCHEMA eq 'full') { $MIGSCHEMA = ''; }
111
112 my $dbh = Mig::db_connect();
113 my $report_file = create_report_name($report_title);
114 $report_file = $MIGGITDIR . $report_file;
115 my $mig_func_schema = $MIGSCHEMA;
116
117 open(my $fh, '>', $report_file) or die "Could not open output file!";
118
119 write_title_page($report_title,$fh,$analyst);
120
121 if (defined $added_page_file and defined $added_page_title) { 
122     print $fh "<<<\n";
123     print $fh "== $added_page_title\n";
124     print "$added_page_file\t$added_page_title\n";
125     open(my $an,'<:encoding(UTF-8)', $added_page_file) or die "Could not open $added_page_file !";
126     while ( my $line = <$an> ) {
127         print $fh $line;
128     }
129     print $fh "\n";
130     close $an;
131 }
132
133 foreach my $func ($dom->findnodes('//function')) {
134     my $fdrop = $func->findvalue('./drop');
135     my $fcreate = $func->findvalue('./create');    
136     my $fname = $func->findvalue('./name');
137     $fdrop =~ s/mig_func_schema/$mig_func_schema/g;
138     $fcreate =~ s/mig_func_schema/$mig_func_schema/g;
139     my $sdrop = $dbh->prepare($fdrop);
140     my $screate = $dbh->prepare($fcreate);
141     print "dropping function $fname ... ";
142     $sdrop->execute();
143     print "creating function $fname\n\n";
144     $screate->execute();
145 }
146
147
148
149 my @report_tags = split(/\./,$tags);
150 foreach my $t (@report_tags) {
151     print "\n\n=========== Starting to process tag $t\n";
152     print   "==========================================\n";
153     print_section_header(ucfirst($t),$fh);
154     my $linecount = $lines_per_page;
155     my $r;
156
157     my @report_names;
158
159     foreach my $report ($dom->findnodes('//report')) {
160         if (index($report->findvalue('./tag'),$t) != -1 and $report->findvalue('./iteration') eq '0') {
161             push @report_names, $report->findvalue('./name');
162         }
163     }
164     
165     print Dumper(@report_names);
166
167     #only has one level of failover now but could change to array of hashes and loops
168     foreach my $rname (@report_names) {
169         my %report0;
170         my %report1;
171         my $check_tables0;
172         my $check_tables1;
173
174         print "\nchecking for $rname ... ";
175         %report0 = find_report($dom,$t,$rname,'0');
176         $check_tables0 = check_table($report0{query},$MIGSCHEMA);
177         if ($check_tables0 == 1) {
178            $r =  print_query($fh,%report0); 
179         } else {
180         %report1 = find_report($dom,$t,$rname,'1');
181         if (defined $report1{query}) {
182             $check_tables1 = check_table($report1{query},$MIGSCHEMA);
183             if ($check_tables1 == 1) {$r = print_query($fh,%report1);}    
184             }
185         }
186     }
187 }
188 # end of main logic
189
190 print "\n";
191 close $fh;
192
193 sub find_report {
194     my $dom = shift;
195     my $tag = shift;
196     my $name = shift;
197     my $iteration = shift;
198     my %report;
199
200     print "iteration $iteration ";
201     foreach my $node ($dom->findnodes('//report')) {
202         if ($node->findvalue('./tag') =~ $tag and $node->findvalue('./iteration') eq $iteration and $node->findvalue('./name') eq $name) {
203             print "succeeded ... ";
204             %report = (
205                 name => $node->findvalue('./name'),
206                 report_title => $node->findvalue('./report_title'),
207                 query => $node->findvalue('./query'),
208                 heading => $node->findvalue('./heading'),
209                 tag => $node->findvalue('./tag'),
210                 iteration => $node->findvalue('./iteration'),
211                 note => $node->findvalue('./note'),
212             );
213             return %report;
214         }
215     }
216     print "failed ... ";
217     return %report = (
218         name => "eaten by grue"
219     );
220 }
221
222 sub print_section_header {
223     my $t = shift;
224     my $fh = shift;
225     $t =~ s/_/ /g;
226     #$t =~ s/(\w+)/\u$1/g;;
227     print $fh "<<<\n";
228     print $fh "== $t Reports\n";
229 }
230
231 sub create_report_name {
232     my $rt = shift;
233     my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
234     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
235     $year += 1900;
236     my $date = $year . '_' . $abbr[$mon] . '_' . $mday;
237     my $report_file = $rt . ' ' . $date . '.asciidoc';
238     $report_file =~ s/ /_/g;
239     return $report_file;
240 }
241
242 sub write_title_page {
243     my $rt = shift;
244     my $fh = shift;
245     my $a = shift;
246
247     my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
248     my $l = length($report_title);
249     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
250     $year += 1900;
251     print $fh "= $rt\n"; 
252     print $fh "$mday $abbr[$mon] $year\n";
253     print $fh "$a\n";
254     print $fh ":title-logo-image: image::eolilogosmall.png[pdfwidth=3in]\n";
255     print $fh ":toc:\n";
256     print $fh "\n";
257 }
258
259 sub check_table {
260     my $query = shift;
261     my $MIGSCHEMA = shift;
262
263     my $i = 0;
264     my $return_flag = 1;   
265     my @qe = split(/ /,$query);
266     my @tables = grep /mig_schema/, @qe;
267
268     print "checking tables ... ";
269     foreach my $table (@tables) {
270         $table =~ s/mig_schema.//g;
271         $table =~ s/\)//g;
272         $table =~ s/\<//g;
273         my $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $MIGSCHEMA . '\' AND table_name = \'' . $table . '\');';
274         my $sth = $dbh->prepare($sql);
275         $sth->execute();
276         while (my @row = $sth->fetchrow_array) {
277             if ($row[0] eq '1') {
278                     next;
279                 } else {
280                     $return_flag = 0;
281                 }
282             if ($row[0] eq '0') {$return_flag = 0;}
283         }
284     }
285     if ($return_flag == 1) {print "succeeded ... ";} else {print "failed ... ";}
286     return $return_flag;
287 }
288
289 sub print_query {
290     my $fh = shift;
291     my %report = @_;
292     my $query = $report{query};
293     $query =~ s/mig_schema/$MIGSCHEMA/g;
294     $query =~ s/mig_func_schema/$mig_func_schema/g;
295     my $sth = $dbh->prepare($query);
296     $sth->execute();
297
298     my $header_flag = 0;
299
300     while (my @row = $sth->fetchrow_array) {
301             if ($header_flag == 0) {
302                 print $fh "\n.*$report{report_title}*\n";
303                 print $fh "|===\n";
304                 my @h = split(/\./,$report{heading});
305                 my $h_length = @h;
306                 my $h_count = 1;
307                 while ($h_count <= $h_length) {
308                     print $fh "|$h[$h_count-1] ";
309                     $h_count++;
310                 }
311                 print $fh "\n";
312                 $header_flag = 1;
313             }
314             my $row_length = @row;
315             my $r = 1;
316             while ($r <= $row_length) {
317                 if (! defined $row[$r-1] ) {
318                     $row[$r-1] = 'none';
319                 }
320                 print $fh "|$row[$r-1] ";
321                 $r++;
322             }
323             print $fh "\n";
324         }
325     if ($header_flag == 1) { 
326         print $fh "|===\n\n"; 
327         print $fh $report{note};
328         print $fh "\n\n";
329     }
330     print "successfully wrote output for $report{name}.\n\n";
331 }
332
333 sub abort {
334     my $msg = shift;
335     print STDERR "$0: $msg", "\n";
336     exit 1;
337 }
338
339