PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Log/Model/Mysql4/Visitor/Aggregator.php

https://bitbucket.org/MXWest/magento-ce-1.5.1.0
PHP | 189 lines | 96 code | 29 blank | 64 comment | 7 complexity | e9c15aceab07dab7fae762ee0628d6c9 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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) 2010 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 visitor aggregator resource
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. class Mage_Log_Model_Mysql4_Visitor_Aggregator
  32. {
  33. /**
  34. * Visitor data table name
  35. *
  36. * @var string
  37. */
  38. protected $_visitorTable;
  39. /**
  40. * Customer data table
  41. *
  42. * @var string
  43. */
  44. protected $_customerTable;
  45. /**
  46. * Log URL data table name.
  47. *
  48. * @var string
  49. */
  50. protected $_urlTable;
  51. /**
  52. * Aggregator data table.
  53. *
  54. * @var string
  55. */
  56. protected $_summaryTable;
  57. /**
  58. * Aggregator type data table.
  59. *
  60. * @var string
  61. */
  62. protected $_summaryTypeTable;
  63. /**
  64. * Database read connection
  65. *
  66. * @var Zend_Db_Adapter_Abstract
  67. */
  68. protected $_read;
  69. /**
  70. * Database write connection
  71. *
  72. * @var Zend_Db_Adapter_Abstract
  73. */
  74. protected $_write;
  75. public function __construct()
  76. {
  77. $resource = Mage::getSingleton('core/resource');
  78. $this->_visitorTable = $resource->getTableName('log/visitor');
  79. $this->_urlTable = $resource->getTableName('log/url_table');
  80. $this->_customerTable = $resource->getTableName('log/customer');
  81. $this->_summaryTable = $resource->getTableName('log/summary_table');
  82. $this->_summaryTypeTable= $resource->getTableName('log/summary_type_table');
  83. $this->_read = $resource->getConnection('log_read');
  84. $this->_write = $resource->getConnection('log_write');
  85. }
  86. public function update()
  87. {
  88. $types = $this->_getSummaryTypes();
  89. foreach( $types as $type ) {
  90. $this->_update($type);
  91. }
  92. }
  93. protected function _getSummaryTypes()
  94. {
  95. $types = $this->_read->fetchAll("SELECT type_id, period, period_type FROM {$this->_summaryTypeTable}");
  96. return $types;
  97. }
  98. protected function _update($type)
  99. {
  100. $countSelect = $this->_read->select()
  101. ->from($this->_summaryTable, 'summary_id')
  102. ->where('type_id=?', $type['type_id'])
  103. ->having("('".now()."' - INTERVAL {$type['period']} {$type['period_type']}) <= MAX(add_date)");
  104. $summaryIds = $this->_read->fetchCol($countSelect);
  105. $customerSelect = $this->_read->select()
  106. ->from($this->_customerTable, 'visitor_id')
  107. ->where("? - INTERVAL {$type['period']} {$type['period_type']} <= login_at", now())
  108. ->where("logout_at IS NULL OR logout_at <= ? - INTERVAL {$type['period']} {$type['period_type']}", now());
  109. $customers = $this->_read->fetchCol($customerSelect);
  110. $customerCount = count($customers);
  111. $customers = ( $customerCount > 0 ) ? $customers : 0;
  112. $customersCondition = $this->_read->quoteInto('visitor_id NOT IN(?)', $customers);
  113. $visitorCount = $this->_read->fetchOne("SELECT COUNT(visitor_id) FROM {$this->_visitorTable} WHERE ('".now()."' - INTERVAL {$type['period']} {$type['period_type']}) <= first_visit_at OR (NOW() - INTERVAL {$type['period']} {$type['period_type']}) <= last_visit_at AND {$customersCondition}");
  114. if( $customerCount == 0 && $visitorCount == 0 ) {
  115. return;
  116. }
  117. $data = array(
  118. 'type_id' => $type['type_id'],
  119. 'visitor_count' => $visitorCount,
  120. 'customer_count' => $customerCount,
  121. 'add_date' => now()
  122. );
  123. if(count($summaryIds)==0) {
  124. $this->_write->insert($this->_summaryTable, $data);
  125. } else {
  126. $conditionSql = $this->_write->quoteInto('summary_id in (?)', $summaryIds);
  127. $this->_write->update($this->_summaryTable, $data, $conditionSql);
  128. }
  129. }
  130. public function updateOneshot($minutes=60, $interval=300)
  131. {
  132. $last_update = $this->_read->fetchOne("SELECT UNIX_TIMESTAMP(MAX(add_date)) FROM {$this->_summaryTable} WHERE type_id IS NULL");
  133. $next_update = $last_update + $interval;
  134. if( time() >= $next_update ) {
  135. $stats = $this->_read->fetchAssoc("SELECT
  136. u.visit_time,
  137. v.visitor_id,
  138. c.customer_id,
  139. ROUND( (UNIX_TIMESTAMP(u.visit_time) - UNIX_TIMESTAMP(".now()." - INTERVAL {$minutes} MINUTE )) / {$interval} ) as _diff,
  140. COUNT(DISTINCT(v.visitor_id)) as visitor_count,
  141. COUNT(DISTINCT(c.customer_id)) as customer_count
  142. FROM
  143. {$this->_urlTable} u
  144. LEFT JOIN {$this->_visitorTable} v ON(v.visitor_id = u.visitor_id)
  145. LEFT JOIN {$this->_customerTable} c on(c.visitor_id = v.visitor_id)
  146. WHERE
  147. UNIX_TIMESTAMP(u.visit_time) > {$next_update}
  148. group by _diff");
  149. foreach( $stats as $stat ) {
  150. $data = array(
  151. 'type_id' => new Zend_Db_Expr('NULL'),
  152. 'visitor_count' => $stat['visitor_count'],
  153. 'customer_count' => $stat['customer_count'],
  154. 'add_date' => $stat['visit_time']
  155. );
  156. $this->_write->insert($this->_summaryTable, $data);
  157. }
  158. }
  159. }
  160. }