<?php
// $Header: /cvsroot/phptsmadmin/phpTSMadmin/htdocs/summary.gantt.php,v 1.4 2009/04/19 04:00:59 wurley Exp $

# @todo: Hotlink the hostname to the detail script.

# Required Libraries
require './common.php';
$form['from'] = get_request('from','REQUEST',false,date('Y-m-d',time()-86400));
$form['to'] = get_request('to','REQUEST',false,date('Y-m-d'));

# Defaults
$reportEnd = strftime('%Y-%m-%d 23:59',time());
$reportStart = strftime('%Y-%m-%d 00:00',time()-86400);
$summaryInfo = objectCache('summaryinfo');

# List of Clients.
$blockTitle['node'] = sprintf(_('Select DATE RANGE on %s'),$app['server']->getValue('server','name'));

$blockBody['node'] = '<form action="cmd.php">';
$blockBody['node'] .= '<input type="hidden" name="cmd" value="summary.gantt" />';
$blockBody['node'] .= sprintf('<input type="hidden" name="index" value="%s" />',$app['server']->getIndex());

$blockBody['node'] .= 'From ';
$blockBody['node'] .= '<select name="from">';
foreach ($summaryInfo->getSummaryRange() as $date) {
        $blockBody['node'] .= sprintf('<option id="%s" %s>%s</option>',
                $date,
                $date == $form['from'] ? 'selected' : '',
                $date);
}
$blockBody['node'] .= '</select>';
$blockBody['node'] .= '&nbsp;';
$blockBody['node'] .= 'To ';
$blockBody['node'] .= '<select name="to">';
foreach ($summaryInfo->getSummaryRange() as $date) {
        $blockBody['node'] .= sprintf('<option id="%s" %s>%s</option>',
                $date,
                $date == $form['to'] ? 'selected' : '',
                $date);
}
$blockBody['node'] .= '</select>';
$blockBody['node'] .= '<input align="left" type="submit" name="submit" value="Go" />';
$blockBody['node'] .= '</form>';

