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 die "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 die "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";
150 print_section_header(ucfirst($t),$fh);
151 my $linecount = $lines_per_page;
156 foreach my $report ($dom->findnodes('//report')) {
157 if (index($report->findvalue('./tag'),$t) != -1 and $report->findvalue('./iteration') eq '0') {
158 push @report_names, $report->findvalue('./name');
162 #only has one level of failover now but could change to array of hashes and loops
163 #but this keeps it simple and in practice I haven't needed more than two
164 foreach my $rname (@report_names) {
170 print "\nchecking for $rname ... ";
171 %report0 = find_report($dom,$t,$rname,'0');
172 $check_tables0 = check_table($report0{query},$MIGSCHEMA);
173 if ($check_tables0 == 1) {
174 $r = print_query($fh,%report0);
176 %report1 = find_report($dom,$t,$rname,'1');
177 if (defined $report1{query}) {
178 $check_tables1 = check_table($report1{query},$MIGSCHEMA);
179 if ($check_tables1 == 1) {$r = print_query($fh,%report1);}
188 ############ end of main logic
194 my $iteration = shift;
197 print "iteration $iteration ";
198 foreach my $node ($dom->findnodes('//report')) {
199 if ($node->findvalue('./tag') =~ $tag and $node->findvalue('./iteration') eq $iteration and $node->findvalue('./name') eq $name) {
200 print "succeeded ... \n";
202 name => $node->findvalue('./name'),
203 report_title => $node->findvalue('./report_title'),
204 query => $node->findvalue('./query'),
205 heading => $node->findvalue('./heading'),
206 tag => $node->findvalue('./tag'),
207 iteration => $node->findvalue('./iteration'),
208 note => $node->findvalue('./note'),
213 print "failed ... \n";
215 name => "eaten by grue"
219 sub print_section_header {
223 #$t =~ s/(\w+)/\u$1/g;;
225 print $fh "== $t Reports\n";
228 sub create_report_name {
230 my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
231 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
233 my $date = $year . '_' . $abbr[$mon] . '_' . $mday;
234 my $report_file = $rt . ' ' . $date . '.asciidoc';
235 $report_file =~ s/ /_/g;
239 sub write_title_page {
244 my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
245 my $l = length($report_title);
246 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
249 print $fh "$mday $abbr[$mon] $year\n";
251 print $fh ":title-logo-image: image::eolilogosmall.png[pdfwidth=3in]\n";
258 my $MIGSCHEMA = shift;
262 my @qe = split(/ /,$query);
267 if ($qe[$i] eq 'FROM' or $qe[$i] eq 'JOIN') {
269 if ($qe[$q] ne '(SELECT') {
270 push @tables, $qe[$q];
275 print "checking tables ... ";
278 foreach my $table (@tables) {
283 if (index($table,'.') != -1) {
284 $schema = (split /\./,$table)[0];
285 $table = (split /\./,$table)[1];
287 if (defined $schema) {
288 $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $schema . '\' AND table_name = \'' . $table . '\');';
290 $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $MIGSCHEMA . '\' AND table_name = \'' . $table . '\');';
292 my $sth = $dbh->prepare($sql);
294 while (my @row = $sth->fetchrow_array) {
295 if ($row[0] eq '1') {
300 if ($row[0] eq '0') {$return_flag = 0;}
303 if ($return_flag == 1) {print "succeeded ... ";} else {print "failed ... ";}
310 my $query = $report{query};
312 my $sth = $dbh->prepare($query);
317 while (my @row = $sth->fetchrow_array) {
318 if ($header_flag == 0) {
319 print $fh "\n.*$report{report_title}*\n";
321 my @h = split(/\./,$report{heading});
324 while ($h_count <= $h_length) {
325 print $fh "|$h[$h_count-1] ";
331 my $row_length = @row;
333 while ($r <= $row_length) {
334 if (! defined $row[$r-1] ) {
337 print $fh "|$row[$r-1] ";
342 if ($header_flag == 1) {
343 print $fh "|===\n\n";
344 print $fh $report{note};
347 print "successfully wrote output for $report{name}.\n\n";
352 print STDERR "$0: $msg", "\n";