52 lines
1.4 KiB
PHP
52 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class AlertIncident extends Model
|
|
{
|
|
/** Sidebar firing-count badge cache key — busted by forgetFiringCount() on every fire/resolve. */
|
|
public const FIRING_COUNT_CACHE = 'alerts:firing_count';
|
|
|
|
protected $guarded = [];
|
|
|
|
protected $casts = [
|
|
'value' => 'float',
|
|
'started_at' => 'datetime',
|
|
'resolved_at' => 'datetime',
|
|
];
|
|
|
|
public function rule(): BelongsTo
|
|
{
|
|
return $this->belongsTo(AlertRule::class, 'alert_rule_id');
|
|
}
|
|
|
|
public function server(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Server::class);
|
|
}
|
|
|
|
public function isFiring(): bool
|
|
{
|
|
return $this->state === 'firing';
|
|
}
|
|
|
|
/**
|
|
* Firing-incident count for the sidebar badge. Cached (the sidebar renders on every page), but
|
|
* the cache is busted the instant an incident fires or resolves (forgetFiringCount), so the badge
|
|
* matches the content on the next render instead of lagging up to the TTL.
|
|
*/
|
|
public static function firingCount(): int
|
|
{
|
|
return (int) Cache::remember(self::FIRING_COUNT_CACHE, 60, fn () => static::where('state', 'firing')->count());
|
|
}
|
|
|
|
public static function forgetFiringCount(): void
|
|
{
|
|
Cache::forget(self::FIRING_COUNT_CACHE);
|
|
}
|
|
}
|