| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* $Id: cacherouter.admin.inc,v 1.1.2.3 2008/12/26 02:27:24 slantview Exp $
|
| 4 |
*
|
| 5 |
* @file cacherouter.admin.inc
|
| 6 |
* Defines the cacherouter administrative interface.
|
| 7 |
*/
|
| 8 |
function cacherouter_admin_stats_page($cache_name = 'default') {
|
| 9 |
global $cache;
|
| 10 |
if (empty($cache_name)) {
|
| 11 |
$cache_name = 'default';
|
| 12 |
}
|
| 13 |
$stats = $cache->getStats($cache_name);
|
| 14 |
|
| 15 |
return theme('cacherouter_admin_stats_page', $cache_name, $stats);
|
| 16 |
}
|
| 17 |
|
| 18 |
function cacherouter_theme() {
|
| 19 |
return array(
|
| 20 |
'cacherouter_admin_stats_page' => array(
|
| 21 |
'arguments' => array('cache_name' => NULL, 'stats' => array()),
|
| 22 |
),
|
| 23 |
'cacherouter_admin_stats_key' => array(
|
| 24 |
'arguments' => array('key1' => '', 'value1' => '', 'key2' => '', 'value2' => ''),
|
| 25 |
),
|
| 26 |
);
|
| 27 |
}
|
| 28 |
|
| 29 |
function theme_cacherouter_admin_stats_page($cache_name, $stats) {
|
| 30 |
global $cache;
|
| 31 |
|
| 32 |
drupal_add_css(drupal_get_path('module', 'cacherouter') .'/cacherouter.css');
|
| 33 |
drupal_add_js(drupal_get_path('module', 'cacherouter') .'/cacherouter.js');
|
| 34 |
|
| 35 |
$output = '<div id="cacherouter-stats">';
|
| 36 |
$output .= t('<h3 class="cr-hdr">Cache Name: </h3>');
|
| 37 |
|
| 38 |
// TODO: should be a formapi form.
|
| 39 |
$output .= '<form action="/admin/settings/cacherouter" id="cr-cache-switch"><select name="cacherouter-cache">';
|
| 40 |
|
| 41 |
foreach ($cache->getBins() as $bin) {
|
| 42 |
if (arg(3) == $bin) {
|
| 43 |
$output .= '<option selected="selected" value="'. url('admin/settings/cacherouter/'. $bin).'">'. $bin .'</option>';
|
| 44 |
}
|
| 45 |
else {
|
| 46 |
$output .= '<option value="'. url('admin/settings/cacherouter/'. $bin).'">'. $bin .'</option>';
|
| 47 |
}
|
| 48 |
}
|
| 49 |
$output .= '</select></form>';
|
| 50 |
|
| 51 |
$output .= '<br />';
|
| 52 |
$output .= '<h3 class="cr-hdr">Cache Type: </h3>';
|
| 53 |
|
| 54 |
// Get cache type and link to documentation
|
| 55 |
$cache_docs = array(
|
| 56 |
'apc' => l('APC', 'http://pecl.php.net/package/APC'),
|
| 57 |
'db' => l('Database', 'http://www.drupal.org/'),
|
| 58 |
'eacc' => l('eAccelerator', 'http://eaccelerator.com/'),
|
| 59 |
'file' => 'File system',
|
| 60 |
'memcache' => l('Memcache', 'http://www.danga.com/memcached/'),
|
| 61 |
'xcache' => l('XCache', 'http://xcache.org/'),
|
| 62 |
);
|
| 63 |
$output .= $cache_docs[$cache->getType($cache_name)];
|
| 64 |
|
| 65 |
// Get cache stats table with graphs.
|
| 66 |
$stats_header = array(t('Memory'), t('Hit / Miss'), t('Get / Set'));
|
| 67 |
$stats_attributes = array('id' => 'cacherouter-stats');
|
| 68 |
$stats_table = array();
|
| 69 |
|
| 70 |
// First do our calculations for percentages and sizes
|
| 71 |
$mem_used = round(($stats['bytes_used'] / $stats['bytes_total']) * 100);
|
| 72 |
$mem_free = round(100 - $mem_used);
|
| 73 |
if ($mem_free == 100) {
|
| 74 |
$mem_free = 99;
|
| 75 |
$mem_used = 1;
|
| 76 |
}
|
| 77 |
|
| 78 |
if ($stats['bytes_used'] == 0 && $stats['bytes_total'] == 0) {
|
| 79 |
$chart1 = theme('image', drupal_get_path('module', 'cacherouter') .'/images/na.png');
|
| 80 |
}
|
| 81 |
else {
|
| 82 |
$chart1 = theme('image', 'http://chart.apis.google.com/chart?chs=250x100&chd=t:'. $mem_used .','. $mem_free . '&cht=p3&chl=Used|Free&chco=3399cc,cbe2f1', '', '', NULL, FALSE);
|
| 83 |
}
|
| 84 |
|
| 85 |
if ($stats['hits'] > 0) {
|
| 86 |
$hits_hit = round(($stats['hits'] / ($stats['hits'] + $stats['misses'])) * 100);
|
| 87 |
}
|
| 88 |
else {
|
| 89 |
$hits_hit = '0';
|
| 90 |
}
|
| 91 |
|
| 92 |
if ($stats['misses'] > 0) {
|
| 93 |
$hits_misses = round(($stats['misses'] / ($stats['hits'] + $stats['misses'])) * 100);
|
| 94 |
}
|
| 95 |
else {
|
| 96 |
$hits_misses = '0';
|
| 97 |
}
|
| 98 |
|
| 99 |
if ($hits_misses == 0 && $hits_hit == 0) {
|
| 100 |
$chart2 = theme('image', drupal_get_path('module', 'cacherouter') .'/images/na.png');
|
| 101 |
}
|
| 102 |
else {
|
| 103 |
$chart2 = theme('image', 'http://chart.apis.google.com/chart?chs=250x100&chd=t:'. $hits_hit .','. $hits_misses .'&cht=p3&chl=Hit|Miss&chco=3399cc,cbe2f1', '', '', NULL, FALSE);
|
| 104 |
}
|
| 105 |
|
| 106 |
if ($stats['gets'] > 0) {
|
| 107 |
$req_gets = round(($stats['gets'] / ($stats['gets'] + $stats['sets'])) * 100);
|
| 108 |
}
|
| 109 |
else {
|
| 110 |
$req_gets = '0';
|
| 111 |
}
|
| 112 |
|
| 113 |
if ($stats['sets'] > 0) {
|
| 114 |
$req_sets = round(($stats['sets'] / ($stats['gets'] + $stats['sets'])) * 100);
|
| 115 |
}
|
| 116 |
|
| 117 |
if ($req_gets == 0 && $req_sets == 0) {
|
| 118 |
$chart3 = theme('image', drupal_get_path('module', 'cacherouter') .'/images/na.png');
|
| 119 |
}
|
| 120 |
else {
|
| 121 |
$chart3 = theme('image', 'http://chart.apis.google.com/chart?chs=250x100&chd=t:'. $req_gets .','. $req_sets .'&cht=p3&chl=Get|Set&chco=3399cc,cbe2f1', '', '', NULL, FALSE);
|
| 122 |
}
|
| 123 |
|
| 124 |
|
| 125 |
// First row is images
|
| 126 |
$stats_table[] = array($chart1, $chart2, $chart3);
|
| 127 |
|
| 128 |
// Next row is stats and key for images
|
| 129 |
$stats_table[] = array(
|
| 130 |
theme('cacherouter_admin_stats_key', t('Used'), _cacherouter_convert_size($stats['bytes_used']) .' ('. $mem_used .'%)',
|
| 131 |
t('Free'), _cacherouter_convert_size($stats['bytes_total'] - $stats['bytes_used']) .' ('. $mem_free .'%)'),
|
| 132 |
theme('cacherouter_admin_stats_key', t('Hits'), $stats['hits'] .' ('. $hits_hit .'%)',
|
| 133 |
t('Misses'), $stats['misses'] . ' ('. $hits_misses .'%)'),
|
| 134 |
theme('cacherouter_admin_stats_key', t('Gets'), $stats['gets'] .' ('. $req_gets .'%)',
|
| 135 |
t('Sets'), $stats['sets'] .' ('. $req_sets .'%)'),
|
| 136 |
);
|
| 137 |
|
| 138 |
$output .= theme('table', $stats_header, $stats_table, $stats_attributes);
|
| 139 |
|
| 140 |
$info_header = array(t('Cache Info'), t('Value'));
|
| 141 |
$info_attributes = array('id' => 'cacherouter-info');
|
| 142 |
$info_table = array();
|
| 143 |
|
| 144 |
// Row 1 - Request Rate
|
| 145 |
$info_table[] = array(
|
| 146 |
t('Request Rate'),
|
| 147 |
sprintf('%.2f %s', $stats['req_rate'], t('Requests / second')),
|
| 148 |
);
|
| 149 |
|
| 150 |
// Row 2 - Hit Rate
|
| 151 |
$info_table[] = array(
|
| 152 |
t('Hit Rate'),
|
| 153 |
sprintf('%.2f %s', $stats['hit_rate'], t('Requests / second')),
|
| 154 |
);
|
| 155 |
|
| 156 |
// Row 3 - Miss Rate
|
| 157 |
$info_table[] = array(
|
| 158 |
t('Miss Rate'),
|
| 159 |
sprintf('%.2f %s', $stats['miss_rate'], t('Requests / second')),
|
| 160 |
);
|
| 161 |
|
| 162 |
// Row 4 - Set Rate
|
| 163 |
$info_table[] = array(
|
| 164 |
t('Set Rate'),
|
| 165 |
sprintf('%.2f %s', $stats['set_rate'], t('Requests / second')),
|
| 166 |
);
|
| 167 |
|
| 168 |
$output .= theme('table', $info_header, $info_table, $info_attributes);
|
| 169 |
$output .= '</div>';
|
| 170 |
return $output;
|
| 171 |
}
|
| 172 |
|
| 173 |
function theme_cacherouter_admin_stats_key($key1, $value1, $key2, $value2) {
|
| 174 |
$output = '<div class="cr-key">';
|
| 175 |
$output .= '<div class="cr-primary-1"></div>';
|
| 176 |
$output .= '<span><strong>'. $key1 .':</strong> '. $value1 .'</span>';
|
| 177 |
$output .= '</div>';
|
| 178 |
$output .= '<div class="cr-key">';
|
| 179 |
$output .= '<div class="cr-primary-2"></div>';
|
| 180 |
$output .= '<span><strong>'. $key2 .':</strong> '. $value2 .'</span>';
|
| 181 |
$output .= '</div>';
|
| 182 |
|
| 183 |
return $output;
|
| 184 |
}
|
| 185 |
|
| 186 |
function _cacherouter_convert_size($fs) {
|
| 187 |
$return = '';
|
| 188 |
|
| 189 |
if ($fs >= 1073741824) {
|
| 190 |
$return = round($fs / 1073741824 * 100) / 100 . " GB";
|
| 191 |
}
|
| 192 |
else if ($fs >= 1048576) {
|
| 193 |
$return = round($fs / 1048576 * 100) / 100 . " MB";
|
| 194 |
}
|
| 195 |
else if ($fs >= 1024) {
|
| 196 |
$return = round($fs / 1024 * 100) / 100 . " KB";
|
| 197 |
}
|
| 198 |
else {
|
| 199 |
$return = $fs . " Bytes";
|
| 200 |
}
|
| 201 |
|
| 202 |
return $return;
|
| 203 |
}
|