Skip to content

Commit

Permalink
refs #57 more tweaks to insights and movers and shakers and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Mar 6, 2014
1 parent 593dbc8 commit ad9ab5d
Show file tree
Hide file tree
Showing 19 changed files with 448 additions and 207 deletions.
155 changes: 100 additions & 55 deletions plugins/Insights/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@ class API extends \Piwik\Plugin\API
*/
private $model;

/**
* Those reports will be included in the insight / moversAndShakers overview reports.
* You can configure any API parameter for each report such as "flat", "limitIncreaser", "minGrowth", ...
* @var array
*/
private $reportIds = array(
'Actions_getPageUrls',
'Actions_getPageTitles',
'Actions_getDownloads',
'Referrers_getAll',
'Referrers_getKeywords',
'Referrers_getCampaigns',
'Referrers_getSocials',
'Referrers_getSearchEngines',
'UserCountry_getCountry',
'Actions_getPageUrls' => array(),
'Actions_getPageTitles' => array(),
'Actions_getDownloads' => array('flat' => 1),
'Referrers_getWebsites' => array(),
'Referrers_getCampaigns' => array(),
'Referrers_getSocials' => array(),
'Referrers_getSearchEngines' => array(),
'UserCountry_getCountry' => array(),
);

protected function __construct()
Expand All @@ -52,45 +56,69 @@ protected function __construct()
$this->model = new Model();
}

public function getInsightsOverview($idSite, $period, $date, $segment = false)
public function canGenerateInsights($period, $date)
{
Piwik::checkUserHasViewAccess(array($idSite));
try {
$model = new Model();
$lastDate = $model->getLastDate($date, $period, 1);
} catch (\Exception $e) {
return false;
}

$reportTableIds = array();
if (empty($lastDate)) {
return false;
}

/** @var DataTable[] $tables */
$tables = array();
foreach ($this->reportIds as $reportId) {
$firstTableId = DataTable\Manager::getInstance()->getMostRecentTableId();
$table = $this->getInsights($idSite, $period, $date, $reportId, $segment, 3, 3, '', 2, 25);
$reportTableIds[] = $table->getId();
DataTable\Manager::getInstance()->deleteTablesExceptIgnored($reportTableIds, $firstTableId);
return true;
}

$tables[] = $table;
}
public function getInsightsOverview($idSite, $period, $date, $segment = false)
{
Piwik::checkUserHasViewAccess(array($idSite));

$map = new DataTable\Map();
$defaultParams = array(
'limitIncreaser' => 3,
'limitDecreaser' => 3,
'minImpactPercent' => 2,
'minGrowthPercent' => 25,
);

foreach ($tables as $table) {
$map->addTable($table, $table->getMetadata('reportName'));
}
$map = $this->generateOverviewReport('getInsights', $idSite, $period, $date, $segment, $defaultParams);

return $map;
}

public function getOverallMoversAndShakers($idSite, $period, $date, $segment = false)
public function getMoversAndShakersOverview($idSite, $period, $date, $segment = false)
{
Piwik::checkUserHasViewAccess(array($idSite));

$reportTableIds = array();
$defaultParams = array(
'limitIncreaser' => 4,
'limitDecreaser' => 4
);

$map = $this->generateOverviewReport('getMoversAndShakers', $idSite, $period, $date, $segment, $defaultParams);

return $map;
}

