Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to disable Flatten feature for a given report #11529

Merged
merged 4 commits into from Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ The Product Changelog at **[piwik.org/changelog](http://piwik.org/changelog)** l

### Breaking Changes
* New config setting `enable_plugin_upload` lets you enable uploading and installing a Piwik plugin ZIP file by a Super User. This used to be enabled by default, but it is now disabled by default now for security reasons.
* New Report class property `Report::$supportsFlatten` lets you define if a report supports flattening (defaults to `true`). If set to `false` it will also set `ViewDataTable\Config::$show_flatten_table` to `false`

### New APIs
* A new event `Controller.triggerAdminNotifications` has been added to let plugins know when they are supposed to trigger notifications in the admin.
Expand Down
6 changes: 6 additions & 0 deletions core/API/DataTablePostProcessor.php
Expand Up @@ -170,6 +170,12 @@ public function applyPivotByFilter(DataTableInterface $dataTable)
public function applyFlattener($dataTable)
{
if (Common::getRequestVar('flat', '0', 'string', $this->request) == '1') {
// skip flattening if not supported by report and remove subtables only
if ($this->report && !$this->report->supportsFlatten()) {
$dataTable->filter('RemoveSubtables');
return $dataTable;
}

$flattener = new Flattener($this->apiModule, $this->apiMethod, $this->request);
if (Common::getRequestVar('include_aggregate_rows', '0', 'string', $this->request) == '1') {
$flattener->includeAggregateRows();
Expand Down
47 changes: 47 additions & 0 deletions core/DataTable/Filter/RemoveSubtables.php
@@ -0,0 +1,47 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\DataTable\Filter;

use Piwik\DataTable;
use Piwik\DataTable\BaseFilter;

/**
* Delete all existing subtables from rows.
*
* **Basic example usage**
*
* $dataTable->filter('RemoveSubtables');
*
* @api
*/
class RemoveSubtables extends BaseFilter
{
/**
* Constructor.
*
* @param DataTable $table The DataTable that will be filtered eventually.
*/
public function __construct($table)
{
parent::__construct($table);
}

/**
* See {@link Limit}.
*
* @param DataTable $table
*/
public function filter($table)
{
$rows = $table->getRows();
foreach ($rows as $row) {
$row->removeSubtable();
}
}
}
17 changes: 17 additions & 0 deletions core/Plugin/Report.php
Expand Up @@ -119,6 +119,14 @@ class Report
*/
protected $hasGoalMetrics = false;

/**
* Set this property to false in case your report can't/shouldn't be flattened.
* In this case, flattener won't be applied even if parameter is provided in a request
* @var bool
* @api
*/
protected $supportsFlatten = true;

/**
* Set it to boolean `true` if your report always returns a constant count of rows, for instance always 24 rows
* for 1-24 hours.
Expand Down Expand Up @@ -493,6 +501,15 @@ public function hasGoalMetrics()
return $this->hasGoalMetrics;
}

/**
* @return bool
* @ignore
*/
public function supportsFlatten()
{
return $this->supportsFlatten;
}

/**
* If the report is enabled the report metadata for this report will be built and added to the list of available
* reports. Overwrite this method and leave it empty in case you do not want your report to be added to the report
Expand Down
10 changes: 10 additions & 0 deletions core/ViewDataTable/Config.php
Expand Up @@ -502,6 +502,7 @@ public function setController($controllerName, $controllerAction)

$this->loadDocumentation();
$this->setShouldShowPivotBySubtable();
$this->setShouldShowFlattener();
}

/** Load documentation from the API */
Expand Down Expand Up @@ -755,6 +756,15 @@ private function setShouldShowPivotBySubtable()
}
}

private function setShouldShowFlattener()
{
$report = ReportsProvider::factory($this->controllerName, $this->controllerAction);

if ($report && !$report->supportsFlatten()) {
$this->show_flatten_table = false;
}
}

public function disablePivotBySubtableIfTableHasNoSubtables(DataTable $table)
{
foreach ($table->getRows() as $row) {
Expand Down
1 change: 1 addition & 0 deletions plugins/Referrers/Reports/GetReferrerType.php
Expand Up @@ -37,6 +37,7 @@ protected function init()
$this->hasGoalMetrics = true;
$this->order = 1;
$this->subcategoryId = 'Referrers_WidgetGetAll';
$this->supportsFlatten = false;
}

public function getDefaultTypeViewDataTable()
Expand Down
12 changes: 6 additions & 6 deletions plugins/UserId/Reports/GetUsers.php
Expand Up @@ -25,11 +25,12 @@ protected function init()
{
parent::init();

$this->name = Piwik::translate('UserId_UserReportTitle');
$this->subcategoryId = 'UserId_UserReportTitle';
$this->documentation = '';
$this->dimension = new UserId();
$this->metrics = array('label', 'nb_visits', 'nb_actions', 'nb_visits_converted');
$this->name = Piwik::translate('UserId_UserReportTitle');
$this->subcategoryId = 'UserId_UserReportTitle';
$this->documentation = '';
$this->dimension = new UserId();
$this->metrics = array('label', 'nb_visits', 'nb_actions', 'nb_visits_converted');
$this->supportsFlatten = false;

// This defines in which order your report appears in the mobile app, in the menu and in the list of widgets
$this->order = 9;
Expand Down Expand Up @@ -60,7 +61,6 @@ public function configureView(ViewDataTable $view)
$view->config->show_related_reports = false;
$view->config->show_insights = false;
$view->config->show_pivot_by_subtable = false;
$view->config->show_flatten_table = false;

if ($view->isViewDataTableId(HtmlTable::ID)) {
$view->config->disable_row_evolution = false;
Expand Down
Expand Up @@ -14,68 +14,26 @@
</row>
<row>
<label>Search Engines</label>
<nb_uniq_visitors>7</nb_uniq_visitors>
<nb_visits>7</nb_visits>
<nb_actions>7</nb_actions>
<nb_users>0</nb_users>
<max_actions>1</max_actions>
<sum_visit_length>0</sum_visit_length>
<bounce_count>7</bounce_count>
<nb_visits_converted>0</nb_visits_converted>
</row>
<row>
<label>Websites</label>
<nb_uniq_visitors>3</nb_uniq_visitors>
<nb_visits>4</nb_visits>
<nb_actions>4</nb_actions>
<nb_users>0</nb_users>
<max_actions>1</max_actions>
<sum_visit_length>0</sum_visit_length>
<bounce_count>4</bounce_count>
<nb_visits_converted>0</nb_visits_converted>
</row>
<row>
<label>Search Engines</label>
<nb_uniq_visitors>3</nb_uniq_visitors>
<nb_visits>3</nb_visits>
<nb_actions>3</nb_actions>
<nb_uniq_visitors>12</nb_uniq_visitors>
<nb_visits>12</nb_visits>
<nb_actions>12</nb_actions>
<nb_users>0</nb_users>
<max_actions>1</max_actions>
<sum_visit_length>0</sum_visit_length>
<bounce_count>3</bounce_count>
<nb_visits_converted>0</nb_visits_converted>
</row>
<row>
<label>Search Engines</label>
<nb_uniq_visitors>2</nb_uniq_visitors>
<nb_visits>2</nb_visits>
<nb_actions>2</nb_actions>
<nb_users>0</nb_users>
<max_actions>1</max_actions>
<sum_visit_length>0</sum_visit_length>
<bounce_count>2</bounce_count>
<bounce_count>12</bounce_count>
<nb_visits_converted>0</nb_visits_converted>
<segment>referrerType==search</segment>
</row>
<row>
<label>Websites</label>
<nb_uniq_visitors>2</nb_uniq_visitors>
<nb_visits>2</nb_visits>
<nb_actions>2</nb_actions>
<nb_users>0</nb_users>
<max_actions>1</max_actions>
<sum_visit_length>0</sum_visit_length>
<bounce_count>2</bounce_count>
<nb_visits_converted>0</nb_visits_converted>
</row>
<row>
<label>Websites</label>
<nb_uniq_visitors>2</nb_uniq_visitors>
<nb_visits>2</nb_visits>
<nb_actions>2</nb_actions>
<nb_uniq_visitors>7</nb_uniq_visitors>
<nb_visits>8</nb_visits>
<nb_actions>8</nb_actions>
<nb_users>0</nb_users>
<max_actions>1</max_actions>
<sum_visit_length>0</sum_visit_length>
<bounce_count>2</bounce_count>
<bounce_count>8</bounce_count>
<nb_visits_converted>0</nb_visits_converted>
<segment>referrerType==website</segment>
</row>
</result>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.