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 print "$reports_xml \n";
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; }
111 if ($MIGSCHEMA eq 'full') { $MIGSCHEMA = ''; }
113 my $dbh = Mig::db_connect();
114 my $report_file = create_report_name($report_title);
115 $report_file = $MIGGITDIR . $report_file;
116 my $mig_func_schema = $MIGSCHEMA;
118 open(my $fh, '>', $report_file) or die "Could not open output file!";
120 write_title_page($report_title,$fh,$analyst);
122 if (defined $added_page_file and defined $added_page_title) {
124 print $fh "== $added_page_title\n";
125 print "$added_page_file\t$added_page_title\n";
126 open(my $an,'<:encoding(UTF-8)', $added_page_file) or die "Could not open $added_page_file !";
127 while ( my $line = <$an> ) {
134 foreach my $func ($dom->findnodes('//function')) {
135 my $fdrop = $func->findvalue('./drop');
136 my $fcreate = $func->findvalue('./create');
137 my $fname = $func->findvalue('./name');
138 $fdrop =~ s/mig_func_schema/$mig_func_schema/g;
139 $fcreate =~ s/mig_func_schema/$mig_func_schema/g;
140 my $sdrop = $dbh->prepare($fdrop);
141 my $screate = $dbh->prepare($fcreate);
142 print "dropping function $fname ... ";
144 print "creating function $fname\n\n";
150 my @report_tags = split(/\./,$tags);
151 foreach my $t (@report_tags) {
152 print "\n\n=========== Starting to process tag $t\n";
153 print "==========================================\n";
154 print_section_header(ucfirst($t),$fh);
155 my $linecount = $lines_per_page;
160 foreach my $report ($dom->findnodes('//report')) {
161 if (index($report->findvalue('./tag'),$t) != -1 and $report->findvalue('./iteration') eq '0') {
162 push @report_names, $report->findvalue('./name');
166 print Dumper(@report_names);
168 #only has one level of failover now but could change to array of hashes and loops
169 foreach my $rname (@report_names) {
175 print "\nchecking for $rname ... ";
176 %report0 = find_report($dom,$t,$rname,'0');
177 $check_tables0 = check_table($report0{query},$MIGSCHEMA);
178 if ($check_tables0 == 1) {
179 $r = print_query($fh,%report0);
181 %report1 = find_report($dom,$t,$rname,'1');
182 if (defined $report1{query}) {
183 $check_tables1 = check_table($report1{query},$MIGSCHEMA);
184 if ($check_tables1 == 1) {$r = print_query($fh,%report1);}
198 my $iteration = shift;
201 print "iteration $iteration ";
202 foreach my $node ($dom->findnodes('//report')) {
203 if ($node->findvalue('./tag') =~ $tag and $node->findvalue('./iteration') eq $iteration and $node->findvalue('./name') eq $name) {
204 print "succeeded ... ";
206 name => $node->findvalue('./name'),
207 report_title => $node->findvalue('./report_title'),
208 query => $node->findvalue('./query'),
209 heading => $node->findvalue('./heading'),
210 tag => $node->findvalue('./tag'),
211 iteration => $node->findvalue('./iteration'),
212 note => $node->findvalue('./note'),
219 name => "eaten by grue"
223 sub print_section_header {
227 #$t =~ s/(\w+)/\u$1/g;;
229 print $fh "== $t Reports\n";
232 sub create_report_name {
234 my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
235 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
237 my $date = $year . '_' . $abbr[$mon] . '_' . $mday;
238 my $report_file = $rt . ' ' . $date . '.asciidoc';
239 $report_file =~ s/ /_/g;
243 sub write_title_page {
248 my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
249 my $l = length($report_title);
250 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
253 print $fh "$mday $abbr[$mon] $year\n";
255 print $fh ":title-logo-image: image::eolilogosmall.png[pdfwidth=3in]\n";
262 my $MIGSCHEMA = shift;
266 my @qe = split(/ /,$query);
267 my @tables = grep /mig_schema/, @qe;
269 print "checking tables ... ";
270 foreach my $table (@tables) {
271 $table =~ s/mig_schema.//g;
274 my $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $MIGSCHEMA . '\' AND table_name = \'' . $table . '\');';
275 my $sth = $dbh->prepare($sql);
277 while (my @row = $sth->fetchrow_array) {
278 if ($row[0] eq '1') {
283 if ($row[0] eq '0') {$return_flag = 0;}
286 if ($return_flag == 1) {print "succeeded ... ";} else {print "failed ... ";}
293 my $query = $report{query};
294 $query =~ s/mig_schema/$MIGSCHEMA/g;
295 $query =~ s/mig_func_schema/$mig_func_schema/g;
296 my $sth = $dbh->prepare($query);
301 while (my @row = $sth->fetchrow_array) {
302 if ($header_flag == 0) {
303 print $fh "\n.*$report{report_title}*\n";
305 my @h = split(/\./,$report{heading});
308 while ($h_count <= $h_length) {
309 print $fh "|$h[$h_count-1] ";
315 my $row_length = @row;
317 while ($r <= $row_length) {
318 if (! defined $row[$r-1] ) {
321 print $fh "|$row[$r-1] ";
326 if ($header_flag == 1) {
327 print $fh "|===\n\n";
328 print $fh $report{note};
331 print "successfully wrote output for $report{name}.\n\n";
336 print STDERR "$0: $msg", "\n";