<?php
/**
 * Summary of caching
 */

require_once "WEB-INF/php/inc.php";

if (! admin_init()) {
  return;
}

$block_cache = $g_mbean_server->lookup("resin:type=BlockManager");
$proxy_cache = $g_mbean_server->lookup("resin:type=ProxyCache");

echo "<h1>Proxy Cache";
print_help("Proxy Cache");
echo "</h1>";

echo "<div id='cache-tabs' style='display: none'>\n";
echo "<ul>\n";
echo "<li><a href='#overview'>Overview</a></li>\n";

if ($proxy_cache) {
  echo "<li><a href='#cached'>Cached</a></li>\n";
  echo "<li><a href='#uncached'>Uncached</a></li>\n";
}  

echo "</ul>";

?>

<div id="overview">
<h2>Overview</h2>
<table class="data">
  <tr title="Percentage of requests that have been served from the proxy cache:">
    <th>Proxy cache miss ratio:<?php print_help("Proxy Cache#miss ratio"); ?></th>
    <td><?= format_miss_ratio($proxy_cache->HitCountTotal,
                              $proxy_cache->MissCountTotal) ?></td>
  </tr>

  <tr title="Percentage of requests that have been served from the proxy cache:">
    <th><?= info("Block cache miss ratio") ?>:</th>
    <td><?= format_miss_ratio($block_cache->HitCountTotal,
                              $block_cache->MissCountTotal) ?></td>
  </tr>

<!-- XXX: show how cacheable apps are: cacheable/non-cacheable -->

  <tr>
    <th>Invocation miss ratio:<?php print_help("Proxy Cache#invocation"); ?></th>
    <td><?= format_miss_ratio($g_server->InvocationCacheHitCountTotal,
                              $g_server->InvocationCacheMissCountTotal) ?></td>
  </tr>

</table>
</div>

<?php

if ($proxy_cache) {
  $cacheable = $proxy_cache->getCachedEntries(20);

  echo "<div id='cached'>";
  echo "<h2>Cached Pages</h2>";

  echo "<table class='data'>";
  echo "<tr><th>Miss Ratio</th><th>url</th></tr>\n";
  foreach ($cacheable as $item) {
    echo "<tr>";
    echo "<td>";

    $total = $item->hitCount + $item->missCount;
    if ($total == 0)
      $total = 1;

    printf("%.2f%% (%d/%d)",
           $item->missCount * 100 / $total, $item->missCount, $total);

    echo "</td><td>{$item->url}</td>";
    echo "</tr>\n";
  }
  echo "</table>";
  echo "</div>";

  $uncacheable = $proxy_cache->getUncachedEntries(20);

  echo "<div id='uncached'>";
  echo "<h3>Uncached Pages</h3>";

  echo "<table class='data'>";
  echo "<tr><th>Count</th><th>url</th></tr>\n";
  foreach ($uncacheable as $item) {
    echo "<tr>";
    echo "<td>{$item->missCount}</td><td>{$item->url}</td>";
    echo "</tr>\n";
  }
  echo "</table>";
  echo "</div>";
}
echo "</div>";

// display_tabs();
display_footer($g_page, javascript_create_tab("cache-tabs"));
