1 Commits
Author SHA1 Message Date
sneak 85ac1e08ce Interval change keeps unreported samples for the reporter (closes #124)
check / check (push) Waiting to run
The interval menu cleared every host's history, and the reporter builds
each report from that history, so the samples recorded since the last
delivered report were never sent. The menu now resets each host with
HostState.reset, which moves the history's entries to clearedHistory,
keeping the last 100 it has cleared, and buildReport reads both. A host
still pending after the reset is sent with the status of its last check.
The per-host marks are unchanged, so a sample a delivered report had is
not sent again, and the page's history still starts over. Reporter is
exported for the new unit tests.

Model: opus-5-5
2026-10-14 09:35:04 +00:00
3 changed files with 105 additions and 52 deletions
+10 -8
View File
@@ -24,14 +24,16 @@ Decide whether the repo moves to the layout `REPO_POLICIES.md` gives, with
- 2026-10-14: changing the interval no longer drops the samples not yet reported
to the backend ([#124](https://git.eeqj.de/sneak/netwatch/issues/124)). The
interval menu calls the new `HostState.clearHistory`, which moves the
history's entries to `clearedHistory`, keeping the last 100 as the history
does, and `buildReport` reads both, so the next report has the samples
recorded before the change that no report has delivered. The page's history
still starts over, and the per-host marks still leave out of a report the
samples a delivered one had. A unit test sends a report, records samples,
clears the history as the interval menu does and finds them in the next
report; `src/main.js` now exports `Reporter` for it
interval menu resets each host with the new `HostState.reset`, which moves the
history's entries to `clearedHistory`, keeping the last 100 of all it has
cleared, and `buildReport` reads both, so the next report has the samples
recorded before the change that no report has delivered. Until its next check,
such a host is sent with the status of its last check, not `pending`. The
page's history still starts over, and the per-host marks still leave out of a
report the samples a delivered one had. Unit tests reset a host as the
interval menu does and find its unreported samples in the next report, and
only the last 100 after more were cleared; `src/main.js` now exports
`Reporter` for them
- 2026-10-07: the files shared from `sneak/prompts` are its copies at commit
`dd4027b` ([#113](https://git.eeqj.de/sneak/netwatch/issues/113)), with this
repository's own entries after the shared content in `.gitignore`,
+27 -15
View File
@@ -257,6 +257,14 @@ function latencyStats(latencies) {
};
}
// The status a check's error gives its host: "error" for a timeout,
// "offline" for any other error, and "online" for none.
function statusFromError(error) {
if (error === "timeout") return "error";
if (error) return "offline";
return "online";
}
export class HostState {
constructor(host, pinned = false) {
this.name = host.name;
@@ -265,8 +273,9 @@ export class HostState {
// error }, or a round skipped while paused, { timestamp,
// latency: null, paused: true }.
this.history = [];
// The entries the history held before the interval menu last
// cleared it, which the reporter may not have sent yet.
// The last 100 (CONFIG.maxHistoryPoints) entries the interval menu
// has cleared from the history, which the reporter may not have
// sent yet.
this.clearedHistory = [];
this.lastLatency = null;
this.status = "pending"; // 'online' | 'offline' | 'error' | 'pending'
@@ -283,9 +292,7 @@ export class HostState {
});
this._trim();
this.lastLatency = result.latency;
if (result.error === "timeout") this.status = "error";
else if (result.error) this.status = "offline";
else this.status = "online";
this.status = statusFromError(result.error);
this.consecutiveFailures = result.error
? this.consecutiveFailures + 1
: 0;
@@ -296,14 +303,16 @@ export class HostState {
this._trim();
}
// Starts the history over, as the interval menu does. Its entries move
// to clearedHistory, which keeps the last maxHistoryPoints of them, as
// the history does, so the reporter still sends those it has not.
clearHistory() {
// What the interval menu does to each host: the history starts over and
// the host waits for its next check. The history's entries move to
// clearedHistory, so the reporter still sends those it has not.
reset() {
this.clearedHistory = [...this.clearedHistory, ...this.history].slice(
-CONFIG.maxHistoryPoints,
);
this.history = [];
this.lastLatency = null;
this.status = "pending";
}
// The min, max, median and average latency of the checks in the history
@@ -445,8 +454,10 @@ function getClientId() {
// of { name, url, status, clearedHistory, history }, `since` maps a host url
// to the Unix-ms timestamp of the last sample already reported for it, and
// `now` is a Date. Only non-paused samples newer than the mark are included,
// from clearedHistory as well as history. Returns null when no host has an
// unreported sample.
// from clearedHistory as well as history. A host whose status is pending,
// as the interval menu leaves it until its next check, is sent with the
// status of its last check, which is the last sample sent. Returns null
// when no host has an unreported sample.
export function buildReport(hosts, clientId, now, since) {
const reportHosts = [];
const marks = new Map();
@@ -468,7 +479,10 @@ export function buildReport(hosts, clientId, now, since) {
reportHosts.push({
name: host.name,
url: host.url,
status: host.status,
status:
host.status === "pending"
? statusFromError(samples.at(-1).error)
: host.status,
history: samples,
});
marks.set(host.url, high);
@@ -1516,9 +1530,7 @@ async function init() {
// Clear all host history and reset state
for (const host of state.allHosts) {
host.clearHistory();
host.lastLatency = null;
host.status = "pending";
host.reset();
}
state.tickCount = 0;
+68 -29
View File
@@ -379,11 +379,21 @@ test("a check that fails in the first round, which is discarded, writes nothing
assert.deepEqual(consoleLines("info"), []);
});
// Every report is delivered here, and only Target has checks to report.
// Between the first report and the second the interval changes: the
// interval menu calls clearHistory on each target. The third report would
// hold no check the first two did not, so it is not sent.
test("checks recorded before an interval change and not yet reported go in the next report, and no check is reported twice", async (t) => {
// The reports Reporter sent in this test, each as the hosts it sent.
function reportedHosts() {
return fetch.mock.calls.map(
(call) => JSON.parse(call.arguments[1].body).hosts,
);
}
// In the next two tests every report is delivered, and only Target has
// checks to report. target.reset() does to Target what the interval menu
// does to each target.
// The interval changes after the first report. The second report is sent
// before Target's first check since then, and the third after it. A fourth
// would hold no check the first three did not, so it is not sent.
test("checks recorded before an interval change and not yet reported go in the next report, with the status of the last check, and no check is reported twice", async (t) => {
const state = new AppState([
{ name: "Target", url: "https://target.test" },
]);
@@ -395,36 +405,42 @@ test("checks recorded before an interval change and not yet reported go in the n
await reporter.flush();
target.pushSample(2000, { latency: 20, error: null });
target.pushSample(3000, { latency: null, error: "timeout" });
target.clearHistory();
target.reset();
await reporter.flush();
target.pushSample(4000, { latency: 40, error: null });
await reporter.flush();
await reporter.flush();
assert.equal(fetch.mock.calls.length, 2);
const [first, second] = fetch.mock.calls.map(
(call) => JSON.parse(call.arguments[1].body).hosts,
);
const name = "Target";
const url = "https://target.test";
assert.deepEqual(first, [
{
name,
url,
status: "online",
history: [{ t: 1000, latency: 10, error: null }],
},
]);
assert.deepEqual(second, [
{
name,
url,
status: "online",
history: [
{ t: 2000, latency: 20, error: null },
{ t: 3000, latency: null, error: "timeout" },
{ t: 4000, latency: 40, error: null },
],
},
assert.deepEqual(reportedHosts(), [
[
{
name,
url,
status: "online",
history: [{ t: 1000, latency: 10, error: null }],
},
],
[
{
name,
url,
status: "error",
history: [
{ t: 2000, latency: 20, error: null },
{ t: 3000, latency: null, error: "timeout" },
],
},
],
[
{
name,
url,
status: "online",
history: [{ t: 4000, latency: 40, error: null }],
},
],
]);
// The page's history starts over.
assert.deepEqual(target.history, [
@@ -432,6 +448,29 @@ test("checks recorded before an interval change and not yet reported go in the n
]);
});
// The interval changes twice, after 60 checks each time, before the first
// report. Of the 120 checks the interval menu cleared, the report has the
// last 100 (CONFIG.maxHistoryPoints).
test("of the checks interval changes cleared before they were reported, the next report has the last 100", async (t) => {
const state = new AppState([
{ name: "Target", url: "https://target.test" },
]);
const target = state.local[0];
const reporter = new Reporter(state, "client", CONFIG.reportInterval);
t.mock.method(globalThis, "fetch", async () => ({ ok: true }));
for (let n = 1; n <= 120; n++) {
target.pushSample(n * 1000, { latency: 10, error: null });
if (n % 60 === 0) target.reset();
}
await reporter.flush();
const sent = reportedHosts()[0][0].history;
assert.equal(sent.length, 100);
assert.equal(sent[0].t, 21000);
assert.equal(sent.at(-1).t, 120000);
});
// The page shows &lt; &gt; &quot; &amp; and &#39; in a row's markup as
// < > " & and '.
test(`a target whose name and URL hold < > " & and ' shows those characters in its row`, () => {