3 ###############################################################################
6 =item B<reporter> --analyst "Analyst Name" --report_title "Report Title"
8 Generates an asciidoc file in the git working directory that can be converted to
9 any appropriate format. The analyst and report parameters are required.
11 Optional parameters are :
13 --added_page_title and --added_page_file
15 If one is used both must be. The added page file can be plain text or asciidoc. This
16 adds an extra arbitrary page of notes to the report. Mig assumes the page file is in the mig git directory.
20 This will define a set of tags to use, if not set it will default to Circs,
21 Holds, Actors, Bibs, Assets & Money.
25 Gives more information about what is happening.
29 Allows you to override the default evergreen_staged_report.xml in the mig-xml folder.
36 ###############################################################################
45 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
46 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
52 my $mig_bin = "$FindBin::Bin/";
53 use lib "$FindBin::Bin/";
55 use open ':encoding(utf8)';
57 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
58 pod2usage(-verbose => 1) if ! $ARGV[1];
61 my $next_arg_is_analyst;
63 my $next_arg_is_report_title;
65 my $next_arg_is_reports_xml;
69 my $next_arg_is_added_page_title;
71 my $next_arg_is_added_page_file;
73 my $parser = XML::LibXML->new();
74 my $lines_per_page = 42;
77 foreach my $arg (@ARGV) {
78 if ($arg eq '--report_title') {
79 $next_arg_is_report_title = 1;
82 if ($next_arg_is_report_title) {
84 $next_arg_is_report_title = 0;
87 if ($arg eq '--analyst') {
88 $next_arg_is_analyst = 1;
91 if ($next_arg_is_analyst) {
93 $next_arg_is_analyst = 0;
96 if ($arg eq '--reports_xml') {
97 $next_arg_is_reports_xml = 1;
100 if ($next_arg_is_reports_xml) {
102 $next_arg_is_reports_xml = 0;
105 if ($arg eq '--tags') {
106 $next_arg_is_tags = 1;
109 if ($next_arg_is_tags) {
111 $next_arg_is_tags = 0;
114 if ($arg eq '--added_page_title') {
115 $next_arg_is_added_page_title = 1;
118 if ($next_arg_is_added_page_title) {
119 $added_page_title = $arg;
120 $next_arg_is_added_page_title = 0;
123 if ($arg eq '--added_page_file') {
124 $next_arg_is_added_page_file = 1;
127 if ($next_arg_is_added_page_file) {
128 $added_page_file = $arg;
129 $next_arg_is_added_page_file = 0;
132 if ($arg eq '--debug') {
138 if (!defined $tags) {$tags = 'circs.holds.actors.bibs.assets.money'};
139 if (!defined $analyst) { abort('--analyst must be supplied'); }
140 if (!defined $report_title) { abort('--report_title must be supplied'); }
142 my $mig_path = abs_path($0);
143 $mig_path =~ s|[^/]+$||;
144 if (!defined $reports_xml) { $reports_xml = $mig_path . '../mig-xml/evergreen_staged_report.xml'; }
145 else { $reports_xml = $mig_path . '/../mig-xml/' . $reports_xml; }
146 my $dom = $parser->parse_file($reports_xml);
148 if (defined $added_page_file or defined $added_page_title) {
149 abort('must specify --added_page_file and --added_page_title') unless defined $added_page_file and defined $added_page_title;
151 if (defined $added_page_file) { $added_page_file = $MIGGITDIR . $added_page_file; }
153 my $dbh = Mig::db_connect();
154 my $report_file = create_report_name($report_title);
155 $report_file = $MIGGITDIR . $report_file;
157 open(my $fh, '>', $report_file) or abort("Could not open output file!");
159 write_title_page($report_title,$fh,$analyst);
161 if (defined $added_page_file and defined $added_page_title) {
163 print $fh "== $added_page_title\n";
164 print "$added_page_file\t$added_page_title\n";
165 open(my $an,'<:encoding(UTF-8)', $added_page_file) or abort("Could not open $added_page_file!");
166 while ( my $line = <$an> ) {
173 foreach my $func ($dom->findnodes('//function')) {
174 my $fdrop = $func->findvalue('./drop');
175 my $fcreate = $func->findvalue('./create');
176 my $fname = $func->findvalue('./name');
177 my $sdrop = $dbh->prepare($fdrop);
178 my $screate = $dbh->prepare($fcreate);
179 print "dropping function $fname ... ";
181 print "creating function $fname\n\n";
186 my @report_tags = split(/\./,$tags);
187 foreach my $t (@report_tags) {
188 print "\n\n=========== Starting to process tag $t\n";
189 print "==========================================\n\n";
192 foreach my $asset ($dom->findnodes('//asset')) {
193 if (index($asset->findvalue('./tag'),$t) != -1) {
194 push @asset_files, $asset->findvalue('./file');
198 foreach my $fname (@asset_files) {
199 my $asset_path = $mig_path . '../mig-asc/' . $fname;
200 open my $a, $asset_path or abort("Could not open $fname.");
201 while ( my $l = <$a> ) {
207 print_section_header(ucfirst($t),$fh);
208 my $linecount = $lines_per_page;
212 foreach my $asset ($dom->findnodes('//asset')) {
213 if (index($asset->findvalue('./tag'),$t) != -1) {
214 push @asset_files, $asset->findvalue('./file');
219 foreach my $report ($dom->findnodes('//report')) {
220 if (index($report->findvalue('./tag'),$t) != -1 and $report->findvalue('./iteration') eq '0') {
221 push @report_names, $report->findvalue('./name');
225 #only has one level of failover now but could change to array of hashes and loops
226 #but this keeps it simple and in practice I haven't needed more than two
227 foreach my $rname (@report_names) {
233 if ($debug_flag == 1) {print "\nchecking for $rname ... ";}
234 %report0 = find_report($dom,$t,$rname,'0',$debug_flag);
235 $check_tables0 = check_table($report0{query},$MIGSCHEMA,$debug_flag,$rname);
236 if ($check_tables0 == 1) {
237 $r = print_query($fh,%report0);
239 %report1 = find_report($dom,$t,$rname,'1',$debug_flag);
240 if (defined $report1{query}) {
241 $check_tables1 = check_table($report1{query},$MIGSCHEMA,$debug_flag,$rname);
242 if ($check_tables1 == 1) {$r = print_query($fh,%report1);}
251 ############ end of main logic
257 my $iteration = shift;
258 my $debug_flag = shift;
261 if ($debug_flag == 1) {print "iteration $iteration ";}
262 foreach my $node ($dom->findnodes('//report')) {
263 if ($node->findvalue('./tag') =~ $tag and $node->findvalue('./iteration') eq $iteration and $node->findvalue('./name') eq $name) {
264 if ($debug_flag == 1) {print "succeeded ... \n";}
266 name => $node->findvalue('./name'),
267 report_title => $node->findvalue('./report_title'),
268 query => $node->findvalue('./query'),
269 heading => $node->findvalue('./heading'),
270 tag => $node->findvalue('./tag'),
271 iteration => $node->findvalue('./iteration'),
272 note => $node->findvalue('./note'),
277 if ($debug_flag == 1) {print "failed ... \n";}
279 name => "eaten by grue"
283 sub print_section_header {
287 #$t =~ s/(\w+)/\u$1/g;;
289 print $fh "== $t Reports\n";
292 sub create_report_name {
294 my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
295 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
297 my $date = $year . '_' . $abbr[$mon] . '_' . $mday;
298 my $report_file = $rt . ' ' . $date . '.asciidoc';
299 $report_file =~ s/ /_/g;
303 sub write_title_page {
308 my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
309 my $l = length($report_title);
310 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
313 print $fh "$mday $abbr[$mon] $year\n";
315 print $fh ":title-logo-image: image::eolilogosmall.png[pdfwidth=3in]\n";
322 my $MIGSCHEMA = shift;
323 my $debug_flag = shift;
324 my $report_name = shift;
326 if ($debug_flag == 1) {print "$query\n";}
330 my @qe = split(/ /,$query);
335 if ($qe[$i] eq 'FROM' or $qe[$i] eq 'JOIN') {
337 if ($qe[$q] ne '(SELECT') {
338 push @tables, $qe[$q];
343 if ($debug_flag == 1) {print "checking tables ... ";}
346 foreach my $table (@tables) {
349 if (index($table,'.') != -1) {
350 $schema = (split /\./,$table)[0];
351 $table = (split /\./,$table)[1];
353 $table = clean_query_string($table);
354 if (defined $schema) {
355 $schema = clean_query_string($schema);
356 $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $schema . '\' AND table_name = \'' . $table . '\');';
358 $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $MIGSCHEMA . '\' AND table_name = \'' . $table . '\');';
360 my $sth = $dbh->prepare($sql);
362 while (my @row = $sth->fetchrow_array) {
363 if ($row[0] eq '1') {
367 if ($debug_flag == 1) {print "detecting $table failed...\n";}
369 if ($row[0] eq '0') {$return_flag = 0;}
372 if ($return_flag == 1 and $debug_flag == 1) {print "succeeded ...\n";}
373 if ($return_flag == 0) {print "!!!!! a table failed the find test for report $report_name\n\n";}
377 sub clean_query_string {
380 $str =~ s/(?!_)[[:punct:]]//g; #remove punct except underscores
389 my $query = $report{query};
390 my $sth = $dbh->prepare($query);
395 while (my @row = $sth->fetchrow_array) {
396 if ($header_flag == 0) {
397 print $fh "\n.*$report{report_title}*\n";
399 my @h = split(/\./,$report{heading});
402 while ($h_count <= $h_length) {
403 print $fh "|$h[$h_count-1] ";
409 my $row_length = @row;
411 while ($r <= $row_length) {
412 if (! defined $row[$r-1] ) {
415 print $fh "|$row[$r-1] ";
420 if ($header_flag == 1) {
421 print $fh "|===\n\n";
422 print $fh $report{note};
425 print "successfully wrote output for $report{name}.\n\n";
430 print STDERR "$0: $msg", "\n";