<?php
// $Header: /cvsroot/phptsmadmin/phpTSMadmin/htdocs/server.stats.php,v 1.6 2009/04/19 04:00:59 wurley Exp $

# Required Libraries
require './common.php';

$summaryInfo = objectCache('summaryinfo');

if (! isset($_REQUEST['mediaactions'])) {
	$blockTitle['form'] = _('Graph Media Statistics.');
	$blockBody['form'] = 'Please select the stats you would like to graph.';

	$blockBody['form'] .= '<form action="cmd.php">';
	$blockBody['form'] .= sprintf('<input type="hidden" name="cmd" value="%s" />',get_request('cmd','REQUEST'));
	$blockBody['form'] .= '<select multiple size=10 name="mediaactions[]">';
	foreach ($summaryInfo->mediaActions(true) as $action) {
		$blockBody['form'] .= sprintf('<option value="%s">%s</option>',$action,$action);
	}
	$blockBody['form'] .= '</select><br />';
	$blockBody['form'] .= '<input align="left" type="submit" name="submit" value="Go" />';
	$blockBody['form'] .= sprintf('<input type="hidden" name="index" value="%s" />',$app['server']->getIndex());
	$blockBody['form'] .= '</form>';

} else {
	$mediawait = $summaryInfo->mediaActions();
	$graph['mediawait']['total'] = 0;
	$graph['mediawait']['count'] = 0;
	$graph['thruput']['total'] = 0;
	$graph['thruput']['count'] = 0;
	$mode = array();

	foreach ($_REQUEST['mediaactions'] as $action) {
		if (isset($mediawait[$action]))
			foreach ($mediawait[$action] as $index => $detail) {
				$start = strtotime(tsmDate($detail['START_TIME'],'nomsec'));
				$end = strtotime(tsmDate($detail['END_TIME'],'nosec'));
				$graph['mediawait']['data'][$detail['ACTIVITY']][$end] = $detail['MEDIAW'];

				$graph['mediawait']['total'] += $detail['MEDIAW'];
				$graph['mediawait']['count']++;
				@$mode[$detail['MEDIAW']]++;

				$graph['thruput']['data'][$detail['ACTIVITY']][$end] = $detail['BYTES']/1024/($end+.1-$start);
			}

	# Drop the first and last entry
	ksort($mode);
	array_pop($mode);
	$total = 0;
	$counter = 0;
	foreach ($mode as $index => $value) {
		if ($counter) {
			$total += $index * $value;
		}
		$counter++;
	}
	if ($counter)
		$graph['mediawait']['mode'] = $total/$counter;

	# Save Session
	$_SESSION['graph'] = $graph;

	# Media Wait Graph
	$blockTitle['mediawait'] = sprintf(_('Migration Information for %s (%s) %s.%s.%s.%s'),
		$app['server']->GetStatusDetail('SERVER_NAME'),
		$app['server']->GetStatusDetail('PLATFORM'),
		$app['server']->GetStatusDetail('VERSION'),
		$app['server']->GetStatusDetail('RELEASE'),
		$app['server']->GetStatusDetail('LEVEL'),
		$app['server']->GetStatusDetail('SUBLEVEL'));

	$blockBody['mediawait'] = '<table class="blockcitem">';
	$blockBody['mediawait'] .= sprintf('<tr><th colspan=2 class="title">%s</th></tr>',_('Media Wait Graph'));
	$blockBody['mediawait'] .= '<tr><td><table class="blockcitem" border=0>';
	$blockBody['mediawait'] .= '<tr><td colspan=2>&nbsp;</td></tr>';
	$blockBody['mediawait'] .= '</table></td>';
	$blockBody['mediawait'] .= sprintf('<td align="right"><img src="image.server.stats.php?index=%s&activity=mediawait" /></td></tr>',
		$app['server']->getIndex());
	$blockBody['mediawait'] .= '</table>';

	# Thruput Graph
	$blockTitle['thruput'] = sprintf(_('Server Thruput for %s (%s) %s.%s.%s.%s'),
		$app['server']->GetStatusDetail('SERVER_NAME'),
		$app['server']->GetStatusDetail('PLATFORM'),
		$app['server']->GetStatusDetail('VERSION'),
		$app['server']->GetStatusDetail('RELEASE'),
		$app['server']->GetStatusDetail('LEVEL'),
		$app['server']->GetStatusDetail('SUBLEVEL'));

	$blockBody['thruput'] = '<table class="blockcitem">';
	$blockBody['thruput'] .= sprintf('<tr><th colspan=2 class="title">%s</th></tr>',_('Server Thruput Graph'));
	$blockBody['thruput'] .= '<tr><td><table class="blockcitem" border=0>';
	$blockBody['thruput'] .= '<tr><td colspan=2>&nbsp;</td></tr>';
	$blockBody['thruput'] .= '</table></td>';
	$blockBody['thruput'] .= sprintf('<td align="right"><img src="image.server.stats.php?index=%s&activity=thruput" /></td></tr>',
		$app['server']->getIndex());
	$blockBody['thruput'] .= '</table>';
	}
}

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