<h1>Site Usage Report</h1>
<form method="get" action="index.php">
<input type="hidden" name="pg" value="<?php echo $_REQUEST['pg']; ?>" />
Date: <input type="text" name="date" value="<?php echo $_REQUEST['date']; ?>" />
<input type="submit" value="Go" />
</form>

<?php

$targetDate = ($_REQUEST['date']) ? strtotime($_REQUEST['date']) : 0;

if (0 >= $targetDate) {
	return;
}

$targetFile = "/www/logs/" . date('Ymd', $targetDate) . "_request_log.php";

$fp = @fopen($targetFile, 'r');

if (!$fp) {
	echo "No report for " . $_REQUEST['date'] . "<br />";
	return;
}

$data = array();
$totalCount = 0;
$totalTime = 0.0;
$totalQueries = 0;

while (!feof($fp)) {
	$line = fgets($fp, 4096);
	$lineItems = split(',', $line);

	$totalCount++;
	$data['vanities'][$lineItems[0]]++;
	$data['domains'][$lineItems[1]]++;
	$totalTime += (float) $lineItems[2];
	$data['pages'][$lineItems[3]]++;
	$totalQueries += (int) $lineItems[4];
	$data['users'][$lineItems[7]]++;
	$data['ips'][$lineItems[8]]++;
}

fclose($fp);

$totalIps = count($data['ips']);
$totalUsers = count($data['users']);

echo "<h1>Report for: " . date("l, F j, Y", $targetDate) . "</h1>";

echo "Total hits: <b>$totalCount</b><br />";
echo "Total time processing: <b>$totalTime seconds</b> (<b>" . ($totalTime / 60) . " minutes</b>)<br />";
echo "Average time processing: <b>" . ($totalTime / $totalCount) . "</b><br />";
echo "Queries processed: <b>$totalQueries</b><br />";
echo "Average queries processed: <b>" . ($totalQueries / $totalCount) . "</b><br />";
echo "Unique IP Addresses: <b>$totalIps</b><br />";
echo "Unique users: <b>$totalUsers</b><br />";

$keys = array_keys($data);

foreach ($keys as $key) {
	arsort($data[$key]);
	
	echo <<<EOL
<br />
<h2>{$key}</h2>
<table>
EOL;

	foreach ($data[$key] as $item=>$count) {
		echo <<<EOL
<tr>
	<td>$item</td>
	<td align="right">$count</td>
</tr>
EOL;
	}

	echo <<<EOL
</table>
EOL;

}
?>