00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class Mage_Log_Model_Mysql4_Visitor_Aggregator
00034 {
00035
00036
00037
00038
00039
00040 protected $_visitorTable;
00041
00042
00043
00044
00045
00046
00047 protected $_customerTable;
00048
00049
00050
00051
00052
00053
00054 protected $_urlTable;
00055
00056
00057
00058
00059
00060
00061 protected $_summaryTable;
00062
00063
00064
00065
00066
00067
00068 protected $_summaryTypeTable;
00069
00070
00071
00072
00073
00074
00075 protected $_read;
00076
00077
00078
00079
00080
00081
00082 protected $_write;
00083
00084 public function __construct()
00085 {
00086 $resource = Mage::getSingleton('core/resource');
00087
00088 $this->_visitorTable = $resource->getTableName('log/visitor');
00089 $this->_urlTable = $resource->getTableName('log/url_table');
00090 $this->_customerTable = $resource->getTableName('log/customer');
00091 $this->_summaryTable = $resource->getTableName('log/summary_table');
00092 $this->_summaryTypeTable= $resource->getTableName('log/summary_type_table');
00093
00094 $this->_read = $resource->getConnection('log_read');
00095 $this->_write = $resource->getConnection('log_write');
00096 }
00097
00098 public function update()
00099 {
00100 $types = $this->_getSummaryTypes();
00101 foreach( $types as $type ) {
00102 $this->_update($type);
00103 }
00104 }
00105
00106 protected function _getSummaryTypes()
00107 {
00108 $types = $this->_read->fetchAll("SELECT type_id, period, period_type FROM {$this->_summaryTypeTable}");
00109 return $types;
00110 }
00111
00112 protected function _update($type)
00113 {
00114 $countSelect = $this->_read->select()
00115 ->from($this->_summaryTable, 'summary_id')
00116 ->where('type_id=?', $type['type_id'])
00117 ->having("('".now()."' - INTERVAL {$type['period']} {$type['period_type']}) <= MAX(add_date)");
00118
00119 $summaryIds = $this->_read->fetchCol($countSelect);
00120
00121 $customerSelect = $this->_read->select()
00122 ->from($this->_customerTable, 'visitor_id')
00123 ->where("? - INTERVAL {$type['period']} {$type['period_type']} <= login_at", now())
00124 ->where("logout_at IS NULL OR logout_at <= ? - INTERVAL {$type['period']} {$type['period_type']}", now());
00125
00126 $customers = $this->_read->fetchCol($customerSelect);
00127
00128 $customerCount = count($customers);
00129
00130 $customers = ( $customerCount > 0 ) ? $customers : 0;
00131
00132 $customersCondition = $this->_read->quoteInto('visitor_id NOT IN(?)', $customers);
00133 $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}");
00134
00135 if( $customerCount == 0 && $visitorCount == 0 ) {
00136 return;
00137 }
00138
00139 $data = array(
00140 'type_id' => $type['type_id'],
00141 'visitor_count' => $visitorCount,
00142 'customer_count' => $customerCount,
00143 'add_date' => now()
00144 );
00145
00146
00147 if(count($summaryIds)==0) {
00148 $this->_write->insert($this->_summaryTable, $data);
00149 } else {
00150 $conditionSql = $this->_write->quoteInto('summary_id in (?)', $summaryIds);
00151 $this->_write->update($this->_summaryTable, $data, $conditionSql);
00152 }
00153
00154 }
00155
00156 public function updateOneshot($minutes=60, $interval=300)
00157 {
00158 $last_update = $this->_read->fetchOne("SELECT UNIX_TIMESTAMP(MAX(add_date)) FROM {$this->_summaryTable} WHERE type_id IS NULL");
00159 $next_update = $last_update + $interval;
00160
00161 if( time() >= $next_update ) {
00162 $stats = $this->_read->fetchAssoc("SELECT
00163 u.visit_time,
00164 v.visitor_id,
00165 c.customer_id,
00166 ROUND( (UNIX_TIMESTAMP(u.visit_time) - UNIX_TIMESTAMP(".now()." - INTERVAL {$minutes} MINUTE )) / {$interval} ) as _diff,
00167 COUNT(DISTINCT(v.visitor_id)) as visitor_count,
00168 COUNT(DISTINCT(c.customer_id)) as customer_count
00169 FROM
00170 {$this->_urlTable} u
00171 LEFT JOIN {$this->_visitorTable} v ON(v.visitor_id = u.visitor_id)
00172 LEFT JOIN {$this->_customerTable} c on(c.visitor_id = v.visitor_id)
00173 WHERE
00174 UNIX_TIMESTAMP(u.visit_time) > {$next_update}
00175 group by _diff");
00176
00177 foreach( $stats as $stat ) {
00178 $data = array(
00179 'type_id' => new Zend_Db_Expr('NULL'),
00180 'visitor_count' => $stat['visitor_count'],
00181 'customer_count' => $stat['customer_count'],
00182 'add_date' => $stat['visit_time']
00183 );
00184 $this->_write->insert($this->_summaryTable, $data);
00185 }
00186 }
00187
00188 }
00189 }