]> git.sur5r.net Git - bacula/bacula/blobdiff - gui/bacula-web/jobs.php
bacula-web: php code cleanup
[bacula/bacula] / gui / bacula-web / jobs.php
index a16564109046a87be93c54b755a4c00364584d44..c54dc2c9efe37c37aef90bcf1a3db69aed27de2e 100644 (file)
 <?php
+/* 
++-------------------------------------------------------------------------+
+| Copyright 2010-2011, Davide Franco                                             |
+|                                                                         |
+| This program is free software; you can redistribute it and/or           |
+| modify it under the terms of the GNU General Public License             |
+| as published by the Free Software Foundation; either version 2          |
+| of the License, or (at your option) any later version.                  |
+|                                                                         |
+| This program is distributed in the hope that it will be useful,         |
+| but WITHOUT ANY WARRANTY; without even the implied warranty of          |
+| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
+| GNU General Public License for more details.                            |
++-------------------------------------------------------------------------+ 
+*/
   session_start();
-  require_once ("paths.php");
-  require_once ($smarty_path."Smarty.class.php");
-  require_once ("classes.inc.php");
-  require_once ("config.inc.php");  
+  include_once( 'config.inc.php' );
 
-  $smarty = new Smarty();     
   $dbSql = new Bweb();
-
-  require("lang.php");
-
-  // Smarty configuration
-  $smarty->compile_check = true;
-  $smarty->debugging = false;
-  $smarty->force_compile = true;
-
-  $smarty->template_dir = "./templates";
-  $smarty->compile_dir = "./templates_c";
-  $smarty->config_dir     = "./configs";
+  // Jobs list
+  $query           = "";
+  $last_jobs   = array();
   
-  // Get the last 10 failed jobs
-  $query          = "";
-  $failed_jobs = array();
+  // Job Status list
+  define( 'STATUS_ALL',                  0 );
+  define( 'STATUS_RUNNING',   1 );
+  define( 'STATUS_WAITING',   2 );
+  define( 'STATUS_COMPLETED', 3 );
+  define( 'STATUS_FAILED',       4 );
+  define( 'STATUS_CANCELED',  5 );
   
-  switch( $dbSql->driver ) 
-  {
-       case 'mysql':
-               $query  = "SELECT SEC_TO_TIME( UNIX_TIMESTAMP(Job.EndTime)-UNIX_TIMESTAMP(Job.StartTime) ) AS elapsed, Job.JobId, Job.Name AS job_name, Job.StartTime, Job.EndTime, Job.Level, Pool.Name AS pool_name, Job.JobStatus ";
-               $query .= "FROM Job ";
-               $query .= "LEFT JOIN Pool ON Job.PoolId=Pool.PoolId ";
-               $query .= "WHERE Job.JobStatus = 'f' ";
-               //$query .= "WHERE Job.EndTime BETWEEN <= NOW() and UNIX_TIMESTAMP(EndTime) >UNIX_TIMESTAMP(NOW())-86400 ";
-               $query .= "ORDER BY Job.EndTime DESC ";  
-               $query .= "LIMIT 10";
-       break;
-       
-       case 'pgsql':
-               $query  = "SELECT (Job.EndTime - Job.StartTime ) AS elapsed, Job.Name, Job.StartTime, Job.EndTime, Job.Level, Pool.Name, Job.JobStatus ";
-               $query .= "FROM Job ";
-               $query .= "LEFT JOIN Pool ON Job.PoolId=Pool.PoolId ";
-               $query .= "WHERE Job.JobStatus = 'f' ";
-               //$query .= "WHERE EndTime <= NOW() and EndTime > NOW() - 86400 * interval '1 second' AND ";
-               $query .= "ORDER BY Job.EndTime DESC";
-               $query .= "LIMIT 10";
-       break;
-  }
-  $jobsresult = $dbSql->db_link->query( $query );
+  $job_status = array( STATUS_ALL       => 'All', 
+                                          STATUS_RUNNING   => 'Running', 
+                                          STATUS_WAITING   => 'Waiting',
+                                          STATUS_COMPLETED => 'Completed',
+                                          STATUS_FAILED        => 'Failed',
+                                          STATUS_CANCELED      => 'Canceled' );
+                                          
+  $dbSql->tpl->assign( 'job_status', $job_status );
   
