Alert Rules use Carbon

Use carbon for time queries, need to be careful of timezone quirks.  Timestamps won't have that issue
This commit is contained in:
Tony Murray 2020-04-07 00:02:16 -05:00
parent 29748fe306
commit 4d5ebfb561
2 changed files with 17 additions and 16 deletions

View File

@ -34,6 +34,7 @@
namespace LibreNMS\Alert;
use App\Models\Device;
use Carbon\Carbon;
use LibreNMS\Alert\AlertUtil;
use LibreNMS\Alert\AlertDB;
@ -112,7 +113,7 @@ class AlertRules
if (is_null($current_state)) {
dbInsert(array('state' => 1, 'device_id' => $device_id, 'rule_id' => $rule['id'], 'open' => 1,'alerted' => 0), 'alerts');
} else {
dbUpdate(['state' => 1, 'open' => 1, 'timestamp' => array('NOW()')], 'alerts', 'device_id = ? && rule_id = ?', [$device_id, $rule['id']]);
dbUpdate(['state' => 1, 'open' => 1, 'timestamp' => Carbon::now()->timestamp], 'alerts', 'device_id = ? && rule_id = ?', [$device_id, $rule['id']]);
}
c_echo(PHP_EOL . 'Status: %rALERT');
}
@ -125,7 +126,7 @@ class AlertRules
if (is_null($current_state)) {
dbInsert(['state' => 0, 'device_id' => $device_id, 'rule_id' => $rule['id'], 'open' => 1, 'alerted' => 0], 'alerts');
} else {
dbUpdate(['state' => 0, 'open' => 1, 'note' => '', 'timestamp' => array('NOW()')], 'alerts', 'device_id = ? && rule_id = ?', [$device_id, $rule['id']]);
dbUpdate(['state' => 0, 'open' => 1, 'note' => '', 'timestamp' => Carbon::now()->timestamp], 'alerts', 'device_id = ? && rule_id = ?', [$device_id, $rule['id']]);
}
c_echo(PHP_EOL . 'Status: %gOK');

View File

@ -25,7 +25,7 @@
namespace App\Models;
use DB;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
@ -39,32 +39,32 @@ class AlertSchedule extends Model
public function scopeIsActive($query)
{
// TODO use Carbon?
return $query->where(function ($query) {
$query->where(function ($query) {
$now = CarbonImmutable::now();
$query->where(function ($query) use ($now) {
// Non recurring simply between start and end
$query->where('recurring', 0)
->where('start', '<=', DB::raw('NOW()'))
->where('end', '>=', DB::raw('NOW()'));
})->orWhere(function ($query) {
->where('start', '<=', $now)
->where('end', '>=', $now);
})->orWhere(function ($query) use ($now) {
$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) {
$query->where('start_recurring_dt', '<=', DB::raw("date_format(NOW(), '%Y-%m-%d')"))
->where(function ($query) {
$query->where('end_recurring_dt', '>=', DB::raw("date_format(NOW(), '%Y-%m-%d')"))
->where(function ($query) use ($now) {
$query->where('start_recurring_dt', '<=', $now->format('%Y-%m-%d'))
->where(function ($query) use ($now) {
$query->where('end_recurring_dt', '>=', $now->format('%Y-%m-%d'))
->orWhereNull('end_recurring_dt')
->orWhere('end_recurring_dt', '0000-00-00')
->orWhere('end_recurring_dt', '');
});
})
// Check the time is between the start and end hour/minutes/seconds
->where('start_recurring_hr', '<=', DB::raw("date_format(NOW(), '%H:%i:%s')"))
->where('end_recurring_hr', '>=', DB::raw("date_format(NOW(), '%H:%i:%s')"))
->where('start_recurring_hr', '<=', $now->format('%H:%i:%s'))
->where('end_recurring_hr', '>=', $now->format('%H:%i:%s'))
// Check we are on the correct day of the week
->where(function ($query) {
->where(function ($query) use ($now) {
/** @var Builder $query */
$query->where('recurring_day', 'like', DB::raw("CONCAT('%', date_format(NOW(), '%w'), '%')"))
$query->where('recurring_day', 'like', '%' . $now->format('%w') . '%')
->orWhereNull('recurring_day')
->orWhere('recurring_day', '');
});