-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathProcessor.php
executable file
·455 lines (386 loc) · 15.1 KB
/
Processor.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
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
445
446
447
448
449
450
451
452
453
454
455
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
*/
namespace Piwik\Plugins\CustomAlerts;
use Piwik\API\Request as ApiRequest;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Context;
use Piwik\DataTable;
use Piwik\Date;
use Piwik\Piwik;
use Piwik\Plugins\API\ProcessedReport;
use Piwik\Scheduler\RetryableException;
use Piwik\Site;
/**
*
*/
class Processor
{
/**
* @var ProcessedReport
*/
private $processedReport;
/**
* @var Validator
*/
private $validator;
/**
* @var Model
*/
private $model;
public function __construct(ProcessedReport $processedReport, Validator $validator, Model $model)
{
$this->processedReport = $processedReport;
$this->validator = $validator;
$this->model = $model;
}
public static function getComparablesDates()
{
return array(
'day' => array(
'CustomAlerts_DayComparedToPreviousWeek' => 7,
'CustomAlerts_DayComparedToPreviousDay' => 1,
'CustomAlerts_DayComparedToPreviousYear' => 365,
),
'week' => array(
'CustomAlerts_WeekComparedToPreviousWeek' => 1,
),
'month' => array(
'CustomAlerts_MonthComparedToPreviousMonth' => 1,
'CustomAlerts_MonthComparedToPreviousYear' => 12,
)
);
}
public static function getGroupConditions()
{
return array(
'CustomAlerts_MatchesAnyExpression' => 'matches_any',
'CustomAlerts_OperationIs' => 'matches_exactly',
'CustomAlerts_OperationIsNot' => 'does_not_match_exactly',
'CustomAlerts_MatchesRegularExpression' => 'matches_regex',
'CustomAlerts_DoesNotMatchRegularExpression' => 'does_not_match_regex',
'CustomAlerts_OperationContains' => 'contains',
'CustomAlerts_OperationDoesNotContain' => 'does_not_contain',
'CustomAlerts_StartsWith' => 'starts_with',
'CustomAlerts_DoesNotStartWith' => 'does_not_start_with',
'CustomAlerts_EndsWith' => 'ends_with',
'CustomAlerts_DoesNotEndWith' => 'does_not_end_with',
);
}
public static function getMetricConditions()
{
return array(
'CustomAlerts_IsLessThan' => 'less_than',
'CustomAlerts_IsGreaterThan' => 'greater_than',
'CustomAlerts_DecreasesMoreThan' => 'decrease_more_than',
'CustomAlerts_IncreasesMoreThan' => 'increase_more_than',
'CustomAlerts_PercentageDecreasesMoreThan' => 'percentage_decrease_more_than',
'CustomAlerts_PercentageIncreasesMoreThan' => 'percentage_increase_more_than',
);
}
/**
* @param $period
* @param $idSite
* @return void
* @throws RetryableException The exception that should be caught and handled by the Scheduler class. Only happens
* if there's an alert that can't be processed yet and needs to be retried.
*/
public function processAlerts($period, $idSite)
{
$alerts = $this->getAllAlerts($period);
$previouslyProcessedAlerts = $this->getPreviouslyProcessedAlerts($period, intval($idSite));
$failedAlerts = [];
foreach ($alerts as $alert) {
// Skip alerts that were processed previously
if (in_array($alert['idalert'], array_column($previouslyProcessedAlerts, 'idalert'))) {
continue;
}
try {
$this->processAlert($alert, $idSite);
} catch (RetryableException $e) {
$failedAlerts[] = $alert;
}
}
if (!empty($failedAlerts)) {
// Build up a retry exception message based of the alerts that failed to process
$alertsString = '';
foreach ($failedAlerts as $failedAlert) {
$alertsString .= "\nID: {$failedAlert['idalert']} Name: {$failedAlert['name']} Login: {$failedAlert['login']} Report: {$failedAlert['report']}";
}
if (CustomAlerts::$currentlyRunningScheduledTaskRetryCount === 3) {
StaticContainer::get(\Piwik\Log\LoggerInterface::class)->warning(Piwik::translate('CustomAlerts_FinalTaskRetryWarning', [$alertsString]));
}
// Throw an exception to let the scheduler know to retry the task
throw new RetryableException(Piwik::translate('CustomAlerts_TaskRetryExceptionMessage', [$alertsString]));
}
}
protected function getPreviouslyProcessedAlerts(string $period, int $idSite): array
{
return $this->model->getTriggeredAlertsFromPastNHours($period, $idSite, 12);
}
private function getAllAlerts($period)
{
return $this->getModel()->getAllAlertsForPeriod($period);
}
private function getModel()
{
return new Model();
}
/**
* @param $alert
* @param $idSite
* @return void
* @throws RetryableException
*/
protected function processAlert($alert, $idSite)
{
if (!$this->shouldBeProcessed($alert, $idSite)) {
return;
}
$valueNew = $this->getValueForAlertInPast($alert, $idSite, 1);
if (is_null($valueNew)) {
$valueNew = 0;
}
if (365 == $alert['compared_to'] && Date::today()->isLeapYear()) {
$alert['compared_to'] = 366;
}
$valueOld = $this->getValueForAlertInPast($alert, $idSite, 1 + $alert['compared_to']);
if ($this->shouldBeTriggered($alert, $valueNew, $valueOld)) {
$this->triggerAlert($alert, $idSite, $valueNew, $valueOld);
}
}
private function shouldBeProcessed($alert, $idSite)
{
if (empty($alert['id_sites']) || !in_array($idSite, $alert['id_sites'])) {
return false;
}
if (!$this->validator->isValidComparableDate($alert['period'], $alert['compared_to'])) {
// actually it would be nice to log or send a notification or whatever that we have skipped an alert
return false;
}
if (!$this->reportExists($idSite, $alert['report'], $alert['metric'])) {
// actually it would be nice to log or send a notification or whatever that we have skipped an alert
return false;
}
return true;
}
private function reportExists($idSite, $report, $metric)
{
try {
$this->validator->checkApiMethodAndMetric($idSite, $report, $metric);
} catch (\Exception $e) {
return false;
}
return true;
}
/**
* @param array $alert
* @param int $idSite
* @param int $subPeriodN
*
* @return array
* @throws RetryableException If the report has an archive status, and it's something other than complete
*/
public function getValueForAlertInPast($alert, $idSite, $subPeriodN)
{
$report = Context::changeIdSite($idSite, function () use ($idSite, $alert) {
return $this->processedReport->getReportMetadataByUniqueId($idSite, $alert['report']);
});
if (empty($report)) {
throw new \Exception("Could not find report for alert '{$alert['report']}'.");
}
$dateInPast = $this->getDateForAlertInPast($idSite, $alert['period'], $subPeriodN);
$params = array(
'format' => 'original',
'idSite' => $idSite,
'period' => $alert['period'],
'date' => $dateInPast,
'flat' => 1,
'disable_queued_filters' => 1,
'filter_limit' => -1
);
// Only include the archive state param for versions of Matomo that allow it
if (version_compare(\Piwik\Version::VERSION, '5.1.0-b1', '>=')) {
$params['fetch_archive_state'] = 1;
}
if (!empty($report['parameters'])) {
$params = array_merge($params, $report['parameters']);
}
$subtableId = DataTable\Manager::getInstance()->getMostRecentTableId();
$table = ApiRequest::processRequest($report['module'] . '.' . $report['action'], $params, $default = []);
// If the response is a DataTable, check the archiving status
if ($table instanceof DataTable) {
$this->checkWhetherArchiveIsComplete($alert, $table);
}
$value = $this->aggregateToOneValue($table, $alert['metric'], $alert['report_condition'], $alert['report_matched']);
DataTable\Manager::getInstance()->deleteAll($subtableId);
return $value;
}
/**
* Checks whether the archive status is complete. We throw an exception if the status is something other than
* complete. If no status is found, we do nothing.
*
* @param array $alert Array containing all the alert information
* @param DataTable $table Should have the archive_state metadata set because the fetch_archive_state query param
* was set as part of the API request.
*
* @return void
* @throws RetryableException If the archive status is found and isn't complete
*/
protected function checkWhetherArchiveIsComplete(array $alert, DataTable $table): void
{
// Don't bother checking older versions of Matomo since the data and constants won't be there
if (version_compare(\Piwik\Version::VERSION, '5.1.0-b1', '<')) {
return;
}
$archiveState = $table->getMetadata(DataTable::ARCHIVE_STATE_METADATA_NAME);
if (empty($archiveState)) {
return;
}
if ($archiveState === \Piwik\Archive\ArchiveState::COMPLETE) {
return;
}
// Throw an exception since the archive status was provided and isn't complete
throw new RetryableException('This alert is not ready to process due to incomplete archiving');
}
private function getDateForAlertInPast($idSite, $period, $subPeriodN)
{
$timezone = Site::getTimezoneFor($idSite);
$date = Date::now();
$date = Date::factory($date->getDatetime(), $timezone);
if ($subPeriodN) {
$date = $date->subPeriod($subPeriodN, $period);
}
return $date->toString();
}
/**
* @param DataTable $dataTable DataTable
* @param string $metric Metric to fetch from row.
* @param string $filterCond Condition to filter for.
* @param string $filterValue Value to find
*
* @return mixed
*/
protected function aggregateToOneValue($dataTable, $metric, $filterCond = '', $filterValue = '')
{
if (!empty($filterValue)) {
$this->filterDataTable($dataTable, $filterCond, $filterValue);
}
if ($dataTable->getRowsCount() > 1) {
$dataTable->filter('Truncate', array(0, null, $metric));
}
$dataTable->applyQueuedFilters();
$dataRow = $dataTable->getFirstRow();
if (!$dataRow) {
return null;
}
$value = $dataRow->getColumn($metric);
if ($value && is_string($value)) {
$value = str_replace(array('%', 's'), '', $value);
}
return $value;
}
/**
* @param $dataTable
* @param $condition
* @param $value
* @throws \Exception
*/
protected function filterDataTable($dataTable, $condition, $value)
{
$invert = false;
$value = Common::unsanitizeInputValue($value);
if ('matches_regex' != $condition && 'does_not_match_regex' != $condition) {
$value = str_replace(array('?', '+', '*'), array('\?', '\+', '\*'), $value);
}
// Some escaping?
switch ($condition) {
case 'matches_any':
return;
case 'matches_exactly':
$pattern = sprintf("^%s$", $value);
break;
case 'matches_regex':
$pattern = $value;
break;
case 'does_not_match_exactly':
$pattern = sprintf("^%s$", $value);
$invert = true;
break;
case 'does_not_match_regex':
$pattern = sprintf("%s", $value);
$invert = true;
break;
case 'contains':
$pattern = $value;
break;
case 'does_not_contain':
$pattern = $value;
$invert = true;
break;
case 'starts_with':
$pattern = sprintf("^%s", $value);
break;
case 'does_not_start_with':
$pattern = sprintf("^%s", $value);
$invert = true;
break;
case 'ends_with':
$pattern = sprintf("%s$", $value);
break;
case 'does_not_end_with':
$pattern = sprintf("%s$", $value);
$invert = true;
break;
default:
throw new \Exception('Filter condition not supported');
}
$dataTable->filter('Pattern', array('label', $pattern, $invert));
}
protected function shouldBeTriggered($alert, $valueNew, $valueOld)
{
if ($this->needsBothValuesToTrigger($alert) && empty($valueOld) && empty($valueNew)) {
return false;
}
if (!empty($valueOld)) {
$percentage = ((($valueNew / $valueOld) * 100) - 100);
} else {
$percentage = $valueNew;
}
$metricMatched = floatval($alert['metric_matched']);
switch ($alert['metric_condition']) {
case 'greater_than':
return ($valueNew > $metricMatched);
case 'less_than':
return ($valueNew < $metricMatched);
case 'decrease_more_than':
return (($valueOld - $valueNew) > $metricMatched);
case 'increase_more_than':
return (($valueNew - $valueOld) > $metricMatched);
case 'percentage_decrease_more_than':
return ((-1 * $metricMatched) > $percentage && $percentage < 0);
case 'percentage_increase_more_than':
return ($metricMatched < $percentage && $percentage >= 0);
}
throw new \Exception('Metric condition is not supported');
}
private function needsBothValuesToTrigger($alert)
{
$comparisons = array(
'decrease_more_than',
'increase_more_than',
'percentage_decrease_more_than',
'percentage_increase_more_than'
);
return in_array($alert['metric_condition'], $comparisons);
}
protected function triggerAlert($alert, $idSite, $valueNew, $valueOld)
{
$this->getModel()->triggerAlert($alert['idalert'], $idSite, $valueNew, $valueOld, Date::now()->getDatetime());
}
}