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

Period.php « ArchiveProcessing « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0ca9ce171b14e62bd71f36b818a233417696e4cf (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik
 * @package Piwik
 */

/**
 * Handles the archiving process for a period
 *
 * This class provides generic methods to archive data for a period (week / month / year).
 *
 * These methods are called by the plugins that do the logic of archiving their own data. \
 * They hook on the event 'ArchiveProcessing_Period.compute'
 *
 * @package Piwik
 * @subpackage Piwik_ArchiveProcessing
 */
class Piwik_ArchiveProcessing_Period extends Piwik_ArchiveProcessing
{
    /**
     * Array of (column name before => column name renamed) of the columns for which sum operation is invalid.
     * The summed value is not accurate and these columns will be renamed accordingly.
     * @var array
     */
    static public $invalidSummedColumnNameToRenamedName = array(
        Piwik_Archive::INDEX_NB_UNIQ_VISITORS => Piwik_Archive::INDEX_SUM_DAILY_NB_UNIQ_VISITORS
    );

    /**
     * @var Piwik_Archive_Single[]
     */
    public $archives = array();

    /**
     * Sums all values for the given field names $aNames over the period
     * See @archiveNumericValuesGeneral for more information
     *
     * @param string|array $aNames
     * @return array
     */
    public function archiveNumericValuesSum($aNames)
    {
        return $this->archiveNumericValuesGeneral($aNames, 'sum');
    }

    /**
     * Get the maximum value for all values for the given field names $aNames over the period
     * See @archiveNumericValuesGeneral for more information
     *
     * @param string|array $aNames
     * @return array
     */
    public function archiveNumericValuesMax($aNames)
    {
        return $this->archiveNumericValuesGeneral($aNames, 'max');
    }

    /**
     * Given a list of fields names, the method will fetch all their values over the period, and archive them using the given operation.
     *
     * For example if $operationToApply = 'sum' and $aNames = array('nb_visits', 'sum_time_visit')
     *  it will sum all values of nb_visits for the period (for example give the number of visits for the month by summing the visits of every day)
     *
     * @param array|string $aNames            Array of strings or string containg the field names to select
     * @param string $operationToApply  Available operations = sum, max, min
     * @throws Exception
     * @return array
     */
    private function archiveNumericValuesGeneral($aNames, $operationToApply)
    {
        $this->loadSubPeriods();
        if (!is_array($aNames)) {
            $aNames = array($aNames);
        }

        // fetch the numeric values and apply the operation on them
        $results = array();
        foreach ($this->archives as $id => $archive) {
            foreach ($aNames as $name) {
                if (!isset($results[$name])) {
                    $results[$name] = 0;
                }
                if ($name == 'nb_uniq_visitors') continue;

                $valueToSum = $archive->getNumeric($name);

                if ($valueToSum !== false) {
                    switch ($operationToApply) {
                        case 'sum':
                            $results[$name] += $valueToSum;
                            break;
                        case 'max':
                            $results[$name] = max($results[$name], $valueToSum);
                            break;
                        case 'min':
                            $results[$name] = min($results[$name], $valueToSum);
                            break;
                        default:
                            throw new Exception("Operation not applicable.");
                            break;
                    }
                }
            }
        }

        if (!Piwik::isUniqueVisitorsEnabled($this->period->getLabel())) {
            unset($results['nb_uniq_visitors']);
        }

        foreach ($results as $name => $value) {
            if ($name == 'nb_uniq_visitors') {
                $value = (float)$this->computeNbUniqVisitors();
            }
            $this->insertRecord($name, $value);
        }

        // if asked for only one field to sum
        if (count($results) == 1) {
            return $results[$name];
        }

        // returns the array of records once summed
        return $results;
    }

    /**
     * This method will compute the sum of DataTables over the period for the given fields $aRecordName.
     * The resulting DataTable will be then added to queue of data to be recorded in the database.
     * It will usually be called in a plugin that listens to the hook 'ArchiveProcessing_Period.compute'
     *
     * For example if $aRecordName = 'UserCountry_country' the method will select all UserCountry_country DataTable for the period
     * (eg. the 31 dataTable of the last month), sum them, then record it in the DB
     *
     *
     * This method works on recursive dataTable. For example for the 'Actions' it will select all subtables of all dataTable of all the sub periods
     *  and get the sum.
     *
     * It returns an array that gives information about the "final" DataTable. The array gives for every field name, the number of rows in the
     *  final DataTable (ie. the number of distinct LABEL over the period) (eg. the number of distinct keywords over the last month)
     *
     * @param string|array $aRecordName                           Field name(s) of DataTable to select so we can get the sum
     * @param array $invalidSummedColumnNameToRenamedName  (current_column_name => new_column_name) for columns that must change names when summed
     *                                                             (eg. unique visitors go from nb_uniq_visitors to sum_daily_nb_uniq_visitors)
     * @param int $maximumRowsInDataTableLevelZero       Max row count of parent datatable to archive
     * @param int $maximumRowsInSubDataTable             Max row count of children datatable(s) to archive
     * @param string $columnToSortByBeforeTruncation        Column name to sort by, before truncating rows (ie. if there are more rows than the specified max row count)
     *
     * @return array  array (
     *                    nameTable1 => number of rows,
     *                nameTable2 => number of rows,
     *                )
     */
    public function archiveDataTable($aRecordName,
                                     $invalidSummedColumnNameToRenamedName = null,
                                     $maximumRowsInDataTableLevelZero = null,
                                     $maximumRowsInSubDataTable = null,
                                     $columnToSortByBeforeTruncation = null)
    {
        // We clean up below all tables created during this function call (and recursive calls)
        $latestUsedTableId = Piwik_DataTable_Manager::getInstance()->getMostRecentTableId();

        $this->loadSubPeriods();
        if (!is_array($aRecordName)) {
            $aRecordName = array($aRecordName);
        }

        $nameToCount = array();
        foreach ($aRecordName as $recordName) {
            $table = $this->getRecordDataTableSum($recordName, $invalidSummedColumnNameToRenamedName);

            $nameToCount[$recordName]['level0'] = $table->getRowsCount();
            $nameToCount[$recordName]['recursive'] = $table->getRowsCountRecursive();

            $blob = $table->getSerialized($maximumRowsInDataTableLevelZero, $maximumRowsInSubDataTable, $columnToSortByBeforeTruncation);
            destroy($table);
            $this->insertBlobRecord($recordName, $blob);
        }
        Piwik_DataTable_Manager::getInstance()->deleteAll($latestUsedTableId);

        return $nameToCount;
    }

    /**
     * This method selects all DataTables that have the name $name over the period.
     * It calls the appropriate methods that sum all these tables together.
     * The resulting DataTable is returned.
     *
     * @param string $name
     * @param array $invalidSummedColumnNameToRenamedName  columns in the array (old name, new name) to be renamed as the sum operation is not valid on them (eg. nb_uniq_visitors->sum_daily_nb_uniq_visitors)
     * @return Piwik_DataTable
     */
    protected function getRecordDataTableSum($name, $invalidSummedColumnNameToRenamedName)
    {
        $table = new Piwik_DataTable();
        foreach ($this->archives as $archive) {
            $archive->preFetchBlob($name);
            $datatableToSum = $archive->getDataTable($name);
            $archive->loadSubDataTables($name, $datatableToSum);
            $table->addDataTable($datatableToSum);
            $archive->freeBlob($name);
        }

        if (is_null($invalidSummedColumnNameToRenamedName)) {
            $invalidSummedColumnNameToRenamedName = self::$invalidSummedColumnNameToRenamedName;
        }
        foreach ($invalidSummedColumnNameToRenamedName as $oldName => $newName) {
            $table->renameColumn($oldName, $newName);
        }
        return $table;
    }

    protected function initCompute()
    {
        parent::initCompute();
    }

    /**
     * Returns the ID of the archived subperiods.
     *
     * @return array  Array of the idArchive of the subperiods
     */
    protected function loadSubperiodsArchive()
    {
        $periods = array();

        // we first compute every subperiod of the archive
        foreach ($this->period->getSubperiods() as $period) {
            $archivePeriod = new Piwik_Archive_Single();
            $archivePeriod->setSite($this->site);
            $archivePeriod->setPeriod($period);
            $archivePeriod->setSegment($this->getSegment());
            $archivePeriod->setRequestedReport($this->getRequestedReport());

            $periods[] = $archivePeriod;
        }
        return $periods;
    }

    /**
     * Main method to process logs for a period.
     * The only logic done here is computing the number of visits, actions, etc.
     *
     * All the other reports are computed inside plugins listening to the event 'ArchiveProcessing_Period.compute'.
     * See some of the plugins for an example.
     */
    protected function compute()
    {
        if (!$this->isThereSomeVisits()) {
            return;
        }
        Piwik_PostEvent('ArchiveProcessing_Period.compute', $this);
    }

    protected function loadSubPeriods()
    {
        if (empty($this->archives)) {
            $this->archives = $this->loadSubperiodsArchive();
        }
    }

    /**
     *
     * @see Piwik_ArchiveProcessing_Day::isThereSomeVisits()
     * @return bool|null
     */
    public function isThereSomeVisits()
    {
        if (!is_null($this->isThereSomeVisits)) {
            return $this->isThereSomeVisits;
        }

        $this->loadSubPeriods();
        if (self::getPluginBeingProcessed($this->getRequestedReport()) == 'VisitsSummary'
            || $this->shouldProcessReportsAllPlugins($this->getSegment(), $this->period)
        ) {
            $toSum = self::getCoreMetrics();
            $record = $this->archiveNumericValuesSum($toSum);
            $this->archiveNumericValuesMax('max_actions');

            $nbVisitsConverted = $record['nb_visits_converted'];
            $nbVisits = $record['nb_visits'];
        } else {
            $archive = new Piwik_Archive_Single();
            $archive->setSite($this->site);
            $archive->setPeriod($this->period);
            $archive->setSegment($this->getSegment());

            $nbVisits = $archive->getNumeric('nb_visits');
            $nbVisitsConverted = 0;
            if ($nbVisits > 0) {
                $nbVisitsConverted = $archive->getNumeric('nb_visits_converted');
            }
        }

        $this->setNumberOfVisits($nbVisits);
        $this->setNumberOfVisitsConverted($nbVisitsConverted);
        $this->isThereSomeVisits = ($nbVisits > 0);
        return $this->isThereSomeVisits;
    }

    /**
     * Processes number of unique visitors for the given period
     *
     * This is the only metric we process from the logs directly,
     * since unique visitors cannot be summed like other metrics.
     *
     * @return int
     */
    protected function computeNbUniqVisitors()
    {
        $select = "count(distinct log_visit.idvisitor) as nb_uniq_visitors";
        $from = "log_visit";
        $where = "log_visit.visit_last_action_time >= ?
	    		AND log_visit.visit_last_action_time <= ? 
	    		AND log_visit.idsite = ?";

        $bind = array($this->getStartDatetimeUTC(), $this->getEndDatetimeUTC(), $this->idsite);

        $query = $this->getSegment()->getSelectQuery($select, $from, $where, $bind);

        return Zend_Registry::get('db')->fetchOne($query['sql'], $query['bind']);
    }

    /**
     * Called at the end of the archiving process.
     * Does some cleaning job in the database.
     */
    protected function postCompute()
    {
        parent::postCompute();

        $numericTable = $this->tableArchiveNumeric->getTableName();
        self::doPurgeOutdatedArchives($numericTable, $this->isArchiveTemporary());

        if (!isset($this->archives)) {
            return;
        }
        foreach ($this->archives as $archive) {
            destroy($archive);
        }
        $this->archives = array();
    }

    const FLAG_TABLE_PURGED = 'lastPurge_';

    // Used to disable Purge Outdated reports during test data setup
    static public $enablePurgeOutdated = true;

    /**
     * Given a monthly archive table, will delete all reports that are now outdated,
     * or reports that ended with an error
     */
    static public function doPurgeOutdatedArchives($numericTable)
    {
        if (!self::$enablePurgeOutdated) {
            return;
        }
        $blobTable = str_replace("numeric", "blob", $numericTable);
        $key = self::FLAG_TABLE_PURGED . $blobTable;
        $timestamp = Piwik_GetOption($key);

        // we shall purge temporary archives after their timeout is finished, plus an extra 6 hours
        // in case archiving is disabled or run once a day, we give it this extra time to run
        // and re-process more recent records...
        // TODO: Instead of hardcoding 6 we should put the actual number of hours between 2 archiving runs
        $temporaryArchivingTimeout = self::getTodayArchiveTimeToLive();
        $purgeEveryNSeconds = max($temporaryArchivingTimeout, 6 * 3600);

        // we only delete archives if we are able to process them, otherwise, the browser might process reports
        // when &segment= is specified (or custom date range) and would below, delete temporary archives that the
        // browser is not able to process until next cron run (which could be more than 1 hour away)
        if (self::isRequestAuthorizedToArchive()
            && (!$timestamp
                || $timestamp < time() - $purgeEveryNSeconds)
        ) {
            Piwik_SetOption($key, time());

            // If Browser Archiving is enabled, it is likely there are many more temporary archives
            // We delete more often which is safe, since reports are re-processed on demand
            if (self::isBrowserTriggerArchivingEnabled()) {
                $purgeArchivesOlderThan = Piwik_Date::factory(time() - 2 * $temporaryArchivingTimeout)->getDateTime();
            } // If archive.php via Cron is building the reports, we should keep all temporary reports from today
            else {
                $purgeArchivesOlderThan = Piwik_Date::factory('today')->getDateTime();
            }
            $result = Piwik_FetchAll("
				SELECT idarchive
				FROM $numericTable
				WHERE name LIKE 'done%'
					AND ((  value = " . Piwik_ArchiveProcessing::DONE_OK_TEMPORARY . "
						    AND ts_archived < ?)
						 OR value = " . Piwik_ArchiveProcessing::DONE_ERROR . ")",
                array($purgeArchivesOlderThan)
            );

            $idArchivesToDelete = array();
            if (!empty($result)) {
                foreach ($result as $row) {
                    $idArchivesToDelete[] = $row['idarchive'];
                }
                $query = "DELETE
    						FROM %s
    						WHERE idarchive IN (" . implode(',', $idArchivesToDelete) . ")
    						";

                Piwik_Query(sprintf($query, $numericTable));

                // Individual blob tables could be missing
                try {
                    Piwik_Query(sprintf($query, $blobTable));
                } catch (Exception $e) {
                }
            }
            Piwik::log("Purging temporary archives: done [ purged archives older than $purgeArchivesOlderThan from $blobTable and $numericTable ] [Deleted IDs: " . implode(',', $idArchivesToDelete) . "]");

            // Deleting "Custom Date Range" reports after 1 day, since they can be re-processed
            // and would take up unecessary space
            $yesterday = Piwik_Date::factory('yesterday')->getDateTime();
            $query = "DELETE
    					FROM %s
    					WHERE period = ?
    						AND ts_archived < ?";
            $bind = array(Piwik::$idPeriods['range'], $yesterday);
            Piwik::log("Purging Custom Range archives: done [ purged archives older than $yesterday from $blobTable and $numericTable ]");

            Piwik_Query(sprintf($query, $numericTable), $bind);

            // Individual blob tables could be missing
            try {
                Piwik_Query(sprintf($query, $blobTable), $bind);
            } catch (Exception $e) {
            }

            // these tables will be OPTIMIZEd daily in a scheduled task, to claim lost space
        } else {
            Piwik::log("Purging temporary archives: skipped.");
        }
    }
}