Skip to content

Commit

Permalink
refs #57 reducing memory usage and improving performance
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Mar 3, 2014
1 parent 4de7581 commit 856a3e6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
63 changes: 33 additions & 30 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\Date;
use Piwik\Log;
use Piwik\Period\Range;
use Piwik\Piwik;
use Piwik\Plugins\API\ProcessedReport;
Expand All @@ -32,17 +33,26 @@ class API extends \Piwik\Plugin\API

public function getInsightsOverview($idSite, $period, $date)
{
Piwik::checkUserHasViewAccess(array($idSite));

/** @var DataTable[] $tables */
$tables = array(
$this->getInsightOverview($idSite, $period, $date, 'Actions_getPageUrls'),
$this->getInsightOverview($idSite, $period, $date, 'Actions_getPageTitles'),
$this->getInsightOverview($idSite, $period, $date, 'Referrers_getKeywords'),
$this->getInsightOverview($idSite, $period, $date, 'Referrers_getCampaigns'),
$this->getInsightOverview($idSite, $period, $date, 'Referrers_getAll'),
$reports = array(
'Actions_getPageUrls',
'Actions_getPageTitles',
'Referrers_getKeywords',
'Referrers_getCampaigns',
'Referrers_getAll'
);

// post event to add other reports?
$tables = array();
foreach ($reports as $report) {
$tableId = DataTable\Manager::getInstance()->getMostRecentTableId();
$tables[] = $this->getInsightOverview($idSite, $period, $date, $report);
DataTable\Manager::getInstance()->deleteAll($tableId);
}

$map = new DataTable\Map();

foreach ($tables as $table) {
Expand All @@ -52,47 +62,40 @@ public function getInsightsOverview($idSite, $period, $date)
return $map;
}

protected function getInsightOverview($idSite, $period, $date, $reportUniqueId, $segment = false)
public function getInsightOverview($idSite, $period, $date, $reportUniqueId, $segment = false, $limitIncreaser = 4,
$limitDecreaser = 4, $minVisitsPercent = 2, $minGrowthPercent = 20, $orderBy = 'absolute',
$considerMovers = true, $considerNew = true, $considerDisappeared = false)
{
Piwik::checkUserHasViewAccess(array($idSite));

$minVisitsPercent = 2;
$metric = 'nb_visits';
$limitIncreaser = 4;
$limitDecreaser = 4;
$minGrowthPercent = 20;
$orderBy = 'absolute';
$considerMovers = true;
$considerNew = true;
$considerDisappeared = false;
// TODO consider disappeared if impact > 10%?
// consider disappeared if impact > 10%?
$report = $this->getReportByUniqueId($idSite, $reportUniqueId);
$totalValue = $this->getTotalValue($idSite, $period, $date, $metric);
$minVisits = $this->getMinVisits($totalValue, $minVisitsPercent);

$report = $this->getReportByUniqueId($idSite, $reportUniqueId);
$currentReport = $this->requestReport($idSite, $period, $date, $report, $metric, $segment);

if ($period == 'day') {
if ($period === 'day') {
// if website is too young, than use website creation date
// for faster performance just compare against last week?
$pastDate = Date::factory($date);
$pastDate = $pastDate->subDay(7);
$lastReport = $this->requestReport($idSite, 'week', $pastDate->toString(), $report, $metric, $segment);
$pastDate = Date::factory($date);
$pastDate = $pastDate->subDay(7);
$lastDate = $pastDate->toString();
$lastReport = $this->requestReport($idSite, 'week', $lastDate, $report, $metric, $segment);
$lastReport->filter('Piwik\Plugins\Insights\DataTable\Filter\Average', array($metric, 7));
} else {
$pastDate = Range::getLastDate($date, $period);
$pastDate = $pastDate[0];
if (empty($pastDate)) {

if (empty($pastDate[0])) {
return new DataTable();
}

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

$totalValue = $this->getTotalValue($idSite, $period, $date, $metric);
$minVisits = $this->getMinVisits($totalValue, $minVisitsPercent);

$lastDate = $pastDate;

return $this->buildInsightsReport($period, $date, $limitIncreaser, $limitDecreaser, $minGrowthPercent, $orderBy, $currentReport, $lastReport, $metric, $considerMovers, $considerNew, $considerDisappeared, $minVisits, $report, $lastDate, $totalValue);
}

Expand Down Expand Up @@ -147,7 +150,7 @@ private function requestReport($idSite, $period, $date, $report, $metric, $segme
'period' => $period,
'date' => $date,
'flat' => 1,
'filter_limit' => 10000,
'filter_limit' => 1000,
'showColumns' => $metric
);

Expand Down
3 changes: 3 additions & 0 deletions plugins/Insights/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Piwik\Plugins\Insights;

use Piwik\Common;
use Piwik\Piwik;
use Piwik\View;

/**
Expand All @@ -23,6 +24,8 @@ public function getInsightOverview()
$period = Common::getRequestVar('period', null, 'string');
$date = Common::getRequestVar('date', null, 'string');

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

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

Expand Down

0 comments on commit 856a3e6

Please sign in to comment.