Files
TeTeHackoandClaude Opus 5.5 77a5f14925 feat(observers): radio setting per observation from the observer's radio history (#1869) (#2292)
Part of #1869.

## Problem

`observers.radio` keeps only the **last** `MHz,kHz,SF,CR` an observer
reported. Once an observer is retuned (say from SF7 to the EU narrow SF8
setting to listen to a neighbouring mesh), every packet it ever heard
reads as heard on the new setting, and there is no way to tell an SF7
packet from an SF8 one.

## What this does

As #1869 proposes, the packet data model is untouched: the setting of an
observation is resolved from its observer and time.

**Ingestor** (all schema in `dbschema.Apply`)

- `observer_radios`: one row per stretch of time an observer, and each
radio of a multi-radio node, stayed on one setting. Every live status
extends the current row and a change starts a new one; retained replays
never extend a row. On the first run it is seeded with every observer's
current radio, which is what the UI already showed for the past.
- Settings are stored canonical through `dbschema.CanonicalRadio`, so
`869.4320068,62.5,7,5` and `869.432,62.50,7,5` are one setting.
- `observation_radios` (`observation_id → rx_radio_id`): multi-radio
openHop nodes tag each packet with `rx_radio_id` and list `radios: [{id,
radio}]` in their status, so their observations resolve per radio.
- The table is sparse: single-radio nodes never send the field, so
`observations` and every query that scans it stay untouched.
  - The row id comes from the observation upsert via `RETURNING id`.
- Rows are pruned with their transmissions, and a purged observer takes
its history along.

**Server** (read-only)

- No `AssertReady` change: an absent table reads as empty and is probed
again later, the same rule as `advert_route_evidence`.
- The history is held in memory. The live poller checks it on every tick
(two primary-key lookups), so a retune or a new `rx_radio_id` is known
before the observation is broadcast. A full reload runs every 10 min.
- `radio` is added to observations, packets, grouped rows (taken from
the header observation) and the live feed.
- `GET /api/packets?radio=MHz,kHz,SF,CR` filters by setting.
- Combined with `observer=`, it means *that* observer heard it on that
setting.
- A radio query is served from memory instead of falling back to SQL,
which would return the window unfiltered.
- `GET /api/radios` (named struct) and `radio_history` on `GET
/api/observers/{id}`.

**Frontend**

- Packet filter fields `radio`, `freq`, `bw`, `sf`, `cr`, e.g. `sf == 8`
or `freq > 869.5 && bw == 62.5`.
- A Radio row in the packet detail and a Radio column in its
observations table (only when the server reports one).
- A radio history table on the observer page once there is more than one
stretch.
- `RfParams.radioText` formats the setting. It escapes all five
characters and returns non-numeric input as it came, escaped.

## Performance

`txMatchesRadio` runs once per observation inside `filterPackets`.

- Each observation costs one map lookup by observer id.
- Observers with a single stretch (nearly all) answer from that lookup
directly.
- Retuned observers add a binary search over their few stretches.
- Multi-radio observers add one lookup by observation id.
- The snapshot is loaded once per filter call.

`BenchmarkTxMatchesRadio`, 30K packets / 150K observations (20
observers, 2 of them retuned):

```
BenchmarkTxMatchesRadio-24    602    2056924 ns/op    0 B/op    0 allocs/op
```

Memory is one entry per stretch plus one per multi-radio observation;
nothing is added per observation otherwise.

## Tests

- **dbschema:** seed runs once, canonical spellings, the tables exist
without `observers.radio`.
- **ingestor:**
  - stretches extend and split; canonical spellings do not split
- two radios of one node hearing one transmission over two paths each
keep their own radio
  - a retained status does not write history
  - prune and purge
  - `rx_radio_id` reaches `PacketData`
- **server:**
- resolution before, between and after stretches, and with an unknown
radio id
  - `observer` + `radio`, and grouped rows carry `radio`
  - incremental refresh, `/api/radios`, `radio_history`, missing tables
  - the benchmark above
- **frontend:** the new filter fields in
`tests/unit/test-packet-filter.js`, and XSS cases through the real
`RfParams.radioText` and the history renderer.
- **E2E:** `tests/e2e/test-radio-setting-1869-e2e.js` checks the grouped
rows' radio, `sf ==` on the packets page, the detail Radio row and the
observer history. It is added to `scripts/non-unit-tests.json` and to
shard 2.

