diff options
author | diosmosis <benakamoorthi@fastmail.fm> | 2014-02-27 17:21:41 +0400 |
---|---|---|
committer | diosmosis <benakamoorthi@fastmail.fm> | 2014-02-27 17:21:41 +0400 |
commit | 13a08d58897ba2921a037b309e75593c3fbbabcd (patch) | |
tree | d0ed9565efaa10a1c6d71d797ddbb319f5a37177 /plugins | |
parent | 4ad2b987467fd57c0ef5a0bbaa8c7f66845d69e1 (diff) |
Refs #4189, added expected screenshot for DBStats, added actual & mock data access class to DBStats, fix bug that occurs when deleting merged assets that do not exist and fix diffviewer generation regression (expected URL was incorrect).
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/DBStats/API.php | 4 | ||||
-rw-r--r-- | plugins/DBStats/DBStats.php | 11 | ||||
-rw-r--r-- | plugins/DBStats/MySQLMetadataDataAccess.php | 70 | ||||
-rwxr-xr-x | plugins/DBStats/MySQLMetadataProvider.php | 52 | ||||
-rw-r--r-- | plugins/DBStats/tests/Mocks/MockDataAccess.php | 151 | ||||
-rw-r--r-- | plugins/DBStats/tests/UI/expected-ui-screenshots/DBStats_admin_page.png | bin | 0 -> 312771 bytes |
6 files changed, 251 insertions, 37 deletions
diff --git a/plugins/DBStats/API.php b/plugins/DBStats/API.php index 6da5604096..c90f327a7c 100644 --- a/plugins/DBStats/API.php +++ b/plugins/DBStats/API.php @@ -34,7 +34,9 @@ class API extends \Piwik\Plugin\API */ protected function __construct() { - $this->metadataProvider = new MySQLMetadataProvider(); + if ($this->metadataProvider === null) { + $this->metadataProvider = new MySQLMetadataProvider(); + } } /** diff --git a/plugins/DBStats/DBStats.php b/plugins/DBStats/DBStats.php index 402f88eb32..3f5a226696 100644 --- a/plugins/DBStats/DBStats.php +++ b/plugins/DBStats/DBStats.php @@ -37,7 +37,8 @@ class DBStats extends \Piwik\Plugin 'Menu.Admin.addItems' => 'addMenu', 'TaskScheduler.getScheduledTasks' => 'getScheduledTasks', 'ViewDataTable.configure' => 'configureViewDataTable', - 'ViewDataTable.getDefaultType' => 'getDefaultTypeViewDataTable' + 'ViewDataTable.getDefaultType' => 'getDefaultTypeViewDataTable', + "TestingEnvironment.addHooks" => 'setupTestEnvironment' ); } @@ -374,4 +375,12 @@ class DBStats extends \Piwik\Plugin $view->config->show_footer_message = Piwik::translate('Mobile_LastUpdated', $lastGenerated); } } + + public function setupTestEnvironment($environment) + { + Piwik::addAction("MySQLMetadataProvider.createDao", function (&$dao) { + require_once dirname(__FILE__) . "/tests/Mocks/MockDataAccess.php"; + $dao = new Mocks\MockDataAccess(); + }); + } } diff --git a/plugins/DBStats/MySQLMetadataDataAccess.php b/plugins/DBStats/MySQLMetadataDataAccess.php new file mode 100644 index 0000000000..e5fee259a1 --- /dev/null +++ b/plugins/DBStats/MySQLMetadataDataAccess.php @@ -0,0 +1,70 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ +namespace Piwik\Plugins\DBStats; + +use Piwik\Db; +use Piwik\Config; +use \Exception; + +/** + * Data Access Object that serves MySQL stats. + */ +class MySQLMetadataDataAccess +{ + public function getDBStatus() + { + if (function_exists('mysql_connect')) { + $configDb = Config::getInstance()->database; + $link = mysql_connect($configDb['host'], $configDb['username'], $configDb['password']); + $status = mysql_stat($link); + mysql_close($link); + $status = explode(" ", $status); + } else { + $fullStatus = Db::fetchAssoc('SHOW STATUS'); + if (empty($fullStatus)) { + throw new Exception('Error, SHOW STATUS failed'); + } + + $status = array( + 'Uptime' => $fullStatus['Uptime']['Value'], + 'Threads' => $fullStatus['Threads_running']['Value'], + 'Questions' => $fullStatus['Questions']['Value'], + 'Slow queries' => $fullStatus['Slow_queries']['Value'], + 'Flush tables' => $fullStatus['Flush_commands']['Value'], + 'Open tables' => $fullStatus['Open_tables']['Value'], + 'Opens' => 'unavailable', // not available via SHOW STATUS + 'Queries per second avg' => 'unavailable' // not available via SHOW STATUS + ); + } + + return $status; + } + + public function getTableStatus($tableName) + { + return Db::fetchRow("SHOW TABLE STATUS LIKE ?", array($tableName)); + } + + public function getAllTablesStatus() + { + return Db::fetchAll("SHOW TABLE STATUS"); + } + + public function getRowCountsByArchiveName($tableName, $extraCols) + { + // otherwise, create data table & cache it + $sql = "SELECT name as 'label', COUNT(*) as 'row_count'$extraCols FROM $tableName GROUP BY name"; + return Db::fetchAll($sql); + } + + public function getColumnsFromTable($tableName) + { + return Db::fetchAll("SHOW COLUMNS FROM " . $tableName); + } +}
\ No newline at end of file diff --git a/plugins/DBStats/MySQLMetadataProvider.php b/plugins/DBStats/MySQLMetadataProvider.php index 549752fa35..4f3b346fb4 100755 --- a/plugins/DBStats/MySQLMetadataProvider.php +++ b/plugins/DBStats/MySQLMetadataProvider.php @@ -15,6 +15,7 @@ use Piwik\DataTable; use Piwik\Db; use Piwik\DbHelper; use Piwik\Option; +use Piwik\Piwik; /** * Utility class that provides general information about databases, including the size of @@ -32,11 +33,20 @@ class MySQLMetadataProvider private $tableStatuses = null; /** + * Data access object. + */ + public $dataAccess = null; + + /** * Constructor. */ public function __construct() { - // empty + Piwik::postTestEvent("MySQLMetadataProvider.createDao", array(&$this->dataAccess)); + + if ($this->dataAccess === null) { + $this->dataAccess = new MySQLMetadataDataAccess(); + } } /** @@ -47,31 +57,7 @@ class MySQLMetadataProvider */ public function getDBStatus() { - if (function_exists('mysql_connect')) { - $configDb = Config::getInstance()->database; - $link = mysql_connect($configDb['host'], $configDb['username'], $configDb['password']); - $status = mysql_stat($link); - mysql_close($link); - $status = explode(" ", $status); - } else { - $fullStatus = Db::fetchAssoc('SHOW STATUS'); - if (empty($fullStatus)) { - throw new Exception('Error, SHOW STATUS failed'); - } - - $status = array( - 'Uptime' => $fullStatus['Uptime']['Value'], - 'Threads' => $fullStatus['Threads_running']['Value'], - 'Questions' => $fullStatus['Questions']['Value'], - 'Slow queries' => $fullStatus['Slow_queries']['Value'], - 'Flush tables' => $fullStatus['Flush_commands']['Value'], - 'Open tables' => $fullStatus['Open_tables']['Value'], - 'Opens' => 'unavailable', // not available via SHOW STATUS - 'Queries per second avg' => 'unavailable' // not available via SHOW STATUS - ); - } - - return $status; + return $this->dataAccess->getDBStatus(); } /** @@ -89,7 +75,7 @@ class MySQLMetadataProvider if (!is_null($this->tableStatuses) && isset($this->tableStatuses[$prefixed])) { return $this->tableStatuses[$prefixed]; } else { - return Db::fetchRow("SHOW TABLE STATUS LIKE ?", array($prefixed)); + return $this->dataAccess->getTableStatus($prefixed); } } @@ -108,7 +94,7 @@ class MySQLMetadataProvider $tablesPiwik = DbHelper::getTablesInstalled(); $this->tableStatuses = array(); - foreach (Db::fetchAll("SHOW TABLE STATUS") as $t) { + foreach ($this->dataAccess->getAllTablesStatus() as $t) { if (in_array($t['Name'], $tablesPiwik)) { $this->tableStatuses[$t['Name']] = $t; } @@ -230,11 +216,8 @@ class MySQLMetadataProvider if ($cachedData !== false && !$forceCache) { $table = DataTable::fromSerializedArray($cachedData); } else { - // otherwise, create data table & cache it - $sql = "SELECT name as 'label', COUNT(*) as 'row_count'$extraCols FROM {$status['Name']} GROUP BY name"; - $table = new DataTable(); - $table->addRowsFromSimpleArray(Db::fetchAll($sql)); + $table->addRowsFromSimpleArray($this->dataAccess->getRowCountsByArchiveName($status['Name'], $extraCols)); $reduceArchiveRowName = array($this, 'reduceArchiveRowName'); $table->filter('GroupBy', array('label', $reduceArchiveRowName)); @@ -277,7 +260,7 @@ class MySQLMetadataProvider static $fixedSizeColumnLength = null; if (is_null($fixedSizeColumnLength)) { $fixedSizeColumnLength = 0; - foreach (Db::fetchAll("SHOW COLUMNS FROM " . $status['Name']) as $column) { + foreach ($this->dataAccess->getColumnsFromTable($status['Name']) as $column) { $columnType = $column['Type']; if (($paren = strpos($columnType, '(')) !== false) { @@ -362,5 +345,4 @@ class MySQLMetadataProvider return $name; } -} - +}
\ No newline at end of file diff --git a/plugins/DBStats/tests/Mocks/MockDataAccess.php b/plugins/DBStats/tests/Mocks/MockDataAccess.php new file mode 100644 index 0000000000..b394a2d599 --- /dev/null +++ b/plugins/DBStats/tests/Mocks/MockDataAccess.php @@ -0,0 +1,151 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ +namespace Piwik\Plugins\DBStats\Mocks; + +use Piwik\Piwik; +use Piwik\Common; + +class MockDataAccess +{ + public static $tableStatuses = array( + 'user' => array(5, 8192, 8192), + 'access' => array(9, 8192, 1024), + 'site' => array(10, 32000, 8192), + 'site_url' => array(8, 8192, 8192), + 'goal' => array(5, 16000, 8192), + 'logger_message' => array(100, 48000, 1024), + 'log_action' => array(3000, 64000, 32000), + 'log_visit' => array(1300, 64000, 16000), + 'log_conversion_item' => array(600, 64000, 16000), + 'log_conversion' => array(1000, 64000, 16000), + 'log_link_visit_action' => array(3000, 64000, 16000), + 'log_profiling' => array(0, 8192, 8192), + 'option' => array(200, 16000, 8192), + 'session' => array(0, 8192, 8192), + 'archive_numeric' => array(8000, 16000, 16000), + 'archive_blob' => array(8000, 128000, 1024) + ); + + public static $numericRowCountsByArchiveName = array( + array('label' => 'numericName1', 'row_count' => 93), + array('label' => 'numericName2', 'row_count' => 203), + array('label' => 'done', 'row_count' => 400), + array('label' => 'done.plugin', 'row_count' => 150), + array('label' => 'numericName3', 'row_count' => 340), + array('label' => 'numericName4', 'row_count' => 240), + ); + + public static $blobRowCountsByArchiveName = array( + array('label' => 'blobName1', 'row_count' => 123), + array('label' => 'blobName2', 'row_count' => 145), + array('label' => 'blobName3', 'row_count' => 83), + array('label' => 'blobName4', 'row_count' => 45), + ); + + public function getDBStatus() + { + return array( + 'Uptime' => 10000, + 'Threads' => 10, + 'Questions' => 15, + 'Slow queries' => 20, + 'Flush tables' => 300, + 'Open tables' => 2, + 'Opens' => 'unavailable', + 'Queries per second avg' => 'unavailable' + ); + } + + public function getTableStatus($tableName) + { + list($rows, $rowLength, $indexRowLength) = self::$tableStatuses[$this->getTableNameKey($tableName)]; + + return array( + 'Name' => $tableName, + 'Engine' => 'InnoDB', + 'Version' => 10, + 'Row_format' => 'Compact', + 'Rows' => $rows, + 'Avg_row_length' => $rowLength, + 'Data_length' => $rows * $rowLength, + 'Max_data_length' => 0, + 'Index_length' => $rows * $indexRowLength, + 'Data_free' => 236978176, + 'Auto_increment' => null, + 'Create_time' => '2014-01-01 23:54:56', + 'Update_time' => null, + 'Check_time' => null, + 'Collation' => 'utf8_general_ci', + 'Checksum' => null, + 'Create_options' => "", + 'Comment' => "" + ); + } + + public function getAllTablesStatus() + { + $result = array(); + foreach (self::$tableStatuses as $tableName => $ignore) { + if ($tableName == "archive_numeric" + || $tableName == "archive_blob" + ) { + continue; + } + + $unprefixed = Common::prefixTable($tableName); + $result[] = $this->getTableStatus($unprefixed); + } + + $result[] = $this->getTableStatus(Common::prefixTable('archive_numeric_2012_01')); + $result[] = $this->getTableStatus(Common::prefixTable('archive_blob_2012_01')); + + $result[] = $this->getTableStatus(Common::prefixTable('archive_numeric_2012_02')); + $result[] = $this->getTableStatus(Common::prefixTable('archive_blob_2012_02')); + + $result[] = $this->getTableStatus(Common::prefixTable('archive_numeric_2012_03')); + $result[] = $this->getTableStatus(Common::prefixTable('archive_blob_2012_03')); + + $result[] = $this->getTableStatus(Common::prefixTable('archive_numeric_2012_04')); + $result[] = $this->getTableStatus(Common::prefixTable('archive_blob_2012_04')); + + return $result; + } + + public function getTableNameKey($tableName) + { + $result = Common::unprefixTable($tableName); + if (strpos($tableName, "archive_numeric")) { + $result = "archive_numeric"; + } else if (strpos($tableName, "archive_blob")) { + $result = "archive_blob"; + } + return $result; + } + + public function getRowCountsByArchiveName($tableName, $extraCols) + { + if (strpos($tableName, "achive_numeric")) { + return self::$numericRowCountsByArchiveName; + } else { + return self::$blobRowCountsByArchiveName; + } + } + + public function getColumnsFromTable($tableName) + { + return array( + array('Field' => 'field1', 'Type' => 'datetime'), + array('Field' => 'field2', 'Type' => 'int(11) unsigned'), + array('Field' => 'field3', 'Type' => 'varchar(10)'), + array('Field' => 'field4', 'Type' => 'varchar(24)'), + array('Field' => 'field5', 'Type' => 'char(32)'), + array('Field' => 'field6', 'Type' => 'timestamp'), + ); + } +}
\ No newline at end of file diff --git a/plugins/DBStats/tests/UI/expected-ui-screenshots/DBStats_admin_page.png b/plugins/DBStats/tests/UI/expected-ui-screenshots/DBStats_admin_page.png Binary files differnew file mode 100644 index 0000000000..6d1e0ba667 --- /dev/null +++ b/plugins/DBStats/tests/UI/expected-ui-screenshots/DBStats_admin_page.png |