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
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\Insights\DataTable\Filter;
use Piwik\DataTable;
class Insight extends DataTable\Filter\CalculateEvolutionFilter
{
private $considerMovers;
private $considerNew;
private $considerDisappeared;
private $currentDataTable;
public function __construct($table, $currentDataTable, $pastDataTable, $columnToRead,
$considerMovers, $considerNew, $considerDisappeared)
{
parent::__construct($table, $pastDataTable, 'growth', $columnToRead, $quotientPrecision = 1);
$this->currentDataTable = $currentDataTable;
$this->considerMovers = $considerMovers;
$this->considerNew = $considerNew;
$this->considerDisappeared = $considerDisappeared;
}
public function filter($table)
{
foreach ($this->currentDataTable->getRows() as $row) {
$this->addRowIfNewOrMover($table, $row);
}
if ($this->considerDisappeared) {
foreach ($this->pastDataTable->getRows() as $row) {
$this->addRowIfDisappeared($table, $row);
}
}
}
private function addRowIfDisappeared(DataTable $table, DataTable\Row $row)
{
if ($this->getRowFromTable($this->currentDataTable, $row)) {
return;
}
$newValue = 0;
$oldValue = $row->getColumn($this->columnValueToRead);
$difference = $newValue - $oldValue;
if ($oldValue == 0 && $newValue == 0) {
$growthPercentage = '0%';
} else {
$growthPercentage = '-100%';
}
$this->addRow($table, $row, $growthPercentage, $newValue, $oldValue, $difference, $isDisappeared = true);
}
private function addRowIfNewOrMover(DataTable $table, DataTable\Row $row)
{
$pastRow = $this->getPastRowFromCurrent($row);
if (!$pastRow && !$this->considerNew) {
return;
} elseif ($pastRow && !$this->considerMovers) {
return;
}
$isNew = false;
$isMover = false;
$isDisappeared = false;
if (!$pastRow) {
$isNew = true;
$oldValue = 0;
} else {
$isMover = true;
$oldValue = $pastRow->getColumn($this->columnValueToRead);
}
$difference = $this->getDividend($row);
if ($difference === false) {
return;
}
$newValue = $row->getColumn($this->columnValueToRead);
$divisor = $this->getDivisor($row);
$growthPercentage = $this->formatValue($difference, $divisor);
$this->addRow($table, $row, $growthPercentage, $newValue, $oldValue, $difference, $isDisappeared, $isNew, $isMover);
}
private function getRowFromTable(DataTable $table, DataTable\Row $row)
{
return $table->getRowFromLabel($row->getColumn('label'));
}
private function addRow(DataTable $table, DataTable\Row $row, $growthPercentage, $newValue, $oldValue, $difference, $disappeared = false, $isNew = false, $isMover = false)
{
$columns = $row->getColumns();
$columns['growth_percent'] = $growthPercentage;
$columns['growth_percent_numeric'] = str_replace('%', '', $growthPercentage);
$columns['grown'] = '-' != substr($growthPercentage, 0 , 1);
$columns['value_old'] = $oldValue;
$columns['value_new'] = $newValue;
$columns['difference'] = $difference;
$columns['importance'] = abs($difference);
$columns['isDisappeared'] = $disappeared;
$columns['isNew'] = $isNew;
$columns['isMover'] = $isMover;
$table->addRowFromArray(array(DataTable\Row::COLUMNS => $columns));
}
}
|