Skip to content

Commit

Permalink
Apply max execution time limit to goals live query (#21625)
Browse files Browse the repository at this point in the history
* Apply max execution time limit to goals live query

* apply max execution time limit also to ecommerce query
  • Loading branch information
sgiehl authored Dec 7, 2023
1 parent 2f30c45 commit d81f28b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
21 changes: 20 additions & 1 deletion plugins/Ecommerce/VisitorDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
namespace Piwik\Plugins\Ecommerce;

use Piwik\Common;
use Piwik\Config;
use Piwik\DataAccess\LogAggregator;
use Piwik\Date;
use Piwik\DbHelper;
use Piwik\Piwik;
use Piwik\Plugins\Ecommerce\Columns\ProductCategory;
use Piwik\Plugins\Live\Model;
use Piwik\Plugins\Live\VisitorDetailsAbstract;
use Piwik\Site;
use Piwik\Tracker\GoalManager;
Expand Down Expand Up @@ -211,7 +215,17 @@ protected function queryEcommerceConversionsForVisits($idVisits)
WHERE log_conversion.idvisit IN ('" . implode("','", $idVisits) . "')
AND idgoal <= " . GoalManager::IDGOAL_ORDER . "
ORDER BY log_conversion.idvisit, log_conversion.server_time ASC";
$ecommerceDetails = $this->getDb()->fetchAll($sql);

$sql = DbHelper::addMaxExecutionTimeHintToQuery($sql, $this->getLiveQueryMaxExecutionTime());

try {
$ecommerceDetails = $this->getDb()->fetchAll($sql);
} catch (\Exception $e) {
$now = Date::now();
Model::handleMaxExecutionTimeError($this->getDb(), $e, '', $now, $now, null, 0, ['sql' => $sql]);
throw $e;
}

return $ecommerceDetails;
}

Expand Down Expand Up @@ -303,4 +317,9 @@ public function finalizeProfile($visits, &$profile)
$profile['totalAbandonedCartsItems'] = $lastVisit->getColumn('totalAbandonedCartsItems');
}
}

private function getLiveQueryMaxExecutionTime()
{
return Config::getInstance()->General['live_query_max_execution_time'];
}
}
22 changes: 19 additions & 3 deletions plugins/Goals/VisitorDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
namespace Piwik\Plugins\Goals;

use Piwik\Common;
use Piwik\Config;
use Piwik\Date;
use Piwik\DbHelper;
use Piwik\Plugins\Live\Model;
use Piwik\Plugins\Live\VisitorDetailsAbstract;

use function Piwik\Plugins\Referrers\getReferrerTypeFromShortName;

class VisitorDetails extends VisitorDetailsAbstract
{
const EVENT_VALUE_PRECISION = 3;

protected $lastGoalResults = [];
protected $lastVisitIds = [];

Expand Down Expand Up @@ -90,7 +92,16 @@ protected function queryGoalConversionsForVisits($idVisits)
AND log_conversion.idgoal > 0
ORDER BY log_conversion.idvisit, log_conversion.server_time ASC
";
$conversions = $this->getDb()->fetchAll($sql);

$sql = DbHelper::addMaxExecutionTimeHintToQuery($sql, $this->getLiveQueryMaxExecutionTime());

try {
$conversions = $this->getDb()->fetchAll($sql);
} catch (\Exception $e) {
$now = Date::now();
Model::handleMaxExecutionTimeError($this->getDb(), $e, '', $now, $now, null, 0, ['sql' => $sql]);
throw $e;
}

foreach ($conversions as &$conversion) {
$conversion['goalName'] = Common::unsanitizeInputValue($conversion['goalName']);
Expand Down Expand Up @@ -148,4 +159,9 @@ protected function getReferrerType($referrerTypeId)

return $referrerType;
}

private function getLiveQueryMaxExecutionTime()
{
return Config::getInstance()->General['live_query_max_execution_time'];
}
}

0 comments on commit d81f28b

Please sign in to comment.