Fix Device::isUnderMaintenance() add location relation

This commit is contained in:
Tony Murray
2020-06-14 12:39:10 -05:00
parent eb6370acd4
commit 0a5c3cc747
6 changed files with 25 additions and 18 deletions

View File

@@ -27,6 +27,7 @@ namespace LibreNMS\Alert;
use App\Models\Device;
use App\Models\User;
use DeviceCache;
use LibreNMS\Config;
use PHPMailer\PHPMailer\PHPMailer;
@@ -196,8 +197,7 @@ class AlertUtil
*/
public static function isMaintenance($device_id)
{
$device = Device::find($device_id);
return !is_null($device) && $device->isUnderMaintenance();
return DeviceCache::get($device_id)->isUnderMaintenance();
}
/**

View File

@@ -453,7 +453,7 @@ class RunAlerts
$noacc = false;
}
if (AlertUtil::isMaintenance($alert['device_id']) > 0) {
if (AlertUtil::isMaintenance($alert['device_id'])) {
$noiss = true;
$noacc = true;
}

View File

@@ -31,6 +31,7 @@ use Date;
use DB;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class AlertSchedule extends Model
{
@@ -167,7 +168,7 @@ class AlertSchedule extends Model
// check inside start and end times or outside start and end times (if we span a day)
$active = $spans_days ? ($after_start && ($now_time < $end_time || $now_time >= $start_time)) : ($now_time >= $start_time && $now_time < $end_time);
return $active && str_contains($this->attributes['recurring_day'], $now->format('N')) ? self::SCHEDULE_ACTIVE : self::SCHEDULE_SET;
return $active && Str::contains($this->attributes['recurring_day'], $now->format('N')) ? self::SCHEDULE_ACTIVE : self::SCHEDULE_SET;
}
// ---- Query scopes ----
@@ -212,11 +213,16 @@ class AlertSchedule extends Model
public function devices()
{
return $this->morphedByMany(\App\Models\Device::class, 'alert_schedulable', 'alert_schedulables', 'schedule_id', 'schedule_id');
return $this->morphedByMany(\App\Models\Device::class, 'alert_schedulable', 'alert_schedulables', 'schedule_id', 'alert_schedulable_id');
}
public function deviceGroups()
{
return $this->morphedByMany(\App\Models\DeviceGroup::class, 'alert_schedulable');
return $this->morphedByMany(\App\Models\DeviceGroup::class, 'alert_schedulable', 'alert_schedulables', 'schedule_id', 'alert_schedulable_id');
}
public function locations()
{
return $this->morphedByMany(\App\Models\Location::class, 'alert_schedulable', 'alert_schedulables', 'schedule_id', 'alert_schedulable_id');
}
}

View File

@@ -133,25 +133,25 @@ class Device extends BaseModel
public function isUnderMaintenance()
{
if (!$this->device_id) {
return false;
}
$query = AlertSchedule::isActive()
->join('alert_schedulables', 'alert_schedule.schedule_id', 'alert_schedulables.schedule_id')
->where(function ($query) {
$query->where(function ($query) {
$query->where('alert_schedulable_type', 'device')
->where('alert_schedulable_id', $this->device_id);
->where(function (Builder $query) {
$query->whereHas('devices', function (Builder $query) {
$query->where('alert_schedulables.alert_schedulable_id', $this->device_id);
});
if ($this->groups) {
$query->orWhere(function ($query) {
$query->where('alert_schedulable_type', 'device_group')
->whereIn('alert_schedulable_id', $this->groups->pluck('id'));
$query->orWhereHas('deviceGroups', function (Builder $query) {
$query->whereIn('alert_schedulables.alert_schedulable_id', $this->groups->pluck('id'));
});
}
if ($this->location) {
$query->orWhere(function ($query) {
$query->where('alert_schedulable_type', 'location')
->where('alert_schedulable_id', $this->location->id);
$query->orWhereHas('locations', function (Builder $query) {
$query->where('alert_schedulables.alert_schedulable_id', $this->location->id);
});
}
});

View File

@@ -85,6 +85,7 @@ class AppServiceProvider extends ServiceProvider
'sensor' => \App\Models\Sensor::class,
'device' => \App\Models\Device::class,
'device_group' => \App\Models\DeviceGroup::class,
'location' => \App\Models\Location::class,
], $sensor_types));
}

View File

@@ -5,7 +5,7 @@
@if($parent_id)
<a href="{{ route('device', $parent_id) }}" title="@lang('VM Host')"><i class="fa fa-server fa-fw fa-lg"></i></a>
@endif
@if(\LibreNMS\Alert\AlertUtil::isMaintenance($device_id))
@if($device->isUnderMaintenance())
<span title="@lang('Scheduled Maintenance')" class="fa fa-wrench fa-fw fa-lg"></span>
@endif
<span style="font-size: 20px;">@deviceLink($device)</span><br/>