Skip to content

Commit

Permalink
refs #57 added tests and fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Mar 2, 2014
1 parent 3f2916b commit b062cb7
Show file tree
Hide file tree
Showing 15 changed files with 1,009 additions and 84 deletions.
45 changes: 25 additions & 20 deletions plugins/Insights/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Piwik\DataTable;
use Piwik\Period\Range;
use Piwik\Piwik;
use Piwik\Plugins\API\ProcessedReport;
use Piwik\API\Request as ApiRequest;
use Piwik\Plugins\VisitsSummary\API as VisitsSummaryAPI;
Expand All @@ -26,14 +27,15 @@ public function getOverallMoversAndShakers($idSite, $period, $date)
{
/** @var DataTable[] $tables */
$tables = array(
$this->getMoversAndShakers($idSite, $period, $date, 'Actions_getPageUrls', 4, 4),
$this->getMoversAndShakers($idSite, $period, $date, 'Actions_getPageTitles', 4, 4),
$this->getMoversAndShakers($idSite, $period, $date, 'Referrers_getKeywords', 4, 4),
$this->getMoversAndShakers($idSite, $period, $date, 'Referrers_getCampaigns', 4, 4),
$this->getMoversAndShakers($idSite, $period, $date, 'Referrers_getAll', 4, 4),
$this->getMoversAndShakers($idSite, $period, $date, 'MultiSites_getAll', 4, 4),
$this->getInsights($idSite, $period, $date, 'Actions_getPageUrls', 4, 4),
$this->getInsights($idSite, $period, $date, 'Actions_getPageTitles', 4, 4),
$this->getInsights($idSite, $period, $date, 'Referrers_getKeywords', 4, 4),
$this->getInsights($idSite, $period, $date, 'Referrers_getCampaigns', 4, 4),
$this->getInsights($idSite, $period, $date, 'Referrers_getAll', 4, 4),
);

// post event to add other reports?
$map = new DataTable\Map();

foreach ($tables as $table) {
Expand All @@ -44,21 +46,22 @@ public function getOverallMoversAndShakers($idSite, $period, $date)
}

// force $limitX and ignore minVisitsPercent, minGrowthPercent
// segment
public function getMoversAndShakers(
$idSite, $period, $date, $reportUniqueId = '', $limitIncreaser = 5, $limitDecreaser = 5, $basedOnTotalMetric = false,
$minVisitsPercent = 2, $minGrowthPercent = 20, $orderBy = 'absolute',
$comparedToXPeriods = 1, $filterBy = '')
public function getInsights(
$idSite, $period, $date, $reportUniqueId, $limitIncreaser = 5, $limitDecreaser = 5,
$basedOnTotalMetric = false, $minVisitsPercent = 2, $minGrowthPercent = 20,
$comparedToXPeriods = 1, $orderBy = 'absolute', $filterBy = '', $segment = false)
{
Piwik::checkUserHasViewAccess(array($idSite));

$metric = 'nb_visits';

$processedReport = new ProcessedReport();
$report = $processedReport->getReportMetadataByUniqueId($idSite, $reportUniqueId);

$lastDate = Range::getDateXPeriodsAgo(abs($comparedToXPeriods), $date, $period);

$currentReport = $this->requestReport($idSite, $period, $date, $report, $metric);
$lastReport = $this->requestReport($idSite, $period, $lastDate[0], $report, $metric);
$currentReport = $this->requestReport($idSite, $period, $date, $report, $metric, $segment);
$lastReport = $this->requestReport($idSite, $period, $lastDate[0], $report, $metric, $segment);

$totalValue = $this->getTotalValue($idSite, $period, $date, $basedOnTotalMetric, $currentReport, $metric);
$minVisits = $this->getMinVisits($totalValue, $minVisitsPercent);
Expand Down Expand Up @@ -91,8 +94,6 @@ public function getMoversAndShakers(
$currentReport,
$lastReport,
$metric,
$totalValue,
$filterBy,
$considerMovers,
$considerNew,
$considerDisappeared
Expand All @@ -108,7 +109,7 @@ public function getMoversAndShakers(
);

$dataTable->filter(
'Piwik\Plugins\Insights\DataTable\Filter\RemoveIrrelevant',
'Piwik\Plugins\Insights\DataTable\Filter\ExcludeLowValue',
array(
$metric,
$minVisits
Expand Down Expand Up @@ -145,7 +146,7 @@ public function getMoversAndShakers(
return $dataTable;
}

private function requestReport($idSite, $period, $date, $report, $metric)
private function requestReport($idSite, $period, $date, $report, $metric, $segment)
{
$params = array(
'method' => $report['module'] . '.' . $report['action'],
Expand All @@ -158,6 +159,10 @@ private function requestReport($idSite, $period, $date, $report, $metric)
'showColumns' => $metric
);

if (!empty($segment)) {
$params['segment'] = $segment;
}

if (!empty($report['parameters']) && is_array($report['parameters'])) {
$params = array_merge($params, $report['parameters']);
}
Expand All @@ -170,9 +175,9 @@ private function requestReport($idSite, $period, $date, $report, $metric)

private function getMinVisits($totalValue, $minVisitsPercent)
{
$minVisits = (int) ceil(($totalValue / 100) * $minVisitsPercent);
$minVisits = ceil(($totalValue / 100) * $minVisitsPercent);

return $minVisits;
return (int) $minVisits;
}

private function getTotalValue($idSite, $period, $date, $basedOnTotalMetric, DataTable $currentReport, $metric)
Expand All @@ -186,7 +191,7 @@ private function getTotalValue($idSite, $period, $date, $basedOnTotalMetric, Dat
$totalValue = 0;
}

return $totalValue;
return (int) $totalValue;
}

$visits = VisitsSummaryAPI::getInstance()->get($idSite, $period, $date, false, array($metric));
Expand Down
37 changes: 37 additions & 0 deletions plugins/Insights/DataTable/Filter/ExcludeLowValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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 ExcludeLowValue extends DataTable\BaseFilter
{
private $minimumValue;
private $columnToRead;

public function __construct($table, $columnToRead, $minimumValue)
{
$this->columnToRead = $columnToRead;
$this->minimumValue = $minimumValue;
}

public function filter($table)
{
if (!$this->minimumValue) {
return;
}

$minimumValue = $this->minimumValue;
$isValueLowPopulation = function ($value) use ($minimumValue) {
return $value < $minimumValue;
};

$table->filter('ColumnCallbackDeleteRow', array($this->columnToRead, $isValueLowPopulation));
}
}
14 changes: 3 additions & 11 deletions plugins/Insights/DataTable/Filter/Insight.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@ class Insight extends DataTable\Filter\CalculateEvolutionFilter
private $considerMovers;
private $considerNew;
private $considerDisappeared;
private $filterBy;
private $totalValue;
private $currentDataTable;

public function __construct($table, $currentDataTable, $pastDataTable, $columnToRead, $totalValue, $filterBy,
public function __construct($table, $currentDataTable, $pastDataTable, $columnToRead,
$considerMovers, $considerNew, $considerDisappeared)
{
parent::__construct($table, $pastDataTable, 'growth', $columnToRead, $quotientPrecision = 1);

$this->totalValue = $totalValue;
$this->filterBy = $filterBy;
$this->currentDataTable = $currentDataTable;
$this->considerMovers = $considerMovers;
$this->considerNew = $considerNew;
Expand Down Expand Up @@ -64,7 +60,7 @@ public function filter($table)
if ($this->considerDisappeared) {
foreach ($this->pastDataTable->getRows() as $key => $row) {

if (!$this->getRowFromTable($this->currentDataTable, $row)) {
if ($this->getRowFromTable($this->currentDataTable, $row)) {
continue;
}

Expand Down Expand Up @@ -93,12 +89,8 @@ private function addRow(DataTable $table, DataTable\Row $row, $growthPercentage,
$columns['value_old'] = $oldValue;
$columns['value_new'] = $newValue;
$columns['difference'] = $difference;

if ($this->totalValue) {
$columns['importance'] = (abs($difference) / $this->totalValue) * 100; // magic formula here
}
$columns['importance'] = abs($difference);

$table->addRowFromArray(array(DataTable\Row::COLUMNS => $columns));
return $columns;
}
}
2 changes: 2 additions & 0 deletions plugins/Insights/DataTable/Filter/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function filter($table)
foreach ($table->getRows() as $key => $row) {

if ($row->getColumn($this->columnToRead) >= 0) {

$countIncreaser++;

if ($countIncreaser > $this->limitIncreaser) {
Expand All @@ -42,6 +43,7 @@ public function filter($table)
if ($countDecreaser > $this->limitDecreaser) {
$table->deleteRow($key);
}

}
}
}
Expand Down
18 changes: 9 additions & 9 deletions plugins/Insights/DataTable/Filter/MinGrowth.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@

class MinGrowth extends BaseFilter
{
private $minGrowthPercent;
private $growthColumn;
private $minValue;
private $columnToRead;

public function __construct($table, $growthColumn, $minGrowthPercent)
public function __construct($table, $columnToRead, $minValue)
{
$this->growthColumn = $growthColumn;
$this->minGrowthPercent = $minGrowthPercent;
$this->columnToRead = $columnToRead;
$this->minValue = abs($minValue);
}

public function filter($table)
{
if (!$this->minGrowthPercent) {
if (!$this->minValue) {
return;
}

foreach ($table->getRows() as $key => $row) {

$growthNumeric = $row->getColumn($this->growthColumn);
$growthNumeric = $row->getColumn($this->columnToRead);

if ($growthNumeric > $this->minGrowthPercent) {
if ($growthNumeric >= $this->minValue) {
continue;
} elseif ($growthNumeric < -$this->minGrowthPercent) {
} elseif ($growthNumeric <= -$this->minValue) {
continue;
}

Expand Down
16 changes: 12 additions & 4 deletions plugins/Insights/DataTable/Filter/OrderBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,18 @@ public function sort(Row $a, Row $b)
return $valA < $valB ? 1 : -1;
}

return strnatcasecmp(
$a->getColumn('nb_visits'),
$b->getColumn('nb_visits')
);
$aVisits = $a->getColumn('nb_visits');
$bVisits = $b->getColumn('nb_visits');

if ($aVisits == $bVisits) {
return 0;
}

if ($aVisits > $bVisits) {
return -1;
}

return 1;
}

}
39 changes: 0 additions & 39 deletions plugins/Insights/DataTable/Filter/RemoveIrrelevant.php

This file was deleted.

2 changes: 1 addition & 1 deletion plugins/Insights/Visualizations/Insight.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function beforeLoadDataTable()
$report = $this->requestConfig->apiMethodToRequestDataTable;
$report = str_replace('.', '_', $report);

$this->requestConfig->apiMethodToRequestDataTable = 'Insights.getMoversAndShakers';
$this->requestConfig->apiMethodToRequestDataTable = 'Insights.getInsights';
$this->requestConfig->request_parameters_to_modify = array(
'reportUniqueId' => $report,
'minVisitsPercent' => $this->requestConfig->min_visits_percent,
Expand Down
Loading

0 comments on commit b062cb7

Please sign in to comment.