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
|
<?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_Plugins
* @package DBStats
*/
namespace Piwik\Plugins\DBStats;
use Piwik\Common;
use Piwik\Date;
use Piwik\Menu\MenuAdmin;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\Plugins\CoreVisualizations\Visualizations\Graph;
use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable;
use Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph\Pie;
use Piwik\ScheduledTask;
use Piwik\ScheduledTime\Weekly;
use \Piwik\Plugin\ViewDataTable;
/**
*
* @package DBStats
*/
class DBStats extends \Piwik\Plugin
{
const TIME_OF_LAST_TASK_RUN_OPTION = 'dbstats_time_of_last_cache_task_run';
/**
* @see Piwik_Plugin::getListHooksRegistered
*/
public function getListHooksRegistered()
{
return array(
'AssetManager.getStylesheetFiles' => 'getStylesheetFiles',
'Menu.Admin.addItems' => 'addMenu',
'TaskScheduler.getScheduledTasks' => 'getScheduledTasks',
'ViewDataTable.configure' => 'configureViewDataTable',
'ViewDataTable.getDefaultType' => 'getDefaultTypeViewDataTable'
);
}
function addMenu()
{
MenuAdmin::getInstance()->add('CoreAdminHome_MenuDiagnostic', 'DBStats_DatabaseUsage',
array('module' => 'DBStats', 'action' => 'index'),
Piwik::isUserIsSuperUser(),
$order = 6);
}
/**
* Gets all scheduled tasks executed by this plugin.
*/
public function getScheduledTasks(&$tasks)
{
$cacheDataByArchiveNameReportsTask = new ScheduledTask(
$this,
'cacheDataByArchiveNameReports',
null,
new Weekly(),
ScheduledTask::LOWEST_PRIORITY
);
$tasks[] = $cacheDataByArchiveNameReportsTask;
}
/**
* Caches the intermediate DataTables used in the getIndividualReportsSummary and
* getIndividualMetricsSummary reports in the option table.
*/
public function cacheDataByArchiveNameReports()
{
$api = API::getInstance();
$api->getIndividualReportsSummary(true);
$api->getIndividualMetricsSummary(true);
$now = Date::now()->getLocalized("%longYear%, %shortMonth% %day%");
Option::set(self::TIME_OF_LAST_TASK_RUN_OPTION, $now);
}
public function getStylesheetFiles(&$stylesheets)
{
$stylesheets[] = "plugins/DBStats/stylesheets/dbStatsTable.less";
}
/** Returns the date when the cacheDataByArchiveNameReports was last run. */
public static function getDateOfLastCachingRun()
{
return Option::get(self::TIME_OF_LAST_TASK_RUN_OPTION);
}
public function getDefaultTypeViewDataTable(&$defaultViewTypes)
{
$defaultViewTypes['DBStats.getDatabaseUsageSummary'] = Pie::ID;
$defaultViewTypes['DBStats.getTrackerDataSummary'] = HtmlTable::ID;
$defaultViewTypes['DBStats.getMetricDataSummary'] = HtmlTable::ID;
$defaultViewTypes['DBStats.getMetricDataSummaryByYear'] = HtmlTable::ID;
$defaultViewTypes['DBStats.getReportDataSummary'] = HtmlTable::ID;
$defaultViewTypes['DBStats.getReportDataSummaryByYear'] = HtmlTable::ID;
$defaultViewTypes['DBStats.getIndividualReportsSummary'] = HtmlTable::ID;
$defaultViewTypes['DBStats.getIndividualMetricsSummary'] = HtmlTable::ID;
$defaultViewTypes['DBStats.getAdminDataSummary'] = HtmlTable::ID;
}
public function configureViewDataTable(ViewDataTable $view)
{
switch ($view->requestConfig->apiMethodToRequestDataTable) {
case 'DBStats.getDatabaseUsageSummar':
$this->configureViewForGetDatabaseUsageSummary($view);
break;
case 'DBStats.getTrackerDataSummary':
$this->configureViewForGetTrackerDataSummary($view);
break;
case 'DBStats.getMetricDataSummary':
$this->configureViewForGetMetricDataSummary($view);
break;
case 'DBStats.getMetricDataSummaryByYear':
$this->configureViewForGetMetricDataSummaryByYear($view);
break;
case 'DBStats.getReportDataSummary':
$this->configureViewForGetReportDataSummary($view);
break;
case 'DBStats.getReportDataSummaryByYear':
$this->configureViewForGetReportDataSummaryByYear($view);
break;
case 'DBStats.getIndividualReportsSummary':
$this->configureViewForGetIndividualReportsSummary($view);
break;
case 'DBStats.getIndividualMetricsSummary':
$this->configureViewForGetIndividualMetricsSummary($view);
break;
case 'DBStats.getAdminDataSummary':
$this->configureViewForGetAdminDataSummary($view);
break;
}
}
private function configureViewForGetDatabaseUsageSummary(ViewDataTable $view)
{
$this->addBaseDisplayProperties($view);
$this->addPresentationFilters($view, $addTotalSizeColumn = true, $addPercentColumn = true);
$view->config->show_offset_information = false;
$view->config->show_pagination_control = false;
if ($view->isViewDataTableId(Graph::ID)) {
$view->config->show_all_ticks = true;
}
// translate the labels themselves
$valueToTranslationStr = array(
'tracker_data' => 'DBStats_TrackerTables',
'report_data' => 'DBStats_ReportTables',
'metric_data' => 'DBStats_MetricTables',
'other_data' => 'DBStats_OtherTables'
);
$translateSummaryLabel = function ($value) use ($valueToTranslationStr) {
return isset($valueToTranslationStr[$value])
? Piwik::translate($valueToTranslationStr[$value])
: $value;
};
$view->config->filters[] = array('ColumnCallbackReplace', array('label', $translateSummaryLabel), $isPriority = true);
}
private function configureViewForGetTrackerDataSummary(ViewDataTable $view)
{
$this->addBaseDisplayProperties($view);
$this->addPresentationFilters($view);
$view->requestConfig->filter_sort_order = 'asc';
$view->config->show_offset_information = false;
$view->config->show_pagination_control = false;
}
private function configureViewForGetMetricDataSummary(ViewDataTable $view)
{
$this->addBaseDisplayProperties($view);
$this->addPresentationFilters($view);
$view->config->title = Piwik::translate('DBStats_MetricTables');
$view->config->addRelatedReports(array(
'DBStats.getMetricDataSummaryByYear' => Piwik::translate('DBStats_MetricDataByYear')
));
}
private function configureViewForGetMetricDataSummaryByYear(ViewDataTable $view)
{
$this->addBaseDisplayProperties($view);
$this->addPresentationFilters($view);
$view->config->title = Piwik::translate('DBStats_MetricDataByYear');
$view->config->addTranslation('label', Piwik::translate('CoreHome_PeriodYear'));
$view->config->addRelatedReports(array(
'DBStats.getMetricDataSummary' => Piwik::translate('DBStats_MetricTables')
));
}
private function configureViewForGetReportDataSummary(ViewDataTable $view)
{
$this->addBaseDisplayProperties($view);
$this->addPresentationFilters($view);
$view->config->title = Piwik::translate('DBStats_ReportTables');
$view->config->addRelatedReports(array(
'DBStats.getReportDataSummaryByYear' => Piwik::translate('DBStats_ReportDataByYear')
));
}
private function configureViewForGetReportDataSummaryByYear(ViewDataTable $view)
{
$this->addBaseDisplayProperties($view);
$this->addPresentationFilters($view);
$view->config->title = Piwik::translate('DBStats_ReportDataByYear');
$view->config->addTranslation('label', Piwik::translate('CoreHome_PeriodYear'));
$view->config->addRelatedReports(array(
'DBStats.getReportDataSummary' => Piwik::translate('DBStats_ReportTables')
));
}
private function configureViewForGetIndividualReportsSummary(ViewDataTable $view)
{
$this->addBaseDisplayProperties($view);
$this->addPresentationFilters($view, $addTotalSizeColumn = false, $addPercentColumn = false,
$sizeColumns = array('estimated_size'));
$view->requestConfig->filter_sort_order = 'asc';
$view->config->addTranslation('label', Piwik::translate('General_Report'));
// this report table has some extra columns that shouldn't be shown
if ($view->isViewDataTableId(HtmlTable::ID)) {
$view->config->columns_to_display = array('label', 'row_count', 'estimated_size');
}
$this->setIndividualSummaryFooterMessage($view);
}
private function configureViewForGetIndividualMetricsSummary(ViewDataTable $view)
{
$this->addBaseDisplayProperties($view);
$this->addPresentationFilters($view, $addTotalSizeColumn = false, $addPercentColumn = false,
$sizeColumns = array('estimated_size'));
$view->requestConfig->filter_sort_order = 'asc';
$view->config->addTranslation('label', Piwik::translate('General_Metric'));
$this->setIndividualSummaryFooterMessage($view);
}
private function configureViewForGetAdminDataSummary(ViewDataTable $view)
{
$this->addBaseDisplayProperties($view);
$this->addPresentationFilters($view);
$view->requestConfig->filter_sort_order = 'asc';
$view->config->show_offset_information = false;
$view->config->show_pagination_control = false;
}
private function addBaseDisplayProperties(ViewDataTable $view)
{
$view->requestConfig->filter_sort_column = 'label';
$view->requestConfig->filter_sort_order = 'desc';
$view->requestConfig->filter_limit = 25;
$view->config->show_exclude_low_population = false;
$view->config->show_table_all_columns = false;
$view->config->show_tag_cloud = false;
$view->config->show_search = false;
if ($view->isViewDataTableId(HtmlTable::ID)) {
$view->config->keep_summary_row = true;
$view->config->disable_row_evolution = true;
$view->config->highlight_summary_row = true;
}
$view->config->addTranslations(array(
'label' => Piwik::translate('DBStats_Table'),
'year' => Piwik::translate('CoreHome_PeriodYear'),
'data_size' => Piwik::translate('DBStats_DataSize'),
'index_size' => Piwik::translate('DBStats_IndexSize'),
'total_size' => Piwik::translate('DBStats_TotalSize'),
'row_count' => Piwik::translate('DBStats_RowCount'),
'percent_total' => '% ' . Piwik::translate('DBStats_DBSize'),
'estimated_size' => Piwik::translate('DBStats_EstimatedSize')
));
}
private function addPresentationFilters(ViewDataTable $view, $addTotalSizeColumn = true, $addPercentColumn = false,
$sizeColumns = array('data_size', 'index_size'))
{
// add total_size column
if ($addTotalSizeColumn) {
$getTotalTableSize = function ($dataSize, $indexSize) {
return $dataSize + $indexSize;
};
$view->config->filters[] = array('ColumnCallbackAddColumn',
array(array('data_size', 'index_size'), 'total_size', $getTotalTableSize), $isPriority = true);
$sizeColumns[] = 'total_size';
}
$runPrettySizeFilterBeforeGeneric = false;
if ($view->isViewDataTableId(HtmlTable::ID)) {
// add summary row only if displaying a table
$view->config->filters[] = array(
'AddSummaryRow', array(0, Piwik::translate('General_Total'), 'label', false), $isPriority = true);
// add percentage column if desired
if ($addPercentColumn
&& $addTotalSizeColumn
) {
$view->config->filters[] = array('ColumnCallbackAddColumnPercentage',
array('percent_total', 'total_size', 'total_size', $quotientPrecision = 0,
$shouldSkipRows = false, $getDivisorFromSummaryRow = true),
$isPriority = true
);
$view->requestConfig->filter_sort_column = 'percent_total';
}
} else if ($view->isViewDataTableId(Graph::ID)) {
if ($addTotalSizeColumn) {
$view->config->columns_to_display = array('label', 'total_size');
// when displaying a graph, we force sizes to be shown as the same unit so axis labels
// will be readable. NOTE: The unit should depend on the smallest value of the data table,
// however there's no way to know this information, short of creating a custom filter. For
// now, just assume KB.
$fixedMemoryUnit = 'K';
$view->config->y_axis_unit = ' K';
$view->requestConfig->filter_sort_column = 'total_size';
$view->requestConfig->filter_sort_order = 'desc';
$runPrettySizeFilterBeforeGeneric = true;
} else {
$view->config->columns_to_display = array('label', 'row_count');
$view->config->y_axis_unit = ' ' . Piwik::translate('General_Rows');
$view->requestConfig->filter_sort_column = 'row_count';
$view->requestConfig->filter_sort_order = 'desc';
}
}
$getPrettySize = array('\Piwik\MetricsFormatter', 'getPrettySizeFromBytes');
$params = !isset($fixedMemoryUnit) ? array() : array($fixedMemoryUnit);
$view->config->filters[] = array('ColumnCallbackReplace', array($sizeColumns, $getPrettySize, $params), $runPrettySizeFilterBeforeGeneric);
// jqPlot will display as, well, ' ', so don't replace the spaces when rendering as a graph
if ($view->isViewDataTableId(HtmlTable::ID)) {
$replaceSpaces = function ($value) {
return str_replace(' ', ' ', $value);
};
$view->config->filters[] = array('ColumnCallbackReplace', array($sizeColumns, $replaceSpaces));
}
$getPrettyNumber = array('\Piwik\MetricsFormatter', 'getPrettyNumber');
$view->config->filters[] = array('ColumnCallbackReplace', array('row_count', $getPrettyNumber));
}
/**
* Sets the footer message for the Individual...Summary reports.
*/
private function setIndividualSummaryFooterMessage(ViewDataTable $view)
{
$lastGenerated = self::getDateOfLastCachingRun();
if ($lastGenerated !== false) {
$view->config->show_footer_message = Piwik::translate('Mobile_LastUpdated', $lastGenerated);
}
}
}
|