/
DATKAI
/
ITCUP
Обзор
Документация
Войти
/
DATKAI
/
ITCUP
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
app/Models/MonitoredHost.php
89 строк
2 KB
ITCUP Admin
feat(monitoring): модуль пинг-мониторинга любых хостов
21 апр 2026, 20:25
21 апр 2026, 20:25
721b0e5
Код
Авторство
О чём код?
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; class MonitoredHost extends Model { protected $fillable = [ 'name', 'host', 'probe_type', 'port', 'http_path', 'timeout_ms', 'group_name', 'location_id', 'asset_id', 'is_active', 'notify_on_down', 'status', 'status_changed_at', 'last_ping_at', 'last_ping_ms', 'last_error', 'notes', ]; protected $casts = [ 'is_active' => 'boolean', 'notify_on_down' => 'boolean', 'status_changed_at' => 'datetime', 'last_ping_at' => 'datetime', 'port' => 'integer', 'timeout_ms' => 'integer', 'last_ping_ms' => 'integer', ]; public const STATUSES = [ 'online' => 'Онлайн', 'offline' => 'Не доступен', 'unknown' => 'Неизвестно', ]; public const STATUS_COLORS = [ 'online' => 'success', 'offline' => 'danger', 'unknown' => 'gray', ]; public const PROBE_TYPES = [ 'tcp' => 'TCP connect (проверка порта)', 'icmp' => 'ICMP ping (классический)', 'http' => 'HTTP запрос (для веб-сервисов)', ]; public function location(): BelongsTo { return $this->belongsTo(Location::class); } public function asset(): BelongsTo { return $this->belongsTo(Asset::class); } public function pings(): HasMany { return $this->hasMany(MonitoredHostPing::class)->orderByDesc('created_at'); } public function availabilityPercent(int $hours): ?float { $since = now()->subHours($hours); $total = $this->pings()->where('created_at', '>=', $since)->count(); if ($total === 0) { return null; } $ok = $this->pings()->where('created_at', '>=', $since)->where('is_reachable', true)->count(); return round(($ok / $total) * 100, 2); } public function statusDuration(): ?string { return $this->status_changed_at?->diffForHumans(null, true); } }