Skip to content

Commit

Permalink
Merge pull request #7119 from piwik/performance_improvements
Browse files Browse the repository at this point in the history
Various performance improvements, especially for Range period and installations with many sites
Matthieu Aubry committed Feb 9, 2015
2 parents eb29435 + 832ecfb commit 19acdb5
Showing 4 changed files with 119 additions and 14 deletions.
12 changes: 9 additions & 3 deletions core/API/DataTableManipulator.php
Original file line number Diff line number Diff line change
@@ -124,7 +124,7 @@ protected function loadSubtable($dataTable, $row)
}
}

$method = $this->getApiMethodForSubtable();
$method = $this->getApiMethodForSubtable($request);
return $this->callApiAndReturnDataTable($this->apiModule, $method, $request);
}

@@ -144,10 +144,16 @@ protected abstract function manipulateSubtableRequest($request);
* @throws Exception
* @return string
*/
private function getApiMethodForSubtable()
private function getApiMethodForSubtable($request)
{
if (!$this->apiMethodForSubtable) {
$meta = API::getInstance()->getMetadata('all', $this->apiModule, $this->apiMethod);
if (!empty($request['idSite'])) {
$idSite = $request['idSite'];
} else {
$idSite = 'all';
}

$meta = API::getInstance()->getMetadata($idSite, $this->apiModule, $this->apiMethod);

if (empty($meta)) {
throw new Exception(sprintf(
48 changes: 37 additions & 11 deletions core/Common.php
Original file line number Diff line number Diff line change
@@ -829,11 +829,20 @@ public static function getLanguageToCountryList()
*/
public static function getSearchEngineUrls()
{
require_once PIWIK_INCLUDE_PATH . '/core/DataFiles/SearchEngines.php';
$cacheId = 'Common.getSearchEngineUrls';
$cache = Cache::getTransientCache();
$searchEngines = $cache->fetch($cacheId);

$searchEngines = $GLOBALS['Piwik_SearchEngines'];
if (empty($searchEngines)) {

Piwik::postEvent('Referrer.addSearchEngineUrls', array(&$searchEngines));
require_once PIWIK_INCLUDE_PATH . '/core/DataFiles/SearchEngines.php';

$searchEngines = $GLOBALS['Piwik_SearchEngines'];

Piwik::postEvent('Referrer.addSearchEngineUrls', array(&$searchEngines));

$cache->save($cacheId, $searchEngines);
}

return $searchEngines;
}
@@ -847,13 +856,21 @@ public static function getSearchEngineUrls()
*/
public static function getSearchEngineNames()
{
$searchEngines = self::getSearchEngineUrls();
$cacheId = 'Common.getSearchEngineNames';
$cache = Cache::getTransientCache();
$nameToUrl = $cache->fetch($cacheId);

if (empty($nameToUrl)) {

$searchEngines = self::getSearchEngineUrls();

$nameToUrl = array();
foreach ($searchEngines as $url => $info) {
if (!isset($nameToUrl[$info[0]])) {
$nameToUrl[$info[0]] = $url;
$nameToUrl = array();
foreach ($searchEngines as $url => $info) {
if (!isset($nameToUrl[$info[0]])) {
$nameToUrl[$info[0]] = $url;
}
}
$cache->save($cacheId, $nameToUrl);
}

return $nameToUrl;
@@ -868,11 +885,20 @@ public static function getSearchEngineNames()
*/
public static function getSocialUrls()
{
require_once PIWIK_INCLUDE_PATH . '/core/DataFiles/Socials.php';
$cacheId = 'Common.getSocialUrls';
$cache = Cache::getTransientCache();
$socialUrls = $cache->fetch($cacheId);

if (empty($socialUrls)) {

require_once PIWIK_INCLUDE_PATH . '/core/DataFiles/Socials.php';

$socialUrls = $GLOBALS['Piwik_socialUrl'];
$socialUrls = $GLOBALS['Piwik_socialUrl'];

Piwik::postEvent('Referrer.addSocialUrls', array(&$socialUrls));
Piwik::postEvent('Referrer.addSocialUrls', array(&$socialUrls));

$cache->save($cacheId, $socialUrls);
}

return $socialUrls;
}
62 changes: 62 additions & 0 deletions core/Period/Range.php
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
namespace Piwik\Period;

use Exception;
use Piwik\Cache;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Date;
@@ -33,6 +34,11 @@ class Range extends Period
protected $label = 'range';
protected $today;

/**
* @var null|Date
*/
protected $defaultEndDate;

/**
* Constructor.
*
@@ -59,6 +65,41 @@ public function __construct($strPeriod, $strDate, $timezone = 'UTC', $today = fa
$this->translator = StaticContainer::get('Piwik\Translation\Translator');
}

private function getCache()
{
return Cache::getTransientCache();
}

private function getCacheId()
{
$end = '';
if ($this->defaultEndDate) {
$end = $this->defaultEndDate->getTimestamp();
}

$today = $this->today->getTimestamp();

return 'range' . $this->strPeriod . $this->strDate . $this->timezone . $end . $today;
}

private function loadAllFromCache()
{
$range = $this->getCache()->fetch($this->getCacheId());

if (!empty($range)) {
foreach ($range as $key => $val) {
$this->$key = $val;
}
}
}

private function cacheAll()
{
$props = get_object_vars($this);

$this->getCache()->save($this->getCacheId(), $props);
}

/**
* Returns the current period as a localized short string.
*
@@ -159,6 +200,12 @@ protected function generate()
return;
}

$this->loadAllFromCache();

if ($this->subperiodsProcessed) {
return;
}

parent::generate();

if (preg_match('/(last|previous)([0-9]*)/', $this->strDate, $regs)) {
@@ -212,13 +259,15 @@ protected function generate()

if ($this->strPeriod != 'range') {
$this->fillArraySubPeriods($startDate, $endDate, $this->strPeriod);
$this->cacheAll();
return;
}

$this->processOptimalSubperiods($startDate, $endDate);
// When period=range, we want End Date to be the actual specified end date,
// rather than the end of the month / week / whatever is used for processing this range
$this->endDate = $endDate;
$this->cacheAll();
}

/**
@@ -460,4 +509,17 @@ private function isEndOfWeekLaterThanEndDate($endDate, $endOfWeek)

return $isEndOfWeekLaterThanEndDate;
}

/**
* Returns the date range string comprising two dates
*
* @return string eg, `'2012-01-01,2012-01-31'`.
*/
public function getRangeString()
{
$dateStart = $this->getDateStart();
$dateEnd = $this->getDateEnd();

return $dateStart->toString("Y-m-d") . "," . $dateEnd->toString("Y-m-d");
}
}
11 changes: 11 additions & 0 deletions core/ViewDataTable/Manager.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
*/
namespace Piwik\ViewDataTable;

use Piwik\Cache;
use Piwik\Common;
use Piwik\Option;
use Piwik\Piwik;
@@ -63,6 +64,14 @@ public static function getIdsWithInheritance($klass)
*/
public static function getAvailableViewDataTables()
{
$cache = Cache::getTransientCache();
$cacheId = 'ViewDataTable.getAvailableViewDataTables';
$dataTables = $cache->fetch($cacheId);

if (!empty($dataTables)) {
return $dataTables;
}

$klassToExtend = '\\Piwik\\Plugin\\ViewDataTable';

/** @var string[] $visualizations */
@@ -107,6 +116,8 @@ public static function getAvailableViewDataTables()
$result[$vizId] = $viz;
}

$cache->save($cacheId, $result);

return $result;
}

0 comments on commit 19acdb5

Please sign in to comment.