10 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
11 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
17 my $mig_bin = "$FindBin::Bin/";
18 use lib "$FindBin::Bin/";
20 use open ':encoding(utf8)';
22 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
25 my $next_arg_is_analyst;
27 my $next_arg_is_report_title;
29 my $next_arg_is_reports_xml;
33 my $next_arg_is_added_page_title;
35 my $next_arg_is_added_page_file;
37 my $parser = XML::LibXML->new();
38 my $lines_per_page = 42;
40 foreach my $arg (@ARGV) {
41 if ($arg eq '--report_title') {
42 $next_arg_is_report_title = 1;
45 if ($next_arg_is_report_title) {
47 $next_arg_is_report_title = 0;
50 if ($arg eq '--analyst') {
51 $next_arg_is_analyst = 1;
54 if ($next_arg_is_analyst) {
56 $next_arg_is_analyst = 0;
59 if ($arg eq '--reports_xml') {
60 $next_arg_is_reports_xml = 1;
63 if ($next_arg_is_reports_xml) {
65 $next_arg_is_reports_xml = 0;
68 if ($arg eq '--tags') {
69 $next_arg_is_tags = 1;
72 if ($next_arg_is_tags) {
74 $next_arg_is_tags = 0;
77 if ($arg eq '--added_page_title') {
78 $next_arg_is_added_page_title = 1;
81 if ($next_arg_is_added_page_title) {
82 $added_page_title = $arg;
83 $next_arg_is_added_page_title = 0;
86 if ($arg eq '--added_page_file') {
87 $next_arg_is_added_page_file = 1;
90 if ($next_arg_is_added_page_file) {
91 $added_page_file = $arg;
92 $next_arg_is_added_page_file = 0;
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'); }
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 else { $reports_xml = $mig_path . '/../mig-xml/' . $reports_xml; }
105 my $dom = $parser->parse_file($reports_xml);
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;
110 if (defined $added_page_file) { $added_page_file = $MIGGITDIR . $added_page_file; }
112 my $dbh = Mig::db_connect();
113 my $report_file = create_report_name($report_title);
114 $report_file = $MIGGITDIR . $report_file;
116 open(my $fh, '>', $report_file) or abort("Could not open output file!");
118 write_title_page($report_title,$fh,$analyst);
120 if (defined $added_page_file and defined $added_page_title) {
122 print $fh "== $added_page_title\n";
123 print "$added_page_file\t$added_page_title\n";
124 open(my $an,'<:encoding(UTF-8)', $added_page_file) or abort("Could not open $added_page_file!");
125 while ( my $line = <$an> ) {
132 foreach my $func ($dom->findnodes('//function')) {
133 my $fdrop = $func->findvalue('./drop');
134 my $fcreate = $func->findvalue('./create');
135 my $fname = $func->findvalue('./name');
136 my $sdrop = $dbh->prepare($fdrop);
137 my $screate = $dbh->prepare($fcreate);
138 print "dropping function $fname ... ";
140 print "creating function $fname\n\n";
145 my @report_tags = split(/\./,$tags);
146 foreach my $t (@report_tags) {
147 print "\n\n=========== Starting to process tag $t\n";
148 print "==========================================\n";
151 foreach my $asset ($dom->findnodes('//asset')) {
152 if (index($asset->findvalue('./tag'),$t) != -1) {
153 push @asset_files, $asset->findvalue('./file');
157 foreach my $fname (@asset_files) {
158 my $asset_path = $mig_path . '../mig-asc/' . $fname;
159 open my $a, $asset_path or abort("Could not open $fname.");
160 while ( my $l = <$a> ) {
166 print_section_header(ucfirst($t),$fh);
167 my $linecount = $lines_per_page;
171 foreach my $asset ($dom->findnodes('//asset')) {
172 if (index($asset->findvalue('./tag'),$t) != -1) {
173 push @asset_files, $asset->findvalue('./file');
178 foreach my $report ($dom->findnodes('//report')) {
179 if (index($report->findvalue('./tag'),$t) != -1 and $report->findvalue('./iteration') eq '0') {
180 push @report_names, $report->findvalue('./name');
184 #only has one level of failover now but could change to array of hashes and loops
185 #but this keeps it simple and in practice I haven't needed more than two
186 foreach my $rname (@report_names) {
192 print "\nchecking for $rname ... ";
193 %report0 = find_report($dom,$t,$rname,'0');
194 $check_tables0 = check_table($report0{query},$MIGSCHEMA);
195 if ($check_tables0 == 1) {
196 $r = print_query($fh,%report0);
198 %report1 = find_report($dom,$t,$rname,'1');
199 if (defined $report1{query}) {
200 $check_tables1 = check_table($report1{query},$MIGSCHEMA);
201 if ($check_tables1 == 1) {$r = print_query($fh,%report1);}
210 ############ end of main logic
216 my $iteration = shift;
219 print "iteration $iteration ";
220 foreach my $node ($dom->findnodes('//report')) {
221 if ($node->findvalue('./tag') =~ $tag and $node->findvalue('./iteration') eq $iteration and $node->findvalue('./name') eq $name) {
222 print "succeeded ... \n";
224 name => $node->findvalue('./name'),
225 report_title => $node->findvalue('./report_title'),
226 query => $node->findvalue('./query'),
227 heading => $node->findvalue('./heading'),
228 tag => $node->findvalue('./tag'),
229 iteration => $node->findvalue('./iteration'),
230 note => $node->findvalue('./note'),
235 print "failed ... \n";
237 name => "eaten by grue"
241 sub print_section_header {
245 #$t =~ s/(\w+)/\u$1/g;;
247 print $fh "== $t Reports\n";
250 sub create_report_name {
252 my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
253 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
255 my $date = $year . '_' . $abbr[$mon] . '_' . $mday;
256 my $report_file = $rt . ' ' . $date . '.asciidoc';
257 $report_file =~ s/ /_/g;
261 sub write_title_page {
266 my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
267 my $l = length($report_title);
268 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
271 print $fh "$mday $abbr[$mon] $year\n";
273 print $fh ":title-logo-image: image::eolilogosmall.png[pdfwidth=3in]\n";
280 my $MIGSCHEMA = shift;
286 my @qe = split(/ /,$query);
291 if ($qe[$i] eq 'FROM' or $qe[$i] eq 'JOIN') {
293 if ($qe[$q] ne '(SELECT') {
294 push @tables, $qe[$q];
299 print "checking tables ... ";
302 foreach my $table (@tables) {
305 if (index($table,'.') != -1) {
306 $schema = (split /\./,$table)[0];
307 $table = (split /\./,$table)[1];
309 $table = clean_query_string($table);
310 if (defined $schema) {
311 $schema = clean_query_string($schema);
312 $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $schema . '\' AND table_name = \'' . $table . '\');';
314 $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $MIGSCHEMA . '\' AND table_name = \'' . $table . '\');';
316 my $sth = $dbh->prepare($sql);
318 while (my @row = $sth->fetchrow_array) {
319 if ($row[0] eq '1') {
323 print "detecting $table failed...\n";
325 if ($row[0] eq '0') {$return_flag = 0;}
328 if ($return_flag == 1) {print "succeeded ...\n";}
332 sub clean_query_string {
335 $str =~ s/(?!_)[[:punct:]]//g; #remove punct except underscores
344 my $query = $report{query};
346 my $sth = $dbh->prepare($query);
351 while (my @row = $sth->fetchrow_array) {
352 if ($header_flag == 0) {
353 print $fh "\n.*$report{report_title}*\n";
355 my @h = split(/\./,$report{heading});
358 while ($h_count <= $h_length) {
359 print $fh "|$h[$h_count-1] ";
365 my $row_length = @row;
367 while ($r <= $row_length) {
368 if (! defined $row[$r-1] ) {
371 print $fh "|$row[$r-1] ";
376 if ($header_flag == 1) {
377 print $fh "|===\n\n";
378 print $fh $report{note};
381 print "successfully wrote output for $report{name}.\n\n";
386 print STDERR "$0: $msg", "\n";