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 print "$reports_xml \n";
106 my $dom = $parser->parse_file($reports_xml);
108 if (defined $added_page_file or defined $added_page_title) {
109 abort('must specify --added_page_file and --added_page_title') unless defined $added_page_file and defined $added_page_title;
111 if (defined $added_page_file) { $added_page_file = $MIGGITDIR . $added_page_file; }
113 my $dbh = Mig::db_connect();
114 my $report_file = create_report_name($report_title);
115 $report_file = $MIGGITDIR . $report_file;
117 open(my $fh, '>', $report_file) or abort("Could not open output file!");
119 write_title_page($report_title,$fh,$analyst);
121 if (defined $added_page_file and defined $added_page_title) {
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 abort("Could not open $added_page_file!");
126 while ( my $line = <$an> ) {
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 my $sdrop = $dbh->prepare($fdrop);
138 my $screate = $dbh->prepare($fcreate);
139 print "dropping function $fname ... ";
141 print "creating function $fname\n\n";
146 my @report_tags = split(/\./,$tags);
147 foreach my $t (@report_tags) {
148 print "\n\n=========== Starting to process tag $t\n";
149 print "==========================================\n";
152 foreach my $asset ($dom->findnodes('//asset')) {
153 if (index($asset->findvalue('./tag'),$t) != -1) {
154 push @asset_files, $asset->findvalue('./file');
158 foreach my $fname (@asset_files) {
159 my $asset_path = $mig_path . '../mig-asc/' . $fname;
160 open my $a, $asset_path or abort("Could not open $fname.");
161 while ( my $l = <$a> ) {
167 print_section_header(ucfirst($t),$fh);
168 my $linecount = $lines_per_page;
172 foreach my $asset ($dom->findnodes('//asset')) {
173 if (index($asset->findvalue('./tag'),$t) != -1) {
174 push @asset_files, $asset->findvalue('./file');
179 foreach my $report ($dom->findnodes('//report')) {
180 if (index($report->findvalue('./tag'),$t) != -1 and $report->findvalue('./iteration') eq '0') {
181 push @report_names, $report->findvalue('./name');
185 #only has one level of failover now but could change to array of hashes and loops
186 #but this keeps it simple and in practice I haven't needed more than two
187 foreach my $rname (@report_names) {
193 print "\nchecking for $rname ... ";
194 %report0 = find_report($dom,$t,$rname,'0');
195 $check_tables0 = check_table($report0{query},$MIGSCHEMA);
196 if ($check_tables0 == 1) {
197 $r = print_query($fh,%report0);
199 %report1 = find_report($dom,$t,$rname,'1');
200 if (defined $report1{query}) {
201 $check_tables1 = check_table($report1{query},$MIGSCHEMA);
202 if ($check_tables1 == 1) {$r = print_query($fh,%report1);}
211 ############ end of main logic
217 my $iteration = shift;
220 print "iteration $iteration ";
221 foreach my $node ($dom->findnodes('//report')) {
222 if ($node->findvalue('./tag') =~ $tag and $node->findvalue('./iteration') eq $iteration and $node->findvalue('./name') eq $name) {
223 print "succeeded ... \n";
225 name => $node->findvalue('./name'),
226 report_title => $node->findvalue('./report_title'),
227 query => $node->findvalue('./query'),
228 heading => $node->findvalue('./heading'),
229 tag => $node->findvalue('./tag'),
230 iteration => $node->findvalue('./iteration'),
231 note => $node->findvalue('./note'),
236 print "failed ... \n";
238 name => "eaten by grue"
242 sub print_section_header {
246 #$t =~ s/(\w+)/\u$1/g;;
248 print $fh "== $t Reports\n";
251 sub create_report_name {
253 my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
254 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
256 my $date = $year . '_' . $abbr[$mon] . '_' . $mday;
257 my $report_file = $rt . ' ' . $date . '.asciidoc';
258 $report_file =~ s/ /_/g;
262 sub write_title_page {
267 my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
268 my $l = length($report_title);
269 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
272 print $fh "$mday $abbr[$mon] $year\n";
274 print $fh ":title-logo-image: image::eolilogosmall.png[pdfwidth=3in]\n";
281 my $MIGSCHEMA = shift;
285 my @qe = split(/ /,$query);
290 if ($qe[$i] eq 'FROM' or $qe[$i] eq 'JOIN') {
292 if ($qe[$q] ne '(SELECT') {
293 push @tables, $qe[$q];
298 print "checking tables ... ";
301 foreach my $table (@tables) {
306 if (index($table,'.') != -1) {
307 $schema = (split /\./,$table)[0];
308 $table = (split /\./,$table)[1];
310 if (defined $schema) {
311 $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $schema . '\' AND table_name = \'' . $table . '\');';
313 $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $MIGSCHEMA . '\' AND table_name = \'' . $table . '\');';
315 my $sth = $dbh->prepare($sql);
317 while (my @row = $sth->fetchrow_array) {
318 if ($row[0] eq '1') {
323 if ($row[0] eq '0') {$return_flag = 0;}
326 if ($return_flag == 1) {print "succeeded ... ";} else {print "failed ... ";}
333 my $query = $report{query};
335 my $sth = $dbh->prepare($query);
340 while (my @row = $sth->fetchrow_array) {
341 if ($header_flag == 0) {
342 print $fh "\n.*$report{report_title}*\n";
344 my @h = split(/\./,$report{heading});
347 while ($h_count <= $h_length) {
348 print $fh "|$h[$h_count-1] ";
354 my $row_length = @row;
356 while ($r <= $row_length) {
357 if (! defined $row[$r-1] ) {
360 print $fh "|$row[$r-1] ";
365 if ($header_flag == 1) {
366 print $fh "|===\n\n";
367 print $fh $report{note};
370 print "successfully wrote output for $report{name}.\n\n";
375 print STDERR "$0: $msg", "\n";