if ($form['from'] && $form['to']) {
initJPGraph(true);

# A new graph with automatic size
$graph = new GanttGraph(0,0,'auto');

# A new activity on row '0'
$item = 0;
foreach ($summaryInfo->getSummary($form['from'],$form['to']) as $tsmActivity) {
	$tsmCaption = '';
	$tsmCSIM = '';
	$waiting = 0;
	$timestart = preg_replace('/(.*:.*):.*/','$1',$tsmActivity['START_TIME']);
	$timeend = preg_replace('/(.*:.*):.*/','$1',$tsmActivity['END_TIME']);
	$timeseconds = strtotime(preg_replace('/([0-9])\..*$/','$1',$tsmActivity['END_TIME'])) -
		strtotime(preg_replace('/([0-9])\..*$/','$1',$tsmActivity['START_TIME']));
	if ($timeseconds) {
		$waiting = $tsmActivity['MEDIAW']/$timeseconds;
	}

	# It seems sometimes, the MEDIAW time can be larger than the activity time?
	if ($waiting > 1) $waiting = 1;

	$priority = 10;
	switch ($tsmActivity['ACTIVITY']) {
		case('TAPE MOUNT'):
			$summary = $tsmActivity['ACTIVITY'].' ('.$tsmActivity['DRIVE_NAME'].')';
			$tsmCSIM = sprintf('%s (%s)',$tsmActivity['VOLUME_NAME'],$tsmActivity['LAST_USE']);
			$priority = 1;
			break;
		case('STGPOOL BACKUP'):
			$summary = $tsmActivity['ACTIVITY'].' ('.$tsmActivity['ENTITY'].')';
			$tsmCSIM = sprintf('%3d MB. %ss Media wait',$tsmActivity['BYTES']/1024/1024,$tsmActivity['MEDIAW']);
			$priority = 4;
			break;
		case('ARCHIVE'):
			$summary = $tsmActivity['ACTIVITY'].' ('.$tsmActivity['ENTITY'].')';
			$tsmCSIM = sprintf('%3d MB',$tsmActivity['BYTES']/1024/1024);
			$priority = 3;
			break;
		case('BACKUP'):
			$summary = $tsmActivity['ACTIVITY'].' ('.$tsmActivity['ENTITY'].')';
			$tsmCSIM = sprintf('%3d MB',$tsmActivity['BYTES']/1024/1024);
			$priority = 2;
			break;
		case('RESTORE'):
			$summary = $tsmActivity['ACTIVITY'].' ('.$tsmActivity['ENTITY'].')';
			$tsmCSIM = sprintf('%3d MB. %ss Media wait',$tsmActivity['BYTES']/1024/1024,$tsmActivity['MEDIAW']);
			$priority = 2;
			break;
		case('FULL_DBBACKUP'):
			$summary = $tsmActivity['ACTIVITY'];
			$tsmCSIM = sprintf('%3d MB. %ss Media wait',$tsmActivity['BYTES']/1024/1024,$tsmActivity['MEDIAW']);
			break;
		case('RECLAMATION'):
			$summary = $tsmActivity['ACTIVITY'].' ('.preg_replace('/^(.*)\s+\(.*\)$/','$1',$tsmActivity['ENTITY']).')';
			$tsmCSIM = sprintf('%3d MB. %ss Media wait',$tsmActivity['BYTES']/1024/1024,$tsmActivity['MEDIAW']);
			$priority = 4;
			break;
		case('MIGRATION'):
			$summary = $tsmActivity['ACTIVITY'].' ('.$tsmActivity['ENTITY'].')';
			$tsmCSIM = sprintf('%3d MB. %ss Media wait',$tsmActivity['BYTES']/1024/1024,$tsmActivity['MEDIAW']);
			$priority = 4;
			break;
		case('MOVE NODEDATA'):
			$summary = $tsmActivity['ACTIVITY'].' ('.$tsmActivity['ENTITY'].')';
			$tsmCSIM = sprintf('%3d MB. %ss Media wait',$tsmActivity['BYTES']/1024/1024,$tsmActivity['MEDIAW']);
			$priority = 4;
			break;
		case('EXPIRATION'):
			$summary = 'EXPIRATION PROCESSING';
			$tsmCaption = sprintf('%ss',$tsmActivity['AFFECTED']);
		default: $summary = $tsmActivity['ACTIVITY'];
	}

	$lineitems[$summary][$item]['wait'] = $waiting;
	$lineitems[$summary][$item]['summary'] = $summary;
	$lineitems[$summary][$item]['start'] = $timestart;
	$lineitems[$summary][$item]['end'] = $timeend;
	$lineitems[$summary][$item]['csim'] = $tsmCSIM;
	$lineitems[$summary][$item]['caption'] = $tsmCaption;
	$lineitems[$summary]['priority'] = $priority;
	$lineitems[$summary]['summary'] = $summary;
	$item++;
}

if ($lineitems) {
	masort($lineitems,'priority,summary',1);

	$item=0;
	foreach ($lineitems as $lineitem => $linedetails) {

		# Put a gap between our priority items.
		if ($item && ($lastPriority <> $linedetails['priority']))
			$item++;

		foreach ($linedetails as $eventdetails) {
			if (! is_array($eventdetails)) continue;
			$activity = new GanttBar($item,$eventdetails['summary'],$eventdetails['start'],$eventdetails['end']);
			$activity->progress->Set($eventdetails['wait']);
			$activity->caption ->Set($eventdetails['caption']);
			$activity->SetCSIMTarget('#',($eventdetails['csim'] ? $eventdetails['csim'] : 'NoCSIM'));
			$activity->title->SetCSIMTarget('#',($eventdetails['csim'] ? $eventdetails['csim'] : 'NoCSIM'));

			$graph->Add($activity);
		}
		$item++;
		$lastPriority = $linedetails['priority'];
	}

	$graph->SetMarginColor('#eeeeff');
	$graph->SetFrame(true,'#eeeeff',0);

	// We want to display day, hour and minute scales
	$graph->ShowHeaders(GANTT_HDAY | GANTT_HHOUR | GANTT_HMIN);
	$graph->hgrid->Show();
	$graph->hgrid->SetRowFillColor('darkred@0.85');

	// Setup hour format
	$graph->scale->hour->SetIntervall(1);
	$graph->scale->hour->SetStyle(HOURSTYLE_H24);
	$graph->scale->minute->SetIntervall(30);

	$graph->scale->dividerh->SetWeight(3);
	$graph->scale->dividerh->SetColor('navy');

	// Display the Gantt chart
	$imageFile = sprintf('%s/gantt.summary.%s.png',realpath($_SESSION[APPCONFIG]->getValue('image','path')),$app['server']->getIndex());
	$imageHTML = sprintf('%sgantt.summary.%s.png',$_SESSION[APPCONFIG]->getValue('image','pathurl'),$app['server']->getIndex());
	$graph->Stroke($imageFile);

} else {
	# @todo: Nice message about no data.
}

$blockTitle['gantt'] = sprintf(_('Activity Summary for server %s'),$app['server']->getValue('server','name'));
$blockBody['gantt'] = '<table class="result"><tr><td>';
$blockBody['gantt'] .= $graph->GetHTMLImageMap('gantt.summary');
$blockBody['gantt'] .= sprintf('<img src="%s" ISMAP USEMAP="#gantt.summary" border=0 />',$imageHTML,$app['server']->getIndex()) ;
$blockBody['gantt'] .= '</td></tr></table>';
}

# End
render_page($blockTitle,$blockBody);
?>
