fix(alerts): исключить неподтверждённые проблемы из журнала мониторинга

This commit is contained in:
Denozordec committed 2026-10-12 16:45:26 +07:00
1 parent d3f9a120e6
commit ca23284486
20 files changed
+1384 -98

No files matched your search

@@ -3,7 +3,7 @@ import { pool, closePool } from "../../db/index.js"
import { initDatabase } from "../../db/bootstrap.js"
import { buildSignalSnapshotFromCollectors } from "./signals-from-collectors.js"
import { evaluateRule } from "./evaluate-rule.js"
import { getAlertsMeta } from "../alerts-service.js"
import { getAlertsMeta, getAlertsOverview } from "../alerts-service.js"
import type { ApiAlertRule } from "../alerts-service.js"
const database = process.env.MMAPP_OWN_SQL_DB
@@ -77,12 +77,58 @@ else {
await reset(); await insert("uptime_ping", snapshot("uptime_ping", [probe()], -15_000), -15_000)
await insert("uptime_ping", null, -1_000, "error")
assert.deepEqual((await build()).probes, []); cases++
// The journal must use the same confirmed facts, rather than reviving native sample tables.
await pool.query("UPDATE uptime_probes SET updated_at=$1 WHERE id=$2", [iso(-900_000), probe().probeId])
await pool.query("INSERT INTO uptime_resource_samples(server_id,sampled_at,status) VALUES($1,$2,'offline')", [ids[0], iso(-1_000)])
await pool.query("INSERT INTO uptime_probe_samples(probe_id,sampled_at,rtt_ms,loss_pct,status) VALUES($1,$2,8,25,'warn')", [probe().probeId, iso(-1_000)])
await pool.query("INSERT INTO alert_history(id,rule_name,severity,message,sent_ok,fired_at) VALUES($1,'owned receipt','info','confirmed delivery receipt',true,$2)", [prefix + "receipt", iso(-1_000)])
const freshJournal = async () => {
await reset()
await insert("uptime_resources", snapshot("uptime_resources", [resource()]))
await insert("uptime_ping", snapshot("uptime_ping", [{ ...probe(), status: "warn", lossPct: 25, rttMs: 8 }]))
}
const journal = async () => {
const overview = await getAlertsOverview()
assert.equal(overview.history.filter(h => h.source === "db" && h.id === prefix + "receipt").length, 1)
assert.equal(overview.history.find(h => h.id === prefix + "receipt")?.sent, true)
return overview.history.filter(h => h.source === "live")
}
await freshJournal()
let live = await journal(); assert.equal(live.length, 2); assert(live.every(h => typeof h.firedAt === "string" && h.sent === false)); cases++
await pool.query("UPDATE alert_history SET fired_at=$1 WHERE id=$2", [iso(), prefix + "receipt"])
assert.equal((await getAlertsOverview()).history[0].id, prefix + "receipt"); cases++
await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [ids[0]])
assert.deepEqual(await journal(), []); cases++
await pool.query("UPDATE servers SET enabled=true WHERE id=$1", [ids[0]])
await pool.query("UPDATE uptime_settings SET resources_enabled=false,ping_enabled=false WHERE id=1")
assert.deepEqual(await journal(), []); cases++
await pool.query("UPDATE uptime_settings SET resources_enabled=true,ping_enabled=true WHERE id=1")
await reset()
await insert("uptime_resources", snapshot("uptime_resources", [resource()], -3_600_000), -3_600_000)
await insert("uptime_ping", snapshot("uptime_ping", [probe()], -3_600_000), -3_600_000)
assert.deepEqual(await journal(), []); cases++
await freshJournal()
await insert("uptime_resources", null, 0, "error"); await insert("uptime_ping", null, 0, "error")
assert.deepEqual(await journal(), []); cases++
await reset(); await insert("uptime_resources", snapshot("uptime_resources", [])); await insert("uptime_ping", snapshot("uptime_ping", []))
assert.deepEqual(await journal(), []); cases++
await freshJournal()
await pool.query("UPDATE servers SET name=$1 WHERE id=$2", [server().name, ids[1]])
live = await journal(); assert.equal(live.length, 1); assert.equal(live[0].ruleName, "Ping-проба"); cases++
await pool.query("UPDATE servers SET name=$1 WHERE id=$2", [server(1).name, ids[1]])
await pool.query("UPDATE uptime_probes SET name=$1 WHERE id=$2", [prefix + "changed-probe", probe().probeId])
live = await journal(); assert.equal(live.length, 1); assert.equal(live[0].ruleName, "Доступность сервера"); cases++
await reset(); assert.deepEqual(await journal(), []); cases++
await pool.query("UPDATE uptime_probes SET name=$1 WHERE id=$2", [probe().name, probe().probeId])
await reset(); await insert("uptime_ping", snapshot("uptime_ping", [probe()], -15_000), -15_000)
await pool.query("UPDATE uptime_probes SET updated_at=$1 WHERE id=$2", [iso(), probe().probeId])
assert.deepEqual((await build()).probes, []); cases++
console.log(`collector sources owned SQL: ${cases} cases; real settings/identity/error/time/due-only/unknown/reconfiguration/evaluation passed`)
} finally {
await reset()
await pool.query("DELETE FROM alert_history WHERE id LIKE $1", [prefix + "%"])
await pool.query("DELETE FROM uptime_resource_samples WHERE server_id=ANY($1::bigint[])", [ids])
await pool.query("DELETE FROM uptime_probe_samples WHERE probe_id LIKE $1", [prefix + "%"])
await pool.query("DELETE FROM uptime_probes WHERE id LIKE $1", [prefix + "%"])
await pool.query("DELETE FROM servers WHERE id = ANY($1::bigint[])", [ids])
assert.equal((await pool.query("SELECT count(*)::int AS n FROM scheduler_runs WHERE id LIKE $1", [prefix + "%"])).rows[0].n, 0)