From 068ab478541b65ac4a2c4e6c652360f5135d7f4f Mon Sep 17 00:00:00 2001 From: miker Date: Mon, 2 Oct 2006 08:58:26 +0000 Subject: [PATCH] adding email support and requiring basic HTML output git-svn-id: svn://svn.open-ils.org/ILS/trunk@6347 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Evergreen/src/extras/report-fail | 21 +++++ Evergreen/src/extras/report-success | 16 ++++ Open-ILS/src/reporter/clark-kent.pl | 135 ++++++++++++++++++++++------ 3 files changed, 143 insertions(+), 29 deletions(-) create mode 100644 Evergreen/src/extras/report-fail create mode 100644 Evergreen/src/extras/report-success diff --git a/Evergreen/src/extras/report-fail b/Evergreen/src/extras/report-fail new file mode 100644 index 0000000000..df3f4d1a93 --- /dev/null +++ b/Evergreen/src/extras/report-fail @@ -0,0 +1,21 @@ +To: {TO} +From: {FROM} +Reply-To: {REPLY_TO} +Subject: Report {REPORT_NAME} failed + +Your report, {REPORT_NAME}, schedued to run at {RUN_TIME} has failed with +the following error message: + +{ERROR_TEXT} + +The SQL command attempted was: + +{SQL} + +If you are unsure of the meaning of this message, please contact PINES staff +and give them both the error message and the SQL command. + +Thanks, + +Clark Kent, your friendly neighorhood reporter. + diff --git a/Evergreen/src/extras/report-success b/Evergreen/src/extras/report-success new file mode 100644 index 0000000000..17ee2e9af7 --- /dev/null +++ b/Evergreen/src/extras/report-success @@ -0,0 +1,16 @@ +To: {TO} +From: {FROM} +Reply-To: {REPLY_TO} +Subject: Report {REPORT_NAME} complete + +Your report, {REPORT_NAME}, schedued to run at {RUN_TIME} completed +at {COMPLETE_TIME} and is available for viewing a the following URL: + +{OUTPUT_URL} + +If you have any general questions, please contact the PINES staff. + +Thanks, + +Clark Kent, your friendly neighorhood reporter. + diff --git a/Open-ILS/src/reporter/clark-kent.pl b/Open-ILS/src/reporter/clark-kent.pl index c0e5ab83ea..3799a1c5b1 100755 --- a/Open-ILS/src/reporter/clark-kent.pl +++ b/Open-ILS/src/reporter/clark-kent.pl @@ -24,6 +24,7 @@ use GD::Graph::pie; use GD::Graph::bars3d; use GD::Graph::lines3d; use Tie::IxHash; +use Email::Send; use open ':utf8'; @@ -53,8 +54,15 @@ my $db_name = $sc->config_value( reporter => setup => database => 'name' ); my $db_user = $sc->config_value( reporter => setup => database => 'user' ); my $db_pw = $sc->config_value( reporter => setup => database => 'password' ); +my $email_server = $sc->config_value( email_notify => 'smtp_server' ); +my $email_sender = $sc->config_value( email_notify => 'sender_address' ); +my $success_template = $sc->config_value( reporter => setup => files => 'success_template' ); +my $fail_template = $sc->config_value( reporter => setup => files => 'fail_template' ); + my $output_base = $sc->config_value( reporter => setup => files => 'output_base' ); +my $base_uri = $sc->config_value( reporter => setup => 'base_uri' ); + my $dsn = "dbi:" . $db_driver . ":dbname=" . $db_name .';host=' . $db_host . ';port=' . $db_port; my ($dbh,$running,$sth,@reports,$run, $current_time); @@ -186,10 +194,7 @@ for my $r ( @reports ) { build_excel("$output_dir/report-data.xls", $r); } - if ( $r->{html_format} ) { - mkdir("$output_dir/html"); - build_html("$output_dir/report-data.html", $r); - } + build_html("$output_dir/report-data.html", $r); $dbh->begin_work; @@ -236,9 +241,20 @@ for my $r ( @reports ) { $dbh->commit; + my $new_r = $dbh->selectrow_hashref(<<" SQL", {}, $r->{id}); + SELECT * FROM reporter.schedule WHERE id = ?; + SQL + + $r->{start_time} = $new_r->{start_time}; + $r->{complete_time} = $new_r->{complete_time}; + + if ($r->{email}) { + send_success($r); + } } otherwise { my $e = shift; + $r->{error_text} = ''.$e; $dbh->rollback; $dbh->do(<<' SQL',{}, $e, $r->{id}); UPDATE reporter.schedule @@ -247,6 +263,18 @@ for my $r ( @reports ) { error_code = 1 WHERE id = ?; SQL + + my $new_r = $dbh->selectrow_hashref(<<" SQL", {}, $r->{id}); + SELECT * FROM reporter.schedule WHERE id = ?; + SQL + + $r->{error_text} = $new_r->{error_text}; + $r->{complete_time} = $new_r->{complete_time}; + + if ($r->{email}) { + send_fail($r); + } + }; $dbh->disconnect; @@ -263,6 +291,51 @@ if ($daemon) { #------------------------------------------------------------------- +sub send_success { + my $r = shift; + open F, $success_template; + my $tmpl = join('',); + close F; + + my $url = $base_uri . '/' . + $r->{report}->{template}->{id} . '/' . + $r->{report}->{id} . '/' . + $r->{id} . '/report-data.html'; + + $tmpl =~ s/{TO}/$r->{email}/smog; + $tmpl =~ s/{FROM}/$email_sender/smog; + $tmpl =~ s/{REPLY_TO}/$email_sender/smog; + $tmpl =~ s/{REPORT_NAME}/$r->{report}->{template}->{name} -- $r->{report}->{name}/smog; + $tmpl =~ s/{RUN_TIME}/$r->{run_time}/smog; + $tmpl =~ s/{COMPLETE_TIME}/$r->{complete_time}/smog; + $tmpl =~ s/{OUTPUT_URL}/$url/smog; + + my $sender = Email::Send->new({mailer => 'SMTP'}); + $sender->mailer_args([Host => $email_server]); + $sender->send($tmpl); +} + +sub send_fail { + my $r = shift; + open F, $success_template; + my $tmpl = join('',); + close F; + + my $sql = $r->{resultset}->toSQL; + + $tmpl =~ s/{TO}/$r->{email}/smog; + $tmpl =~ s/{FROM}/$email_sender/smog; + $tmpl =~ s/{REPLY_TO}/$email_sender/smog; + $tmpl =~ s/{REPORT_NAME}/$r->{report}->{template}->{name} -- $r->{report}->{name}/smog; + $tmpl =~ s/{RUN_TIME}/$r->{run_time}/smog; + $tmpl =~ s/{ERROR_TEXT}/$r->{error_text}/smog; + $tmpl =~ s/{SQL}/$sql/smog; + + my $sender = Email::Send->new({mailer => 'SMTP'}); + $sender->mailer_args([Host => $email_server]); + $sender->send($tmpl); +} + sub build_csv { my $file = shift; my $r = shift; @@ -300,7 +373,6 @@ sub build_html { my $r = shift; my $index = new FileHandle (">$file"); - my $raw = new FileHandle (">$file.raw.html"); # index header print $index <<" HEADER"; @@ -320,40 +392,45 @@ sub build_html { $$r{report}{description}


