/
AddRatioColumn.php
188 lines (156 loc) · 5.05 KB
/
AddRatioColumn.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
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik
* @package Piwik
*/
namespace Piwik\API\DataTableManipulator;
use Piwik\API\DataTableManipulator;
use Piwik\DataTable;
use Piwik\DataTable\Row;
use Piwik\DataTable\Filter;
use Piwik\Piwik;
use Piwik\Metrics;
use Piwik\Plugins\API\API;
/**
* This class is responsible for adding ratio columns.
*
* @package Piwik
* @subpackage Piwik_API
*/
class AddRatioColumn extends DataTableManipulator
{
protected $roundPrecision = 2;
private $totalValues = array();
/**
* @param DataTable $table
* @return \Piwik\DataTable|\Piwik\DataTable\Map
*/
public function addColumns($table)
{
return $this->manipulate($table);
}
/**
* Adds ratio metrics if possible.
*
* @param DataTable $dataTable
* @return DataTable
*/
protected function manipulateDataTable($dataTable)
{
$metricsToCalculate = Metrics::getMetricIdsToProcessRatio();
$parentTable = $this->getFirstLevelDataTable();
foreach ($parentTable->getRows() as $row) {
foreach ($metricsToCalculate as $metricId) {
$this->addColumnValueToTotal($row, $metricId);
}
}
foreach ($dataTable->getRows() as $row) {
foreach ($metricsToCalculate as $metricId) {
$this->addRatioColumnIfNeeded($row, $metricId);
}
}
return $dataTable;
}
protected function getFirstLevelDataTable()
{
$reportMetadata = API::getInstance()->getReportMetadata();
$firstLevelReport = array();
foreach ($reportMetadata as $report) {
if (!empty($report['actionToLoadSubTables'])
&& $this->apiMethod == $report['actionToLoadSubTables']
&& $this->apiModule == $report['module']) {
$firstLevelReport = $report;
break;
}
}
if (empty($firstLevelReport)) {
// it is not a subtable report
$module = $this->apiModule;
$action = $this->apiMethod;
} else {
$module = $firstLevelReport['module'];
$action = $firstLevelReport['action'];
}
$request = $this->request;
return $this->callApiAndReturnDataTable($module, $action, $request);
}
private function addColumnValueToTotal(Row $row, $columnIdRaw)
{
$value = $this->getColumn($row, $columnIdRaw);
if (false === $value) {
return;
}
if (array_key_exists($columnIdRaw, $this->totalValues)) {
$this->totalValues[$columnIdRaw] += $value;
} else {
$this->totalValues[$columnIdRaw] = $value;
}
}
private function addRatioColumnIfNeeded(Row $row, $columnIdRaw)
{
if (!array_key_exists($columnIdRaw, $this->totalValues)) {
return;
}
$value = $this->getColumn($row, $columnIdRaw);
if (false === $value) {
return;
}
$mappingIdToName = Metrics::$mappingFromIdToName;
$columnIdReadable = $mappingIdToName[$columnIdRaw];
$relativeValue = $this->getPercentage($value, $this->totalValues[$columnIdRaw]);
$row->addColumn($columnIdReadable . '_ratio_report', $relativeValue);
}
private function getPercentage($value, $totalValue)
{
$percentage = Piwik::getPercentageSafe($value, $totalValue, $this->roundPrecision);
return $percentage . '%';
}
/**
* Returns column from a given row.
* Will work with 2 types of datatable
* - raw datatables coming from the archive DB, which columns are int indexed
* - datatables processed resulting of API calls, which columns have human readable english names
*
* @param Row|array $row
* @param int $columnIdRaw see consts in Archive::
* @param bool|array $mappingIdToName
* @return mixed Value of column, false if not found
*/
protected function getColumn($row, $columnIdRaw, $mappingIdToName = false)
{
if (empty($mappingIdToName)) {
$mappingIdToName = Metrics::$mappingFromIdToName;
}
$columnIdReadable = $mappingIdToName[$columnIdRaw];
if ($row instanceof Row) {
$raw = $row->getColumn($columnIdRaw);
if ($raw !== false) {
return $raw;
}
return $row->getColumn($columnIdReadable);
}
return false;
}
/**
* Make sure to get all rows of the first level table.
*
* @param array $request
*/
protected function manipulateSubtableRequest(&$request)
{
$request['ratio'] = 0;
$request['filter_limit'] = -1;
$request['filter_offset'] = 0;
$parametersToRemove = array('flat', '');
foreach ($parametersToRemove as $param) {
if (array_key_exists($param, $request)) {
unset($request[$param]);
}
}
}
}