Files
MikrotikManager/backend/src/services/traffic-flow-read-window.ts
T
Denozordec bec8de7c95
Docker images / prepare-release (push) Successful in 17s
Docker images / backend-test (push) Failing after 7m23s
Docker images / backend-image (push) Skipped
Docker images / frontend-image (push) Successful in 3m10s
Docker images / updater-image (push) Successful in 1m33s
Docker images / netflow-agent-image (push) Successful in 41s
Docker images / publish-release (push) Skipped
Docker images / notify-webhook (push) Skipped
fix(netflow): ограничить чтение истории и показывать фактическое окно
2026-11-22 03:00:26 +07:00

28 lines
1.3 KiB
TypeScript

export class FlowSqlSourceTooLarge extends Error {
readonly statusCode = 409
readonly code = "STATE_UNAVAILABLE"
constructor() {
super("Источник NetFlow превышает допустимый объём; сократите область или окно")
}
}
export class FlowSourceWindowTooLarge extends FlowSqlSourceTooLarge {}
type FlowReadWindow = { requestedMinutes: number; effectiveMinutes: number }
/** Retrying a smaller historical window keeps the same response reservation.
* Only an explicit source-size refusal allows fallback; other errors propagate. */
export async function readFlowWindow<T extends object>(requestedMinutes: number, read: (minutes: number) => Promise<T>, signal?: AbortSignal): Promise<T & { sourceWindow?: FlowReadWindow }> {
let effectiveMinutes = requestedMinutes
for (;;) {
signal?.throwIfAborted()
try {
const result = await read(effectiveMinutes)
return effectiveMinutes === requestedMinutes ? result : { ...result, sourceWindow: { requestedMinutes, effectiveMinutes } }
} catch (error) {
if (!(error instanceof FlowSourceWindowTooLarge) || effectiveMinutes <= 1) throw error
effectiveMinutes = Math.max(1, Math.floor(effectiveMinutes / (effectiveMinutes <= 15 ? 2 : 4)))
}
}
}