HEADER - + my @links; + # add a link to the raw output html - print $index "Tabular Output"; + push @links, "Tabular Output" if ($r->{html_format}); # add a link to the CSV output - print $index " -- CSV Output" if ($r->{csv_format}); + push @links, "Excel Output" if ($r->{excel_format}); # add a link to the CSV output - print $index " -- Excel Output" if ($r->{excel_format}); + push @links, "CSV Output" if ($r->{csv_format}); + print $index join(' -- ', @links); print $index "



"; - # create the raw output html file - print $raw "$$r{report}{name}"; - - print $raw <<' CSS'; - - CSS - - print $raw ""; - - { no warnings; - print $raw "'; - print $raw "' for (@{$r->{data}}); - } + if ($r->{html_format}) { + # create the raw output html file + my $raw = new FileHandle (">$file.raw.html"); + print $raw "$$r{report}{name}"; + + print $raw <<' CSS'; + + CSS + + print $raw "
".join('',@{$r->{column_labels}}).'
".join('',@$_ ).'
"; + + { no warnings; + print $raw "'; + print $raw "' for (@{$r->{data}}); + } - print $raw '
".join('',@{$r->{column_labels}}).'
".join('',@$_ ).'
'; + print $raw ''; - $raw->close; + $raw->close; + } # Time for a pie chart if ($r->{chart_pie}) { -- 2.43.2