#!/usr/bin/perl -w # -*- mode: cperl; c-basic-offset: 8; indent-tabs-mode: nil; -*- # ***** BEGIN LICENSE BLOCK ***** # Version: MPL 1.1 # # The contents of this file are subject to the Mozilla Public License Version # 1.1 (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # http://www.mozilla.org/MPL/ # # Software distributed under the License is distributed on an "AS IS" basis, # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License # for the specific language governing rights and limitations under the # License. # # The Original Code is Litmus. # # The Initial Developer of the Original Code is # the Mozilla Corporation. # Portions created by the Initial Developer are Copyright (C) 2006 # the Initial Developer. All Rights Reserved. # # Contributor(s): # Chris Cooper # # ***** END LICENSE BLOCK ***** use strict; $|++; use lib qw(..); use Date::Manip; use Getopt::Long; use Litmus::DBI; use Litmus::Config; use Litmus::Mailer qw( sendMessage ); use Litmus::DB::Testcase; use vars qw( $litmus_dbh ); END { if ($litmus_dbh) { $litmus_dbh->disconnect; } } $litmus_dbh = Litmus::DBI->db_ReadOnly() or die; my $help; my $html_mail; my $batch; GetOptions('help|?' => \$help,'html' => \$html_mail,'batch' => \$batch); if ($help) { &usage; exit; } my ($sql,$sth); $sql= qq{ SELECT tr.testresult_id, pr.name AS product, br.name AS branch, pl.name AS platform, o.name AS opsys, u.email, tc.summary FROM test_results tr, platforms pl, opsyses o, products pr, branches br, users u, testcases tc WHERE tr.result_status_id=? AND tr.valid=1 AND tr.submission_time>=DATE_SUB(NOW(), INTERVAL 1 DAY) AND tr.testcase_id=tc.testcase_id AND tr.branch_id=br.branch_id AND tr.opsys_id=o.opsys_id AND pl.platform_id=o.platform_id AND tc.product_id=pr.product_id AND tr.user_id=u.user_id AND (pr.product_id = 1 OR pr.product_id=3) ORDER BY pr.name ASC, br.name ASC, pl.name ASC, o.name ASC; }; $sth = $litmus_dbh->prepare($sql); $sth->execute(2); my @failed_results; while (my $hashref = $sth->fetchrow_hashref) { $hashref->{'branch'} =~ s/ Branch//; push @failed_results, $hashref; } my $combined_message_body = ""; my $failed_results_message_body = ""; if (scalar @failed_results > 0) { $failed_results_message_body = &format_results(\@failed_results, 'Failed', $html_mail); } else { # No Failed Results today. } if ($failed_results_message_body ne "") { if (!$batch) { my $title = 'Failed results submitted to Litmus in the past day'; my $rv = &create_and_send_message($title, $failed_results_message_body, $html_mail); } else { $combined_message_body .= $failed_results_message_body; } } $sth->execute(3); my @unclear_results; while (my $hashref = $sth->fetchrow_hashref) { $hashref->{'branch'} =~ s/ Branch//; push @unclear_results, $hashref; } $sth->finish; my $unclear_results_message_body = ""; if (scalar @unclear_results > 0) { $unclear_results_message_body = &format_results(\@unclear_results, 'Unclear', $html_mail); } else { # No Unclear Results today. } if ($unclear_results_message_body ne "") { if (!$batch) { my $title = 'Unclear results submitted to Litmus in the past day'; my $rv = &create_and_send_message($title, $unclear_results_message_body, $html_mail); } else { $combined_message_body .= $unclear_results_message_body; } } my $match_limit = 100; my @added_testcases = Litmus::DB::Testcase->getNewTestcases( 1, $match_limit ); my $added_testcases_message_body = ""; if (scalar @added_testcases > 0) { $added_testcases_message_body = &format_testcases(\@added_testcases, 'Added to', $html_mail); } else { # No testcases added today. } if ($added_testcases_message_body ne "") { if (!$batch) { my $title = 'Testcases added to Litmus in the past day'; my $rv = &create_and_send_message($title, $added_testcases_message_body, $html_mail); } else { $combined_message_body .= $added_testcases_message_body; } } my @changed_testcases = Litmus::DB::Testcase->getRecentlyUpdated( 1, $match_limit ); my $changed_testcases_message_body = ""; if (scalar @changed_testcases > 0) { $changed_testcases_message_body = &format_testcases(\@changed_testcases, 'Changed in', $html_mail); } else { # No testcases added today. } if ($changed_testcases_message_body ne "") { if (!$batch) { my $title = 'Testcases changed in Litmus in the past day'; my $rv = &create_and_send_message($title, $changed_testcases_message_body, $html_mail); } else { $combined_message_body .= $changed_testcases_message_body; } } if ($batch and $combined_message_body ne "") { my $title = ''; my $rv = &create_and_send_message($title, $combined_message_body, $html_mail); } exit; ######################################################################### sub usage { print "\nUsage: daily_report.pl [--html]\n\n"; } ######################################################################### sub message_header($$) { my ($title,$html_mail) = @_; my $today = &Date::Manip::UnixDate("now","%Y/%m/%d"); my $subject = "[litmus] Daily Report"; if ($title) { $subject .= " - $title"; } $subject .= " - $today"; my $header .= "Subject: $subject\n"; $header .= "Content-Type: text/html\n"; $header .= "To: " . join(',',@Litmus::Config::nightly_report_recipients) . "\n\n"; if ($html_mail) { $header .= ' '; $header .= $subject; $header .= ' '; } return $header; } ######################################################################### sub message_footer($) { my ($html_mail) = @_; my $footer = ""; if ($html_mail) { $footer = "

