From 4fe0b436d604af6e3d3e8ab4adff69c71f264a91 Mon Sep 17 00:00:00 2001 From: Davide Franco Date: Fri, 8 Jul 2011 19:38:30 +0200 Subject: [PATCH] bacula-web: Improved countJobs() function in bweb class --- gui/bacula-web/includes/bweb.inc.php | 47 ++++++++++++++-------------- gui/bacula-web/index.php | 22 ++++++------- gui/bacula-web/jobs.php | 4 +-- 3 files changed, 35 insertions(+), 38 deletions(-) diff --git a/gui/bacula-web/includes/bweb.inc.php b/gui/bacula-web/includes/bweb.inc.php index a847878117..c1a57c5f1a 100644 --- a/gui/bacula-web/includes/bweb.inc.php +++ b/gui/bacula-web/includes/bweb.inc.php @@ -273,54 +273,55 @@ class Bweb extends DB } - public function CountJobs( $delay = LAST_DAY, $status = 'any' ) + public function countJobs( $start_timestamp, $end_timestamp, $status = 'ALL', $level = 'ALL', $jobname = 'ALL', $client = 'ALL' ) { $query = "SELECT COUNT(JobId) AS job_nb FROM Job "; $where_delay = ""; - $where_status = ""; - // Interval condition for SQL query - if( $delay != ALL ) { - $end_date = mktime(); - $start_date = $end_date - $delay; - - $start_date = date( "Y-m-d H:i:s", $start_date ); - $end_date = date( "Y-m-d H:i:s", $end_date ); - - $where_delay = "WHERE EndTime BETWEEN '$start_date' AND '$end_date' "; + // Calculate sql query interval + $start_date = date( "Y-m-d H:i:s", $start_timestamp); + $end_date = date( "Y-m-d H:i:s", $end_timestamp); + + switch( $this->driver ) + { + case 'sqlite': + case 'mysql': + $query .= "WHERE (EndTime BETWEEN '$start_date' AND '$end_date') "; + break; + case 'pgsql': + $query .= "WHERE (EndTime BETWEEN timestamp '$start_date' AND timestamp '$end_date') "; + break; } - if( $status != 'any' ) { + if( $status != 'ALL' ) { switch( $status ) { case 'completed': - $where_status = "JobStatus = 'T' "; + $query .= " AND JobStatus = 'T' "; break; case 'failed': - $where_status = "JobStatus IN ('f','E') "; + $query .= " AND JobStatus IN ('f','E') "; break; case 'canceled': - $where_status = "JobStatus = 'A' "; + $query .= " AND JobStatus = 'A' "; break; case 'waiting': - $where_status = "JobStatus IN ('F','S','M','m','s','j','c','d','t') "; + $query .= " AND JobStatus IN ('F','S','M','m','s','j','c','d','t') "; break; } // end switch } - if( !empty($where_delay) ) - $query = $query . $where_delay . 'AND ' . $where_status; - else { - if( !empty($where_status) ) - $query = $query . 'WHERE ' . $where_status; + if( $level != 'ALL' ) { + $query .= " AND Level = '$level'"; } - + + // Execute the query $jobs = $this->db_link->query( $query ); if (PEAR::isError( $jobs ) ) { $this->TriggerDBError("Unable to get last $status jobs number from catalog", $jobs); }else { - $jobs = $jobs->fetchRow( DB_FETCHMODE_ASSOC ); + $jobs = $jobs->fetchRow(); return $jobs['job_nb']; } } diff --git a/gui/bacula-web/index.php b/gui/bacula-web/index.php index 762cb52e13..61fa605ee2 100644 --- a/gui/bacula-web/index.php +++ b/gui/bacula-web/index.php @@ -47,19 +47,15 @@ $dbSql->tpl->assign('clientes_totales',$nb_clients["nb_client"] ); // Backup Job list for report.tpl and last_run_report.tpl $dbSql->tpl->assign( 'jobs_list', $dbSql->Get_BackupJob_Names() ); -// Last 24 hours completed jobs number -$dbSql->tpl->assign( 'completed_jobs', $dbSql->CountJobs( LAST_DAY, 'completed' ) ); - -// Last 24 hours failed jobs number -$dbSql->tpl->assign( 'failed_jobs', $dbSql->CountJobs( LAST_DAY, 'failed' ) ); - -// Last 24 hours waiting jobs number -$dbSql->tpl->assign( 'waiting_jobs', $dbSql->CountJobs( LAST_DAY, 'waiting' ) ); - -// Last 24 hours Job Levels -$dbSql->tpl->assign( 'incr_jobs', $dbSql->CountJobsbyLevel( LAST_DAY, 'I') ); -$dbSql->tpl->assign( 'diff_jobs', $dbSql->CountJobsbyLevel( LAST_DAY, 'D') ); -$dbSql->tpl->assign( 'full_jobs', $dbSql->CountJobsbyLevel( LAST_DAY, 'F') ); +// Last 24 hours status (completed, failed and waiting jobs) +$dbSql->tpl->assign( 'completed_jobs', $dbSql->countJobs( NOW-DAY, NOW, 'completed' ) ); +$dbSql->tpl->assign( 'failed_jobs', $dbSql->countJobs( NOW-DAY, NOW, 'failed' ) ); +$dbSql->tpl->assign( 'waiting_jobs', $dbSql->countJobs( NOW-DAY, NOW, 'waiting' ) ); + +// Last 24 hours jobs Level +$dbSql->tpl->assign( 'incr_jobs', $dbSql->countJobs( NOW-DAY, NOW, 'ALL', J_INCR) ); +$dbSql->tpl->assign( 'diff_jobs', $dbSql->countJobs( NOW-DAY, NOW, 'ALL', J_DIFF) ); +$dbSql->tpl->assign( 'full_jobs', $dbSql->countJobs( NOW-DAY, NOW, 'ALL', J_FULL) ); // Last 24 hours Job status graph $data = array(); diff --git a/gui/bacula-web/jobs.php b/gui/bacula-web/jobs.php index c54dc2c9ef..5dbb2acf5d 100644 --- a/gui/bacula-web/jobs.php +++ b/gui/bacula-web/jobs.php @@ -144,9 +144,9 @@ // Count jobs if( isset( $_POST['status'] ) ) - $total_jobs = $dbSql->CountJobs( ALL, $_POST['status'] ); + $total_jobs = $dbSql->countJobs( FIRST_DAY, NOW, $_POST['status'] ); else - $total_jobs = $dbSql->CountJobs( ALL ); + $total_jobs = $dbSql->countJobs( FIRST_DAY, NOW ); $dbSql->tpl->assign( 'total_jobs', $total_jobs ); -- 2.39.5