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
|
<?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\UserCountry;
use Piwik\ArchiveProcessor;
use Piwik\DataArray;
use Piwik\DataTable;
use Piwik\Metrics;
use Piwik\Plugins\UserCountry\LocationProvider;
class Archiver extends \Piwik\Plugin\Archiver
{
const COUNTRY_RECORD_NAME = 'UserCountry_country';
const REGION_RECORD_NAME = 'UserCountry_region';
const CITY_RECORD_NAME = 'UserCountry_city';
const DISTINCT_COUNTRIES_METRIC = 'UserCountry_distinctCountries';
// separate region, city & country info in stored report labels
const LOCATION_SEPARATOR = '|';
private $latLongForCities = array();
protected $maximumRows;
const COUNTRY_FIELD = 'location_country';
const REGION_FIELD = 'location_region';
const CITY_FIELD = 'location_city';
protected $dimensions = array(self::COUNTRY_FIELD, self::REGION_FIELD, self::CITY_FIELD);
/**
* @var DataArray[] $arrays
*/
protected $arrays;
const LATITUDE_FIELD = 'location_latitude';
const LONGITUDE_FIELD = 'location_longitude';
public function aggregateDayReport()
{
foreach ($this->dimensions as $dimension) {
$this->arrays[$dimension] = new DataArray();
}
$this->aggregateFromVisits();
$this->aggregateFromConversions();
$this->insertDayReports();
}
public function aggregateMultipleReports()
{
$dataTableToSum = array(
self::COUNTRY_RECORD_NAME,
self::REGION_RECORD_NAME,
self::CITY_RECORD_NAME,
);
$columnsAggregationOperation = null;
$nameToCount = $this->getProcessor()->aggregateDataTableRecords(
$dataTableToSum,
$maximumRowsInDataTableLevelZero = null,
$maximumRowsInSubDataTable = null,
$columnToSortByBeforeTruncation = null,
$columnsAggregationOperation,
$columnsToRenameAfterAggregation = null,
$countRowsRecursive = array()
);
$this->getProcessor()->insertNumericRecord(self::DISTINCT_COUNTRIES_METRIC,
$nameToCount[self::COUNTRY_RECORD_NAME]['level0']);
}
protected function aggregateFromVisits()
{
$additionalSelects = array('MAX(log_visit.location_latitude) as location_latitude',
'MAX(log_visit.location_longitude) as location_longitude');
$query = $this->getLogAggregator()->queryVisitsByDimension($this->dimensions, $where = false, $additionalSelects);
if ($query === false) {
return;
}
while ($row = $query->fetch()) {
$this->makeRegionCityLabelsUnique($row);
$this->rememberCityLatLong($row);
foreach ($this->arrays as $dimension => $dataArray) {
$dataArray->sumMetricsVisits($row[$dimension], $row);
}
}
}
/**
* Makes sure the region and city of a query row are unique.
*
* @param array $row
*/
private function makeRegionCityLabelsUnique(&$row)
{
// remove the location separator from the region/city/country we get from the query
foreach ($this->dimensions as $column) {
$row[$column] = str_replace(self::LOCATION_SEPARATOR, '', $row[$column]);
}
if (!empty($row[self::REGION_FIELD])) {
$row[self::REGION_FIELD] = $row[self::REGION_FIELD] . self::LOCATION_SEPARATOR . $row[self::COUNTRY_FIELD];
}
if (!empty($row[self::CITY_FIELD])) {
$row[self::CITY_FIELD] = $row[self::CITY_FIELD] . self::LOCATION_SEPARATOR . $row[self::REGION_FIELD];
}
}
protected function rememberCityLatLong($row)
{
if (!empty($row[self::CITY_FIELD])
&& !empty($row[self::LATITUDE_FIELD])
&& !empty($row[self::LONGITUDE_FIELD])
&& empty($this->latLongForCities[$row[self::CITY_FIELD]])
) {
$this->latLongForCities[$row[self::CITY_FIELD]] = array($row[self::LATITUDE_FIELD], $row[self::LONGITUDE_FIELD]);
}
}
protected function aggregateFromConversions()
{
$query = $this->getLogAggregator()->queryConversionsByDimension($this->dimensions);
if ($query === false) {
return;
}
while ($row = $query->fetch()) {
$this->makeRegionCityLabelsUnique($row);
foreach ($this->arrays as $dimension => $dataArray) {
$dataArray->sumMetricsGoals($row[$dimension], $row);
}
}
foreach ($this->arrays as $dataArray) {
$dataArray->enrichMetricsWithConversions();
}
}
protected function insertDayReports()
{
$tableCountry = $this->arrays[self::COUNTRY_FIELD]->asDataTable();
$this->getProcessor()->insertNumericRecord(self::DISTINCT_COUNTRIES_METRIC, $tableCountry->getRowsCount());
$report = $tableCountry->getSerialized();
$this->getProcessor()->insertBlobRecord(self::COUNTRY_RECORD_NAME, $report);
$tableRegion = $this->arrays[self::REGION_FIELD]->asDataTable();
$report = $tableRegion->getSerialized($this->maximumRows, $this->maximumRows, Metrics::INDEX_NB_VISITS);
$this->getProcessor()->insertBlobRecord(self::REGION_RECORD_NAME, $report);
$tableCity = $this->arrays[self::CITY_FIELD]->asDataTable();
$this->setLatitudeLongitude($tableCity);
$report = $tableCity->getSerialized($this->maximumRows, $this->maximumRows, Metrics::INDEX_NB_VISITS);
$this->getProcessor()->insertBlobRecord(self::CITY_RECORD_NAME, $report);
}
/**
* Utility method, appends latitude/longitude pairs to city table labels, if that data
* exists for the city.
*/
private function setLatitudeLongitude(DataTable $tableCity)
{
foreach ($tableCity->getRows() as $row) {
$label = $row->getColumn('label');
if (isset($this->latLongForCities[$label])) {
// get lat/long for city
list($lat, $long) = $this->latLongForCities[$label];
$lat = round($lat, LocationProvider::GEOGRAPHIC_COORD_PRECISION);
$long = round($long, LocationProvider::GEOGRAPHIC_COORD_PRECISION);
// set latitude + longitude metadata
$row->setMetadata('lat', $lat);
$row->setMetadata('long', $long);
}
}
}
}
|