-
Notifications
You must be signed in to change notification settings - Fork 11
/
Bandwidth.php
216 lines (186 loc) · 7.89 KB
/
Bandwidth.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
<?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\Bandwidth;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\FrontController;
use Piwik\Plugin\ViewDataTable;
use Piwik\Plugins\Bandwidth\Columns\Bandwidth as BandwidthColumn;
use Piwik\Plugins\CoreVisualizations\Metrics\Formatter\Numeric;
use Piwik\Plugins\CoreVisualizations\Visualizations\Sparklines;
class Bandwidth extends \Piwik\Plugin
{
// module => action. The ones that are defined here will be enriched by bandwidth columns when displayed in the UI
private $reportsToEnrich = [
'Actions' => [
'getPageUrls',
'getPageUrl',
'getPageTitles',
'getPageTitle',
'getDownloads',
'getDownload',
'getOutlink',
'getOutlinks',
'getEntryPageTitles',
'getEntryPageUrls',
'getExitPageTitles',
'getExitPageUrls',
'getSiteSearchKeywords',
'getSiteSearchNoResultKeywords',
'getPageTitlesFollowingSiteSearch',
'getPageUrlsFollowingSiteSearch',
],
'CustomDimensions' => [
'getCustomDimension',
],
];
// we will only show columns in that report in the UI if there was at least one byte tracked for the defined metric
private $enrichReportIfTotalHasValue = [
'Actions.getPageUrls' => Metrics::COLUMN_TOTAL_PAGEVIEW_BANDWIDTH,
'Actions.getPageTitles' => Metrics::COLUMN_TOTAL_PAGEVIEW_BANDWIDTH,
'Actions.getDownloads' => Metrics::COLUMN_TOTAL_DOWNLOAD_BANDWIDTH,
'*' => Metrics::COLUMN_TOTAL_OVERALL_BANDWIDTH // for all other reports use this
];
/**
* @see \Piwik\Plugin::registerEvents
*/
public function registerEvents()
{
$hooks = [
'ViewDataTable.configure' => 'configureViewDataTable',
'Actions.Archiving.addActionMetrics' => 'addActionMetrics',
'Metrics.getDefaultMetricTranslations' => 'addMetricTranslations',
'Actions.getCustomActionDimensionFieldsAndJoins' => 'provideActionDimensionFields',
'Metrics.getDefaultMetricSemanticTypes' => 'addMetricSemanticTypes',
'Metrics.addMetricIdToNameMapping' => 'addMetricIdToNameMapping',
'Metrics.getEvolutionUnit' => 'getEvolutionUnit',
];
foreach ($this->reportsToEnrich as $module => $actions) {
foreach ($actions as $action) {
$hooks['API.' . $module . '.' . $action . '.end'] = 'enrichApi';
}
}
return $hooks;
}
public function renderSparklines(&$out)
{
$out .= FrontController::getInstance()->dispatch('Bandwidth', 'sparklines');
}
public function addMetricTranslations(&$translations)
{
$metrics = Metrics::getMetricTranslations();
$translations = array_merge($translations, $metrics);
}
public function addMetricSemanticTypes(array &$types): void
{
$metricTypes = Metrics::getMetricSemanticTypes();
$types = array_merge($types, $metricTypes);
}
public function getEvolutionUnit(&$unit, $column, $idSite)
{
if (!property_exists(Numeric::class, 'byteSizeUnit')) {
// don't use a default unit in Matomo versions where it didn't exist yet
return;
}
foreach (Metrics::getBandwidthMetrics() as $metric) {
if ($metric->getName() === $column) {
$unit = ' ' . Numeric::$byteSizeUnit;
return;
}
}
foreach (Metrics::getOverallMetrics() as $metric) {
if ($metric->getName() === $column) {
$unit = ' ' . Numeric::$byteSizeUnit;
return;
}
}
}
public function addActionMetrics(&$metricsConfig)
{
foreach (Metrics::getActionMetrics() as $metric => $config) {
$metricsConfig[$metric] = $config;
}
}
public function addMetricIdToNameMapping(&$mapping)
{
foreach (Metrics::getBandwidthMetrics() as $metric) {
$metricId = $metric->getMetricId();
if (!is_int($metricId)) {
continue;
}
$mapping[$metricId] = $metric->getName();
}
}
public function configureViewDataTable(ViewDataTable $view)
{
$module = $view->requestConfig->getApiModuleToRequest();
$method = $view->requestConfig->getApiMethodToRequest();
if ($module === 'API' && $method === 'get' && property_exists($view->config, 'selectable_columns')) {
// here we want to make sure the total column is selectable
$selectable = $view->config->selectable_columns ?: [];
$columns = array_values(Metrics::getNumericRecordNameToColumnsMapping());
$view->config->selectable_columns = array_merge($selectable, $columns);
$view->config->addTranslations(Metrics::getMetricTranslations());
}
if ($module === 'API' && $method === 'get' && $view->isViewDataTableId(Sparklines::ID)) {
// TODO: should use a metric class or something so we don't have to manually support comparisons here
/** @var Sparklines $view */
$view->config->addSparklineMetric(Metrics::COLUMN_TOTAL_OVERALL_BANDWIDTH);
}
if (array_key_exists($module, $this->reportsToEnrich) && in_array($method, $this->reportsToEnrich[$module])) {
$idSite = Common::getRequestVar('idSite');
$date = Common::getRequestVar('date');
$period = Common::getRequestVar('period', 'month', 'string');
if (array_key_exists($module . '.' . $method, $this->enrichReportIfTotalHasValue)) {
$columnToCompare = $this->enrichReportIfTotalHasValue[$module . '.' . $method];
} else {
$columnToCompare = $this->enrichReportIfTotalHasValue['*'];
}
$bandwidthDimension = new BandwidthColumn();
$isUsed = $bandwidthDimension->isUsedInSite($idSite, $period, $date, $columnToCompare);
if (!$isUsed) {
return;
}
$view->config->columns_to_display[] = 'avg_bandwidth';
$view->config->columns_to_display[] = 'sum_bandwidth';
$view->config->addTranslations(Metrics::getMetricTranslations());
}
}
public function enrichApi(DataTable\DataTableInterface $dataTable, $params)
{
$dataTable->filter(function (DataTable $dataTable) {
$extraProcessedMetrics = $dataTable->getMetadata(DataTable::EXTRA_PROCESSED_METRICS_METADATA_NAME);
if (empty($extraProcessedMetrics)) {
$extraProcessedMetrics = [];
}
foreach (Metrics::getBandwidthMetrics() as $metric) {
$extraProcessedMetrics[] = $metric;
}
foreach (Metrics::getOverallMetrics() as $metric) {
$extraProcessedMetrics[] = $metric;
}
$dataTable->setMetadata(DataTable::EXTRA_PROCESSED_METRICS_METADATA_NAME, $extraProcessedMetrics);
});
$dataTable->filter(function (DataTable $dataTable) {
$metricIdsToName = [];
foreach (Metrics::getBandwidthMetrics() as $metric) {
$metricId = $metric->getMetricId();
if (!empty($metricId)) {
$metricIdsToName[$metricId] = $metric->getName();
}
}
$dataTable->queueFilter('ReplaceColumnNames', [$metricIdsToName]);
});
}
public function provideActionDimensionFields(&$fields, &$joins)
{
$column = new BandwidthColumn();
$fields[] = $column->getColumnName();
}
}