Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ArchiveProcessing.php « modules - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d600b196ff4de6ffcee110eeb5573a59e6799292 (plain)
1
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php

/**
 * The ArchiveProcessing module is a module that reads the Piwik logs from the DB and
 * compute all the reports, which are then stored in the database.
 * 
 * A record in the Database for a given report is defined by
 * - idarchive	= unique ID that is associated to all the data of this archive (idsite+period+date)
 * - idsite		= the ID of the website 
 * - date1 		= starting day of the period
 * - date2 		= ending day of the period
 * - period 	= integer that defines the period (day/week/etc.). @see period::getId()
 * - ts_archived = timestamp when the archive was processed
 * - name 		= the name of the report (ex: uniq_visitors or search_keywords_by_search_engines)
 * - value 		= the actual data
 */
require_once 'TablePartitioning.php';
require_once 'ArchiveProcessing/Record.php';
require_once 'DataTable.php';

abstract class Piwik_ArchiveProcessing
{
	const DONE_OK = 1;
	const DONE_ERROR = 2;


	protected $idArchive;
	protected $periodId;
	protected $dateStart;
	protected $dateEnd;
	protected $tableArchiveNumeric;
	protected $tableArchiveBlob;
	protected $maxTimestampArchive;
	
	// Attributes that can be used by plugins
	public $idsite	= null;
	public $period 	= null;
	public $site 	= null;
	
	public $strDateStart;
	public $strDateEnd;
	
	public $logTable;
	public $logVisitActionTable;
	public $logActionTable;


	protected function loadArchiveProperties()
	{		
		$this->idsite = $this->site->getId();
		
		$this->periodId = $this->period->getId();
		
		$this->dateStart = $this->period->getDateStart();
		$this->dateEnd = $this->period->getDateEnd();
		
		$this->tableArchiveNumeric = new Piwik_TablePartitioning_Monthly('archive_numeric');
		$this->tableArchiveNumeric->setTimestamp($this->dateStart->get());
		$this->tableArchiveBlob = new Piwik_TablePartitioning_Monthly('archive_blob');
		$this->tableArchiveBlob->setTimestamp($this->dateStart->get());

		$this->strDateStart = $this->dateStart->toString();
		$this->strDateEnd = $this->dateEnd->toString();
		
		$this->maxTimestampArchive = 0;
		if( $this->period->getNumberOfSubperiods() == 0
			&& $this->period->toString() == date("Y-m-d")
			)
		{
			$this->maxTimestampArchive = time() - Zend_Registry::get('config')->General->time_before_archive_considered_outdated;
		}
	}
	
	
	public function getTableArchiveNumericName()
	{
		return $this->tableArchiveNumeric;
	}
	public function getTableArchiveBlobName()
	{
		return $this->tableArchiveBlob;
	}
	
	
	// to be used only once
	public function setPeriod( Piwik_Period $period ) 
	{
		$this->period = $period;
	}
	
	
	public function setSite( Piwik_Site $site )
	{
		$this->site = $site;
	}
	
	public function loadArchive()
	{
		$this->loadArchiveProperties();
		$this->idArchive = $this->isArchived();
		if(!$this->idArchive)
		{
			$this->archivesSubperiods = $this->loadSubperiodsArchive();
			
			$this->initCompute();
			$this->compute();
			$this->postCompute();
			
			Piwik::log("New archive computed, id = {$this->idArchive}");
		}
		else
		{
			Piwik::log("Archive already available, id = {$this->idArchive}");
		}
		
		return $this->idArchive;
	}
	
	/**
	 * This methods reads the subperiods if necessary, 
	 * and computes the archive of the current period.
	 */
	abstract protected function compute();
	
	protected function initCompute()
	{
		$this->loadNextIdarchive();
		
		$record = new Piwik_ArchiveProcessing_Record_Numeric('done', Piwik_ArchiveProcessing::DONE_ERROR);
		$this->insertRecord( $record);
		$record->delete();
		
		$this->logTable 			= Piwik::prefixTable('log_visit');
		$this->logVisitActionTable 	= Piwik::prefixTable('log_link_visit_action');
		$this->logActionTable	 	= Piwik::prefixTable('log_action');
	}
	
