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 237e811
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions classes/profile_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ 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';
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 237e811

Please sign in to comment.