1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33: 34: 35: 36:
37: class Mage_Log_Model_Aggregation extends Mage_Core_Model_Abstract
38: {
39:
40: 41: 42: 43: 44:
45: protected $_lastRecord;
46:
47: 48: 49:
50: protected function _construct()
51: {
52: $this->_init('log/aggregation');
53: }
54:
55: 56: 57:
58: public function run()
59: {
60: $this->_lastRecord = $this->_timestamp($this->_round($this->getLastRecordDate()));
61: foreach (Mage::app()->getStores(false) as $store) {
62: $this->_process($store->getId());
63: }
64: }
65:
66: 67: 68: 69: 70: 71:
72: private function _removeEmpty($lastDate)
73: {
74: return $this->_getResource()->removeEmpty($lastDate);
75: }
76:
77: 78: 79: 80: 81: 82:
83: private function _process($store)
84: {
85: $lastDateRecord = null;
86: $start = $this->_lastRecord;
87: $end = time();
88: $date = $start;
89:
90: while ($date < $end) {
91: $to = $date + 3600;
92: $counts = $this->_getCounts($this->_date($date), $this->_date($to), $store);
93: $data = array(
94: 'store_id'=>$store,
95: 'visitor_count'=>$counts['visitors'],
96: 'customer_count'=>$counts['customers'],
97: 'add_date'=>$this->_date($date)
98: );
99:
100: if ($counts['visitors'] || $counts['customers']) {
101: $this->_save($data, $this->_date($date), $this->_date($to));
102: }
103:
104: $lastDateRecord = $date;
105: $date = $to;
106: }
107: return $lastDateRecord;
108: }
109:
110: 111: 112: 113: 114: 115: 116:
117: private function _save($data, $from, $to)
118: {
119: if ($logId = $this->_getResource()->getLogId($from, $to)) {
120: $this->_update($logId, $data);
121: } else {
122: $this->_insert($data);
123: }
124: }
125:
126: private function _update($id, $data)
127: {
128: return $this->_getResource()->saveLog($data, $id);
129: }
130:
131: private function _insert($data)
132: {
133: return $this->_getResource()->saveLog($data);
134: }
135:
136: private function _getCounts($from, $to, $store)
137: {
138: return $this->_getResource()->getCounts($from, $to, $store);
139: }
140:
141: public function getLastRecordDate()
142: {
143: $result = $this->_getResource()->getLastRecordDate();
144: if (!$result)
145: $result = $this->_date(strtotime('now - 2 months'));
146:
147: return $result;
148: }
149:
150: private function _date($in, $offset = null)
151: {
152: $out = $in;
153: if (is_numeric($in))
154: $out = date("Y-m-d H:i:s", $in);
155: return $out;
156: }
157:
158: private function _timestamp($in, $offset = null)
159: {
160: $out = $in;
161: if (!is_numeric($in))
162: $out = strtotime($in);
163: return $out;
164: }
165:
166: 167: 168: 169:
170: private function _round($in)
171: {
172: return date("Y-m-d H:00:00", $this->_timestamp($in));
173: }
174: }
175: