Просмотр исходного кода

fix(rack): show live ports and offer to clean stale profile ports (#2024)

Nacho 3 недель назад
Родитель
Сommit
e5fe875b35

+ 7 - 1
app/public/locales/en/translation.json

@@ -3134,6 +3134,12 @@
     "betaTitle": "Preview: much work remains",
     "betaText": "This view is under active development and not fully reliable yet. Do not use it in production; feedback is welcome.",
     "disabledTitle": "The rack view is disabled",
-    "disabledText": "It is an optional view. Enable it in Settings → Services → Rack view."
+    "disabledText": "It is an optional view. Enable it in Settings → Services → Rack view.",
+    "staleTitle": "{{count}} ports the device no longer reports",
+    "staleHint": "The faceplate shows only the ports the poller sees. Stored profile: {{units}}.",
+    "staleCabled": "{{count}} of them still have a cable: remove the cable first.",
+    "staleClean": "Clean up ports",
+    "staleCleaned": "{{count}} stale ports removed from the profile",
+    "staleNothing": "No ports can be cleaned up"
   }
 }

+ 7 - 1
app/public/locales/es/translation.json

@@ -3134,6 +3134,12 @@
     "betaTitle": "Vista previa: queda mucho trabajo por hacer",
     "betaText": "Esta vista está en desarrollo activo y todavía no es fiable al 100%. No la utilices todavía en producción; esperamos tus comentarios.",
     "disabledTitle": "La vista de rack está desactivada",
-    "disabledText": "Es una vista opcional. Actívala en Ajustes → Servicios → Vista de rack."
+    "disabledText": "Es una vista opcional. Actívala en Ajustes → Servicios → Vista de rack.",
+    "staleTitle": "{{count}} puertos que el dispositivo ya no informa",
+    "staleHint": "El frontal pinta solo las bocas que ve el sondeo. Perfil guardado: {{units}}.",
+    "staleCabled": "{{count}} de ellos aún tienen un cable: quita el cable para poder limpiarlos.",
+    "staleClean": "Limpiar puertos",
+    "staleCleaned": "{{count}} puertos obsoletos quitados del perfil",
+    "staleNothing": "No hay puertos que se puedan limpiar"
   }
 }

+ 42 - 0
app/src/lib/rackStale.ts

@@ -0,0 +1,42 @@
+/** #2010: perfiles de rack sembrados antes de 2.51.0.
+ *
+ * El filtro por `family` (lag, vlan, bridge, tunnel, virtual, cpu) solo corría
+ * al sembrar un perfil, así que los guardados antes de esa versión conservan
+ * pseudo-puertos para siempre y el frontal nunca coincide con el panel de
+ * puertos del detalle. Aquí se decide qué puertos del perfil ya no existen
+ * según el sondeo, sin tocar nada: la pantalla pinta lo vivo y la limpieza es
+ * una acción explícita del usuario.
+ */
+
+export interface StalePortsEntry {
+  mac: string
+  mountId: string
+  /** ids guardados que el dispositivo ya no informa como físicos */
+  ids: string[]
+  /** subconjunto de `ids` con un cable encima: no se pueden quitar solos */
+  cabled: string[]
+}
+
+export function computeStalePorts(
+  mounts: { id: string; device_mac?: string }[],
+  profiles: Map<string, { ports: { id: string }[] }>,
+  liveByMac: Map<string, string[]>,
+  hasCable: (mountId: string, portId: string) => boolean,
+): StalePortsEntry[] {
+  const out: StalePortsEntry[] = []
+  for (const m of mounts) {
+    const mac = m.device_mac?.toLowerCase()
+    if (!mac) continue
+    const prof = profiles.get(mac)
+    const live = liveByMac.get(mac)
+    // Sin perfil o sin sondeo (unidad caída, sin agente) no se marca nada:
+    // una lista vacía no es prueba de que el puerto ya no exista.
+    if (!prof || !live || live.length === 0) continue
+    const liveSet = new Set(live)
+    const ids = prof.ports.filter((p) => !liveSet.has(p.id)).map((p) => p.id)
+    if (ids.length === 0) continue
+    const cabled = ids.filter((id) => hasCable(m.id, id))
+    out.push({ mac, mountId: m.id, ids, cabled })
+  }
+  return out
+}

+ 123 - 7
app/src/pages/Rack.tsx

@@ -20,7 +20,7 @@ import {
   BackgroundVariant,
 } from '@xyflow/react'
 import '@xyflow/react/dist/style.css'
-import { Cable, Cctv, Link2, Monitor, Network, Pencil, Plus, Router as RouterIcon, Save, Server, Trash2, TriangleAlert } from 'lucide-react'
+import { Cable, Cctv, Link2, Monitor, Network, Pencil, Plus, RefreshCw, Router as RouterIcon, Save, Server, Trash2, TriangleAlert } from 'lucide-react'
 import { toast } from 'sonner'
 import { SectionHeader } from '@/components/SectionHeader'
 import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'
@@ -35,6 +35,7 @@ import { COL_PX, U_PX, mountRelPos, rackHeightPx, rackWidthPx, relPosToCell } fr
 import type { CableVisibility, GhostState } from '@/components/rack/types'
 import { findSlot, type Footprint } from '@/lib/rackGeometry'
 import { FACEPLATES, getFaceplate, layoutPhysicalPorts, seedPorts, suggestFaceplate, type RackPortKind } from '@/lib/rackFaceplates'
+import { computeStalePorts } from '@/lib/rackStale'
 import * as api from '@/lib/rackApi'
 import type { CableDTO, MountDTO, ProfileDTO, RackDTO } from '@/lib/rackApi'
 import type { Router } from '@/data/types'
@@ -77,6 +78,12 @@ function RackCanvas() {
     [np.routers],
   )
   const [portLabels, setPortLabels] = useState<Map<string, Record<string, string>>>(new Map())
+  // #2010: bocas que el sondeo ve HOY como físicas, por MAC. Los perfiles
+  // sembrados antes de 2.51.0 guardaron también LAG, VLAN y virtuales y
+  // ninguna ruta los reconciliaba, así que el frontal y el panel de puertos
+  // del detalle no coincidían nunca. Con esto el frontal pinta lo vivo y la
+  // limpieza de lo guardado es explícita.
+  const [livePhysicalPorts, setLivePhysicalPorts] = useState<Map<string, string[]>>(new Map())
   const services = useServicesVisibility()[0]
   const auth = useAuth()
   const isAdmin = auth?.role === 'admin'