private function generateOverviewReport($method, $idSite, $period, $date, $segment, array $defaultParams)
{
$tableManager = DataTable\Manager::getInstance();

/** @var DataTable[] $tables */
$tables = array();
foreach ($this->reportIds as $reportId) {
$firstTableId = DataTable\Manager::getInstance()->getMostRecentTableId();
$table = $this->getMoversAndShakers($idSite, $period, $date, $reportId, $segment, 4, 4);
foreach ($this->reportIds as $reportId => $reportParams) {
foreach ($defaultParams as $key => $defaultParam) {
if (!array_key_exists($key, $reportParams)) {
$reportParams[$key] = $defaultParam;
}
}

$firstTableId = $tableManager->getMostRecentTableId();
$table = $this->requestApiMethod($method, $idSite, $period, $date, $reportId, $segment, $reportParams);
$reportTableIds[] = $table->getId();
DataTable\Manager::getInstance()->deleteTablesExceptIgnored($reportTableIds, $firstTableId);
$tableManager->deleteTablesExceptIgnored($reportTableIds, $firstTableId);

$tables[] = $table;
}
Expand All @@ -105,37 +133,24 @@ public function getOverallMoversAndShakers($idSite, $period, $date, $segment = f
}

public function getMoversAndShakers($idSite, $period, $date, $reportUniqueId, $segment = false,
$limitIncreaser = 4, $limitDecreaser = 4)
$comparedToXPeriods = 1, $limitIncreaser = 4, $limitDecreaser = 4)
{
$orderBy = 'absolute';
$minGrowthPercent = 30;
$minMoversPercent = 2;
$minNewPercent = 2;
$minDisappearedPercent = 2;

Piwik::checkUserHasViewAccess(array($idSite));

$metric = 'nb_visits';
$metric = 'nb_visits';
$orderBy = InsightReport::ORDER_BY_ABSOLUTE;

$reportMetadata = $this->model->getReportByUniqueId($idSite, $reportUniqueId);

$totalValue = $this->model->getTotalValue($idSite, $period, $date, $metric);
$currentReport = $this->model->requestReport($idSite, $period, $date, $reportUniqueId, $metric, $segment);

if ($period === 'day') {
// if website is too young, than use website creation date
// for faster performance just compare against last week?
$pastDate = $this->model->getLastDate($date, $period, 7);
$lastReport = $this->model->requestReport($idSite, 'week', $pastDate, $reportUniqueId, $metric, $segment);
$lastReport->filter('Piwik\Plugins\Insights\DataTable\Filter\Average', array($metric, 7));
$lastDate = Range::factory('week', $pastDate);
$lastDate = $lastDate->getRangeString();
} else {
$lastDate = $this->model->getLastDate($date, $period, 1);
$lastReport = $this->model->requestReport($idSite, $period, $lastDate, $reportUniqueId, $metric, $segment);
}
$lastDate = $this->model->getLastDate($date, $period, $comparedToXPeriods);
$lastTotalValue = $this->model->getTotalValue($idSite, $period, $lastDate, $metric);
$lastReport = $this->model->requestReport($idSite, $period, $lastDate, $reportUniqueId, $metric, $segment);

$insight = new InsightReport();
return $insight->generateInsight($reportMetadata, $period, $date, $lastDate, $metric, $currentReport, $lastReport, $totalValue, $minMoversPercent, $minNewPercent, $minDisappearedPercent, $minGrowthPercent, $orderBy, $limitIncreaser, $limitDecreaser);
return $insight->generateMoverAndShaker($reportMetadata, $period, $date, $lastDate, $metric, $currentReport, $lastReport, $totalValue, $lastTotalValue, $orderBy, $limitIncreaser, $limitDecreaser);
}

public function getInsights(
Expand All @@ -148,11 +163,15 @@ public function getInsights(
$metric = 'nb_visits';

$reportMetadata = $this->model->getReportByUniqueId($idSite, $reportUniqueId);
$lastDate = $this->model->getLastDate($date, $period, $comparedToXPeriods);

$currentReport = $this->model->requestReport($idSite, $period, $date, $reportUniqueId, $metric, $segment);
$lastReport = $this->model->requestReport($idSite, $period, $lastDate, $reportUniqueId, $metric, $segment);
$totalValue = $this->model->getRelevantTotalValue($currentReport, $idSite, $period, $date, $metric);

$lastDate = $this->model->getLastDate($date, $period, $comparedToXPeriods);
$lastReport = $this->model->requestReport($idSite, $period, $lastDate, $reportUniqueId, $metric, $segment);

$minGrowthPercentPositive = abs($minGrowthPercent);
$minGrowthPercentNegative = -1 * $minGrowthPercentPositive;
$minMoversPercent = -1;
$minNewPercent = -1;
$minDisappearedPercent = -1;
Expand All @@ -174,6 +193,32 @@ public function getInsights(
}

$insight = new InsightReport();
return $insight->generateInsight($reportMetadata, $period, $date, $lastDate, $metric, $currentReport, $lastReport, $totalValue, $minMoversPercent, $minNewPercent, $minDisappearedPercent, $minGrowthPercent, $orderBy, $limitIncreaser, $limitDecreaser);
return $insight->generateInsight($reportMetadata, $period, $date, $lastDate, $metric, $currentReport, $lastReport, $totalValue, $minMoversPercent, $minNewPercent, $minDisappearedPercent, $minGrowthPercentPositive, $minGrowthPercentNegative, $orderBy, $limitIncreaser, $limitDecreaser);
}

private function requestApiMethod($method, $idSite, $period, $date, $reportId, $segment, $additionalParams)
{
$params = array(
'method' => 'Insights.' . $method,
'idSite' => $idSite,
'date' => $date,
'period' => $period,
'format' => 'original',
'reportUniqueId' => $reportId,
);

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

if (!empty($additionalParams)) {
foreach ($additionalParams as $key => $value) {
$params[$key] = $value;
}
}

$request = new ApiRequest($params);
return $request->process();
}

}
16 changes: 12 additions & 4 deletions plugins/Insights/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Piwik\Common;
use Piwik\Piwik;
use Piwik\Plugins\Insights\Visualizations\Insight;
use Piwik\View;

/**
Expand All @@ -20,27 +21,34 @@ class Controller extends \Piwik\Plugin\Controller

public function getInsightsOverview()
{
$view = $this->prepareWidget($apiReport = 'getInsightsOverview');
$view = $this->prepareWidget('insightsOverviewWidget.twig', $apiReport = 'getInsightsOverview');

return $view->render();
}

public function getOverallMoversAndShakers()
{
$view = $this->prepareWidget($apiReport = 'getOverallMoversAndShakers');
$view = $this->prepareWidget('moversAndShakersOverviewWidget.twig', $apiReport = 'getMoversAndShakersOverview');

return $view->render();
}

private function prepareWidget($apiReport)
private function prepareWidget($template, $apiReport)
{
$idSite = Common::getRequestVar('idSite', null, 'int');
$period = Common::getRequestVar('period', null, 'string');
$date = Common::getRequestVar('date', null, 'string');

Piwik::checkUserHasViewAccess(array($idSite));

$view = new View('@Insights/overviewWidget.twig');
if (!API::getInstance()->canGenerateInsights($period, $date)) {

$view = new View('@Insights/cannotDisplayReport.twig');
$this->setBasicVariablesView($view);
return $view;
}

$view = new View('@Insights/' . $template);
$this->setBasicVariablesView($view);

$view->reports = API::getInstance()->$apiReport($idSite, $period, $date);
Expand Down
14 changes: 8 additions & 6 deletions plugins/Insights/DataTable/Filter/MinGrowth.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,30 @@

class MinGrowth extends BaseFilter
{
private $minValue;
private $minPositiveGrowth;
private $minNegativeGrowth;
private $columnToRead;

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

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

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

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

if ($growthNumeric >= $this->minValue) {
if ($growthNumeric >= $this->minPositiveGrowth && $growthNumeric >= 0) {
continue;
} elseif ($growthNumeric <= -$this->minValue) {
} elseif ($growthNumeric <= $this->minNegativeGrowth && $growthNumeric < 0) {
continue;
}

Expand Down
Loading

0 comments on commit ad9ab5d

Please sign in to comment.