Files
MikrotikManager/packages/contracts/src/uptime-speed.ts
T

72 lines
5.6 KiB
TypeScript

import { z } from "zod"
const rate = z.number().finite().nonnegative().nullable()
export const speedHistoryRunSchema = z.object({
id:z.string().min(1), probeId:z.string().nullable().optional(),
srcServerId:z.string().regex(/^[1-9][0-9]*$/), dstServerId:z.string().regex(/^[1-9][0-9]*$/),
srcInterface:z.string(), dstInterface:z.string(),
protocol:z.enum(["tcp","udp"]), direction:z.enum(["transmit","receive","both"]), durationSec:z.number().int().positive(),
txAvgMbps:rate, rxAvgMbps:rate, status:z.enum(["done","error"]), error:z.string().nullable().optional(),
createdAt:z.string().datetime({offset:true}),
srcAddress:z.string().nullable().optional(),dstAddress:z.string().nullable().optional(),
srcInterfaceAddress:z.string().nullable().optional(),dstInterfaceAddress:z.string().nullable().optional(),
afterBtPing:z.object({rttMs:rate,lossPct:z.number().finite().min(0).max(100).nullable(),error:z.string().nullable()}).nullable().optional(),
})
export const speedHistoryResponseSchema=z.object({runs:z.array(speedHistoryRunSchema).superRefine((rows,ctx)=>{
const ids=new Set<string>();for(const row of rows){if(ids.has(row.id))ctx.addIssue({code:"custom",message:"Повторный ID истории скорости"});ids.add(row.id)}
})})
export type SpeedHistoryRun=z.infer<typeof speedHistoryRunSchema>
const probeTimestamp = z.string().datetime({ offset: true }).nullable()
export const speedProbeReadSchema = z.object({
id: z.string().min(1), srcServerId: z.string().regex(/^[1-9][0-9]*$/), dstServerId: z.string().regex(/^[1-9][0-9]*$/),
srcInterface: z.string(), dstInterface: z.string(), protocol: z.enum(["tcp", "udp"]), direction: z.enum(["both", "transmit", "receive"]),
durationSec: z.string().regex(/^[0-9]+$/).refine(value => Number.isSafeInteger(Number(value)) && Number(value) >= 3), enabled: z.boolean(),
lastRunAt: probeTimestamp, lastTxAvgMbps: rate, lastRxAvgMbps: rate, lastStatus: z.enum(["done", "error"]).nullable(), lastError: z.string().nullable(),
lastPingRttMs: rate.optional(), lastPingLossPct: z.number().finite().min(0).max(100).nullable().optional(),
lastPingAt: probeTimestamp.optional(), lastPingError: z.string().nullable().optional(),
})
export const speedProbesResponseSchema = z.object({ probes: z.array(speedProbeReadSchema).refine(rows => new Set(rows.map(row => row.id)).size === rows.length, "Повторный ID speed-пробы") })
export type SpeedProbeRead = z.infer<typeof speedProbeReadSchema>
const speedPositiveInteger = z.union([z.number(), z.string().regex(/^[1-9][0-9]*$/).transform(Number)]).pipe(z.number().int().positive().max(Number.MAX_SAFE_INTEGER))
export const speedProbeWriteSchema = z.object({
id: z.string().trim().min(1), srcServerId: speedPositiveInteger, dstServerId: speedPositiveInteger,
srcInterface: z.string().trim(), dstInterface: z.string().trim(),
protocol: z.enum(["tcp", "udp"]), direction: z.enum(["both", "transmit", "receive"]),
durationSec: speedPositiveInteger.pipe(z.number().min(3).max(2147483647)), enabled: z.boolean(),
}).refine(row => row.srcServerId !== row.dstServerId, "Источник и назначение должны отличаться")
export const speedProbesWriteSchema = z.object({ probes: z.array(speedProbeWriteSchema).refine(rows => new Set(rows.map(row => row.id)).size === rows.length, "Повторный ID speed-пробы") })
export const speedProbesSavedSchema = z.object({ ok: z.literal(true), warning: z.string().min(1).optional() })
export type SpeedProbeWrite = z.infer<typeof speedProbeWriteSchema>
/** A changed device pair has no measurements; configuration writes cannot invent them. */
export const emptySpeedProbeObservations = {
lastRunAt: null, lastTxAvgMbps: null, lastRxAvgMbps: null, lastStatus: null, lastError: null,
lastPingRttMs: null, lastPingLossPct: null, lastPingAt: null, lastPingError: null,
}
const speedCommandText = z.string().refine(value => !value.includes(String.fromCharCode(0)), "Недопустимый символ в параметре теста")
const speedOptionalId = speedCommandText.trim().transform(value => value || undefined).optional()
export const speedTestCommandSchema = z.object({
runId: speedOptionalId, probeId: speedOptionalId,
srcServerId: speedPositiveInteger, dstServerId: speedPositiveInteger,
srcInterface: speedCommandText.trim().default(""), dstInterface: speedCommandText.trim().default(""),
protocol: z.enum(["tcp", "udp"]).default("tcp"),
direction: z.enum(["transmit", "receive", "both"]).default("both"),
durationSec: speedPositiveInteger.pipe(z.number().min(3).max(2147483647)).default(10),
maxMbps: z.number().int().min(1).max(1000).optional(),
}).refine(row => row.srcServerId !== row.dstServerId, "Источник и назначение должны отличаться")
export type SpeedTestCommand = z.infer<typeof speedTestCommandSchema>
export type SpeedMeasurementParameters = {
srcServerId: string | number; dstServerId: string | number; srcInterface?: string; dstInterface?: string;
protocol: "tcp" | "udp"; direction: "both" | "transmit" | "receive"; durationSec: string | number
}
/** Compare validated configuration across HTTP read/write forms; enable/order are not measurement parameters. */
export function sameSpeedMeasurement(a: SpeedMeasurementParameters, b: SpeedMeasurementParameters): boolean {
return String(a.srcServerId) === String(b.srcServerId) && String(a.dstServerId) === String(b.dstServerId)
&& (a.srcInterface ?? "").trim() === (b.srcInterface ?? "").trim()
&& (a.dstInterface ?? "").trim() === (b.dstInterface ?? "").trim()
&& a.protocol === b.protocol && a.direction === b.direction && Number(a.durationSec) === Number(b.durationSec)
}