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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\VisitTime;
use Piwik\DataArray;
use Piwik\DataTable;
use Piwik\Date;
class Archiver extends \Piwik\Plugin\Archiver
{
const SERVER_TIME_RECORD_NAME = 'VisitTime_serverTime';
const LOCAL_TIME_RECORD_NAME = 'VisitTime_localTime';
public function aggregateDayReport()
{
$this->aggregateByLocalTime();
$this->aggregateByServerTime();
}
public function aggregateMultipleReports()
{
$dataTableRecords = array(
self::LOCAL_TIME_RECORD_NAME,
self::SERVER_TIME_RECORD_NAME,
);
$columnsAggregationOperation = null;
$this->getProcessor()->aggregateDataTableRecords(
$dataTableRecords,
$maximumRowsInDataTableLevelZero = null,
$maximumRowsInSubDataTable = null,
$columnToSortByBeforeTruncation = null,
$columnsAggregationOperation,
$columnsToRenameAfterAggregation = null,
$countRowsRecursive = array());
}
protected function aggregateByServerTime()
{
$dataArray = $this->getLogAggregator()->getMetricsFromVisitByDimension(array("label" => "HOUR(log_visit.visit_first_action_time)"));
$query = $this->getLogAggregator()->queryConversionsByDimension(array("label" => "HOUR(log_conversion.server_time)"));
if ($query === false) {
return;
}
while ($conversionRow = $query->fetch()) {
$dataArray->sumMetricsGoals($conversionRow['label'], $conversionRow);
}
$dataArray->enrichMetricsWithConversions();
$dataArray = $this->convertTimeToLocalTimezone($dataArray);
$this->ensureAllHoursAreSet($dataArray);
$report = $dataArray->asDataTable()->getSerialized();
$this->getProcessor()->insertBlobRecord(self::SERVER_TIME_RECORD_NAME, $report);
}
protected function aggregateByLocalTime()
{
$array = $this->getLogAggregator()->getMetricsFromVisitByDimension("HOUR(log_visit.visitor_localtime)");
$this->ensureAllHoursAreSet($array);
$report = $array->asDataTable()->getSerialized();
$this->getProcessor()->insertBlobRecord(self::LOCAL_TIME_RECORD_NAME, $report);
}
protected function convertTimeToLocalTimezone(DataArray &$array)
{
$date = Date::factory($this->getProcessor()->getParams()->getDateStart()->getDateStartUTC())->toString();
$timezone = $this->getProcessor()->getParams()->getSite()->getTimezone();
$converted = array();
foreach ($array->getDataArray() as $hour => $stats) {
$datetime = $date . ' ' . $hour . ':00:00';
$hourInTz = (int)Date::factory($datetime, $timezone)->toString('H');
$converted[$hourInTz] = $stats;
}
return new DataArray($converted);
}
private function ensureAllHoursAreSet(DataArray &$array)
{
$data = $array->getDataArray();
for ($i = 0; $i <= 23; $i++) {
if (empty($data[$i])) {
$array->sumMetricsVisits($i, DataArray::makeEmptyRow());
}
}
}
}
|