@@ -93,21 +100,29 @@ function RackCanvas() {
     let cancelled = false
     void (async () => {
       for (const mac of labelMacs) {
-        if (portLabels.has(mac)) continue
+        if (portLabels.has(mac) && livePhysicalPorts.has(mac)) continue
         const rid = routerIdByMac.get(mac)
         if (!rid) continue
         try {
           const res = await fetch(`/api/routers/${encodeURIComponent(rid)}`)
           if (!res.ok) continue
           const detail = await res.json()
-          const eth: { id?: string; iface?: string; name?: string; label?: string }[] = detail?.extras?.ethPorts ?? []
+          const eth: { id?: string; iface?: string; name?: string; label?: string; family?: string }[] = detail?.extras?.ethPorts ?? []
           const map: Record<string, string> = {}
+          // #2010: mismo filtro que el sembrado (mountRouter): familia vacía
+          // y nada de wlan. Solo se apunta si el sondeo devolvió algo, para
+          // no dar por obsoleto un perfil cuando la unidad está caída.
+          const live: string[] = []
           for (const p of eth) {
             const id = p.id || p.iface || p.name || ''
             const label = p.label || p.iface || ''
             if (id && label) map[id] = label
+            if (id && !p.family && !id.startsWith('wlan')) live.push(id)
+          }
+          if (!cancelled) {
+            setPortLabels((prev) => new Map(prev).set(mac, map))
+            if (live.length > 0) setLivePhysicalPorts((prev) => new Map(prev).set(mac, live))
           }
-          if (!cancelled) setPortLabels((prev) => new Map(prev).set(mac, map))
         } catch {
           // Sin detalle: el tooltip cae al id, como antes.
         }
@@ -116,7 +131,7 @@ function RackCanvas() {
     return () => {
       cancelled = true
     }
-  }, [labelMacs, routerIdByMac, portLabels])
+  }, [labelMacs, routerIdByMac, portLabels, livePhysicalPorts])
   const [loaded, setLoaded] = useState(false)
   const [activeRackId, setActiveRackId] = useState<string | null>(null)
   const [patchMode, setPatchMode] = useState(false)
@@ -355,10 +370,20 @@ function RackCanvas() {
       if (m.device_mac) {
         const declared = working.profiles.get(m.device_mac)?.ports
         if (declared && declared.length > 0) {
+          const zone = plate.portZone ?? { xStart: 0.24, xEnd: 0.94 }
+          // #2010: si el sondeo ya dice qué bocas son físicas, el frontal pinta
+          // solo esas. Lo guardado sigue intacto hasta que el usuario limpie
+          // (banner de puertos obsoletos), así que esto es solo presentación.
+          const live = livePhysicalPorts.get(m.device_mac.toLowerCase())
+          const liveSet = live && live.length > 0 ? new Set(live) : null
+          const kept = liveSet ? declared.filter((p) => liveSet.has(p.id)) : declared
+          if (liveSet && kept.length !== declared.length) {
+            if (kept.length === 0) return seedPorts(plate)
+            return layoutPhysicalPorts(kept.map((p) => ({ id: p.id, kind: p.kind as RackPortKind })), zone)
+          }
           // Sanado de layouts legacy: si las bocas declaradas invaden la
           // zona de la etiqueta de su plantilla, se re-disponen con la zona
           // correcta (p. ej. perfiles creados antes de la zona por plantilla).
-          const zone = plate.portZone ?? { xStart: 0.24, xEnd: 0.94 }
           const minX = Math.min(...declared.map((p) => p.x))
           if (minX < zone.xStart - 0.02) {
             return layoutPhysicalPorts(
@@ -372,7 +397,7 @@ function RackCanvas() {
       }
       return seedPorts(plate)
     },
-    [working.profiles, plateFor],
+    [working.profiles, plateFor, livePhysicalPorts],
   )
 
 
@@ -410,6 +435,64 @@ function RackCanvas() {
     [working.cables, deletedCables],
   )
 
+  // #2010: puertos guardados en el perfil que el dispositivo ya no informa
+  // como físicos. Se calcula con el sondeo (una consulta por MAC montada, la
+  // misma del tooltip) y solo cuando ese sondeo dio bocas: una unidad caída o
+  // un perfil sin detalle no marcan nada.
+  const stalePorts = useMemo(
+    () =>
+      computeStalePorts(
+        working.mounts,
+        working.profiles,
+        livePhysicalPorts,
+        (mountId, portId) => cablesOnPort(mountId, portId).length > 0,
+      ),
+    [working.mounts, working.profiles, livePhysicalPorts, cablesOnPort],
+  )
+
+  const staleTotal = stalePorts.reduce((n, s) => n + s.ids.length, 0)
+  const staleRemovable = stalePorts.reduce((n, s) => n + (s.ids.length - s.cabled.length), 0)
+  const staleCabled = stalePorts.reduce((n, s) => n + s.cabled.length, 0)
+  const staleUnits = stalePorts
+    .map((s) => `${routerNameByMac.get(s.mac) ?? s.mac} (${s.ids.length})`)
+    .join(', ')
+
+  // Limpieza explícita: quita del perfil las bocas que ya no existen, dejando
+  // intactas las que tienen cable y los kinds/etiquetas de las que sobreviven.
+  const [cleaningStale, setCleaningStale] = useState(false)
+  const cleanStalePorts = useCallback(async () => {
+    const targets = stalePorts
+      .map((s) => ({ mac: s.mac, drop: s.ids.filter((id) => !s.cabled.includes(id)) }))
+      .filter((s) => s.drop.length > 0)
+    if (targets.length === 0) {
+      toast.error(t('rack.staleNothing'))
+      return
+    }
+    setCleaningStale(true)
+    let removed = 0
+    try {
+      for (const target of targets) {
+        const prof = working.profiles.get(target.mac)
+        if (!prof) continue
+        const drop = new Set(target.drop)
+        await api.upsertProfile(target.mac, {
+          faceplate_id: prof.faceplate_id,
+          u_height: prof.u_height,
+          col_span: prof.col_span,
+          color: prof.color,
+          ports: prof.ports.filter((p) => !drop.has(p.id)),
+        })
+        removed += target.drop.length
+      }
+      toast.success(t('rack.staleCleaned', { count: removed }))
+      reload().catch((e) => toast.error(String(e)))
+    } catch (e) {
+      toast.error(String(e))
+    } finally {
+      setCleaningStale(false)
+    }
+  }, [stalePorts, working.profiles, reload, t])
+
   // --- Nodos ---
   useEffect(() => {
     if (!loaded) return
@@ -1203,6 +1286,39 @@ function RackCanvas() {
         </div>
       </div>
 
+      {/* #2010: perfiles sembrados antes de 2.51.0 conservan LAG/VLAN/CPU.
+          El frontal ya pinta solo lo vivo; esto ofrece limpiar lo guardado. */}
+      {stalePorts.length > 0 && (
+        <div
+          className="mt-3 flex flex-wrap items-start gap-3 rounded-xl border border-warn/30 bg-warn/10 px-4 py-3"
+          role="note"
+          data-testid="rack-stale-ports"
+        >
+          <TriangleAlert size={16} className="mt-0.5 shrink-0 text-warn" aria-hidden />
+          <div className="min-w-0 flex-1">
+            <p className="text-[13px] font-semibold text-text-primary">
+              {t('rack.staleTitle', { count: staleTotal })}
+            </p>
+            <p className="mt-0.5 text-[12px] leading-snug text-text-secondary">
+              {t('rack.staleHint', { units: staleUnits })}
+            </p>
+            {staleCabled > 0 && (
+              <p className="mt-1 text-[12px] leading-snug text-warn">
+                {t('rack.staleCabled', { count: staleCabled })}
+              </p>
+            )}
+          </div>
+          <button
+            type="button"
+            onClick={() => void cleanStalePorts()}
+            disabled={!isAdmin || cleaningStale || staleRemovable === 0}
+            className="flex h-8 items-center gap-1.5 rounded-lg border border-warn/40 bg-surface px-3 text-[12px] font-semibold text-warn transition-colors hover:bg-warn/10 disabled:opacity-40"
+          >
+            <RefreshCw size={14} /> {t('rack.staleClean')}
+          </button>
+        </div>
+      )}
+
       <div ref={canvasWrapRef} className="mt-3 flex gap-3" style={{ height: canvasH }}>
         {/* Picker (izquierda, arrastrable al canvas) */}
         <RackPicker