Files
meshcore-analyzer/cmd
Kpa-clawbot 4898541bce fix(ingestor): observer metadata nested stats + SNR/RSSI case fallback (#336)
## Problem

Two data integrity bugs in the Go ingestor cause observer metadata and
signal quality data to be missing for all Go-backend users.

### #320 — Observer metadata never populated

`extractObserverMeta()` reads `battery_mv`, `uptime_secs`, and
`noise_floor` from the **top level** of the MQTT status message.
However, the actual MQTT payload nests these under a `stats` object:

```json
{
  "status": "online",
  "origin": "ObserverName",
  "model": "Heltec V3",
  "firmware_version": "v1.14.0-9f1a3ea",
  "stats": {
    "battery_mv": 4174,
    "uptime_secs": 80277,
    "noise_floor": -110
  }
}
```

Result: battery, uptime, and noise floor are always NULL in the
database.

### #321 — SNR and RSSI always missing on raw packets

The raw packet handler reads `msg["SNR"]` and `msg["RSSI"]` (uppercase
only). Some MQTT bridges send these as lowercase `snr`/`rssi`. The
companion BLE handler already has a case-insensitive fallback — the raw
packet path did not.

Result: SNR/RSSI are NULL for all raw packet observations from bridges
that use lowercase keys.

## Fix

### #320 — Nested stats with top-level fallback

- Added `nestedOrTopLevel()` helper that checks `msg["stats"][key]`
first, then `msg[key]`
- `extractObserverMeta` now uses this helper for `battery_mv`,
`uptime_secs`, `noise_floor`
- Top-level fallback preserved for backward compatibility with bridges
that flatten the structure
- Safe type assertion: `stats, _ :=
msg["stats"].(map[string]interface{})` — no crash if stats is missing or
wrong type

### #321 — Lowercase SNR/RSSI fallback

- Raw packet handler now uses `else if` to check lowercase `snr`/`rssi`
when uppercase keys are absent
- Matches the pattern already used in the companion channel and direct
message handlers

## Tests

10 new test cases added:

| Test | What it verifies |
|------|-----------------|
| `TestExtractObserverMetaNestedStats` | All 5 fields populated from
nested stats object |
| `TestExtractObserverMetaNestedStatsPrecedence` | Nested stats wins
over top-level when both present |
| `TestExtractObserverMetaFlatFallback` | Flat structure still works
(backward compat) |
| `TestExtractObserverMetaEmptyStats` | Empty stats object — no crash,
model still works |
| `TestExtractObserverMetaStatsNotAMap` | stats is a string — no crash,
falls back to top-level |
| `TestExtractObserverMetaNoiseFloorFloat` | Float precision preserved
(noise_floor REAL migration) |
| `TestHandleMessageWithLowercaseSNRRSSI` | Lowercase snr/rssi both
stored correctly |
| `TestHandleMessageSNRRSSIUppercaseWins` | When both cases present,
uppercase takes precedence |
| `TestHandleMessageNoSNRRSSI` | Neither key present — nil, no crash |
| Existing `TestExtractObserverMeta` | Still passes (flat structure
backward compat) |

All tests pass: `go test ./... -count=1` and `go vet ./...` clean.

Closes #320
Closes #321

---------

Co-authored-by: Kpa-clawbot <259247574+Kpa-clawbot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-31 17:53:04 -07:00
..