`go test -race` in `cmd/server`, `cmd/ingestor` and `internal/dbschema`
passes, along with `sh test-all.sh`, `eslint@8` (0 errors) and the XSS
diff gate. The new E2E test and `test-e2e-playwright.js` pass against
the fixture.

## Browser validation

This runs on a live instance with six observers, one of them retuned to
869.618/62.5/SF8/CR8:

- The packet detail shows `Radio: 869.432 MHz · SF7 · BW 62.5 kHz · CR
4/5`.
- `sf == 7` shows 106 of 106 grouped rows and `sf == 8` shows 0 of 107;
the SF8 observer has not heard anything yet.
- `/api/radios` lists both settings.
- No page errors.

## Notes

- **The customizer (rule 8)** gets nothing: the only constants are
server-side refresh intervals.
- **Attribution timing:** packets heard between a retune and the
observer's next status message (minutes) are attributed to the previous
setting, because a stretch starts when the status is handled. This is
documented at `RecordObserverRadios`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 21:08:40 +01:00

74 lines
3.3 KiB
JavaScript

/* #1268 — LoRa RF parameters per node, from /api/rf-params (the MeshCore
* map's view of each node's radio settings), rendered in the node detail
* views of nodes.js. Pure helpers; no fetching here. */
window.RfParams = (function () {
'use strict';
function esc(s) {
// All five OWASP characters, like app.js escapeHtml: radioText (#1869)
// renders observer-published strings.
return String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
}
function preset(p, presets) {
if (!p || !presets) return null;
for (var i = 0; i < presets.length; i++) {
var q = presets[i];
if (Math.abs(q.freq - p.freq) < 0.001 && q.sf === p.sf && Math.abs(q.bw - p.bw) < 0.01 && q.cr === p.cr) return q;
}
return null;
}
function ago(ms) {
var h = Math.floor(ms / 3600000);
if (h < 1) return 'under 1h ago';
if (h < 24) return h + 'h ago';
return Math.floor(h / 24) + 'd ago';
}
function settingText(s) {
return Number(s.freq).toFixed(3) + ' MHz · SF' + s.sf + ' · BW ' + s.bw + ' kHz';
}
function deviationTitle(dev) {
return dev.agree + ' of ' + dev.peers + ' nearby nodes on this band use ' + settingText(dev.majority);
}
// deviationBadge marks a node whose frequency, SF or bandwidth differs
// from most same-band nodes around it (dev from /api/rf-params
// deviations); the badge names the node's own differing values.
function deviationBadge(p, dev) {
if (!p || !dev) return '';
var parts = (dev.fields || []).map(function (f) {
if (f === 'freq') return Number(p.freq).toFixed(3);
if (f === 'sf') return 'SF' + p.sf;
return 'BW' + p.bw;
});
return ' <span class="badge-lora-diff" title="' + esc('LoRa settings differ: ' + deviationTitle(dev)) + '">LoRa ' + esc(parts.join(' ')) + '</span>';
}
function detailHtml(p, presets, nowMs, dev) {
if (!p) {
return '<span class="text-muted" title="Not on the MeshCore map, or no advert there within the configured age">Not on the MeshCore map</span>';
}
var q = preset(p, presets);
var when = Date.parse(p.last_advert);
return Number(p.freq).toFixed(3) + ' MHz · SF' + p.sf + ' · BW ' + p.bw + ' kHz · CR 4/' + p.cr +
(q ? ' <span class="badge-region">' + esc(q.title) + '</span>' : '') +
(isNaN(when) ? '' : ' <span class="text-muted" title="Advert received by the MeshCore map">(map advert ' + ago(nowMs - when) + ')</span>') +
(dev ? '<div class="lora-deviation" data-testid="lora-deviation">Differs from ' + esc(dev.agree + ' of ' + dev.peers + ' nearby nodes on this band, which use ' + settingText(dev.majority)) + '</div>' : '');
}
// radioText renders the "MHz,kHz,SF,CR" string an observer reports
// (#1869: the setting a packet was heard on). Unparseable input comes back
// escaped as it is.
function radioText(radio) {
var p = String(radio || '').split(',');
var num = /^\s*\d+(\.\d+)?\s*$/;
if (p.length !== 4 || !p.every(function (x) { return num.test(x); })) return esc(radio || '');
return esc(Number(p[0]).toFixed(3) + ' MHz · SF' + p[2].trim() + ' · BW ' + p[1].trim() + ' kHz · CR 4/' + p[3].trim());
}
return { detailHtml: detailHtml, deviationBadge: deviationBadge, radioText: radioText };
})();