-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Archiver.php
301 lines (276 loc) · 10 KB
/
Archiver.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
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
<?php
namespace Piwik\Plugins\PerformanceAudit;
require PIWIK_INCLUDE_PATH . '/plugins/PerformanceAudit/vendor/autoload.php';
use Exception;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\DataAccess\ArchiveTableCreator;
use Piwik\DataTable;
use Piwik\DataTable\Row;
use Piwik\Date;
use Piwik\Db;
use Piwik\Log\Logger;
use Piwik\Period;
use Piwik\Plugin\Archiver as BaseArchiver;
use Piwik\Plugins\PerformanceAudit\Columns\Metrics\Audit;
use Piwik\Plugins\PerformanceAudit\Columns\Metrics\Max;
use Piwik\Plugins\PerformanceAudit\Columns\Metrics\Median;
use Piwik\Plugins\PerformanceAudit\Columns\Metrics\Min;
use Piwik\Tracker\Db\DbException;
use Piwik\Tracker\PageUrl;
/**
* Class encapsulating logic to process Day/Period Archiving for the Actions reports.
*/
class Archiver extends BaseArchiver
{
/**
* The prefix for all database records
*
* @var string
*/
private const DATABASE_RECORD_PREFIX = 'PerformanceAudit_Report_';
/**
* Archives performance audit reports for a day.
*
* @return bool
* @throws DbException
*/
public function aggregateDayReport()
{
$this->aggregateReport();
return true;
}
/**
* Archives performance audit reports for more than a day.
*
* @return bool
* @throws DbException
*/
public function aggregateMultipleReports()
{
$this->aggregateReport();
return true;
}
/**
* Determine if archiver should run if no new reports are available.
*
* @return bool
* @throws Exception
*/
public static function shouldRunEvenWhenNoVisits()
{
$deletedDuplicateCount = self::deleteArchiveDuplicates();
StaticContainer::get(Logger::class)->debug($deletedDuplicateCount . ' archive entries got deleted');
return true;
}
/**
* Aggregates logs for given time period.
*
* @return void
* @throws DbException|Exception
*/
private function aggregateReport()
{
$params = $this->getProcessor()->getParams();
$period = $params->getPeriod();
$idSites = $params->getIdSites();
$metrics = array_values(Audit::METRICS);
$emulatedDevices = EmulatedDevice::getList(EmulatedDevice::Both);
foreach ($idSites as $idSite) {
StaticContainer::get(Logger::class)->info("Will process performance audit for website id = {$idSite}, period = {$period}");
foreach ($metrics as $metric) {
foreach ($emulatedDevices as $emulatedDevice) {
$table = new DataTable();
$table->setMaximumAllowedRows(0);
$emulatedDeviceId = EmulatedDevice::getIdFor($emulatedDevice);
$rows = $this->fetchAllMetrics($idSite, $metric, $period, $emulatedDeviceId);
foreach ($rows as $row) {
$url = PageUrl::reconstructNormalizedUrl($row['url'], $row['url_prefix']);
$url = Common::unsanitizeInputValue($url);
$table->addRowFromArray([
Row::COLUMNS => [
'label' => $url,
(new Min())->getName() => $row['min'],
(new Median())->getName() => $row['median'],
(new Max())->getName() => $row['max'],
],
Row::METADATA => [
'url' => $url
]
]);
}
$recordName = sprintf(
self::DATABASE_RECORD_PREFIX . '%s_%s',
ucfirst($metric),
ucfirst($emulatedDevice)
);
$this->insertTable($table, $recordName);
}
}
}
}
/**
* Fetch all needed metrics for given period.
*
* @param string $idSite
* @param string $key
* @param Period $period
* @param int $emulatedDevice
* @return array
* @throws DbException
*/
private function fetchAllMetrics(string $idSite, string $key, Period $period, int $emulatedDevice)
{
$baseParameter = [
$idSite,
$period->getDateTimeStart(),
$period->getDateTimeEnd(),
$emulatedDevice,
$key
];
$actionIds = $this->fetchLogPerformanceActionIds($baseParameter);
if (empty($actionIds)) {
return [];
}
$actionIdCounts = array_count_values(array_column($actionIds, 'idaction'));
$middleRowIndices = $this->calculateMiddleRowIndices($actionIdCounts);
$middleRowQueries = [];
foreach ($middleRowIndices as $actionId => $actionMiddleRowIndices) {
$middleRowQueries[] = sprintf(
'(`lp_sub`.`idaction` = %d AND `lp_sub`.`row_number` IN (%s))',
(int) $actionId,
implode(',', $actionMiddleRowIndices)
);
}
$whereMiddleRowStatement = implode(' OR ', $middleRowQueries);
return Db::getReader()->fetchAll('
SELECT
`lp_sub`.`name` AS `url`,
`lp_sub`.`url_prefix` AS `url_prefix`,
`lp_sub`.`idaction` AS `idaction`,
MIN(`lp_sub`.`min`) AS `min`,
ROUND(AVG(`lp_sub`.`median`), 1) AS `median`,
MAX(`lp_sub`.`max`) AS `max`
FROM (
SELECT
`la`.`name`,
`la`.`url_prefix`,
`lp`.`idaction`,
`lp`.`min`,
`lp`.`median`,
`lp`.`max`,
@row_number := IF(@previous_value = `lp`.`idaction`, @row_number + 1, 0) AS `row_number`,
@previous_value := `lp`.`idaction`
FROM
(SELECT @row_number := 0) AS `rn`,
(SELECT @previous_value := -1) AS `pv`,
`' . Common::prefixTable('log_performance'). '` AS `lp`
INNER JOIN `' . Common::prefixTable('log_action'). '` AS `la`
ON `lp`.`idaction` = `la`.`idaction`
WHERE
`lp`.`idsite` = ? AND
`lp`.`created_at` BETWEEN ? AND ? AND
`lp`.`emulated_device` = ? AND
`lp`.`key` = ?
ORDER BY `lp`.`idsite`, `lp`.`median`
) AS `lp_sub`
WHERE ' . $whereMiddleRowStatement . '
GROUP BY `lp_sub`.`idaction`
', $baseParameter);
}
/**
* Insert DataTable into blob database table.
*
* @param DataTable $table
* @param string $recordName
* @throws Exception
*/
protected function insertTable(DataTable $table, string $recordName)
{
$maxRows = Config::getInstance()->General['datatable_archiving_maximum_rows_actions'];
$maximumRowsInSubDataTable = Config::getInstance()->General['datatable_archiving_maximum_rows_subtable_actions'];
$report = $table->getSerialized($maxRows, $maximumRowsInSubDataTable, (new Median())->getName());
$this->getProcessor()->insertBlobRecord($recordName, $report);
}
/**
* Get all action IDs for given parameters.
*
* @param array $parameter
* @return array
* @throws DbException
*/
private function fetchLogPerformanceActionIds(array $parameter)
{
return Db::getReader()->fetchAll('
SELECT `lp`.`idaction`
FROM `' . Common::prefixTable('log_performance'). '` AS `lp`
WHERE
`lp`.`idsite` = ? AND
`lp`.`created_at` BETWEEN ? AND ? AND
`lp`.`emulated_device` = ? AND
`lp`.`key` = ?
', $parameter);
}
/**
* Delete all duplicate entries from archive table.
*
* @return int
* @throws Exception
*/
private static function deleteArchiveDuplicates()
{
// Table name is already prefixed
$currentTable = ArchiveTableCreator::getBlobTable(Date::factory('now'));
$archiveDuplicateEntries = Db::getReader()->fetchAll('
SELECT
MIN(`idarchive`) AS `idarchive`,
`name`,
COUNT(*) AS `duplicates`
FROM
`' . $currentTable . '`
WHERE
`name` LIKE ?
GROUP BY
`name`,
`idsite`,
`date1`,
`date2`,
`period`,
`value`
HAVING
`duplicates` > 1
', [self::DATABASE_RECORD_PREFIX . '%']);
if (count($archiveDuplicateEntries) < 1) {
return 0;
}
$archiveDuplicateIds = array_column($archiveDuplicateEntries, 'idarchive');
$archiveDuplicateNames = array_column($archiveDuplicateEntries, 'name');
$archiveDuplicateIdPlaceholder = rtrim(str_repeat('?,', count($archiveDuplicateIds)), ',');
$archiveDuplicateNamePlaceholder = rtrim(str_repeat('?,', count($archiveDuplicateNames)), ',');
return Db::deleteAllRows(
$currentTable,
'WHERE `idarchive` IN (' . $archiveDuplicateIdPlaceholder . ') AND `name` IN (' . $archiveDuplicateNamePlaceholder . ')',
'`idarchive` ASC',
100000,
array_merge($archiveDuplicateIds, $archiveDuplicateNames)
);
}
/**
* Calculate middle rows indices for given counts per array element.
*
* @param array $arrayCounts
* @return array
*/
private function calculateMiddleRowIndices(array $arrayCounts)
{
$middleRowIndices = [];
foreach ($arrayCounts as $id => $count) {
$isEven = $count % 2 === 0;
$middleRowIndices[$id] = ($isEven) ?
[intval($count / 2) - 1, intval($count / 2)] :
[intval(floor($count / 2))];
}
return $middleRowIndices;
}
}