Docker images / prepare-release (push) Successful in 14s
Docker images / backend-test (push) Successful in 12m24s
Docker images / frontend-image (push) Successful in 2m21s
Docker images / updater-image (push) Successful in 41s
Docker images / netflow-agent-image (push) Successful in 32s
Docker images / backend-image (push) Successful in 1m52s
Docker images / publish-release (push) Successful in 17s
Docker images / notify-webhook (push) Successful in 2s
164 lines
12 KiB
TypeScript
164 lines
12 KiB
TypeScript
"use client"
|
||
|
||
import type { KeyboardEvent, MouseEvent, PointerEvent } from "react"
|
||
import { ArrowDownLeftIcon, ArrowUpRightIcon, ChevronDownIcon, ChevronUpIcon, GlobeIcon, LayersIcon } from "lucide-react"
|
||
import type { Server, WanUplink } from "@/lib/data"
|
||
import { fmtRate } from "@/lib/fmt-rate"
|
||
import { mapCardMeta, mapCardMetrics } from "@/lib/network-map-card-metrics"
|
||
import type { MapDestinationMode } from "@/lib/network-map-url-state"
|
||
import { CountryFlagSvg } from "./country-flag-svg"
|
||
import { ServiceBrandIcon } from "./service-brand-icon"
|
||
|
||
const mono = "var(--font-mono,Geist Mono,monospace)"
|
||
const sans = "var(--font-sans,Geist,sans-serif)"
|
||
const muted = "var(--muted-foreground)"
|
||
const ink = "var(--foreground)"
|
||
const role = {
|
||
"home-router": {code:"HR",color:"var(--chart-2)"}, "jump-host": {code:"JH",color:"var(--chart-1)"},
|
||
"exit-node": {code:"EN",color:"var(--chart-3)"}, "ix-node": {code:"IX",color:"var(--chart-4)"},
|
||
}
|
||
const statusColor = { online:"var(--success)",degraded:"var(--warning)",offline:"var(--destructive)",unknown:muted }
|
||
|
||
function keys(event: KeyboardEvent<SVGGElement>, open: () => void) {
|
||
if (event.key === "Enter" || event.key === " ") { event.preventDefault(); event.stopPropagation(); open() }
|
||
}
|
||
|
||
function NameLines({ x, y, lines }: { x:number;y:number;lines:string[] }) {
|
||
return <g fontFamily={sans} fill={ink} fontSize={13} fontWeight={600} pointerEvents="none">
|
||
{lines.map((part,i) => <text key={i} x={x} y={y+i*14}>{part}</text>)}
|
||
</g>
|
||
}
|
||
|
||
function FocusRing({width,height,radius}: {width:number;height:number;radius:number}) {
|
||
return <rect className="network-map-focus-ring" x={-width/2-4} y={-height/2-4} width={width+8} height={height+8}
|
||
rx={radius+3} fill="none" stroke="var(--ring)" strokeWidth={2}/>
|
||
}
|
||
|
||
function Frame({width,height,kind,radius,stroke,selected,halo,fill="var(--card)"}: {
|
||
width:number;height:number;kind:"wan"|"card"|"dest";radius:number;stroke:string;selected:boolean;halo?:boolean;fill?:string
|
||
}) {
|
||
return <>
|
||
{selected && halo && <rect x={-3} y={-3} width={width+6} height={height+6} rx={radius+3} fill="none" stroke={stroke} strokeWidth={1.3} strokeDasharray="4 3"/>}
|
||
<rect data-map-box={kind} width={width} height={height} rx={radius} fill={fill} stroke={stroke} strokeWidth={selected?2:1.5}/>
|
||
</>
|
||
}
|
||
|
||
function Signal({x,y,status}:{x:number;y:number;status:Server["status"]}) {
|
||
return <circle data-node-status={status} cx={x} cy={y} r={4.3} fill={statusColor[status]} stroke="var(--card)" strokeWidth={1.7}/>
|
||
}
|
||
|
||
function shortRate(rate: string) {
|
||
return rate.replace(/(\d+(?:[.,]\d+)?) ([ГМК]?)бит\/с/g, (_, value: string, unit: string) => {
|
||
const reading = Number(value.replace(",","."))
|
||
return reading === 0 ? "0" : reading.toLocaleString("ru-RU",{maximumFractionDigits:2})+(unit||"б/с")
|
||
})
|
||
}
|
||
|
||
type PointerDown = (e: PointerEvent<SVGGElement>)=>void
|
||
|
||
export function ServerNode({ n,width,isSel,isVis,isDragged,hideCatalogLatency,channelProblem,rates,onClick,onDblClick,onHoverChange,onContextMenu,onPointerDown }: {
|
||
n:Server & {x:number;y:number};width:number;isSel:boolean;isVis:boolean;isDragged:boolean
|
||
hideCatalogLatency?:boolean;channelProblem?:"down"|"degraded";rates?:{rxMbps:number|null;txMbps:number|null}
|
||
onClick:()=>void;onDblClick:()=>void;onHoverChange:(hover:boolean)=>void;onContextMenu:(e:MouseEvent)=>void;onPointerDown:PointerDown
|
||
}) {
|
||
const {height,lines} = mapCardMetrics(n.name,"device",width)
|
||
const {code,color} = role[n.type]
|
||
const status = n.status !== "offline" && channelProblem ? "degraded" : n.status
|
||
const offline = status === "offline", warn = status === "degraded"
|
||
const border = isSel ? ink : warn ? "color-mix(in oklch,var(--warning) 50%,transparent)" : offline ? "var(--destructive)" : "var(--border)"
|
||
const iso = /^[A-Za-z]{2}$/.test(n.country?.trim() || "") ? n.country.trim() : ""
|
||
const metaY = 34 + (lines.length-1)*14, metaX = iso?60:42
|
||
const site = n.site?.trim() && n.site.trim() !== "—" ? n.site.trim() : "Без площадки"
|
||
const reading = offline ? "Нет ответа" : channelProblem === "down" ? "Отказ канала" : channelProblem ? "Деградация канала" :
|
||
!hideCatalogLatency && n.latency != null ? `RTT · ${n.latency} мс` : "Нет данных"
|
||
return <g role="button" className="network-map-focusable" aria-label={"Устройство "+n.name} aria-pressed={isSel} tabIndex={isVis?0:-1}
|
||
transform={`translate(${n.x},${n.y})`} data-node-kind="device" data-node-id={n.id} style={{cursor:isDragged?"grabbing":"grab"}} opacity={isVis?1:.38}
|
||
onKeyDown={e=>keys(e,onClick)} onClick={e=>{e.stopPropagation();onClick()}}
|
||
onDoubleClick={e=>{e.stopPropagation();onDblClick()}} onContextMenu={e=>{e.preventDefault();e.stopPropagation();onContextMenu(e)}}
|
||
onPointerDown={e=>{e.stopPropagation();onPointerDown(e)}} onMouseEnter={()=>onHoverChange(true)} onMouseLeave={()=>onHoverChange(false)}>
|
||
<title>{[n.name,site,rates?`RouterOS · RX ${fmtRate(rates.rxMbps)} · TX ${fmtRate(rates.txMbps)}`:reading].join(" · ")}</title>
|
||
<FocusRing width={width} height={height} radius={12}/>
|
||
<g transform={`translate(${-width/2},${-height/2})`}>
|
||
<Frame width={width} height={height} kind="card" radius={12} selected={isSel} stroke={border} fill={offline?"color-mix(in oklch,var(--destructive) 10%,var(--card))":"var(--card)"}/>
|
||
<rect data-role-tile={code} x={7} y={7} width={29} height={29} rx={7} fill={color} fillOpacity={.18}/>
|
||
<text x={21.5} y={25.5} textAnchor="middle" fill={color} fontSize={11.5} fontWeight={700} fontFamily={mono}>{code}</text>
|
||
<Signal x={35} y={8} status={status}/>
|
||
<NameLines x={42} y={19} lines={lines}/>
|
||
{iso && <g data-country-flag={iso.toUpperCase()} transform={`translate(42,${metaY-8})`}><CountryFlagSvg iso={iso} size={14}/></g>}
|
||
<text x={metaX} y={metaY} fill={muted} fontFamily={mono} fontSize={10.5}>{mapCardMeta(site,width-metaX-7)}</text>
|
||
{rates ? (["rx","tx"] as const).map((dir,i)=>{
|
||
const x=i?width/2+1:8
|
||
const RxTx=dir==="rx"?ArrowDownLeftIcon:ArrowUpRightIcon
|
||
const rate=dir==="rx"?rates.rxMbps:rates.txMbps
|
||
return <g key={dir} data-rate-dir={dir}>
|
||
<title>{`RouterOS ${dir.toUpperCase()} · ${fmtRate(rate)}`}</title>
|
||
<RxTx x={x} y={height-19} width={12} height={12} color={dir==="rx"?"var(--chart-rx)":"var(--chart-tx)"}/>
|
||
<text x={x+14} y={height-9} fill={ink} fontFamily={mono} fontSize={11} fontWeight={500}>{shortRate(fmtRate(rate))}</text>
|
||
</g>
|
||
}) : <text x={8} y={height-9} fill={warn?"var(--status-degraded-fg)":ink} fontSize={11} fontWeight={500} fontFamily={warn?sans:mono}>{mapCardMeta(reading,width-15)}</text>}
|
||
</g>
|
||
</g>
|
||
}
|
||
|
||
export function WanSatNode({x,y,wan,width,color,index,active,isSel,isDragged,traffic,onSelect,onPointerDown}: {
|
||
x:number;y:number;wan:WanUplink;width:number;color:string;index:number;active:boolean;isSel:boolean;isDragged:boolean;traffic?:string
|
||
onSelect:()=>void;onPointerDown:PointerDown
|
||
}) {
|
||
const name=wan.isp||wan.name
|
||
const {height,lines}=mapCardMetrics(name,"wan",width)
|
||
const status = active || traffic?.startsWith("Live ") ? "online" : "unknown"
|
||
return <g data-node-kind="wan" data-wan-index={index} role="button" aria-label={"Аплинк "+wan.name} aria-pressed={isSel} tabIndex={0} className="network-map-focusable"
|
||
transform={`translate(${x},${y})`} style={{cursor:isDragged?"grabbing":"grab"}} onKeyDown={e=>keys(e,onSelect)}
|
||
onPointerDown={e=>{e.stopPropagation();onPointerDown(e)}} onClick={e=>{e.stopPropagation();onSelect()}}>
|
||
<title>{[name,wan.name,wan.iface,traffic||"Нет данных"].filter(Boolean).join(" · ")}</title>
|
||
<FocusRing width={width} height={height} radius={24}/>
|
||
<g transform={`translate(${-width/2},${-height/2})`}>
|
||
<Frame width={width} height={height} kind="wan" radius={24} stroke={color} halo selected={isSel}/>
|
||
<circle cx={19} cy={21} r={12} fill="var(--muted)"/>
|
||
<GlobeIcon x={11} y={13} width={16} height={16} color={muted}/>
|
||
<Signal x={29} y={11} status={status}/>
|
||
<NameLines x={35} y={18} lines={lines}/>
|
||
<text x={35} y={33+(lines.length-1)*14} fill={muted} fontFamily={mono} fontSize={10.5}>{mapCardMeta(traffic?shortRate(traffic):"Нет данных",width-42)}</text>
|
||
</g>
|
||
</g>
|
||
}
|
||
|
||
export function ServiceNode({label,share,x,y,width,isSel,isVis,isDragged,destMode,iso,dim,shareLabel,category,rate,expanded,onClick,onPointerDown}: {
|
||
label:string;share:number|null;x:number;y:number;width:number;isSel:boolean;isVis:boolean;isDragged:boolean;destMode:MapDestinationMode
|
||
iso?:string;dim?:boolean;shareLabel?:string;category?:string;rate?:string;expanded?:boolean;onClick:()=>void;onPointerDown:PointerDown
|
||
}) {
|
||
const {height,lines} = mapCardMetrics(label,"destination",width)
|
||
const kind=expanded!==undefined?"minor-services":destMode==="categories"?"category":destMode==="countries"?"country":"service"
|
||
const aria=kind==="minor-services"?label:(kind==="service"?"Сервис ":kind==="country"?"Страна ":"Категория ")+label
|
||
const percent=share!=null?`${Math.round(share*100)}%`:"—"
|
||
const speed = rate?.replace(/\d+(?:[.,]\d+)?(?= [ГМК]?бит\/с)/g,
|
||
value => Number(value.replace(",",".")).toLocaleString("ru-RU",{maximumFractionDigits:2}))
|
||
const attrs=kind==="service"?{"data-service-brand":label}:kind==="country"?{"data-country-flag":iso}:kind==="category"?{"data-traffic-category":label}:{}
|
||
const Chevron=expanded?ChevronUpIcon:ChevronDownIcon
|
||
const subtitle=category||(kind==="country"?"Страна":"Сервис")
|
||
return <g data-node-kind={kind} role="button" aria-label={aria} aria-pressed={expanded===undefined?isSel:undefined} aria-expanded={expanded} tabIndex={isVis?0:-1} className="network-map-focusable"
|
||
transform={`translate(${x},${y})`} style={{cursor:expanded!==undefined?"pointer":isDragged?"grabbing":"grab"}} opacity={!isVis||dim ? .38 : 1}
|
||
onKeyDown={e=>keys(e,onClick)} onClick={e=>{e.stopPropagation();onClick()}} onPointerDown={e=>{e.stopPropagation();onPointerDown(e)}}>
|
||
<title>{[label,subtitle,speed,share===null?"Доля неизвестна":percent+" "+(shareLabel??"трафика окна")].filter(Boolean).join(" · ")}</title>
|
||
<FocusRing width={width} height={height} radius={9}/>
|
||
<g transform={`translate(${-width/2},${-height/2})`}>
|
||
<Frame width={width} height={height} kind="dest" radius={9} stroke={isSel?"var(--chart-4)":"var(--border)"} selected={isSel} halo/>
|
||
<circle cx={22} cy={21} r={14.5} fill="var(--muted)"/>
|
||
<g {...attrs} aria-hidden="true">
|
||
{kind==="minor-services"?<Chevron x={14} y={13} width={16} height={16} color={muted}/>:
|
||
kind==="category"?<LayersIcon x={14} y={13} width={16} height={16} color="var(--chart-4)"/>:
|
||
kind==="country"&&iso&&/^[a-z]{2}$/i.test(iso)?<g transform="translate(11,13)"><CountryFlagSvg iso={iso} size={22}/></g>:
|
||
kind==="country"?<GlobeIcon x={14} y={13} width={16} height={16} color={muted}/>:
|
||
<g transform="translate(11,10)"><ServiceBrandIcon label={label} size={22}/></g>}
|
||
</g>
|
||
<NameLines x={42} y={18} lines={lines}/>
|
||
<text x={kind==="minor-services" && width<150 ? 8 : 42} y={33+(lines.length-1)*14} fill={muted} fontFamily={sans} fontSize={11}>
|
||
{kind==="minor-services"&&!expanded?subtitle:mapCardMeta(subtitle,width-49)}</text>
|
||
<text x={8} y={height-12} fill={ink} fontFamily={mono} fontSize={11} fontWeight={500}>{mapCardMeta(speed||"Нет данных",width-46)}</text>
|
||
<text x={width-7} y={height-12} textAnchor="end" fill={muted} fontFamily={mono} fontSize={10.5}>{percent}</text>
|
||
{share!==null && <><rect x={8} y={height-7} width={width-16} height={2.5} rx={1.25} fill="var(--muted)"/>
|
||
<rect data-service-share={share*100} x={8} y={height-7} width={(width-16)*Math.max(0,Math.min(1,share))} height={2.5} rx={1.25} fill="var(--chart-4)"/></>}
|
||
</g>
|
||
</g>
|
||
}
|