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; }
112 if ($MIGSCHEMA eq 'full') { $MIGSCHEMA = ''; }
114 my $dbh = Mig::db_connect();
115 my $report_file = create_report_name($report_title);
116 $report_file = $MIGGITDIR . $report_file;
117 my $mig_func_schema = $MIGSCHEMA;
119 open(my $fh, '>', $report_file) or die "Could not open output file!";
121 write_title_page($report_title,$fh,$analyst);
123 if (defined $added_page_file and defined $added_page_title) {
125 print $fh "== $added_page_title\n";
126 print "$added_page_file\t$added_page_title\n";
127 open(my $an,'<:encoding(UTF-8)', $added_page_file) or die "Could not open $added_page_file !";
128 while ( my $line = <$an> ) {
135 foreach my $func ($dom->findnodes('//function')) {
136 my $fdrop = $func->findvalue('./drop');
137 my $fcreate = $func->findvalue('./create');
138 my $fname = $func->findvalue('./name');
139 $fdrop =~ s/mig_func_schema/$mig_func_schema/g;
140 $fcreate =~ s/mig_func_schema/$mig_func_schema/g;
141 my $sdrop = $dbh->prepare($fdrop);
142 my $screate = $dbh->prepare($fcreate);
143 print "dropping function $fname ... ";
145 print "creating function $fname\n\n";
151 my @report_tags = split(/\./,$tags);
152 foreach my $t (@report_tags) {
153 print "\n\n=========== Starting to process tag $t\n";
154 print "==========================================\n";
155 print_section_header(ucfirst($t),$fh);
156 my $linecount = $lines_per_page;
161 foreach my $report ($dom->findnodes('//report')) {
162 if (index($report->findvalue('./tag'),$t) != -1 and $report->findvalue('./iteration') eq '0') {
163 push @report_names, $report->findvalue('./name');
167 print Dumper(@report_names);
169 #only has one level of failover now but could change to array of hashes and loops
170 foreach my $rname (@report_names) {
176 print "\nchecking for $rname ... ";
177 %report0 = find_report($dom,$t,$rname,'0');
178 $check_tables0 = check_table($report0{query},$MIGSCHEMA);
179 if ($check_tables0 == 1) {
180 $r = print_query($fh,%report0);
182 %report1 = find_report($dom,$t,$rname,'1');
183 if (defined $report1{query}) {
184 $check_tables1 = check_table($report1{query},$MIGSCHEMA);
185 if ($check_tables1 == 1) {$r = print_query($fh,%report1);}
199 my $iteration = shift;
202 print "iteration $iteration ";
203 foreach my $node ($dom->findnodes('//report')) {
204 if ($node->findvalue('./tag') =~ $tag and $node->findvalue('./iteration') eq $iteration and $node->findvalue('./name') eq $name) {
205 print "succeeded ... ";
207 name => $node->findvalue('./name'),
208 report_title => $node->findvalue('./report_title'),
209 query => $node->findvalue('./query'),
210 heading => $node->findvalue('./heading'),
211 tag => $node->findvalue('./tag'),
212 iteration => $node->findvalue('./iteration'),
213 note => $node->findvalue('./note'),
220 name => "eaten by grue"
224 sub print_section_header {
228 #$t =~ s/(\w+)/\u$1/g;;
230 print $fh "== $t Reports\n";
233 sub create_report_name {
235 my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
236 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
238 my $date = $year . '_' . $abbr[$mon] . '_' . $mday;
239 my $report_file = $rt . ' ' . $date . '.asciidoc';
240 $report_file =~ s/ /_/g;
244 sub write_title_page {
249 my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
250 my $l = length($report_title);
251 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
254 print $fh "$mday $abbr[$mon] $year\n";
256 print $fh ":title-logo-image: image::eolilogosmall.png[pdfwidth=3in]\n";
263 my $MIGSCHEMA = shift;
267 my @qe = split(/ /,$query);
268 my @tables = grep /mig_schema/, @qe;
270 print "checking tables ... ";
271 foreach my $table (@tables) {
272 $table =~ s/mig_schema.//g;
275 my $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $MIGSCHEMA . '\' AND table_name = \'' . $table . '\');';
276 my $sth = $dbh->prepare($sql);
278 while (my @row = $sth->fetchrow_array) {
279 if ($row[0] eq '1') {
284 if ($row[0] eq '0') {$return_flag = 0;}
287 if ($return_flag == 1) {print "succeeded ... ";} else {print "failed ... ";}
294 my $query = $report{query};
295 $query =~ s/mig_schema/$MIGSCHEMA/g;
296 $query =~ s/mig_func_schema/$mig_func_schema/g;
297 my $sth = $dbh->prepare($query);
302 while (my @row = $sth->fetchrow_array) {
303 if ($header_flag == 0) {
304 print $fh "\n.*$report{report_title}*\n";
306 my @h = split(/\./,$report{heading});
309 while ($h_count <= $h_length) {
310 print $fh "|$h[$h_count-1] ";
316 my $row_length = @row;
318 while ($r <= $row_length) {
319 if (! defined $row[$r-1] ) {
322 print $fh "|$row[$r-1] ";
327 if ($header_flag == 1) {
328 print $fh "|===\n\n";
329 print $fh $report{note};
332 print "successfully wrote output for $report{name}.\n\n";
337 print STDERR "$0: $msg", "\n";