fix(uptime): показывать реальные наблюдения и пропуски RTT на дашборде

This commit is contained in:
Denozordec committed 2026-10-03 23:42:45 +07:00
1 parent 8f65d1620d
commit 9d3129372b
18 files changed
+169 -203

No files matched your search

+5 -3
View File
@@ -16,6 +16,7 @@ import {
updateProbeShowOnDashboard,
updateUptimeSettings,
} from "../services/uptime-collector.js"
import {buildUptimeProbeObservations} from "../services/uptime-probe-observations.js"
import { recordSpeedTestFailure, runBandwidthSpeedTest } from "../services/uptime-speed-service.js"
function rangeToMinutes(range: string | undefined): number {
@@ -471,8 +472,8 @@ const uptimeRoutes: FastifyPluginAsyncZod = async (app) => {
app.get("/uptime/overview", async (req, reply) => {
const q = req.query as { range?: string }
const minutes = rangeToMinutes(q.range)
const sinceIso = new Date(Date.now() - minutes * 60_000).toISOString()
const rangeEndAt=new Date().toISOString()
const sinceIso = new Date(Date.parse(rangeEndAt) - minutes * 60_000).toISOString()
const allServers = await db.select().from(servers)
const probeRows = await readProbeRows()
const probeSamples = await readProbeSamplesSince(sinceIso)
@@ -498,6 +499,7 @@ const uptimeRoutes: FastifyPluginAsyncZod = async (app) => {
series,
enabled: p.enabled,
showOnDashboard: p.showOnDashboard,
observations:buildUptimeProbeObservations(rows.filter(r=>Date.parse(r.sampledAt)<=Date.parse(rangeEndAt))),
}
})
@@ -527,7 +529,7 @@ const uptimeRoutes: FastifyPluginAsyncZod = async (app) => {
}
}))
return reply.send({ probes, resources })
return reply.send({ probes, resources,rangeStartAt:sinceIso,rangeEndAt })
})
}
@@ -0,0 +1,6 @@
import assert from "node:assert/strict"
import {buildUptimeProbeObservations} from "./uptime-probe-observations.js"
assert.deepEqual(buildUptimeProbeObservations([]),{samples:[]})
const good={sampledAt:"2026-10-03T00:00:00Z",rttMs:0,lossPct:0,status:"up" as const},failed={sampledAt:"2026-10-03T00:01:00Z",rttMs:null,lossPct:100,status:"down" as const}
assert.deepEqual(buildUptimeProbeObservations([failed,good]),{samples:[{sampledAt:good.sampledAt,rtt:0,loss:0,status:"up"},{sampledAt:failed.sampledAt,rtt:null,loss:100,status:"down"}]});assert.equal(good.rttMs,0)
console.log("uptime probe observations tests ok")
@@ -0,0 +1,4 @@
import type {UptimeProbeSampleRow} from "../db/schema.js"
type Sample=Pick<UptimeProbeSampleRow,"sampledAt"|"rttMs"|"lossPct"|"status">
/** Additive metadata; no samples means no proof of loss or failure. */
export function buildUptimeProbeObservations(rows:readonly Sample[]){return {samples:rows.map(row=>({sampledAt:row.sampledAt,rtt:row.rttMs,loss:row.lossPct,status:row.status})).sort((a,b)=>Date.parse(a.sampledAt)-Date.parse(b.sampledAt))}}