-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataProcessor.php
96 lines (76 loc) · 2.48 KB
/
DataProcessor.php
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
<?php
/**
* @license GNU GPL v3+
*/
namespace Piwik\Plugins\CampaignVisitorsByTime;
use Piwik\Site;
/**
* Processes visitor by grouping it by keyword and campaign
*/
class DataProcessor {
private $campaigns = [];
private $keywords = [];
private $siteTimezones = [];
public function processDatasets( \Zend_Db_Statement $dataSets ): void {
// @codingStandardsIgnoreStart
while ( $row = $dataSets->fetch() ) {
// @codingStandardsIgnoreEnd
$campaign = $row['referer_name'];
$keyword = $row['referer_keyword'];
$timestamp = $row['timestamp_floored'];
if ( !array_key_exists( $campaign, $this->campaigns ) ) {
$this->initCampaign( $campaign );
}
if ( !array_key_exists( $keyword, $this->keywords[$campaign] ) ) {
$this->initKeywordForCampaign( $campaign, $keyword );
}
$timestampFloored = $this->parseTimestamp(
$timestamp,
$this->getTimeZoneForSite( $row['idsite'] )
);
$this->campaigns[$campaign][$timestampFloored] += intval( $row['numVisitors'] );
$this->keywords[$campaign][$keyword][$timestampFloored] = intval( $row['numVisitors'] );
$this->campaigns[$campaign]['total'] += intval( $row['numVisitors'] );
$this->keywords[$campaign][$keyword]['total'] += intval( $row['numVisitors'] );
}
}
public function getCampaigns(): array {
return $this->campaigns;
}
public function getKeywords(): array {
return $this->keywords;
}
public function getCampaignKeywordData( $campaign ): array {
return $this->keywords[$campaign] ?? [];
}
private function initCampaign( $campaign ): void {
$this->campaigns[$campaign] = $this->initResultArray();
$this->keywords[$campaign] = [];
}
private function initKeywordForCampaign( $campaign, $keyword ): void {
$this->keywords[$campaign][$keyword] = $this->initResultArray();
}
private function initResultArray(): array {
$arr = [];
for ( $i = 0; $i < 24; $i++ ) {
$hour = str_pad( $i, 2, '0', STR_PAD_LEFT );
$arr[$hour . '00h'] = 0;
$arr[$hour . '15h'] = 0;
$arr[$hour . '30h'] = 0;
$arr[$hour . '45h'] = 0;
}
$arr['total'] = 0;
return $arr;
}
private function parseTimestamp( $mysqlTime, $siteTimeZone ): string {
$date = new \DateTime( $mysqlTime );
$date->setTimezone( new \DateTimeZone( $siteTimeZone ) );
return $date->format( 'Hi' ) . 'h';
}
protected function getTimeZoneForSite( $site ) {
if ( !isset( $this->siteTimezones[$site] ) ) {
$this->siteTimezones[$site] = Site::getTimezoneFor( $site );
}
return $this->siteTimezones[$site];
}
}