Calculate downtime from device_outages table (#15397)

* Calculate downtime from device_outages table

* refactor
This commit is contained in:
Tony Murray
2023-10-06 20:37:23 -05:00
committed by GitHub
parent 6d9178cd42
commit e53436b6a3
4 changed files with 29 additions and 6 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use App\View\SimpleTemplate;
use Carbon\Carbon;
use Fico7489\Laravel\Pivot\Traits\PivotEventTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -259,6 +260,24 @@ class Device extends BaseModel
return $name;
}
/**
* Get the current DeviceOutage if there is one (if device is down)
*/
public function getCurrentOutage(): ?DeviceOutage
{
return $this->relationLoaded('outages')
? $this->outages->whereNull('up_again')->sortBy('going_down', descending: true)->first()
: $this->outages()->whereNull('up_again')->orderBy('going_down', 'desc')->first();
}
/**
* Get the time this device went down
*/
public function downSince(): Carbon
{
return Carbon::createFromTimestamp((int) $this->getCurrentOutage()?->going_down);
}
/**
* Check if user can access this device.
*