feat(system): add worker and VPS catalogs with guarded local removal

This commit is contained in:
DCCONSTRUCTIONS
2026-11-10 01:11:24 +03:00
parent ad5a929e06
commit e8025bc99f
25 changed files with 1556 additions and 116 deletions
@@ -42,7 +42,7 @@ const defaultQuickActions: Record<
observation: ["cameras", "world-map"],
missions: ["routes", null],
data: ["recordings", "datasets"],
system: ["modules", "integrations"],
system: ["modules", "network"],
polygon: ["observatory", "local-device"],
};
@@ -0,0 +1,54 @@
import {useCallback, useEffect, useState} from 'react';
import type {VPSHost} from '../fleet/vps';
import {startSequentialPolling} from './sequentialPolling';
export type ServerService = {kind:'arnavi'|'wireguard';id:string;name:string;endpoint:string;state:string;last_seen:number|null;details:VPSHost|{id:string;name:string;endpoint:string;boards:number;last_verified:number|null;state:string}};
export type Server = {id:string;name:string;address:string;revision:number;services:ServerService[]};
export type ServerRemovalPlan = {confirmation:string;name:string;blocked:string[];services:string[];vehicle_connections:number;worker_connections:number};
export async function serverRequest<T>(path='', method='GET', body?:unknown, signal?:AbortSignal):Promise<T> {
const response = await fetch(`/api/v1/system/servers${path}`, {method, cache:'no-store', credentials:'same-origin',
signal:AbortSignal.any([...(signal?[signal]:[]),AbortSignal.timeout(method === 'GET' ? 20000 : 240000)]),
headers:body === undefined ? {} : {'Content-Type':'application/json'}, body:body === undefined ? undefined : JSON.stringify(body)});
const value = await response.json().catch(() => ({}));
if (!response.ok) throw Error(typeof value.detail === 'string' ? value.detail : 'Не удалось получить данные VPS.');
return value as T;
}
export function useServers() {
const [items,setItems]=useState<Server[]|null>(null),[error,setError]=useState(''),[generation,setGeneration]=useState(0);
const refresh=useCallback(()=>setGeneration(n=>n+1),[]);
useEffect(()=>{
const controller=new AbortController();
const stop=startSequentialPolling(async()=>{try{const value=await serverRequest<{items:Server[]}>('', 'GET', undefined, controller.signal);if(!controller.signal.aborted){setItems(value.items);setError('');}}catch(e){if(!controller.signal.aborted)setError(e instanceof Error?e.message:'Не удалось получить VPS.');}},5000);
return()=>{stop();controller.abort();};
},[generation]);
return {items,error,refresh,generation};
}
export interface ServiceObservation {id:string;kind:ServerService['kind'];state:'online'|'unavailable';checked_at:number}
export function useServerHealth(servers:Server[]|null,generation=0) {
const [observations,setObservations]=useState<Record<string,ServiceObservation[]>>({});
const keys=(servers??[]).map(s=>s.id).sort().join('|');
useEffect(()=>{
const controller=new AbortController();setObservations({});
const stop=startSequentialPolling(async()=>{
await Promise.all(keys.split('|').filter(Boolean).map(async id=>{
let items:ServiceObservation[]=[];
try{items=(await serverRequest<{items:ServiceObservation[]}>(`/${id}/health`,'GET',undefined,controller.signal)).items;}catch{/* Unknown is distinct from a successful observation. */}
if(!controller.signal.aborted)setObservations(current=>({...current,[id]:items}));
}));
},15000);
return()=>{stop();controller.abort();};
},[keys,generation]);
return observations;
}
export function serviceAvailability(service:ServerService, observations:ServiceObservation[]=[], now=Date.now()/1000) {
const item=observations.find(s=>s.kind===service.kind&&s.id===service.id);
if(!item||now-item.checked_at>40)return {tone:'neutral' as const,label:'Связь не проверена',online:false};
return item.state==='online'?{tone:'success' as const,label:'На связи',online:true}:{tone:'neutral' as const,label:'Нет ответа',online:false};
}
export function serverAvailability(server:Server,observations:ServiceObservation[]=[]) {
const states=server.services.map(s=>serviceAvailability(s,observations));
if(!states.length)return {tone:'neutral' as const,label:'Нет подключённых служб'};
if(states.every(s=>s.online))return {tone:'success' as const,label:'На связи'};
if(states.some(s=>s.online))return {tone:'warning' as const,label:'Часть служб доступна'};
return {tone:'neutral' as const,label:states.some(s=>s.label==='Нет ответа')?'Нет ответа от служб':'Связь не проверена'};
}