Skip to content

Commit

Permalink
refs #57 some refactorings to have better testable code, loads of tes…
Browse files Browse the repository at this point in the history
…ts and bugfixes
  • Loading branch information
tsteur committed Mar 5, 2014
1 parent 4e51781 commit a9e8651
Show file tree
Hide file tree
Showing 18 changed files with 864 additions and 339 deletions.
18 changes: 18 additions & 0 deletions core/DataTable/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ public function deleteTable($id)
}
}

/**
* Deletes all tables starting from the $firstTableId to the most recent table id except the ones that are
* supposed to be ignored.
*
* @param int[] $idsToBeIgnored
* @param int $firstTableId
*/
public function deleteTablesExceptIgnored($idsToBeIgnored, $firstTableId = 0)
{
$lastTableId = $this->getMostRecentTableId();

for ($index = $firstTableId; $index <= $lastTableId; $index++) {
if (!in_array($index, $idsToBeIgnored)) {
$this->deleteTable($index);
}
}
}

/**
* Remove the table from the manager (table has already been unset)
*
Expand Down
312 changes: 37 additions & 275 deletions plugins/Insights/API.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion plugins/Insights/DataTable/Filter/ExcludeLowValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function filter($table)

$value = $row->getColumn($this->columnToRead);

if ($this->minimumValue > $value) {
if ($this->minimumValue > abs($value)) {
$table->deleteRow($key);
}
}
Expand Down
52 changes: 30 additions & 22 deletions plugins/Insights/DataTable/Filter/OrderBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

class OrderBy extends BaseFilter
{
private $columnToRead;
private $columnsToCheck;

public function __construct($table, $columnToRead)
public function __construct($table, $columnToRead, $columnSecondOrder, $columnThirdOrder = '')
{
$this->columnToRead = $columnToRead;
$this->columnsToCheck = array($columnToRead, $columnSecondOrder, $columnThirdOrder);
}

public function filter($table)
Expand All @@ -26,27 +26,46 @@ public function filter($table)
return;
}

$table->sort(array($this, 'sort'), $this->columnToRead);
$table->sort(array($this, 'sort'), $this->columnsToCheck[0]);
}

public function sort(Row $a, Row $b)
{
$valA = $a->getColumn($this->columnToRead);
$valB = $b->getColumn($this->columnToRead);
foreach ($this->columnsToCheck as $column) {
if ($column) {

if (!isset($valA) && !isset($valB)) {
$valA = $a->getColumn($column);
$valB = $b->getColumn($column);
$sort = $this->sortVal($valA, $valB);

if (isset($sort)) {
return $sort;
}
}
}

return 0;
}

private function sortVal($valA, $valB)
{
if ((!isset($valA) || $valA === false) && (!isset($valB) || $valB === false)) {
return 0;
}

if (!isset($valA)) {
if (!isset($valA) || $valA === false) {
return 1;
}

if (!isset($valB)) {
if (!isset($valB) || $valB === false) {
return -1;
}

if ($valA > 0 && $valB < 0) {
if ($valA === $valB) {
return null;
}

if ($valA >= 0 && $valB < 0) {
return -1;
}

Expand All @@ -58,18 +77,7 @@ public function sort(Row $a, Row $b)
return $valA < $valB ? 1 : -1;
}

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

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

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

return 1;
return null;
}

}
169 changes: 169 additions & 0 deletions plugins/Insights/InsightReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?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;

use Piwik\DataTable;

/**
* API for plugin Insights
*
* @method static \Piwik\Plugins\Insights\API getInstance()
*/
class InsightReport
{
const ORDER_BY_RELATIVE = 'relative';
const ORDER_BY_ABSOLUTE = 'absolute';
const ORDER_BY_IMPORTANCE = 'importance';

/**
* @param array $reportMetadata
* @param string $period
* @param string $date
* @param string $lastDate
* @param string $metric
* @param DataTable $currentReport
* @param DataTable $lastReport
* @param int $totalValue
* @param int $minVisitsMoversPercent Exclude rows who moved and the difference is not at least min percent
* visits of totalVisits. -1 excludes movers.
* @param int $minVisitsNewPercent Exclude rows who are new and the difference is not at least min percent
* visits of totalVisits. -1 excludes all new.
* @param int $minVisitsDisappearedPercent Exclude rows who are disappeared and the difference is not at least min
* percent visits of totalVisits. -1 excludes all disappeared.
* @param int $minGrowthPercent The actual growth of a row must be at least percent compared to the
* previous value (not total value)
* @param string $orderBy Order by absolute, relative, importance
* @param int $limitIncreaser
* @param int $limitDecreaser
* @return DataTable
*/
public function generateInsight($reportMetadata, $period, $date, $lastDate, $metric, $currentReport, $lastReport, $totalValue, $minVisitsMoversPercent, $minVisitsNewPercent, $minVisitsDisappearedPercent, $minGrowthPercent, $orderBy, $limitIncreaser, $limitDecreaser)
{
$minChangeMovers = $this->getMinVisits($totalValue, $minVisitsMoversPercent);
$minIncreaseNew = $this->getMinVisits($totalValue, $minVisitsNewPercent);
$minDecreaseDisappeared = $this->getMinVisits($totalValue, $minVisitsDisappearedPercent);

$dataTable = new DataTable();
$dataTable->filter(
'Piwik\Plugins\Insights\DataTable\Filter\Insight',
array(
$currentReport,
$lastReport,
$metric,
$considerMovers = (-1 !== $minVisitsMoversPercent),
$considerNew = (-1 !== $minVisitsNewPercent),
$considerDisappeared = (-1 !== $minVisitsDisappearedPercent)
)
);

$dataTable->filter(
'Piwik\Plugins\Insights\DataTable\Filter\MinGrowth',
array(
'growth_percent_numeric',
$minGrowthPercent,
)
);

if ($minIncreaseNew) {
$dataTable->filter(
'Piwik\Plugins\Insights\DataTable\Filter\ExcludeLowValue',
array(
'difference',
$minIncreaseNew,
'isNew'
)
);
}

if ($minChangeMovers) {
$dataTable->filter(
'Piwik\Plugins\Insights\DataTable\Filter\ExcludeLowValue',
array(
'difference',
$minChangeMovers,
'isMover'
)
);
}

if ($minDecreaseDisappeared) {
$dataTable->filter(
'Piwik\Plugins\Insights\DataTable\Filter\ExcludeLowValue',
array(
'difference',
$minDecreaseDisappeared,
'isDisappeared'
)
);
}

$dataTable->filter(
'Piwik\Plugins\Insights\DataTable\Filter\OrderBy',
array(
$this->getOrderByColumn($orderBy),
$orderBy === self::ORDER_BY_RELATIVE ? $this->getOrderByColumn(self::ORDER_BY_ABSOLUTE) : $this->getOrderByColumn(self::ORDER_BY_RELATIVE),
$metric
)
);

$dataTable->filter(
'Piwik\Plugins\Insights\DataTable\Filter\Limit',
array(
'growth_percent_numeric',
$limitIncreaser,
$limitDecreaser
)
);

$dataTable->setMetadataValues(array(
'reportName' => $reportMetadata['name'],
'metricName' => $reportMetadata['metrics'][$metric],
'date' => $date,
'lastDate' => $lastDate,
'period' => $period,
'report' => $reportMetadata,
'totalValue' => $totalValue,
'minChangeMovers' => $minChangeMovers,
'minIncreaseNew' => $minIncreaseNew,
'minDecreaseDisappeared' => $minDecreaseDisappeared,
'minGrowthPercent' => $minGrowthPercent,
'minVisitsMoversPercent' => $minVisitsMoversPercent,
'minVisitsNewPercent' => $minVisitsNewPercent,
'minVisitsDisappearedPercent' => $minVisitsDisappearedPercent
));

return $dataTable;
}

private function getOrderByColumn($orderBy)
{
if (self::ORDER_BY_RELATIVE == $orderBy) {
$orderByColumn = 'growth_percent_numeric';
} elseif (self::ORDER_BY_ABSOLUTE == $orderBy) {
$orderByColumn = 'difference';
} elseif (self::ORDER_BY_IMPORTANCE == $orderBy) {
$orderByColumn = 'importance';
} else {
throw new \Exception('Unsupported orderBy');
}

return $orderByColumn;
}

private function getMinVisits($totalValue, $percent)
{
if ($percent <= 0) {
return 0;
}

$minVisits = ceil(($totalValue / 100) * $percent);

return (int) $minVisits;
}
}
Loading

0 comments on commit a9e8651

Please sign in to comment.