cleaned up docs
[migration-tools.git] / mig-bin / mig-reporter
index ee91f43..7ba239e 100755 (executable)
@@ -1,15 +1,19 @@
 #!/usr/bin/perl
-
+# -*- coding: iso-8859-15 -*-
 ###############################################################################
 =pod
 
-=item B<reporter> --analyst "Analyst Name" --report_title "Report Title"
+=item B<reporter> --title "Report Title"
 
 Generates an asciidoc file in the git working directory that can be converted to 
 any appropriate format.  The analyst and report parameters are required.
 
 Optional parameters are : 
 
+-- analyst
+
+Default to "Equinox Open Library Initiative"
+
 --added_page_title and --added_page_file 
 
 If one is used both must be.  The added page file can be plain text or asciidoc.  This
@@ -63,7 +67,7 @@ use open ':encoding(utf8)';
 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
 pod2usage(-verbose => 1) if ! $ARGV[1];
 
-my $analyst;
+my $analyst = 'Equinox Open Library Initiative';;
 my $report_title;
 my $reports_xml = 'evergreen_staged_report.xml';
 my $tags;
@@ -80,6 +84,7 @@ my $fh;
 my $ret = GetOptions(
     'analyst:s'           => \$analyst,
     'report_title:s'      => \$report_title,
+    'title:s'             => \$report_title,
     'reports_xml:s'       => \$reports_xml,
     'tags:s'              => \$tags,
     'added_page_title:s'  => \$added_page_title,
@@ -89,7 +94,7 @@ my $ret = GetOptions(
 );
 
 if (!defined $tags) {$tags = 'circs.holds.actors.bibs.assets.money.notices'};
-if (!defined $report_title) { abort('--report_title must be supplied'); }
+if (!defined $report_title) { abort('--report_title or --title must be supplied'); }
 if (!defined $analyst) { abort('--analyst must be supplied'); }
 
 my $mig_path = abs_path($0);
@@ -109,6 +114,7 @@ $report_file = $MIGGITDIR . $report_file;
 
 open($fh, '>', $report_file) or abort("Could not open output file $report_file!");
 write_title_page($report_title,$fh,$analyst,$captions);
+load_javascript($fh);
 
 if (defined $added_page_file and defined $added_page_title) { 
     print $fh "<<<\n";
@@ -134,6 +140,18 @@ foreach my $func ($dom->findnodes('//function')) {
     $screate->execute();
 }
 
+foreach my $table ($dom->findnodes('//table')) {
+    my $tdrop = $table->findvalue('./drop');
+    my $tcreate = $table->findvalue('./create');
+    my $tname = $table->findvalue('./name');
+    my $sdrop = $dbh->prepare($tdrop);
+    my $screate = $dbh->prepare($tcreate);
+    print "dropping table $tname ... ";
+    $sdrop->execute();
+    print "creating table $tname\n\n";
+    $screate->execute();
+}
+
 $tags = lc($tags);
 my @report_tags = split(/\./,$tags);
 foreach my $t (@report_tags) {
@@ -187,7 +205,7 @@ foreach my $t (@report_tags) {
         if ($debug eq 'on') {print "\nchecking for $rname ... ";}
         %report0 = find_report($dom,$t,$rname,'0',$debug);
         $check_tables0 = check_table($report0{query},$MIGSCHEMA,$debug,$rname);
-        if ($check_tables0 == 1) { $r =  print_query($fh,%report0); } else {
+        if ($check_tables0 == 1) { $r = print_query($fh,%report0); } else {
                %report1 = find_report($dom,$t,$rname,'1',$debug);
             if (defined $report1{query}) {
                $check_tables1 = check_table($report1{query},$MIGSCHEMA,$debug,$rname);
@@ -200,6 +218,14 @@ foreach my $t (@report_tags) {
 
 print "\n";
 
+foreach my $table ($dom->findnodes('//table')) {
+    my $tdrop = $table->findvalue('./drop');
+    my $tname = $table->findvalue('./name');
+    my $sdrop = $dbh->prepare($tdrop);
+    print "cleaning up table $tname ... \n";
+    $sdrop->execute();
+}
+
 close $fh;
 
 ############ end of main logic
@@ -240,7 +266,11 @@ sub find_report {
                 tag => $node->findvalue('./tag'),
                 iteration => $node->findvalue('./iteration'),
                 note => $node->findvalue('./note'),
-            );
+                               display => $node->findvalue('./display'),
+                       chart_labels => $node->findvalue('./chart_labels'),
+                               divwidth => $node->findvalue('./divwidth'),
+                               divheight => $node->findvalue('./divheight'),
+               );
             return %report;
         }
     }
@@ -293,6 +323,14 @@ sub write_title_page {
     print $fh "\n";
 }
 
+sub load_javascript {
+       my $fh = shift;
+
+       print $fh "++++\n";
+       print $fh "<script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>\n";
+       print $fh "++++\n";
+}
+
 sub check_table {
     my $query = shift;
     my $MIGSCHEMA = shift;
@@ -362,13 +400,24 @@ sub clean_query_string {
 sub print_query {
     my $fh = shift;
     my %report = @_;
+
+       my $display = $report{display};
+       my $height = $report{divheight};
+       my $width = $report{divwidth};
+       if (!defined $display) { $display = 'table'; }
+       my $rname = $report{name};
     my $query = $report{query};
+       my $title = $report{report_title};
     my $sth = $dbh->prepare($query);
     $sth->execute();
 
+       if ($height) { $height = $height . 'px'; }
+       if ($width)  { $width = $width . 'px'; }
     my $header_flag = 0;
 
-    while (my @row = $sth->fetchrow_array) {
+       #print asciidoc
+       if ($display eq 'table') {
+       while (my @row = $sth->fetchrow_array) {
             if ($header_flag == 0) {
                 print $fh "\n.*$report{report_title}*\n";
                 print $fh "|===\n";
@@ -393,11 +442,47 @@ sub print_query {
             }
             print $fh "\n";
         }
-    if ($header_flag == 1) { 
-        print $fh "|===\n\n"; 
-        print $fh $report{note};
-        print $fh "\n\n";
-    }
+       if ($header_flag == 1) { 
+               print $fh "|===\n\n"; 
+               print $fh $report{note};
+               print $fh "\n\n";
+       }
+       }
+
+    #print chart 
+       if ($display eq 'pie_chart' or $display eq 'donut_chart') {
+               my @h = split(/\./,$report{heading});
+           my @l = split(/\./,$report{chart_labels});
+       
+               print $fh "++++\n";
+               if (defined $height and defined $width) { print $fh "<div id=\"$rname\" style=\"width: $width; height: $height;\"></div>\n"; }
+                       else { print $fh "<div id=\"$rname\"></div>\n"; }
+               print $fh "<script type=\"text/javascript\">\n";
+               print $fh "google.charts.load('current', {'packages':['corechart']});\n";
+               print $fh "google.charts.setOnLoadCallback(drawChart);\n";
+               print $fh "function drawChart() {\n";
+               print $fh "  var data = google.visualization.arrayToDataTable([\n";
+               #loop through data here 
+               print $fh "['$l[0]', '$l[1]' ],\n";
+               while (my @row = $sth->fetchrow_array) {
+                       my $row_length = @row;
+                       my $r = 1;
+                       while ($r < $row_length) {
+                print $fh "['$h[$r-1]', $row[$r-1] ],\n";
+                $r++;
+            }
+                       if ($r = $row_length) { print $fh "['$h[$r-1]', $row[$r-1] ]\n"; }      
+               }
+               print $fh "]);\n";
+               if ($display eq 'pie_chart') { print $fh "var options = {'title':'$title'};\n"; }
+        if ($display eq 'donut_chart') { print $fh "var options = {'title':'$title', pieHole: 0.4};\n"; }
+               print $fh "var chart = new google.visualization.PieChart(document.getElementById('$rname'));\n";
+        print $fh "chart.draw(data, options);\n";
+               print $fh "}\n";
+               print $fh "</script>\n";
+               print $fh "++++\n";
+       }
+
     print "successfully wrote output for $report{name}.\n\n";
 }