diff options
author | Thomas Steur <thomas.steur@googlemail.com> | 2014-02-28 07:20:41 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@googlemail.com> | 2014-02-28 07:20:41 +0400 |
commit | 3055a6571d93272d72a5eb3201bf797577c63488 (patch) | |
tree | 7c88f98495655758e9c122e2e65eb79102e809af /plugins/Insights/DataTable | |
parent | db6199d21047c042ca337f7a86b314a81a0488d2 (diff) |
refs #57 started to work on displaying Insights. Added a widget for the dashboard and a new Insights visualization. More to come... still early version so expect a lot of things to change but I am still happy about feedback
Diffstat (limited to 'plugins/Insights/DataTable')
-rw-r--r-- | plugins/Insights/DataTable/Filter/Insight.php | 104 | ||||
-rw-r--r-- | plugins/Insights/DataTable/Filter/Limit.php | 48 | ||||
-rw-r--r-- | plugins/Insights/DataTable/Filter/MinGrowth.php | 44 | ||||
-rw-r--r-- | plugins/Insights/DataTable/Filter/OrderBy.php | 67 | ||||
-rw-r--r-- | plugins/Insights/DataTable/Filter/RemoveIrrelevant.php | 39 |
5 files changed, 302 insertions, 0 deletions
diff --git a/plugins/Insights/DataTable/Filter/Insight.php b/plugins/Insights/DataTable/Filter/Insight.php new file mode 100644 index 0000000000..4adabdb123 --- /dev/null +++ b/plugins/Insights/DataTable/Filter/Insight.php @@ -0,0 +1,104 @@ +<?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 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, + $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; + $this->considerDisappeared = $considerDisappeared; + } + + public function filter($table) + { + foreach ($this->currentDataTable->getRows() as $key => $row) { + $pastRow = $this->getPastRowFromCurrent($row); + $oldValue = 0; + + if (!$pastRow && !$this->considerNew) { + continue; + } + + if ($pastRow && $this->considerMovers) { + $oldValue = $pastRow->getColumn($this->columnValueToRead); + } elseif ($pastRow) { + continue; + } + + $difference = $this->getDividend($row); + if ($difference === false) { + continue; + } + + $newValue = $row->getColumn($this->columnValueToRead); + $divisor = $this->getDivisor($row); + + $growthPercentage = $this->formatValue($difference, $divisor); + + $this->addRow($table, $row, $growthPercentage, $newValue, $oldValue, $difference); + } + + if ($this->considerDisappeared) { + foreach ($this->pastDataTable->getRows() as $key => $row) { + + if (!$this->getRowFromTable($this->currentDataTable, $row)) { + continue; + } + + $newValue = 0; + $oldValue = $row->getColumn($this->columnValueToRead); + $difference = $newValue - $oldValue; + + $growthPercentage = '-100%'; + + $this->addRow($table, $row, $growthPercentage, $newValue, $oldValue, $difference); + } + } + } + + private function getRowFromTable(DataTable $table, DataTable\Row $row) + { + return $table->getRowFromLabel($row->getColumn('label')); + } + + private function addRow(DataTable $table, DataTable\Row $row, $growthPercentage, $newValue, $oldValue, $difference) + { + $columns = $row->getColumns(); + $columns['growth_percent'] = $growthPercentage; + $columns['growth_percent_numeric'] = str_replace('%', '', $growthPercentage); + $columns['grown'] = $newValue >= $oldValue; + $columns['value_old'] = $oldValue; + $columns['value_new'] = $newValue; + $columns['difference'] = $difference; + + if ($this->totalValue) { + $columns['importance'] = (abs($difference) / $this->totalValue) * 100; // magic formula here + } + + $table->addRowFromArray(array(DataTable\Row::COLUMNS => $columns)); + return $columns; + } +}
\ No newline at end of file diff --git a/plugins/Insights/DataTable/Filter/Limit.php b/plugins/Insights/DataTable/Filter/Limit.php new file mode 100644 index 0000000000..9d39a39ff8 --- /dev/null +++ b/plugins/Insights/DataTable/Filter/Limit.php @@ -0,0 +1,48 @@ +<?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\BaseFilter; + +class Limit extends BaseFilter +{ + private $limitIncreaser; + private $limitDecreaser; + private $columnToRead; + + public function __construct($table, $columnToRead, $limitIncreaser, $limitDecreaser) + { + $this->columnToRead = $columnToRead; + $this->limitIncreaser = (int) $limitIncreaser; + $this->limitDecreaser = (int) $limitDecreaser; + } + + public function filter($table) + { + $countIncreaser = 0; + $countDecreaser = 0; + + foreach ($table->getRows() as $key => $row) { + + if ($row->getColumn($this->columnToRead) >= 0) { + $countIncreaser++; + + if ($countIncreaser > $this->limitIncreaser) { + $table->deleteRow($key); + } + + } else { + $countDecreaser++; + + if ($countDecreaser > $this->limitDecreaser) { + $table->deleteRow($key); + } + } + } + } +}
\ No newline at end of file diff --git a/plugins/Insights/DataTable/Filter/MinGrowth.php b/plugins/Insights/DataTable/Filter/MinGrowth.php new file mode 100644 index 0000000000..244eebf9d9 --- /dev/null +++ b/plugins/Insights/DataTable/Filter/MinGrowth.php @@ -0,0 +1,44 @@ +<?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\BaseFilter; +use Piwik\DataTable; + +class MinGrowth extends BaseFilter +{ + private $minGrowthPercent; + private $growthColumn; + + public function __construct($table, $growthColumn, $minGrowthPercent) + { + $this->growthColumn = $growthColumn; + $this->minGrowthPercent = $minGrowthPercent; + } + + public function filter($table) + { + if (!$this->minGrowthPercent) { + return; + } + + foreach ($table->getRows() as $key => $row) { + + $growthNumeric = $row->getColumn($this->growthColumn); + + if ($growthNumeric > $this->minGrowthPercent) { + continue; + } elseif ($growthNumeric < -$this->minGrowthPercent) { + continue; + } + + $table->deleteRow($key); + } + } +}
\ No newline at end of file diff --git a/plugins/Insights/DataTable/Filter/OrderBy.php b/plugins/Insights/DataTable/Filter/OrderBy.php new file mode 100644 index 0000000000..abb5c6bc7c --- /dev/null +++ b/plugins/Insights/DataTable/Filter/OrderBy.php @@ -0,0 +1,67 @@ +<?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\BaseFilter; +use Piwik\DataTable\Row; + +class OrderBy extends BaseFilter +{ + private $columnToRead; + + public function __construct($table, $columnToRead) + { + $this->columnToRead = $columnToRead; + } + + public function filter($table) + { + if (!$table->getRowsCount()) { + return; + } + + $table->sort(array($this, 'sort'), $this->columnToRead); + } + + public function sort(Row $a, Row $b) + { + $valA = $a->getColumn($this->columnToRead); + $valB = $b->getColumn($this->columnToRead); + + if (!isset($valA) && !isset($valB)) { + return 0; + } + + if (!isset($valA)) { + return 1; + } + + if (!isset($valB)) { + return -1; + } + + if ($valA > 0 && $valB < 0) { + return -1; + } + + if ($valA < 0 && $valB < 0) { + return $valA < $valB ? -1 : 1; + } + + if ($valA != $valB) { + return $valA < $valB ? 1 : -1; + } + + return strnatcasecmp( + $a->getColumn('nb_visits'), + $b->getColumn('nb_visits') + ); + } + +}
\ No newline at end of file diff --git a/plugins/Insights/DataTable/Filter/RemoveIrrelevant.php b/plugins/Insights/DataTable/Filter/RemoveIrrelevant.php new file mode 100644 index 0000000000..7f82d1645d --- /dev/null +++ b/plugins/Insights/DataTable/Filter/RemoveIrrelevant.php @@ -0,0 +1,39 @@ +<?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 RemoveIrrelevant extends DataTable\BaseFilter +{ + private $minRequiredValue; + private $columnToRead; + + public function __construct($table, $columnToRead, $minRequiredValue) + { + $this->columnToRead = $columnToRead; + $this->minRequiredValue = $minRequiredValue; + } + + public function filter($table) + { + if (!$this->minRequiredValue) { + return; + } + + foreach ($table->getRows() as $key => $row) { + + $value = $row->getColumn($this->columnToRead); + + if ($this->minRequiredValue > $value) { + $table->deleteRow($key); + } + } + } +}
\ No newline at end of file |