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 class Mage_Log_Model_Mysql4_Aggregation extends Mage_Core_Model_Mysql4_Abstract
00028 {
00029 public function _construct()
00030 {
00031 $this->_init('log/summary_table', 'log_summary_id');
00032 }
00033
00034 public function getLastRecordDate()
00035 {
00036 $select = $this->_getReadAdapter()->select()
00037 ->from($this->getTable('summary_table'), array('date'=>'MAX(add_date)'));
00038
00039 return $this->_getReadAdapter()->fetchOne($select);
00040 }
00041
00042 public function getCounts($from, $to, $store)
00043 {
00044 $result = array('customers'=>0, 'visitors'=>0);
00045 $select = $this->_getReadAdapter()->select()
00046 ->from($this->getTable('customer'), 'visitor_id')
00047 ->where('login_at >= ?', $from)
00048 ->where('login_at <= ?', $to);
00049 if ($store) {
00050 $select->where('store_id = ?', $store);
00051 }
00052
00053 $customers = $this->_getReadAdapter()->fetchCol($select);
00054 $result['customers'] = count($customers);
00055
00056
00057 $select = $this->_getReadAdapter()->select();
00058 $select->from($this->getTable('visitor'), 'COUNT(*)')
00059 ->where('first_visit_at >= ?', $from)
00060 ->where('first_visit_at <= ?', $to);
00061
00062 if ($store) {
00063 $select->where('store_id = ?', $store);
00064 }
00065 if ($result['customers']) {
00066 $select->where('visitor_id NOT IN(?)', $customers);
00067 }
00068
00069 $result['visitors'] = $this->_getReadAdapter()->fetchOne($select);
00070
00071
00072 return $result;
00073 }
00074
00075 public function saveLog($data, $id = null)
00076 {
00077 if (is_null($id)) {
00078 $this->_getWriteAdapter()->insert($this->getTable('summary_table'), $data);
00079 } else {
00080 $condition = $this->_getWriteAdapter()->quoteInto('summary_id = ?', $id);
00081 $this->_getWriteAdapter()->update($this->getTable('summary_table'), $data, $condition);
00082 }
00083 }
00084
00085 public function removeEmpty($date)
00086 {
00087 $condition = $this->_getWriteAdapter()->quoteInto('add_date < ? AND customer_count = 0 AND visitor_count = 0', $date);
00088 $this->_getWriteAdapter()->delete($this->getTable('summary_table'), $condition);
00089 }
00090
00091 public function getLogId($from, $to)
00092 {
00093 $select = $this->_getReadAdapter()->select()
00094 ->from($this->getTable('summary_table'), 'summary_id')
00095 ->where('add_date >= ?', $from)
00096 ->where('add_date <= ?', $to);
00097
00098 return $this->_getReadAdapter()->fetchOne($select);
00099 }
00100 }