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_Aggregation extends Mage_Core_Model_Abstract
00028 {
00029 protected function _construct()
00030 {
00031 $this->_init('log/aggregation');
00032 }
00033
00034 public function run()
00035 {
00036 $this->_lastRecord = $this->_timestamp($this->_round($this->getLastRecordDate()));
00037 $stores = Mage::getResourceModel('core/store_collection');
00038
00039 foreach ($stores as $store) {
00040 $this->_process($store->getId());
00041 }
00042 }
00043
00044 private function _removeEmpty($last)
00045 {
00046 return $this->_getResource()->removeEmpty($last);
00047 }
00048
00049 private function _process($store)
00050 {
00051 $lastDateRecord = null;
00052 $start = $this->_lastRecord;
00053 $end = time();
00054 $date = $start;
00055
00056 while($date < $end){
00057 $to = $date + 3600;
00058 $counts = $this->_getCounts($this->_date($date), $this->_date($to), $store);
00059 $data = array(
00060 'store_id'=>$store,
00061 'visitor_count'=>$counts['visitors'],
00062 'customer_count'=>$counts['customers'],
00063 'add_date'=>$this->_date($date)
00064 );
00065
00066 if ($counts['visitors'] || $counts['customers']) {
00067 $this->_save($data, $this->_date($date), $this->_date($to));
00068 }
00069
00070 $lastDateRecord = $date;
00071 $date = $to;
00072 }
00073 return $lastDateRecord;
00074 }
00075
00076 private function _save($data, $from, $to)
00077 {
00078 if ($logId = $this->_getResource()->getLogId($from, $to)) {
00079 $this->_update($logId, $data);
00080 } else {
00081 $this->_insert($data);
00082 }
00083 }
00084
00085 private function _update($id, $data)
00086 {
00087 return $this->_getResource()->saveLog($data, $id);
00088 }
00089
00090 private function _insert($data)
00091 {
00092 return $this->_getResource()->saveLog($data);
00093 }
00094
00095 private function _getCounts($from, $to, $store)
00096 {
00097 return $this->_getResource()->getCounts($from, $to, $store);
00098 }
00099
00100 public function getLastRecordDate()
00101 {
00102 $result = $this->_getResource()->getLastRecordDate();
00103 if (!$result)
00104 $result = $this->_date(strtotime('now - 2 months'));
00105
00106 return $result;
00107 }
00108
00109 private function _date($in, $offset = null)
00110 {
00111 $out = $in;
00112 if (is_numeric($in))
00113 $out = date("Y-m-d H:i:s", $in);
00114 return $out;
00115 }
00116
00117 private function _timestamp($in, $offset = null)
00118 {
00119 $out = $in;
00120 if (!is_numeric($in))
00121 $out = strtotime($in);
00122 return $out;
00123 }
00124
00125 private function _round($in)
00126 {
00127 return date("Y-m-d H:00:00", $this->_timestamp($in));
00128 }
00129 }