*/ use HasFactory; /** * A probe that hasn't reported in this many seconds is considered stale * and excluded from quorum calculations. */ public const ACTIVE_WINDOW_SECONDS = 120; protected $fillable = [ 'node_id', 'last_seen_at', 'active', ]; protected function casts(): array { return [ 'last_seen_at' => 'datetime', 'active' => 'boolean', ]; } /** * Upsert a probe from a received result (called by ResultConsumer). */ public static function recordSeen(string $nodeId): self { return self::updateOrCreate( ['node_id' => $nodeId], ['last_seen_at' => now(), 'active' => true], ); } /** * Count probes considered active for quorum purposes. * * Active = flagged active AND reported within ACTIVE_WINDOW_SECONDS. */ public static function activeCount(): int { return self::query() ->where('active', true) ->where('last_seen_at', '>=', now()->subSeconds(self::ACTIVE_WINDOW_SECONDS)) ->count(); } }