Index: memcache_admin.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/memcache/memcache_admin/memcache_admin.module,v
retrieving revision 1.3.2.6
diff -u -p -r1.3.2.6 memcache_admin.module
--- memcache_admin.module	31 Jul 2007 15:54:11 -0000	1.3.2.6
+++ memcache_admin.module	30 Jan 2008 13:09:09 -0000
@@ -19,10 +19,18 @@ function memcache_admin_init() {
   }
 }
 
+
+/**
+ * Implementation of hook_perm
+ */
 function memcache_admin_perm() {
   return array('access memcache statistics');
 }
 
+
+/**
+ * Implementation of hook_menu
+ */
 function memcache_admin_menu($may_cache) {
   $items = array();
 
@@ -52,49 +60,55 @@ function memcache_admin_menu($may_cache)
       $clusters[$cluster]['servers'][] = $server;
       $clusters[$cluster]['bin'] = _memcache_admin_get_bin_for_cluster($cluster);
     }
-
+    
     $count = 0;
     foreach($clusters as $cluster => $cluster_info) {
       if ($cluster_info['bin']) {
+        if (empty($current_cluster)) {
+          $current_cluster = arg(3);
+          if (empty($current_cluster)) {
+            $current_cluster = $cluster;
+          }
+        }
 
         $items[] = array(
           'path' => 'admin/logs/memcache/' . $cluster,
-          'type' =>  MENU_LOCAL_TASK,
+          'type' =>  $count == 0 ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
           'callback' => 'memcache_admin_stats',
           'callback arguments' => array($cluster),
           'title' => $cluster,
           'access' => user_access('access memcache statistics'),
           'weight' => $count,
         );
-
         $count++;
-      }
-    }
-
-    if ($cluster = arg(3)) {
-      $count = 0;
-      foreach(array('default', 'reset', 'malloc', 'maps', 'cachedump', 'slabs', 'items', 'sizes') as $type) {
-        $items[] = array(
-          'path' => 'admin/settings/memcache/' . $cluster . '/' . $type,
-          'type' => $type == 'default' ?  MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
-          'callback' => 'memcache_admin_stats',
-          'callback arguments' => array($cluster, $type),
-          'title' => $type,
-          'access' => user_access('access memcache statistics'),
-          'weight' => $count,
-        );
+        
+        if ($cluster == $current_cluster) {
+          $sub_count = 0;
+          foreach(array('default', 'reset', 'malloc', 'maps', 'slabs', 'items', 'sizes') as $type) {
+            $items[] = array(
+              'path' => 'admin/logs/memcache/' . $cluster . '/' . $type,
+              'type' => $type == 'default' ?  MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
+              'callback' => 'memcache_admin_stats',
+              'callback arguments' => array($cluster, $type),
+              'title' => $type,
+              'access' => user_access('access memcache statistics'),
+              'weight' => $sub_count,
+            );
+            $sub_count++;
+          }
+        }
 
-        $count++;
       }
     }
-    else {
-      drupal_goto('admin/logs/memcache/default');
-    }
   }
-
+  
   return $items;
 }
 
+
+/**
+ * Settings form
+ */
 function memcache_admin_admin_settings() {
   $form['show_memcache_statistics'] = array('#type' => 'checkbox',
     '#title' => t('Show memcache statistics at the bottom of each page'),
@@ -104,17 +118,43 @@ function memcache_admin_admin_settings()
   return system_settings_form($form);
 }
 
+/**
+ * Memcahe Stats page
+ *
+ * @param string $cluster - which cluster to view?
+ * @param string $type - which type of stat, eg: default, reset, malloc, maps, cachedump, slabs, items or sizes
+ * @return string
+ */
 function memcache_admin_stats($cluster = 'default', $type = 'default') {
   $bin = _memcache_admin_get_bin_for_cluster($cluster);
 
   if ($bin) {
     $stats = dmemcache_stats($bin, $type);
+    
 
     if (is_array($stats) && count($stats)) {
       $output = "";
 
       foreach ($stats as $server => $values) {
         if (is_array($values)) {
+          //Do some custome value tweaks for specific stat page types.
+          switch ($type) {
+            case 'default' :
+              $values['uptime'] = format_interval($values['uptime']);
+              $values['time'] = format_date($values['time']);
+              $values['bytes'] = format_size($values['bytes']);
+              $values['bytes_read'] = format_size($values['bytes_read']);
+              $values['bytes_written'] = format_size($values['bytes_written']);
+              $values['limit_maxbytes'] = format_size($values['limit_maxbytes']);
+              
+              //Custom Entries
+              $values['hit_percentage'] = number_format(100.0 * $values['get_hits'] / $values['cmd_get'], 2) . '%';
+              
+              $mem_used = $values['bytes'] / $values['limit_maxbytes'];
+              $values['mem_used'] = number_format(100.0 * $mem_used, 2) . '%';
+              break;
+          }
+          
           $output .= theme('memcache_admin_stats_table', $server, $values);
         }
         else {
@@ -133,7 +173,13 @@ function memcache_admin_stats($cluster =
 }
 
 
-
+/**
+ * Theme function for rendering the output from memcache_admin_stats
+ *
+ * @param string $server - Server name:port for caption for the table
+ * @param array $stats - array of key/value string pairs for the table results
+ * @return string
+ */
 function theme_memcache_admin_stats_table($server, $stats) {
   $rows = array();
 
@@ -143,7 +189,7 @@ function theme_memcache_admin_stats_tabl
       foreach ($value as $k => $v) {
         $rs[] = array($k, $v);
       }
-      $rows[] = array($key, theme('table', array('',''), $rs));
+      $rows[] = array($key, theme('table', array(), $rs));
     }
     else {
       $rows[] = array($key, $value);
@@ -154,7 +200,12 @@ function theme_memcache_admin_stats_tabl
 }
 
 
-
+/**
+ * Retrieve the cluster for any given bin
+ *
+ * @param string $cluster - Cluster ID
+ * @return string
+ */
 function _memcache_admin_get_bin_for_cluster($cluster) {
   static $cluster_map = array();
 
