fix(probes): проверять расписание и сохранять подтверждённые данные
This commit is contained in:
1 parent
1d27127b52
commit
271291a910
10 files changed
+108
-98
No files matched your search
@@ -0,0 +1,6 @@
|
||||
import assert from "node:assert/strict"
|
||||
import {test} from "node:test"
|
||||
import {readProbeSchedule} from "./probe-schedule-response"
|
||||
export const probe={id:"probe-fixture",srcServerId:"1",dstServerId:"2",srcInterface:"",dstInterface:"",protocol:"tcp",direction:"both",durationSec:"10",enabled:true,lastRunAt:null,lastTxAvgMbps:0,lastRxAvgMbps:null,lastStatus:"done",lastError:null}
|
||||
test("probe schedule keeps confirmed empty and nullable metrics distinct",()=>{assert.deepEqual(readProbeSchedule({probes:[]}),[]);assert.equal(readProbeSchedule({probes:[probe]})[0].lastRxAvgMbps,null);assert.equal(readProbeSchedule({probes:[probe]})[0].lastTxAvgMbps,0)})
|
||||
test("probe schedule rejects incomplete/duplicate/malformed rows",()=>{for(const raw of [{},{probes:null},{probes:[{}]},{probes:[probe,probe]},{probes:[{...probe,enabled:"yes"}]},{probes:[{...probe,lastRxAvgMbps:-1}]},{probes:[{...probe,lastStatus:"unknown-status"}]},{probes:[{...probe,lastRunAt:"bad-date"}]}])assert.throws(()=>readProbeSchedule(raw))})
|
||||
@@ -0,0 +1,13 @@
|
||||
import {z} from "zod"
|
||||
const metric=z.number().finite().nonnegative().nullable()
|
||||
const timestamp=z.string().refine(v=>Number.isFinite(Date.parse(v))).nullable()
|
||||
export const speedProbeSchema=z.object({
|
||||
id:z.string().min(1),srcServerId:z.string().regex(/^[1-9]\d*$/),dstServerId:z.string().regex(/^[1-9]\d*$/),srcInterface:z.string(),dstInterface:z.string(),
|
||||
protocol:z.enum(["tcp","udp"]),direction:z.enum(["both","transmit","receive"]),durationSec:z.string().regex(/^\d+$/).refine(v=>Number.isSafeInteger(Number(v))&&Number(v)>=3),enabled:z.boolean(),
|
||||
lastRunAt:timestamp,lastTxAvgMbps:metric,lastRxAvgMbps:metric,lastStatus:z.enum(["done","error"]).nullable(),lastError:z.string().nullable()
|
||||
})
|
||||
const response=z.object({probes:z.array(speedProbeSchema).refine(rows=>new Set(rows.map(r=>r.id)).size===rows.length)})
|
||||
export type SpeedProbeApiRow=z.infer<typeof speedProbeSchema>
|
||||
export function readProbeSchedule(raw:unknown):SpeedProbeApiRow[]{
|
||||
const parsed=response.safeParse(raw);if(!parsed.success)throw Error("Данные проб скорости не подтверждены сервером.");return parsed.data.probes
|
||||
}
|
||||
Reference in new issue
Block a user