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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
<?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\API\Filter;
use Piwik\API\Request;
use Piwik\Common;
use Piwik\Config;
use Piwik\DataTable;
use Piwik\DataTable\DataTableInterface;
use Piwik\DataTable\Simple;
use Piwik\Http\BadRequestException;
use Piwik\Metrics;
use Piwik\Period;
use Piwik\Period\Factory;
use Piwik\Piwik;
use Piwik\Plugin\Report;
use Piwik\Plugins\API\Filter\DataComparisonFilter\ComparisonRowGenerator;
use Piwik\Segment;
use Piwik\Segment\SegmentExpression;
use Piwik\Site;
/**
* Handles the API portion of the data comparison feature.
*
* If the `compareSegments`/`comparePeriods`/`compareDates` parameters are supplied this class will fetch
* the data to compare with and store this data next to each row in the root report.
*
* Additionally, `..._change` columns will be added that show the percentage change for a column. This is only
* done when comparing periods, since segments are subsets of visits, so it doesn't make sense to consider
* differences between them as "changes".
*
* ### Comparing multiple periods
*
* It is possible to compare multiple periods with other multiple periods. For example, it
* is possible to compare period=day, date=2018-02-03,2018-02-13 with period=day, date=2018-03-01,2018-03-15.
* When done, this filter will compare the first period in the first set w/ the first period in the second set,
* etc. So in the previous example, 2018-02-03 will be compared with 2018-03-01, 2018-02-04 with 2018-03-02, etc.
*
* ### Metadata
*
* This filter adds the following metadata to DataTables:
*
* - 'compareSegments': The list of segments being compared. The first entry will always be the value of the `segment` query param.
* - 'comparePeriods': The list of labels of periods being compared. The first entry will always be the value of the
* `period` query param.
* - 'compareDates': The list of dates being compared. The first entry will always be the value of the `date` query param.
* - 'comparisonSeries': Prettified labels for every comparison series in order.
*
* This filter adds the following metadata to rows in the comparison DataTables:
*
* - 'compareSegment': The segment of the data for the comparison row.
* - 'compareSegmentPretty': The prettified label for the segment.
* - 'comparePeriod': The period label for the data in the comparison row. This does not have to match a value in the
* `comparePeriods` query parameter if comparing multiple periods.
* - 'compareDate': The date for the period in the data in the comparison row. This does not have to match a value in the
* `compareDates` query parameter if comparing multiple periods.
* - 'comparePeriodPretty': The prettified label for the period.
* - 'compareSeriesPretty': Prettified label for the comparison data represented by the row. This will match an entry
* in the DataTable's `comparisonSeries` metadata.
*/
class DataComparisonFilter
{
/**
* @var array
*/
private $request;
/**
* @var int
*/
private $segmentCompareLimit;
/**
* @var int
*/
private $periodCompareLimit;
/**
* @var string
*/
private $segmentName;
/**
* @var string[]
*/
private $compareSegments;
/**
* @var string[]
*/
private $compareDates;
/**
* @var string[]
*/
private $comparePeriods;
/**
* @var int[]
*/
private $compareSegmentIndices;
/**
* @var int[]
*/
private $comparePeriodIndices;
/**
* @var bool
*/
private $isRequestMultiplePeriod;
/**
* @var ComparisonRowGenerator
*/
private $comparisonRowGenerator;
/**
* @var array
*/
private $columnMappings;
/**
* @var bool
*/
private $invertCompareChangeCompute;
public function __construct($request, Report $report = null)
{
$this->request = $request;
$generalConfig = Config::getInstance()->General;
$this->segmentCompareLimit = (int) $generalConfig['data_comparison_segment_limit'];
$this->checkComparisonLimit($this->segmentCompareLimit, 'data_comparison_segment_limit');
$this->periodCompareLimit = (int) $generalConfig['data_comparison_period_limit'];
$this->checkComparisonLimit($this->periodCompareLimit, 'data_comparison_period_limit');
$this->segmentName = $this->getSegmentNameFromReport($report);
$this->compareSegments = self::getCompareSegments();
if (count($this->compareSegments) > $this->segmentCompareLimit + 1) {
throw new BadRequestException(Piwik::translate('General_MaximumNumberOfSegmentsComparedIs', [$this->segmentCompareLimit]));
}
$this->compareDates = self::getCompareDates($request);
$this->comparePeriods = self::getComparePeriods($request);
if (count($this->compareDates) !== count($this->comparePeriods)) {
throw new BadRequestException(Piwik::translate('General_CompareDatesParamMustMatchComparePeriods', ['compareDates', 'comparePeriods']));
}
if (count($this->compareDates) > $this->periodCompareLimit + 1) {
throw new BadRequestException(Piwik::translate('General_MaximumNumberOfPeriodsComparedIs', [$this->periodCompareLimit]));
}
if (count($this->compareSegments) == 1
&& count($this->comparePeriods) == 1
) {
return;
}
$this->checkMultiplePeriodCompare();
// map segments/periods to their indexes in the query parameter arrays for comparisonIdSubtable matching
$this->compareSegmentIndices = array_flip($this->compareSegments);
foreach ($this->comparePeriods as $index => $period) {
$date = $this->compareDates[$index];
$this->comparePeriodIndices[$period][$date] = $index;
}
$this->invertCompareChangeCompute = Common::getRequestVar('invert_compare_change_compute', $default = 0, $type = 'int', $request) == 1;
if ($this->invertCompareChangeCompute && count($this->comparePeriods) != 2) {
throw new \Exception("invert_compare_change_compute=1 can only be used when comparing two periods.");
}
$this->columnMappings = $this->getColumnMappings();
$this->comparisonRowGenerator = new ComparisonRowGenerator($this->segmentName, $this->isRequestMultiplePeriod(), $this->columnMappings);
}
public static function isCompareParamsPresent($request = null)
{
return !empty(Common::getRequestVar('compareSegments', [], $type = 'array', $request))
|| !empty(Common::getRequestVar('comparePeriods', [], $type = 'array', $request))
|| !empty(Common::getRequestVar('compareDates', [], $type = 'array', $request));
}
/**
* @param DataTable\DataTableInterface $table
*/
public function compare(DataTable\DataTableInterface $table)
{
if (empty($this->compareSegments)
&& empty($this->comparePeriods)
) {
return;
}
$method = Common::getRequestVar('method', $default = null, $type = 'string', $this->request);
if ($method == 'Live') {
throw new \Exception("Data comparison is not enabled for the Live API.");
}
// optimization, if empty, single table, don't need to make extra queries
if ($table->getRowsCount() == 0) {
return;
}
$comparisonSeries = [];
// fetch data first
$reportsToCompare = self::getReportsToCompare($this->compareSegments, $this->comparePeriods, $this->compareDates);
foreach ($reportsToCompare as $index => $modifiedParams) {
$compareMetadata = $this->getMetadataFromModifiedParams($modifiedParams);
$comparisonSeries[] = $compareMetadata['compareSeriesPretty'];
$compareTable = $this->requestReport($method, $modifiedParams);
$this->comparisonRowGenerator->compareTables($compareMetadata, $table, $compareTable);
}
// calculate changes (including processed metric changes)
// NOTE: it doesn't make to sense to calculate these values for segments, since segments are subsets of all visits, where periods are
// time periods (so things can change from one to another).
if (count($this->comparePeriods) > 1) {
$this->compareChangePercents($table);
}
// format comparison table metrics
$this->formatComparisonTables($table);
// add comparison parameters as metadata
$table->filter(function (DataTable $singleTable) use ($comparisonSeries) {
if (isset($this->compareSegments)) {
$singleTable->setMetadata('compareSegments', $this->compareSegments);
}
if (isset($this->comparePeriods)) {
$singleTable->setMetadata('comparePeriods', $this->comparePeriods);
}
if (isset($this->compareDates)) {
$singleTable->setMetadata('compareDates', $this->compareDates);
}
$singleTable->setMetadata('comparisonSeries', $comparisonSeries);
});
}
public static function getReportsToCompare($compareSegments, $comparePeriods, $compareDates)
{
$permutations = [];
// NOTE: the order of these loops determines the order of the rows in the comparison table. ie,
// if we loop over dates then segments, then we'll see comparison rows change segments before changing
// periods. this is because this loop determines in what order we fetch report data.
foreach ($compareDates as $index => $date) {
foreach ($compareSegments as $segment) {
$period = $comparePeriods[$index];
$params = [];
$params['segment'] = $segment;
if (!empty($period)
&& !empty($date)
) {
$params['date'] = $date;
$params['period'] = $period;
}
$permutations[] = $params;
}
}
return $permutations;
}
/**
* @param $paramsToModify
* @return DataTable
*/
private function requestReport($method, $paramsToModify)
{
$params = array_merge(
[
'filter_limit' => -1,
'filter_offset' => 0,
'filter_sort_column' => '',
'filter_truncate' => -1,
'compare' => 0,
'totals' => 1,
'disable_queued_filters' => 1,
'format_metrics' => 0,
'label' => '',
'flat' => Common::getRequestVar('flat', 0, 'int', $this->request),
],
$paramsToModify
);
$params['keep_totals_row'] = Common::getRequestVar('keep_totals_row', 0, 'int', $this->request);
$params['keep_totals_row_label'] = Common::getRequestVar('keep_totals_row_label', '', 'string', $this->request);
if (!isset($params['idSite'])) {
$params['idSite'] = Common::getRequestVar('idSite', null, 'string', $this->request);
}
if (!isset($params['period'])) {
$params['period'] = Common::getRequestVar('period', null, 'string', $this->request);
}
if (!isset($params['date'])) {
$params['date'] = Common::getRequestVar('date', null, 'string', $this->request);
}
$idSubtable = Common::getRequestVar('idSubtable', 0, 'int', $this->request);
if ($idSubtable > 0) {
$comparisonIdSubtables = Common::getRequestVar('comparisonIdSubtables', $default = false, 'json', $this->request);
if (empty($comparisonIdSubtables)) {
throw new \Exception("Comparing segments/periods with subtables only works when the comparison idSubtables are supplied as well.");
}
$segmentIndex = empty($paramsToModify['segment']) ? 0 : $this->compareSegmentIndices[$paramsToModify['segment']];
$periodIndex = empty($paramsToModify['period']) ? 0 : $this->comparePeriodIndices[$paramsToModify['period']][$paramsToModify['date']];
$seriesIndex = self::getComparisonSeriesIndex(null, $periodIndex, $segmentIndex, count($this->compareSegments));
if (!isset($comparisonIdSubtables[$seriesIndex])) {
throw new \Exception("Invalid comparisonIdSubtables parameter: no idSubtable found for segment $segmentIndex and period $periodIndex");
}
$comparisonIdSubtable = $comparisonIdSubtables[$seriesIndex];
if ($comparisonIdSubtable === -1) { // no subtable in comparison row
$table = new DataTable();
$table->setMetadata('site', new Site($params['idSite']));
$table->setMetadata('period', Period\Factory::build($params['period'], $params['date']));
return $table;
}
$params['idSubtable'] = $comparisonIdSubtable;
}
return Request::processRequest($method, $params);
}
private function formatComparisonTables(DataTableInterface $tableOrMap)
{
$tableOrMap->filter(function (DataTable $table) {
$rows = $table->getRows();
$totalRow = $table->getTotalsRow();
if ($totalRow) {
$rows[] = $totalRow;
}
foreach ($rows as $row) {
/** @var DataTable $comparisonTable */
$comparisonTable = $row->getComparisons();
if (!empty($comparisonTable)) { // sanity check
$columnMappings = $this->columnMappings;
$comparisonTable->filter(DataTable\Filter\ReplaceColumnNames::class, [$columnMappings]);
}
$subtable = $row->getSubtable();
if ($subtable) {
$this->formatComparisonTables($subtable);
}
}
});
}
private function checkComparisonLimit($n, $configName)
{
if ($n <= 1) {
throw new \Exception("The [General] $configName INI config option must be greater than 1.");
}
}
private function getMetadataFromModifiedParams($modifiedParams)
{
$metadata = [];
$period = isset($modifiedParams['period']) ? $modifiedParams['period'] : reset($this->comparePeriods);
$date = isset($modifiedParams['date']) ? $modifiedParams['date'] : reset($this->compareDates);
$segment = isset($modifiedParams['segment']) ? $modifiedParams['segment'] : reset($this->compareSegments);
$metadata['compareSegment'] = $segment;
$segmentObj = new Segment($segment, []);
$metadata['compareSegmentPretty'] = $segmentObj->getStoredSegmentName(false);
$metadata['comparePeriod'] = $period;
$metadata['compareDate'] = $date;
$prettyPeriod = Factory::build($period, $date)->getLocalizedLongString();
$metadata['comparePeriodPretty'] = ucfirst($prettyPeriod);
$metadata['compareSeriesPretty'] = self::getComparisonSeriesLabelSuffixFromParts(
$metadata['comparePeriodPretty'], $metadata['compareSegmentPretty']);
return $metadata;
}
private static function getComparisonSeriesLabelSuffixFromParts($periodPretty, $segmentPretty)
{
$comparisonLabels = [
$periodPretty,
$segmentPretty,
];
$comparisonLabels = array_filter($comparisonLabels);
return '(' . implode(') (', $comparisonLabels) . ')';
}
private function getSegmentNameFromReport(Report $report = null)
{
if (empty($report)) {
return null;
}
$dimension = $report->getDimension();
if (empty($dimension)) {
return null;
}
$segments = $dimension->getSegments();
if (empty($segments)) {
return null;
}
/** @var \Piwik\Plugin\Segment $segment */
$segment = reset($segments);
$segmentName = $segment->getSegment();
return $segmentName;
}
private function checkMultiplePeriodCompare()
{
if ($this->isRequestMultiplePeriod()) {
foreach ($this->comparePeriods as $index => $period) {
if (!Period::isMultiplePeriod($this->compareDates[$index], $period)) {
throw new \Exception("Cannot compare: original request is multiple period and cannot be compared with single periods.");
}
}
} else {
foreach ($this->comparePeriods as $index => $period) {
if (Period::isMultiplePeriod($this->compareDates[$index], $period)) {
throw new \Exception("Cannot compare: original request is single period and cannot be compared with multiple periods.");
}
}
}
}
private function isRequestMultiplePeriod()
{
if ($this->isRequestMultiplePeriod === null) {
$period = Common::getRequestVar('period', $default = null, 'string', $this->request);
$date = Common::getRequestVar('date', $default = null, 'string', $this->request);
$this->isRequestMultiplePeriod = Period::isMultiplePeriod($date, $period);
}
return $this->isRequestMultiplePeriod;
}
private function compareChangePercents(DataTableInterface $result)
{
$segmentCount = count($this->compareSegments);
$result->filter(function (DataTable $table) use ($segmentCount) {
$rows = $table->getRows();
$totalRow = $table->getTotalsRow();
if ($totalRow) {
$rows[] = $totalRow;
}
foreach ($rows as $row) {
$comparisons = $row->getComparisons();
if (empty($comparisons)) {
continue;
}
/** @var DataTable\Row[] $rows */
$rows = array_values($comparisons->getRows());
foreach ($rows as $index => $compareRow) {
list($periodIndex, $segmentIndex) = self::getIndividualComparisonRowIndices($table, $index, $segmentCount);
if (!$this->invertCompareChangeCompute && $index < $segmentCount) {
continue; // do not calculate for first period
} else if ($this->invertCompareChangeCompute && $periodIndex != 0) {
continue; // when inverting change calculation, only calculate for first period rows
}
if (!$this->invertCompareChangeCompute) {
$otherPeriodRowIndex = $segmentIndex;
$otherPeriodRow = $comparisons[$otherPeriodRowIndex];
} else {
$otherPeriodIndex = 1;
$otherPeriodRowIndex = self::getComparisonSeriesIndex($table, $otherPeriodIndex, $segmentIndex, $segmentCount);
$otherPeriodRow = $comparisons[$otherPeriodRowIndex];
}
foreach ($compareRow->getColumns() as $name => $value) {
$changeTo = $this->computeChangePercent($otherPeriodRow, $compareRow, $name);
$compareRow->addColumn($name . '_change', $changeTo);
$changeFrom = $this->computeChangePercent($compareRow, $otherPeriodRow, $name);
$compareRow->addColumn($name . '_change_from', $changeFrom);
}
}
}
});
}
private function computeChangePercent(DataTable\Row $fromRow, DataTable\Row $toRow, $columnName)
{
$value = $toRow ? $toRow->getColumn($columnName) : 0;
$value = $value ?: 0;
$valueToCompare = $fromRow ? $fromRow->getColumn($columnName) : 0;
$valueToCompare = $valueToCompare ?: 0;
$change = DataTable\Filter\CalculateEvolutionFilter::calculate($value, $valueToCompare, $precision = 1, $appendPercent = false);
if ($change >= 0) {
$change = '+' . $change;
}
$change .= '%';
return $change;
}
/**
* Returns the period and segment indices for a given comparison index.
*
* @param DataTable|null $table
* @param $comparisonRowIndex
* @param null $segmentCount
* @return array
*/
public static function getIndividualComparisonRowIndices($table, $comparisonRowIndex, $segmentCount = null)
{
$segmentCount = $segmentCount ?: count($table->getMetadata('compareSegments'));
$segmentIndex = $comparisonRowIndex % $segmentCount;
$periodIndex = floor($comparisonRowIndex / $segmentCount);
return [$periodIndex, $segmentIndex];
}
/**
* Returns the series index for a comparison based on the period and segment indices.
*
* @param DataTable|null $table
* @param int $periodIndex
* @param int $segmentIndex
* @param int|null $segmentCount
* @return int
*/
public static function getComparisonSeriesIndex($table, $periodIndex, $segmentIndex, $segmentCount = null)
{
$segmentCount = $segmentCount ?: count($table->getMetadata('compareSegments'));
return $periodIndex * $segmentCount + $segmentIndex;
}
private static function getCompareSegments($request = null)
{
$segments = Common::getRequestVar('compareSegments', $default = [], $type = 'array', $request);
array_unshift($segments, Common::getRequestVar('segment', '', 'string', $request));
$segments = Common::unsanitizeInputValues($segments);
return $segments;
}
private static function getComparePeriods($request = null)
{
$periods = Common::getRequestVar('comparePeriods', $default = [], $type = 'array', $request);
array_unshift($periods, Common::getRequestVar('period', '', 'string', $request));
return array_values($periods);
}
private static function getCompareDates($request = null)
{
$dates = Common::getRequestVar('compareDates', $default = [], $type = 'array', $request);
array_unshift($dates, Common::getRequestVar('date', '', 'string', $request));
return array_values($dates);
}
/**
* Returns the pretty series label for a specific comparison based on the currently set comparison query parameters.
*
* @param int $labelSeriesIndex The index of the comparison. Comparison series order is determined by {@see self::getReportsToCompare()}.
*/
public static function getPrettyComparisonLabelFromSeriesIndex($labelSeriesIndex)
{
$compareSegments = self::getCompareSegments();
$comparePeriods = self::getComparePeriods();
$compareDates = self::getCompareDates();
list($periodIndex, $segmentIndex) = self::getIndividualComparisonRowIndices(null, $labelSeriesIndex, count($compareSegments));
$segmentObj = new Segment($compareSegments[$segmentIndex], []);
$prettySegment = $segmentObj->getStoredSegmentName(false);
$prettyPeriod = Factory::build($comparePeriods[$periodIndex], $compareDates[$periodIndex])->getLocalizedLongString();
$prettyPeriod = ucfirst($prettyPeriod);
return self::getComparisonSeriesLabelSuffixFromParts($prettyPeriod, $prettySegment);
}
private function getColumnMappings()
{
$allMappings = Metrics::getMappingFromIdToName();
$mappings = [];
foreach ($allMappings as $index => $name) {
$mappings[$index] = $name;
$mappings[$index . '_change'] = $name . '_change';
$mappings[$index . '_change_from'] = $name . '_change_from';
}
return $mappings;
}
}
|