-  if( PEAR::isError( $jobsresult ) ) {
-         echo "SQL query = $query <br />";
-         die("Unable to get last failed jobs from catalog" . $jobsresult->getMessage() );
-  }else {
-         while( $job = $jobsresult->fetchRow( DB_FETCHMODE_ASSOC ) ) {
-               array_push( $failed_jobs, $job);
-         }
+  // Global variables
+  $job_level = array( 'D' => 'Diff', 'I' => 'Incr', 'F' => 'Full' );
+  
+  $query .= "SELECT Job.JobId, Job.Name AS Job_name, Job.StartTime, Job.EndTime, Job.Level, Job.JobBytes, Job.JobFiles, Pool.Name, Job.JobStatus, Pool.Name AS Pool_name, Status.JobStatusLong ";
+  $query .= "FROM Job ";
+  $query .= "LEFT JOIN Pool ON Job.PoolId=Pool.PoolId ";
+  $query .= "LEFT JOIN Status ON Job.JobStatus = Status.JobStatus ";
+  
+  // Filter by status
+  if( isset( $_POST['status'] ) ) {
+       switch( $_POST['status'] ) 
+       {
+               case STATUS_RUNNING:
+                       $query .= "WHERE Job.JobStatus = 'R' ";
+               break;
+               case STATUS_WAITING:
+                       $query .= "WHERE Job.JobStatus IN ('F','S','M','m','s','j','c','d','t','p','C') ";
+               break;
+               case STATUS_COMPLETED:
+                       $query .= "WHERE Job.JobStatus IN ('T', 'E') ";
+               break;
+               case STATUS_FAILED:
+                       $query .= "WHERE Job.JobStatus = 'f' ";
+               break;
+               case STATUS_CANCELED:
+                       $query .= "WHERE Job.JobStatus = 'A' ";
+               break;
+       }
+    $dbSql->tpl->assign('job_status_filter', $_POST['status'] );
   }
-  $smarty->assign( 'failed_jobs', $failed_jobs );
   
-  // Get the last completed jobs (last 24 hours)
-  $query          = "";
-  $completed_jobs = array();
+  // order by
+  $query .= "ORDER BY Job.JobId DESC ";
   
-  // Interval calculation
-  $end_date   = mktime();
-  $start_date = $end_date - LAST_DAY;
-                       
-  $start_date = date( "Y-m-d H:m:s", $start_date );
-  $end_date   = date( "Y-m-d H:m:s", $end_date );
+  // Jobs per page
+  $jobs_per_page = array( 25 => '25', 50 => '50', 75 => '75', 100 => '100', 150 => '150' );
+    
+  // Determine how many jobs to display
+  if( isset($_POST['jobs_per_page']) ) {
+       $query .= "LIMIT " . $_POST['jobs_per_page'];
+    $dbSql->tpl->assign( 'jobs_per_page_selected', $_POST['jobs_per_page'] );
+  }else
+       $query .= "LIMIT 25 ";
+  $dbSql->tpl->assign( 'jobs_per_page', $jobs_per_page );
   
-  switch( $dbSql->driver ) 
-  {
-       case 'mysql':
-               $query  = "SELECT SEC_TO_TIME( UNIX_TIMESTAMP(Job.EndTime)-UNIX_TIMESTAMP(Job.StartTime) ) AS elapsed, Job.JobId, Job.Name AS job_name, Job.StartTime, Job.EndTime, Job.Level, Pool.Name AS pool_name, Job.JobStatus ";
-               $query .= "FROM Job ";
-               $query .= "LEFT JOIN Pool ON Job.PoolId=Pool.PoolId ";
-               $query .= "WHERE Job.JobStatus = 'T' AND ";
-               $query .= "Job.EndTime BETWEEN '$start_date' AND '$end_date' ";
-               $query .= "ORDER BY Job.EndTime DESC ";  
-               
-       break;
-       
-       case 'pgsql':
-               $query  = "SELECT (Job.EndTime - Job.StartTime ) AS elapsed, Job.Name, Job.StartTime, Job.EndTime, Job.Level, Pool.Name, Job.JobStatus ";
-               $query .= "FROM Job ";
-               $query .= "LEFT JOIN Pool ON Job.PoolId=Pool.PoolId ";
-               $query .= "WHERE EndTime BETWEEN '$start_date' and '$end_date' ";
-               $query .= "ORDER BY Job.EndTime DESC";
-       break;
-  }
   $jobsresult = $dbSql->db_link->query( $query );
   
   if( PEAR::isError( $jobsresult ) ) {
-         echo "SQL query = $query <br />";
-         die("Unable to get last failed jobs from catalog" . $jobsresult->getMessage() );
+         $dbSql->TriggerDBError( "Unable to get last failed jobs from catalog", $jobresult );
   }else {
          while( $job = $jobsresult->fetchRow( DB_FETCHMODE_ASSOC ) ) {
-               array_push( $completed_jobs, $job);
+               
+               // Determine icon for job status
+               switch( $job['jobstatus'] ) {
+                       case J_RUNNING:
+                               $job['Job_icon'] = "running.png";
+                       break;
+                       case J_COMPLETED:
+                               $job['Job_icon'] = "ok.png";
+                       break;
+                       case J_CANCELED:
+                               $job['Job_icon'] = "canceled.png";
+                       break;
+                       case J_COMPLETED_ERROR:
+                               $job['Job_icon'] = "warning.png";
+                       break;
+                       case J_FATAL:
+                               $job['Job_icon'] = "error.png";
+                       break;
+                       case J_WAITING_CLIENT:
+                       case J_WAITING_SD:
+                       case J_WAITING_MOUNT_MEDIA:
+                       case J_WAITING_NEW_MEDIA:
+                       case J_WAITING_STORAGE_RES:
+                       case J_WAITING_JOB_RES:
+                       case J_WAITING_CLIENT_RES:
+                       case J_WAITING_MAX_JOBS:
+                       case J_WAITING_START_TIME:
+                       case J_NOT_RUNNING:
+                               $job['Job_icon'] = "waiting.png";
+                       break;
+               } // end switch
+               
+               // Odd or even row
+               if( count($last_jobs) % 2)
+                       $job['Job_classe'] = 'odd';
+               
+               // Elapsed time for the job
+               $start = $job['starttime'];
+               $end   = $job['endtime'];
+               
+               // Job execution execution time
+               $job['elapsed_time'] = CTimeUtils::Get_Elapsed_Time( $start, $end);
+               // Job Level
+        $job['Level'] = $job_level[ $job['level'] ];
+               // Job Size
+               $job['jobbytes'] = CUtils::Get_Human_Size( $job['jobbytes'] );
+
+               array_push( $last_jobs, $job);
          }
   }
-  $smarty->assign( 'completed_jobs', $completed_jobs );
+  $dbSql->tpl->assign( 'last_jobs', $last_jobs );
+  
+  // Count jobs
+  if( isset( $_POST['status'] ) )
+       $total_jobs = $dbSql->CountJobs( ALL, $_POST['status'] );
+  else
+       $total_jobs = $dbSql->CountJobs( ALL );
+  
+  $dbSql->tpl->assign( 'total_jobs', $total_jobs );
   
-  $smarty->display('jobs.tpl');
+  // Process and display the template 
+  $dbSql->tpl->display('jobs.tpl');
 ?>