/
DATKAI
/
ITCUP
Обзор
Документация
Войти
/
DATKAI
/
ITCUP
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
app/Models/NetworkMap.php
101 строка
4 KB
ITCUP Admin
feat(netmap): редактор карт сети на Cytoscape.js с живым мониторингом
21 апр 2026, 20:49
21 апр 2026, 20:49
32a04ab
Код
Авторство
О чём код?
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; class NetworkMap extends Model { protected $fillable = [ 'name', 'description', 'data', 'created_by', 'location_id', 'sort_order', ]; protected $casts = [ 'data' => 'array', ]; public const NODE_TYPES = [ 'router' => ['label' => 'Роутер', 'icon' => 'heroicon-o-globe-alt', 'color' => '#ea580c'], 'switch' => ['label' => 'Коммутатор', 'icon' => 'heroicon-o-squares-2x2', 'color' => '#0891b2'], 'firewall' => ['label' => 'Файрвол', 'icon' => 'heroicon-o-shield-check', 'color' => '#dc2626'], 'ap' => ['label' => 'Точка доступа', 'icon' => 'heroicon-o-wifi', 'color' => '#7c3aed'], 'server' => ['label' => 'Сервер', 'icon' => 'heroicon-o-server', 'color' => '#059669'], 'nas' => ['label' => 'NAS/СХД', 'icon' => 'heroicon-o-circle-stack', 'color' => '#0891b2'], 'pc' => ['label' => 'ПК', 'icon' => 'heroicon-o-computer-desktop', 'color' => '#2563eb'], 'laptop' => ['label' => 'Ноутбук', 'icon' => 'heroicon-o-computer-desktop', 'color' => '#3b82f6'], 'printer' => ['label' => 'Принтер/МФУ', 'icon' => 'heroicon-o-printer', 'color' => '#6b7280'], 'phone' => ['label' => 'IP-телефон', 'icon' => 'heroicon-o-phone', 'color' => '#0284c7'], 'camera' => ['label' => 'Камера', 'icon' => 'heroicon-o-video-camera', 'color' => '#db2777'], 'cloud' => ['label' => 'Облако', 'icon' => 'heroicon-o-cloud', 'color' => '#94a3b8'], 'internet' => ['label' => 'Интернет', 'icon' => 'heroicon-o-globe-europe-africa','color' => '#1d4ed8'], 'generic' => ['label' => 'Устройство', 'icon' => 'heroicon-o-cube', 'color' => '#64748b'], ]; public function creator(): BelongsTo { return $this->belongsTo(User::class, 'created_by'); } public function location(): BelongsTo { return $this->belongsTo(Location::class); } /** * Возвращает data со свежими статусами для узлов, у которых задан monitored_host_id. */ public function dataWithStatuses(): array { $data = $this->data ?? ['nodes' => [], 'edges' => []]; $hostIds = collect($data['nodes'] ?? []) ->pluck('monitored_host_id') ->filter() ->unique() ->values(); if ($hostIds->isEmpty()) { return $data; } $hosts = MonitoredHost::whereIn('id', $hostIds) ->get() ->keyBy('id'); $data['nodes'] = collect($data['nodes'] ?? [])->map(function (array $node) use ($hosts) { $hostId = $node['monitored_host_id'] ?? null; if ($hostId && isset($hosts[$hostId])) { $host = $hosts[$hostId]; $node['status'] = $host->status; $node['status_tooltip'] = sprintf( '%s · %s · %s мс · %s', MonitoredHost::STATUSES[$host->status] ?? $host->status, $host->host, $host->last_ping_ms ?? '—', $host->last_ping_at?->format('d.m.Y H:i:s') ?? '—' ); } else { $node['status'] = null; } return $node; })->toArray(); return $data; } public function nodesCount(): int { return count($this->data['nodes'] ?? []); } public function edgesCount(): int { return count($this->data['edges'] ?? []); } }