<?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");

if (! require_professional()) {
  return;
} else if (! $proxy_cache) {
  echo "<div class=\"req-pro-message\">This feature requires &lt;proxy-cache> in resin.xml</div>\n";
  return;
}
echo "<h1>";
echo gettext('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'>" . gettext('Overview') . "</a></li>\n";

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

echo "</ul>";

?>

<div id="overview">
<h2><?= gettext('Overview')?></h2>
<table class="data" summary="<?= gettext('Proxy Cache Overview')?>">
  <tr title="<?= gettext('Percentage of requests that have been served from the proxy cache:')?>">
    <th scope="row"><?= gettext('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="<?= gettext('Percentage of requests that have been served from the proxy cache:')?>">
    <th scope="row"><?= gettext('Block cache miss ratio:') ?><?php print_help("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 scope="row"><?= gettext('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>" . gettext('Cached Pages') . "</h2>";

  echo "<table class='data'>";
  echo "<tr><th>" . gettext('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>" . gettext('Uncached Pages') . "</h3>";

  echo "<table class='data'>";
  echo "<tr><th>" . gettext('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"));
