PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/shell/log.php

https://bitbucket.org/mkrasuski/magento-ce
PHP | 191 lines | 111 code | 18 blank | 62 comment | 24 complexity | 2715cec50ab8441e6a6f55b0b8578f37 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Shell
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. require_once 'abstract.php';
  27. /**
  28. * Magento Log Shell Script
  29. *
  30. * @category Mage
  31. * @package Mage_Shell
  32. * @author Magento Core Team <core@magentocommerce.com>
  33. */
  34. class Mage_Shell_Log extends Mage_Shell_Abstract
  35. {
  36. /**
  37. * Log instance
  38. *
  39. * @var Mage_Log_Model_Log
  40. */
  41. protected $_log;
  42. /**
  43. * Retrieve Log instance
  44. *
  45. * @return Mage_Log_Model_Log
  46. */
  47. protected function _getLog()
  48. {
  49. if (is_null($this->_log)) {
  50. $this->_log = Mage::getModel('log/log');
  51. }
  52. return $this->_log;
  53. }
  54. /**
  55. * Convert count to human view
  56. *
  57. * @param int $number
  58. * @return string
  59. */
  60. protected function _humanCount($number)
  61. {
  62. if ($number < 1000) {
  63. return $number;
  64. } else if ($number >= 1000 && $number < 1000000) {
  65. return sprintf('%.2fK', $number / 1000);
  66. } else if ($number >= 1000000 && $number < 1000000000) {
  67. return sprintf('%.2fM', $number / 1000000);
  68. } else {
  69. return sprintf('%.2fB', $number / 1000000000);
  70. }
  71. }
  72. /**
  73. * Convert size to human view
  74. *
  75. * @param int $number
  76. * @return string
  77. */
  78. protected function _humanSize($number)
  79. {
  80. if ($number < 1000) {
  81. return sprintf('%d b', $number);
  82. } else if ($number >= 1000 && $number < 1000000) {
  83. return sprintf('%.2fKb', $number / 1000);
  84. } else if ($number >= 1000000 && $number < 1000000000) {
  85. return sprintf('%.2fMb', $number / 1000000);
  86. } else {
  87. return sprintf('%.2fGb', $number / 1000000000);
  88. }
  89. }
  90. /**
  91. * Run script
  92. *
  93. */
  94. public function run()
  95. {
  96. if ($this->getArg('clean')) {
  97. $days = $this->getArg('days');
  98. if ($days > 0) {
  99. Mage::app()->getStore()->setConfig(Mage_Log_Model_Log::XML_LOG_CLEAN_DAYS, $days);
  100. }
  101. $this->_getLog()->clean();
  102. echo "Log cleaned\n";
  103. } else if ($this->getArg('status')) {
  104. $resource = $this->_getLog()->getResource();
  105. $adapter = $resource->getReadConnection();
  106. // log tables
  107. $tables = array(
  108. $resource->getTable('log/customer'),
  109. $resource->getTable('log/visitor'),
  110. $resource->getTable('log/visitor_info'),
  111. $resource->getTable('log/url_table'),
  112. $resource->getTable('log/url_info_table'),
  113. $resource->getTable('log/quote_table'),
  114. $resource->getTable('reports/viewed_product_index'),
  115. $resource->getTable('reports/compared_product_index'),
  116. $resource->getTable('reports/event'),
  117. $resource->getTable('catalog/compare_item'),
  118. );
  119. $rows = 0;
  120. $dataLengh = 0;
  121. $indexLength = 0;
  122. $line = '-----------------------------------+------------+------------+------------+' . "\n";
  123. echo $line;
  124. echo sprintf('%-35s|', 'Table Name');
  125. echo sprintf(' %-11s|', 'Rows');
  126. echo sprintf(' %-11s|', 'Data Size');
  127. echo sprintf(' %-11s|', 'Index Size');
  128. echo "\n";
  129. echo $line;
  130. foreach ($tables as $table) {
  131. $query = $adapter->quoteInto('SHOW TABLE STATUS LIKE ?', $table);
  132. $status = $adapter->fetchRow($query);
  133. if (!$status) {
  134. continue;
  135. }
  136. $rows += $status['Rows'];
  137. $dataLengh += $status['Data_length'];
  138. $indexLength += $status['Index_length'];
  139. echo sprintf('%-35s|', $table);
  140. echo sprintf(' %-11s|', $this->_humanCount($status['Rows']));
  141. echo sprintf(' %-11s|', $this->_humanSize($status['Data_length']));
  142. echo sprintf(' %-11s|', $this->_humanSize($status['Index_length']));
  143. echo "\n";
  144. }
  145. echo $line;
  146. echo sprintf('%-35s|', 'Total');
  147. echo sprintf(' %-11s|', $this->_humanCount($rows));
  148. echo sprintf(' %-11s|', $this->_humanSize($dataLengh));
  149. echo sprintf(' %-11s|', $this->_humanSize($indexLength));
  150. echo "\n";
  151. echo $line;
  152. } else {
  153. echo $this->usageHelp();
  154. }
  155. }
  156. /**
  157. * Retrieve Usage Help Message
  158. *
  159. */
  160. public function usageHelp()
  161. {
  162. return <<<USAGE
  163. Usage: php -f log.php -- [options]
  164. php -f log.php -- clean --days 1
  165. clean Clean Logs
  166. --days <days> Save log, days. (Minimum 1 day, if defined - ignoring system value)
  167. status Display statistics per log tables
  168. help This help
  169. USAGE;
  170. }
  171. }
  172. $shell = new Mage_Shell_Log();
  173. $shell->run();