<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class provides information on TSM Server.
 *
 * @package    PTA
 * @subpackage Server
 * @category   Controllers
 * @author     Deon George
 * @copyright  (c) 2010 phpTSMadmin Development Team
 * @license    http://phptsmadmin.sf.net/license.html
 */
class Controller_Server extends Controller_TemplateDefault {
	/**
	 * Default Index for Controller
	 */
	public function action_index() {
	}

	public function action_gantt() {
		$end = strftime('%Y-%m-%d 23:59',time());
		$start = strftime('%Y-%m-%d 00:00',time()-86400);

		$ao = ORM::factory('SUMMARY')
			->where_open()
			->where('START_TIME','>=',$start)
			->and_where('END_TIME','>=',$end)
			->where_close()
			->or_where_open()
			->where('END_TIME','<=',$end)
			->and_where('END_TIME','>=',$start)
			->or_where_close();

		$graph = new JPGraph_Gantt;

		foreach ($ao->find_all() as $a) {
			$sort = 10;
			$csim = '';
			$caption = '';
			$wait = 0; // @todo

			switch ($a->ACTIVITY) {
				case('TAPE MOUNT'):
					$summary = sprintf('%s (%s)',$a->ACTIVITY,$a->DRIVE_NAME);
					$csim = sprintf('%s (%s)',$a->VOLUME_NAME,$a->LAST_USE);
					$sort = 1;
					break;

				case('STGPOOL BACKUP'):
					$summary = sprintf('%s (%s)',$a->ACTIVITY,$a->ENTITY);
					$csim = sprintf('%3d MB. %ss Media wait',$a->BYTES/1024/1024,$a->MEDIAW);
					$sort = 4;
					break;

				case('ARCHIVE'):
					$summary = sprintf('%s (%s)',$a->ACTIVITY,$a->ENTITY);
					$csim = sprintf('%3d MB',$a->BYTES/1024/1024);
					$sort = 3;
					break;

				case('BACKUP'):
					$summary = sprintf('%s (%s)',$a->ACTIVITY,$a->ENTITY);
					$csim = sprintf('%3d MB',$a->BYTES/1024/1024);
					$sort = 2;
					break;

				case('RESTORE'):
					$summary = sprintf('%s (%s)',$a->ACTIVITY,$a->ENTITY);
					$csim = sprintf('%3d MB. %ss Media wait',$a->BYTES/1024/1024,$a->MEDIAW);
					$sort = 2;
					break;

				case('RETRIEVE'):
					$summary = sprintf('%s (%s)',$a->ACTIVITY,$a->ENTITY);
					$csim = sprintf('%3d MB. %ss Media wait',$a->BYTES/1024/1024,$a->MEDIAW);
					$sort = 2;
					break;

				case('FULL_DBBACKUP'):
					$summary = $a->ACTIVITY;
					$csim = sprintf('%3d MB. %ss Media wait',$a->BYTES/1024/1024,$a->MEDIAW);
					break;

				case('RECLAMATION'):
					$summary = $a->ACTIVITY.' ('.preg_replace('/^(.*)\s+\(.*\)$/','$1',$a->ENTITY).')';
					$csim = sprintf('%3d MB. %ss Media wait',$a->BYTES/1024/1024,$a->MEDIAW);
					$sort = 4;
					break;

				case('MIGRATION'):
					$summary = $a->ACTIVITY.' ('.$a->ENTITY.')';
					$csim = sprintf('%3d MB. %ss Media wait',$a->BYTES/1024/1024,$a->MEDIAW);
					$sort = 4;
					break;

				case('MOVE NODEDATA'):
					$summary = $a->ACTIVITY.' ('.$a->ENTITY.')';
					$csim = sprintf('%3d MB. %ss Media wait',$a->BYTES/1024/1024,$a->MEDIAW);
					$sort = 4;
					break;

				case('EXPIRATION'):
					$summary = 'EXPIRATION PROCESSING';
					$caption = sprintf('%ss',$a->AFFECTED);

				default:
					$summary = $a->ACTIVITY;
			}

			$graph->add($summary,$a->START_TIME,$a->END_TIME)
				->sort($sort)
				->progress($wait)
				->caption($caption)
				->csim($csim);
		}

		$graph->jpgraph->SetMargin(1,1,1,1);
		$graph->jpgraph->SetMarginColor('#FFFFFF');
		$graph->jpgraph->SetFrame(FALSE,'#FFFFFF',3);
		$graph->jpgraph->SetColor('#F6F6F8');

		// We want to display day, hour and minute scales
		$graph->jpgraph->ShowHeaders(GANTT_HDAY | GANTT_HHOUR | GANTT_HMIN);

		$graph->jpgraph->hgrid->Show();
		$graph->jpgraph->hgrid->SetRowFillColor('#AAAACC@0.85');

		// Setup hour format
		$graph->jpgraph->scale->day->SetFontColor('white');
		$graph->jpgraph->scale->day->SetBackgroundColor('blue');
		$graph->jpgraph->scale->hour->SetFontColor('white');
		$graph->jpgraph->scale->hour->SetBackgroundColor('blue');
		$graph->jpgraph->scale->hour->SetIntervall(1);
		$graph->jpgraph->scale->hour->SetStyle(HOURSTYLE_H24);
		$graph->jpgraph->scale->minute->SetFontColor('white');
		$graph->jpgraph->scale->minute->SetBackgroundColor('blue');
		$graph->jpgraph->scale->minute->SetIntervall(30);

		$url = $graph->draw('gantt');

		Block::add(array(
			'title'=>_('Server Gantt Activity'),
			'body'=>HTML::Image('media/'.$url,array('alt'=>_('Service Gantt Activity'))),
		));
	}

	// @todo Other work in progress
	public function xaction_gantt() {
		$end = date('Y-m-d H:i');
		$start = date('Y-m-d H:i',time()-86400);

		$startActivites = array();
		$startActivities['811'] = array('start'=>'Expire Inventory');
		$startActivities['812'] = array('success'=>'Expire Inventory');
		$startActivities['2562'] = array('start'=>'Event Records Delete');
		$startActivities['2564'] = array('success'=>'Event Records Delete');
		$startActivities['2102'] = array('start'=>'Activity Log Delete');
		$startActivities['2102'] = array('success'=>'Activity Log Delete');
		$startActivities['2280'] = array('start'=>'DB Backup');
		$startActivities['4550'] = array('success'=>'DB Backup');
		$startActivities['406'] = array('start'=>'Session');
		$startActivities['403'] = array('success'=>'Session');

		$ao = ORM::factory('ACTLOG')
			->where('DATE_TIME','>=',$start)
			->and_where('DATE_TIME','<=',$end)
			->and_where('ORIGINATOR','=','SERVER')
			->and_where('MSGNO','in',array_keys($startActivities));

		$activity = array();
		foreach ($ao->find_all() as $a) {
			# New Event.
			if (isset($startActivities[$a->MSGNO]['start'])) {
				$activity[$startActivities[$a->MSGNO]['start']][]['start'] = $a->DATE_TIME;

			} elseif (isset($startActivities[$a->MSGNO]['success'])) {
				$item = (isset($activity[$startActivities[$a->MSGNO]['success']]) ? count($activity[$startActivities[$a->MSGNO]['success']])-1 : 0);

				# Is there a start event.
				if (! isset($activity[$startActivities[$a->MSGNO]['success']][$item]['start']))
					$activity[$startActivities[$a->MSGNO]['success']][$item]['start'] = $a->DATE_TIME;

				$activity[$startActivities[$a->MSGNO]['success']][$item]['end'] = $a->DATE_TIME;
			}
		}
	}
}
?>
