<?php
// $Header: /cvsroot/phptsmadmin/phpTSMadmin/htdocs/node.occupancy.php,v 1.9 2009/04/19 04:57:32 wurley Exp $

# This page should return the summary of the client occupancy.

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

# Defaults
$nodes = objectCache('nodes');

# Header
$blockTitle['graph'] = sprintf(_('Client Occupancy Summary on %s'),$app['server']->getValue('server','name'));
$blockBody['graph'] = '<table class="result_table">';

$blockTitle['occ'] = sprintf(_('Client Occupancy Summary on %s'),$app['server']->getValue('server','name'));
$blockBody['occ'] = '<table class="result_table">';

$counter = 0;
$grandtotal = 0;

# Work out our stgpools with data in them.
$stgpooltotal = array();
$blockBody['occ'] .= sprintf('<tr><td class="heading">%s</td><td class="heading">%s</td><td class="heading">%s</td>',
	'Node','Last Acc','Type');
foreach ($nodes->getNodes() as $node) {
	foreach ($node->getStoragePools() as $stgp) {
		if (! isset($stgpooltotal[$stgp])) {
			$blockBody['occ'] .= sprintf('<td class="heading">%s</td>',$stgp);
			$stgpooltotal[$stgp] = 0;
		}
	}
}
$blockBody['occ'] .= sprintf('<td class="heading">%s</td></tr>','Total');

# Now show the nodes having data in the stgpools.
$history = array();
foreach ($nodes->getNodes() as $node) {
	foreach ($node->getOccupancy() as $type => $stgpools) {
		$nodetotal = 0;
		$blockBody['occ'] .= sprintf('<tr class="%s"><td>%s</td><td>%s</td><td>%s</td>',
			($counter++%2==0?'even':'odd'),
			$node->getName(),
			tsmDate($node->time['lastacc'],'notime'),
			$type);

		foreach ($stgpooltotal as $stgpool => $details) {
			if (isset($stgpools[$stgpool])) {
				$blockBody['occ'] .= sprintf('<td align="right">%s</td>',number_format($stgpools[$stgpool]['physical']));
				$nodetotal += $stgpools[$stgpool]['physical'];
				$stgpooltotal[$stgpool] += $stgpools[$stgpool]['physical'];

			} else
				$blockBody['occ'] .= sprintf('<td>%s</td>','&nbsp;');
		}

		$blockBody['occ'] .= sprintf('<td align="right">&nbsp;%s</td></tr>',number_format($nodetotal));
		$grandtotal += $nodetotal;

		$oldtime = strtotime(tsmDate($node->time['lastacc'],'sec'));
		$diff = round((time() - $oldtime)/86400);
		@$history[$diff] += $nodetotal;
	}
}

# Store our history information for when we call the graphing php.
$_SESSION['graph']['occupancy'] = $history;
ksort($_SESSION['graph']['occupancy']);

# Show our dbbackup graph
$blockBody['graph'] .= '<tr><td><table class="result_table">';
$blockBody['graph'] .= sprintf('<tr><td class="heading">%s</td><td class="heading">%s</td><td class="heading">%%</td></tr>','Days Since Access','Storage Used');
$counter = 0;
foreach ($_SESSION['graph']['occupancy'] as $age => $size) {
	$blockBody['graph'] .= sprintf('<tr class="%s"><td>%s</td><td align="right">%s</td><td align="right">%s</td></tr>',
		($counter++%2==0?'even':'odd'),$age,number_format($size),sprintf('%2.1f%%',$size/$grandtotal*100));
}
$blockBody['graph'] .= '</table></td>';
$blockBody['graph'] .= '<td>&nbsp;</td>';
$blockBody['graph'] .= sprintf('<td align="right"><img src="image.occupancy.php?index=%s" /></td></tr>',
	$app['server']->getIndex());
$blockBody['graph'] .= '</table>';

$blockBody['occ'] .= sprintf('<tr class="highlight"><td class="titlel" colspan=3>%s</td>','TOTAL');
foreach ($stgpooltotal as $stgpool => $total)
	$blockBody['occ'] .= sprintf('<td class="titler">%s</td>',number_format($total));
$blockBody['occ'] .= sprintf('<td class="titler">&nbsp;%s</td></tr>',number_format($grandtotal));
$blockBody['occ'] .= '</table>';

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