Skip to content

Commit

Permalink
Fix get_min_duration_for_reason exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Fragonite committed Dec 4, 2023
1 parent edbe3f8 commit 91e805b
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions classes/profile_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ public static function get_min_duration_for_group_and_reason(string $group, int
*
* @param int $reason the profile type or REASON_*
* @param bool $usecache whether or not to even bother with caching. This allows for a forceful cache update.
* @return float duration (as seconds) of the fastest profile for a given reason.
* @return float|null duration (as seconds) of the fastest profile for a given reason.
*/
public static function get_min_duration_for_reason(int $reason, bool $usecache = true): float {
public static function get_min_duration_for_reason(int $reason, bool $usecache = true): ?float {
$quota = self::$quotas[$reason];

$cachefield = 'profile_type_' . $reason . '_min_duration_s';
$cache = \cache::make('tool_excimer', 'request_metadata');
$result = $cache->get(self::ALL_GROUP_CACHE_KEY) ?: array();
$result = $cache->get(self::ALL_GROUP_CACHE_KEY) ?: [];

if (!$usecache || empty($result) || !isset($result[$cachefield])) {
// Get and set cache.
Expand All @@ -145,9 +145,19 @@ public static function get_min_duration_for_reason(int $reason, bool $usecache =
WHERE $reasons != ?
ORDER BY duration DESC
";
$resultset = $db->get_records_sql($sql, [
profile::REASON_NONE,
], $quota - 1, 1); // Will fetch the Nth item based on the quota.

try {
$resultset = $db->get_records_sql(
$sql,
[profile::REASON_NONE],
$quota - 1, // Will fetch the Nth item based on the quota.
1
);
} catch (\dml_exception $e) {
debugging('tool_excimer: Failed to get min duration: '.$e->getMessage());
return null;
}

// Cache the results in (avoids recalculation later).
$newvalue = (end($resultset)->min_duration ?? 0.0);
// Updates the cache value if the calculated value is different.
Expand Down

0 comments on commit 91e805b

Please sign in to comment.