diff options
author | johmathe <johmathe@google.com> | 2008-08-16 20:51:11 +0400 |
---|---|---|
committer | johmathe <johmathe@google.com> | 2008-08-16 20:51:11 +0400 |
commit | a44b3da8160494034834fb35502ea05f0ff6d20a (patch) | |
tree | fb61a453df72cb537d6684c555c78e22b509f483 /plugins/DBStats/API.php | |
parent | b88dbbc1a66d3952cd76d6f689e4f174036906fc (diff) |
Adds the database usage statistics plugin.
git-svn-id: http://dev.piwik.org/svn/trunk@605 59fd770c-687e-43c8-a1e3-f5a4ff64c105
Diffstat (limited to 'plugins/DBStats/API.php')
-rw-r--r-- | plugins/DBStats/API.php | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/plugins/DBStats/API.php b/plugins/DBStats/API.php new file mode 100644 index 0000000000..6405c6a2bb --- /dev/null +++ b/plugins/DBStats/API.php @@ -0,0 +1,93 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later + * @version $Id: API.php 482 2008-05-18 17:22:35Z matt $ + * + * @package Piwik_DBStats + */ + +/** + * + * @package Piwik_DBStats_API + */ +class Piwik_DBStats_API extends Piwik_Apiable +{ + static private $instance = null; + static public function getInstance() + { + if (self::$instance == null) + { + $c = __CLASS__; + self::$instance = new $c(); + } + return self::$instance; + } + + static public function getDBStatus() + { + $configDb = Piwik_Config::getInstance()->database; + + // we decode the password. Password is html encoded because it's enclosed between " double quotes + $configDb['password'] = htmlspecialchars_decode($configDb['password']); + if(!isset($configDb['port'])) + { + // before 0.2.4 there is no port specified in config file + $configDb['port'] = '3306'; + } + + $link = mysql_connect($configDb['host'], $configDb['username'], $configDb['password']); + $status = mysql_stat($link); + mysql_close($link); + return $status; + } + + static function get_size($size) { + $bytes = array('B','KB','MB','GB','TB'); + foreach($bytes as $val) { + if($size > 1024){ + $size = $size / 1024; + }else{ + break; + } + } + return round($size, 1)." ".$val; + } + + static public function getTableStatus($table, $field = '') + { + $db = Zend_Registry::get('db'); + // http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html + $tables = $db->fetchAll("SHOW TABLE STATUS LIKE ?", $table); + + if ($field == '') + { + return $tables[0]; + } + else + { + return $tables[0][$field]; + } + } + + static public function getAllTablesStatus() + { + $db = Zend_Registry::get('db'); + // http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html + $tablesPiwik = Piwik::getTablesInstalled(); + foreach($tablesPiwik as $table) + { + $t = self::getTableStatus($table); + $t['Data_length'] = self::get_size($t['Data_length']); + $t['Index_length'] = self::get_size($t['Index_length']); + $t['Index_length'] = self::get_size($t['Index_length']); + $t['Rows'] = self::get_size($t['Rows']); + $tables[] = $t; + } + + return $tables; + } +} + |