PageRenderTime 114ms CodeModel.GetById 32ms RepoModel.GetById 3ms app.codeStats 0ms

/magento/app/code/core/Mage/Log/Model/Aggregation.php

https://bitbucket.org/jit_bec/shopifine
PHP | 174 lines | 88 code | 18 blank | 68 comment | 8 complexity | 7aac64f627713f8de899f5fe9a55d411 MD5 | raw file
Possible License(s): LGPL-3.0
  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@magentocommerce.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.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Log
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Log Aggregation Model
  28. *
  29. * @method Mage_Log_Model_Resource_Aggregation getResource()
  30. * @method Mage_Log_Model_Resource_Aggregation _getResource()
  31. *
  32. * @category Mage
  33. * @package Mage_Log
  34. * @author Magento Core Team <core@magentocommerce.com>
  35. */
  36. class Mage_Log_Model_Aggregation extends Mage_Core_Model_Abstract
  37. {
  38. /**
  39. * Last record data
  40. *
  41. * @var string
  42. */
  43. protected $_lastRecord;
  44. /**
  45. * Init model
  46. */
  47. protected function _construct()
  48. {
  49. $this->_init('log/aggregation');
  50. }
  51. /**
  52. * Run action
  53. */
  54. public function run()
  55. {
  56. $this->_lastRecord = $this->_timestamp($this->_round($this->getLastRecordDate()));
  57. foreach (Mage::app()->getStores(false) as $store) {
  58. $this->_process($store->getId());
  59. }
  60. }
  61. /**
  62. * Remove empty records before $lastDate
  63. *
  64. * @param string $lastDate
  65. * @return void
  66. */
  67. private function _removeEmpty($lastDate)
  68. {
  69. return $this->_getResource()->removeEmpty($lastDate);
  70. }
  71. /**
  72. * Process
  73. *
  74. * @param int $store
  75. * @return mixed
  76. */
  77. private function _process($store)
  78. {
  79. $lastDateRecord = null;
  80. $start = $this->_lastRecord;
  81. $end = time();
  82. $date = $start;
  83. while ($date < $end) {
  84. $to = $date + 3600;
  85. $counts = $this->_getCounts($this->_date($date), $this->_date($to), $store);
  86. $data = array(
  87. 'store_id'=>$store,
  88. 'visitor_count'=>$counts['visitors'],
  89. 'customer_count'=>$counts['customers'],
  90. 'add_date'=>$this->_date($date)
  91. );
  92. if ($counts['visitors'] || $counts['customers']) {
  93. $this->_save($data, $this->_date($date), $this->_date($to));
  94. }
  95. $lastDateRecord = $date;
  96. $date = $to;
  97. }
  98. return $lastDateRecord;
  99. }
  100. /**
  101. * Save log data
  102. *
  103. * @param array $data
  104. * @param string $from
  105. * @param string $to
  106. */
  107. private function _save($data, $from, $to)
  108. {
  109. if ($logId = $this->_getResource()->getLogId($from, $to)) {
  110. $this->_update($logId, $data);
  111. } else {
  112. $this->_insert($data);
  113. }
  114. }
  115. private function _update($id, $data)
  116. {
  117. return $this->_getResource()->saveLog($data, $id);
  118. }
  119. private function _insert($data)
  120. {
  121. return $this->_getResource()->saveLog($data);
  122. }
  123. private function _getCounts($from, $to, $store)
  124. {
  125. return $this->_getResource()->getCounts($from, $to, $store);
  126. }
  127. public function getLastRecordDate()
  128. {
  129. $result = $this->_getResource()->getLastRecordDate();
  130. if (!$result)
  131. $result = $this->_date(strtotime('now - 2 months'));
  132. return $result;
  133. }
  134. private function _date($in, $offset = null)
  135. {
  136. $out = $in;
  137. if (is_numeric($in))
  138. $out = date("Y-m-d H:i:s", $in);
  139. return $out;
  140. }
  141. private function _timestamp($in, $offset = null)
  142. {
  143. $out = $in;
  144. if (!is_numeric($in))
  145. $out = strtotime($in);
  146. return $out;
  147. }
  148. /**
  149. * @param $in
  150. * @return string
  151. */
  152. private function _round($in)
  153. {
  154. return date("Y-m-d H:00:00", $this->_timestamp($in));
  155. }
  156. }