Skip to content

Instantly share code, notes, and snippets.

@kemo
Created April 2, 2012 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kemo/2281992 to your computer and use it in GitHub Desktop.
Save kemo/2281992 to your computer and use it in GitHub Desktop.
View Model for on-the-fly MySQL profiling
<?php defined('SYSPATH') or die('No direct script access.');
/**
* @author Kemal Delalic <kemal.delalic@gmail.com>
*/
class View_Admin_System_Index extends View_Admin_Layout {
/**
* @var cache for self::mysql_processes()
*/
protected $_mysql_processes;
public function mysql_processes()
{
if ($this->_mysql_processes !== NULL)
return $this->_mysql_processes;
$result = array();
$processes = DB::query(Database::SELECT, 'SHOW FULL PROCESSLIST')
->execute();
foreach ($processes as $process)
{
$push = $process;
$push['style'] = ($process['State'] == 'Locked')
? 'backgrund-color:#c00 !important;color:#fff !important;'
: '';
switch (trim(strtolower($process['State'])))
{
case 'closing tables' :
case 'sorting result' :
case 'writing to net' :
case 'update' :
case 'checking permissions' :
case 'sending data' :
$push['state_label'] = 'label notice';
break;
case 'kocked' :
$push['state_label'] = 'label important';
break;
default:
$push['state_label'] = '';
break;
}
switch (trim($process['Command']))
{
case 'Sleep' :
$push['command_label'] = 'label warning';
break;
default:
$push['command_label'] = 'label notice';
break;
}
if ($process['Time'] < 20)
{
$push['time_label'] = '';
}
elseif ($process['Time'] >= 20)
{
$push['time_label'] = 'label warning';
}
elseif ($process['Time'] > 60)
{
$push['time_label'] = 'label important';
}
$result[] = $push;
}
return $this->_mysql_processes = $result;
}
protected $_querycache_status;
public function querycache_status()
{
if ($this->_querycache_status !== NULL)
return $this->_querycache_status;
$result = array();
$status = DB::query(Database::SELECT, "SHOW STATUS LIKE 'Qcache%'")
->execute();
foreach ($status as $s)
{
$push = $s;
$result[] = $push;
}
return $this->_querycache_status = $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment