Correct logic for recurring alert rules that span UTC days (#14145)

* Correct logic for recurring alert rules that span UTC days. Evaluate the day of week in local time.

* Enable previously broken test cases

* Update TestScheduledMaintenance.php

Co-authored-by: Tony Murray <murraytony@gmail.com>
This commit is contained in:
Nash Kaminski
2022-08-18 20:20:25 -05:00
committed by GitHub
parent 7d3811a2b0
commit 2741612eb6
2 changed files with 9 additions and 8 deletions

View File

@@ -173,11 +173,12 @@ class AlertSchedule extends Model
{
return $query->where(function ($query) {
$now = CarbonImmutable::now('UTC');
$localDayNum = CarbonImmutable::now()->format('N');
$query->where('start', '<=', $now)
->where('end', '>=', $now)
->where(function ($query) use ($now) {
->where(function ($query) use ($now, $localDayNum) {
$query->where('recurring', 0) // Non recurring simply between start and end
->orWhere(function ($query) use ($now) {
->orWhere(function ($query) use ($now, $localDayNum) {
$query->where('recurring', 1)
// Check the time is after the start date and before the end date, or end date is not set
->where(function ($query) use ($now) {
@@ -190,14 +191,14 @@ class AlertSchedule extends Model
// outside, spans days
$query->whereTime('start', '>', DB::raw('time(`end`)'))
->where(function ($query) use ($now) {
$query->whereTime('end', '<=', $now->toTimeString())
->orWhereTime('start', '>', $now->toTimeString());
$query->whereTime('start', '<=', $now->toTimeString())
->orWhereTime('end', '>', $now->toTimeString());
});
});
})
// Check we are on the correct day of the week
->where(function ($query) use ($now) {
$query->where('recurring_day', 'like', $now->format('%N%'))
->where(function ($query) use ($localDayNum) {
$query->where('recurring_day', 'like', "%${localDayNum}%")
->orWhereNull('recurring_day');
});
});