Visit Litmus: http:/litmus.mozilla.org/

\n"; $footer .= "\n\n\n"; } else { $footer = "Visit Litmus: http:/litmus.mozilla.org/\n\n"; } return $footer; } ######################################################################### sub format_results(\@$$) { my ($results, $result_type, $html_mail) = @_; my $formatted_results = ""; if ($html_mail) { $formatted_results = "

${result_type} results submitted to Litmus in the past day:

"; $formatted_results .= ' '; my $class='odd'; foreach my $hashref (@$results) { if ($class eq 'odd') { $class = 'even'; } else { $class = 'odd'; } $formatted_results .= '' . "\n"; $formatted_results .= '\n"; $formatted_results .= '\n"; $formatted_results .= '\n"; $formatted_results .= '\n"; $formatted_results .= '\n"; $formatted_results .= '\n"; $formatted_results .= '\n"; $formatted_results .= "\n"; } $formatted_results .= "
ID Product Branch Platform Opsys Tester Testcase Summary
' . $hashref->{'testresult_id'} . "' . $hashref->{'product'} . "' . $hashref->{'branch'} . "' . $hashref->{'platform'} . "' . $hashref->{'opsys'} . "' . $hashref->{'email'} . "' . $hashref->{'summary'} . "
\n
\n"; } else { $formatted_results .= "${result_type} results submitted to Litmus in the past day:\n\n"; my $header = sprintf( "%-8s %-8s %-6s %-8s %-8s %-15s %-20s", "ID", "Product", "Branch", "Platform", "Opsys", "Tester", "Testcase Summary" ); $formatted_results .= "$header\n"; foreach my $hashref (@$results) { my $result_link = sprintf("%-8d", $hashref->{'testresult_id'}, $hashref->{'testresult_id'} ); $formatted_results .= $result_link; my $result = sprintf( " %-8.8s %-6.6s %-8.8s %-8.8s %-15.15s %-20.20s", $hashref->{'product'}, $hashref->{'branch'}, $hashref->{'platform'}, $hashref->{'opsys'}, $hashref->{'email'}, $hashref->{'summary'} ); $formatted_results .= "$result\n"; } $formatted_results .= "\n\n"; } return $formatted_results; } ######################################################################## sub format_testcases(\@$$) { my ($testcases, $testcase_type, $html_mail) = @_; my $formatted_testcases = ""; if ($html_mail) { $formatted_testcases = "

Testcases $testcase_type Litmus in the past day:

"; $formatted_testcases .= ' '; my $class='odd'; foreach my $hashref (@$testcases) { if ($class eq 'odd') { $class = 'even'; } else { $class = 'odd'; } $formatted_testcases .= '' . "\n"; $formatted_testcases .= '\n"; $formatted_testcases .= '\n"; $formatted_testcases .= "\n"; } $formatted_testcases .= "
ID Summary
' . $hashref->{'testcase_id'} . "' . $hashref->{'summary'} . "
\n
\n"; } else { $formatted_testcases .= "${testcase_type} Litmus in the past day:\n\n"; my $header = sprintf( "%-8s %-20s", "ID", "Summary" ); $formatted_testcases .= "$header\n"; foreach my $hashref (@$testcases) { my $result_link = sprintf("%-8d", $hashref->{'testcase_id'}, $hashref->{'testcase_id'} ); $formatted_testcases .= $result_link; my $testcase = sprintf( " %-20.20s", $hashref->{'summary'} ); $formatted_testcases .= "$testcase\n"; } $formatted_testcases .= "\n\n"; } return $formatted_testcases; } ######################################################################### sub create_and_send_message($$$) { my($title,$message_body,$html_mail) = @_; my $message_header = &message_header($title, $html_mail); my $message_footer = &message_footer($html_mail); my $message = $message_header . $message_body . $message_footer; my $rv = sendMessage($message); if (!$rv) { warn('[litmus] FAIL - Unable to send daily report - ' . $title); } return $rv; }