	protected function postCompute()
	{
		
//		echo "<br>".Piwik_ArchiveProcessing_Record_Manager::getInstance()->toString();
		
		// delete the first done = ERROR 
		Zend_Registry::get('db')->query("
							DELETE FROM ".$this->tableArchiveNumeric." 
							WHERE idarchive = ? AND name = 'done'",
					array($this->idArchive)
				);
		
		$record = new Piwik_ArchiveProcessing_Record_Numeric('done', Piwik_ArchiveProcessing::DONE_OK);
		
		// save in the database the records
		$records = Piwik_ArchiveProcessing_Record_Manager::getInstance()->getRecords();
		
		foreach($records as $record)
		{
			$this->insertRecord( $record);	
		}
		
		// delete the records from the global manager
		foreach($records as $record)
		{
			$record->delete();	
		}
		unset($records);
		
		// we delete all tables from the table register
		Piwik_ArchiveProcessing_Record_Manager::getInstance()->deleteAll();
	} 
	
	
	protected function loadNextIdarchive()
	{
		$db = Zend_Registry::get('db');
		$id = $db->fetchOne("SELECT max(idarchive) FROM ".$this->tableArchiveNumeric);
		if(empty($id))
		{
			$id = 0;
		}
		$this->idArchive = $id + 1;
		
	}
	protected function insertRecord($record)
	{
		// table to use to save the data
		if(is_numeric($record->value))
		{
			$table = $this->tableArchiveNumeric;
		}
		else
		{
			$table = $this->tableArchiveBlob;
		}
		
		$query = "INSERT INTO ".$table." (idarchive, idsite, date1, date2, period, ts_archived, name, value)
					VALUES (?,?,?,?,?,?,?,?)";
		Zend_Registry::get('db')->query($query, 
							array(	$this->idArchive,
									$this->idsite, 
									$this->strDateStart, 
									$this->strDateEnd, 
									$this->periodId, 
									date("Y-m-d H:i:s"),
									$record->name,
									$record->value,
							)
					);
	}
	
	/**
	 * Returns the ID of the archived subperiods.
	 */
	protected function loadSubperiodsArchive()
	{
		$periods = array();
		
		// we first compute every subperiod of the archive
		foreach($this->period->getSubperiods() as $period)
		{
			$archivePeriod = new Piwik_Archive;
			$archivePeriod->setSite( $this->site );
			$archivePeriod->setPeriod( $period );
			$archivePeriod->prepareArchive();
			
			$periods[] = $archivePeriod;
		}
		
		return $periods;
	}
	
	protected function isArchived()
	{
//		Piwik::log("Is archive site=$idsite for period = ".$this->period->getLabel()." for date_start = $strDateStart ?");
		$bindSQL = array(	$this->idsite, 
								$this->strDateStart, 
								$this->strDateEnd, 
								$this->periodId, 
								);
		$timeStampWhere = '';
		if( $this->maxTimestampArchive != 0)
		{
			$timeStampWhere = " AND UNIX_TIMESTAMP(ts_archived) >= ? ";
			$bindSQL[] = $this->maxTimestampArchive;
		}
			
		$idarchive = Zend_Registry::get('db')->fetchOne("
						SELECT idarchive
						FROM ".$this->tableArchiveNumeric."
						WHERE idsite = ?
							AND date1 = ?
							AND date2 = ?
							AND period = ?
							AND name = 'done'
							AND value = ".Piwik_ArchiveProcessing::DONE_OK."
							$timeStampWhere
						ORDER BY ts_archived DESC",
						$bindSQL
					);
		if(!empty($idarchive))
		{
			return $idarchive;
		}
		else
		{
			return false;
		}
	}
	
	static function factory($name )
	{
		switch($name)
		{
			case 'day':
				require_once 'ArchiveProcessing/Day.php';			
				$process = new Piwik_ArchiveProcessing_Day;
			break;
			
			case 'week':
			case 'month':
			case 'year':
				require_once 'ArchiveProcessing/Period.php';	
				$process = new Piwik_ArchiveProcessing_Period;
			break;
			
			default:
				throw new Exception("Unknown period specified $name");
			break;
		}
		return $process;
	}
	
}
?>