mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-09-26 18:27:58 +00:00
Implements #1727. ## What this adds **Mobile client-RX coverage** — an opt-in, crowdsourced RF-coverage feature. A roaming MeshCore **companion** radio (driven by the open-source [corescope-rx](https://github.com/efiten/corescope-rx) PWA, GPLv3) reports which nodes it heard directly, tagged with the phone's GPS and the packet's SNR/RSSI. CoreScope ingests these into a new `client_receptions` table and renders per-node **hex coverage** on the Reach page, plus a standalone **Coverage dashboard** (`#/rx-coverage`) with a top-mobile-observers leaderboard. Also includes **`GET /api/nodes/resolve?prefix=<hex>`** — a read-only node-name lookup by pubkey prefix (`{name, pubkey, ambiguous}`), used by the companion app for friendly names. ## Opt-in — default OFF (zero impact on existing deployments) The whole feature is gated behind one config flag, **disabled by default**: ```jsonc "clientRxCoverage": { "enabled": false } ``` When disabled (the default): the ingestor writes **no** `client_receptions`; the three coverage endpoints return a clean **404**; the UI hides the Coverage nav link, the `#/rx-coverage` route, and the Reach-page toggle. `/api/nodes/resolve` is always available (not coverage-specific). ## How it works ``` companion ──BLE 0x88 (snr+rssi+raw)──▶ corescope-rx PWA ──▶ MQTT meshcore/client/{pubkey}/packets │ ingestor (gated) ──▶ client_receptions (GPS + SNR + heard-key) │ server: pure-Go hex grid ──▶ GeoJSON ──▶ Reach hex overlay + Coverage dashboard ``` - **Direct-only capture:** records only what the companion heard itself and directly — a 0-hop advert's pubkey, or `path[last]` (last forwarder) for FLOOD routes; ≥2-byte path-hash required. Upstream hops discarded. - **No new deps:** hexbins are a pure-Go pointy-top grid over Web Mercator (`cmd/server/hexgrid.go`) computed at query time (`CGO_ENABLED=0` / `modernc.org/sqlite` friendly); frontend uses the existing Leaflet. - **Trust:** companion pubkey = identity; an EMQX ACL binds each client to publish only to its own `meshcore/client/{pubkey}/packets` topic. Payload contract in `docs/client-rx-coverage.md`. ## How to enable / try it 1. In `config.json`, set `"clientRxCoverage": { "enabled": true }` and restart server + ingestor. 2. Point an EMQX (or any broker) listener so a client can publish to `meshcore/client/<pubkey>/packets`; the ingestor already subscribes under `meshcore/#`. 3. Run the [corescope-rx](https://github.com/efiten/corescope-rx) PWA on an Android phone paired (BLE) to a MeshCore companion — it captures heard nodes + GPS and publishes. 4. View results: per-node Reach page → toggle **coverage**, or the **Coverage** dashboard at `#/rx-coverage`. ## What's where - **Ingestor:** `cmd/ingestor/client_reception.go` (ingest), `db.go` (`client_receptions` + `client_observers` schema), `main.go` (gated dispatch), `config.go` (flag). - **Server:** `cmd/server/rx_coverage.go` + `rx_dashboard.go` (endpoints, self-guard 404 when off), `hexgrid.go` (pure-Go grid), `node_resolve.go` (resolve), `routes.go` / `types.go` / `config.go` (wiring + flag + `/api/config/client` field). - **Frontend:** `public/rx-coverage.js` (dashboard), `node-reach-coverage.js` + `.css` (overlay), `node-reach.js` (Reach toggle, flag-gated), `roles.js` (reads the flag, hides nav when off). - **Docs:** `docs/client-rx-coverage.md`. ## Testing - Go: `cd cmd/server && go test ./...` and `cd cmd/ingestor && go test ./...` — green, including new gate tests (`coverage_gate_test.go` in both: off → no rows / 404, on → works) and the rx-coverage / resolve / hexgrid suites. - JS: `node test-coverage-gate.js`, `node test-node-reach-coverage.js` (wired into CI). The Playwright `test-node-reach-coverage-e2e.js` is wired into the e2e job and **skips when `clientRxCoverage` is disabled**, so it's safe under the default-off config. ## Notes for reviewers - The four new routes are registered in `cmd/server/openapi_known_gaps.json` (the existing OpenAPI-completeness ratchet), matching how other not-yet-spec'd routes are tracked. Happy to write full OpenAPI spec entries instead if you prefer. - Commits are split per layer (ingestor / server endpoints / resolve / frontend / CI) for review. --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: Erwin Fiten <e.fiten@opteco.be>
255 lines
7.6 KiB
Go
255 lines
7.6 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/meshcore-analyzer/dbschema"
|
|
)
|
|
|
|
// PruneOldPackets deletes transmissions (and their child observations)
|
|
// older than `days`. Returns count of transmissions deleted.
|
|
//
|
|
// Owned by the ingestor per #1283: the writer process is the only one
|
|
// allowed to hold the DB write lock; previously this lived in
|
|
// cmd/server/db.go and raced ingestor INSERTs (SQLITE_BUSY).
|
|
func (s *Store) PruneOldPackets(days int) (int64, error) {
|
|
if days <= 0 {
|
|
return 0, nil
|
|
}
|
|
cutoff := time.Now().UTC().AddDate(0, 0, -days).Format(time.RFC3339)
|
|
|
|
// Tagged for writer-perf visibility (#1340).
|
|
var n int64
|
|
err := s.WriterTx("prune_packets", func(tx *sql.Tx) error {
|
|
// Delete child observations first (no CASCADE in SQLite).
|
|
if _, err := tx.Exec(`DELETE FROM observations WHERE transmission_id IN (
|
|
SELECT id FROM transmissions WHERE first_seen < ?
|
|
)`, cutoff); err != nil {
|
|
return fmt.Errorf("prune observations: %w", err)
|
|
}
|
|
|
|
res, err := tx.Exec(`DELETE FROM transmissions WHERE first_seen < ?`, cutoff)
|
|
if err != nil {
|
|
return fmt.Errorf("prune transmissions: %w", err)
|
|
}
|
|
n, _ = res.RowsAffected()
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if n > 0 {
|
|
log.Printf("[prune] deleted %d transmissions older than %d days", n, days)
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// PruneOldClientReceptions deletes mobile client-RX coverage rows older than
|
|
// `days` (by rx_at), and client_observers (companion names) whose last_seen has
|
|
// aged out. This bounds the otherwise-unbounded client_receptions table the
|
|
// opt-in coverage feature feeds. 0 disables. Owned by the ingestor writer
|
|
// (#1283). Returns the number of client_receptions rows deleted.
|
|
func (s *Store) PruneOldClientReceptions(days int) (int64, error) {
|
|
if days <= 0 {
|
|
return 0, nil
|
|
}
|
|
cutoff := time.Now().UTC().AddDate(0, 0, -days).Format(time.RFC3339)
|
|
|
|
var n int64
|
|
err := s.WriterTx("prune_client_receptions", func(tx *sql.Tx) error {
|
|
res, err := tx.Exec(`DELETE FROM client_receptions WHERE rx_at < ?`, cutoff)
|
|
if err != nil {
|
|
return fmt.Errorf("prune client_receptions: %w", err)
|
|
}
|
|
n, _ = res.RowsAffected()
|
|
// Drop companion name rows not refreshed within the window.
|
|
if _, err := tx.Exec(`DELETE FROM client_observers WHERE last_seen < ?`, cutoff); err != nil {
|
|
return fmt.Errorf("prune client_observers: %w", err)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if n > 0 {
|
|
log.Printf("[prune] deleted %d client_receptions older than %d days", n, days)
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// SoftDeleteBlacklistedObservers marks observers in the blacklist as
|
|
// inactive=1 so they are hidden from API responses. Owned by ingestor
|
|
// per #1287. Runs once at startup.
|
|
func (s *Store) SoftDeleteBlacklistedObservers(blacklist []string) {
|
|
n, err := dbschema.SoftDeleteBlacklistedObservers(s.db, blacklist)
|
|
if err != nil {
|
|
log.Printf("[observer-blacklist] warning: soft-delete failed: %v", err)
|
|
return
|
|
}
|
|
if n > 0 {
|
|
log.Printf("[observer-blacklist] soft-deleted %d blacklisted observer(s)", n)
|
|
}
|
|
}
|
|
|
|
// PruneNeighborEdges deletes rows older than maxAgeDays from
|
|
// neighbor_edges. Owned by the ingestor per #1287 (was in cmd/server).
|
|
// Returns DB rows deleted.
|
|
func (s *Store) PruneNeighborEdges(maxAgeDays int) (int64, error) {
|
|
if maxAgeDays <= 0 {
|
|
return 0, nil
|
|
}
|
|
cutoff := time.Now().UTC().Add(-time.Duration(maxAgeDays) * 24 * time.Hour).Format(time.RFC3339)
|
|
res, err := s.db.Exec("DELETE FROM neighbor_edges WHERE last_seen < ?", cutoff)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("prune neighbor_edges: %w", err)
|
|
}
|
|
n, _ := res.RowsAffected()
|
|
if n > 0 {
|
|
log.Printf("[neighbor-prune] removed %d DB rows older than %d days", n, maxAgeDays)
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// ─── from_pubkey backfill (#1143) ──────────────────────────────────────────
|
|
//
|
|
// Moved from cmd/server/from_pubkey_migration.go in #1287. Runs from the
|
|
// ingestor's maintenance loop. Populates transmissions.from_pubkey for
|
|
// ADVERT rows whose value is still NULL, by parsing decoded_json.pubKey.
|
|
|
|
// FromPubkeyBackfillStats holds progress for /api/healthz exposure.
|
|
// The ingestor exposes these via stats_file.go so the server can read
|
|
// them without writing.
|
|
type FromPubkeyBackfillStats struct {
|
|
Total int64 `json:"total"`
|
|
Processed int64 `json:"processed"`
|
|
Done bool `json:"done"`
|
|
}
|
|
|
|
// BackfillFromPubkey scans transmissions where from_pubkey IS NULL and
|
|
// payload_type = 4 (ADVERT) and populates from_pubkey from decoded_json.
|
|
// Chunked + yields between batches. Safe to call repeatedly; once a row
|
|
// is set to either "" or hex it never matches the WHERE clause again.
|
|
func (s *Store) BackfillFromPubkey(chunkSize int, yieldDuration time.Duration, progress func(total, processed int64, done bool)) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
log.Printf("[backfill] from_pubkey panic recovered: %v", r)
|
|
}
|
|
if progress != nil {
|
|
progress(0, 0, true) // signal done; values overwritten below if collected
|
|
}
|
|
}()
|
|
if chunkSize <= 0 {
|
|
chunkSize = 5000
|
|
}
|
|
|
|
var total int64
|
|
if err := s.db.QueryRow(
|
|
"SELECT COUNT(*) FROM transmissions WHERE from_pubkey IS NULL AND payload_type = 4",
|
|
).Scan(&total); err != nil {
|
|
log.Printf("[backfill] from_pubkey count error: %v", err)
|
|
return
|
|
}
|
|
if total == 0 {
|
|
log.Println("[backfill] from_pubkey: nothing to do")
|
|
if progress != nil {
|
|
progress(0, 0, true)
|
|
}
|
|
return
|
|
}
|
|
if progress != nil {
|
|
progress(total, 0, false)
|
|
}
|
|
log.Printf("[backfill] from_pubkey starting: %d ADVERT rows", total)
|
|
|
|
stmt, err := s.db.Prepare("UPDATE transmissions SET from_pubkey = ? WHERE id = ?")
|
|
if err != nil {
|
|
log.Printf("[backfill] from_pubkey prepare: %v", err)
|
|
return
|
|
}
|
|
defer stmt.Close()
|
|
|
|
var processed int64
|
|
for {
|
|
rows, err := s.db.Query(
|
|
"SELECT id, decoded_json FROM transmissions WHERE from_pubkey IS NULL AND payload_type = 4 LIMIT ?",
|
|
chunkSize)
|
|
if err != nil {
|
|
log.Printf("[backfill] from_pubkey select: %v", err)
|
|
return
|
|
}
|
|
type row struct {
|
|
id int64
|
|
pk string
|
|
}
|
|
batch := make([]row, 0, chunkSize)
|
|
for rows.Next() {
|
|
var id int64
|
|
var dj sql.NullString
|
|
if err := rows.Scan(&id, &dj); err != nil {
|
|
continue
|
|
}
|
|
batch = append(batch, row{id: id, pk: extractPubkeyFromAdvertJSON(dj.String)})
|
|
}
|
|
rows.Close()
|
|
if len(batch) == 0 {
|
|
break
|
|
}
|
|
|
|
tx, err := s.db.Begin()
|
|
if err != nil {
|
|
log.Printf("[backfill] from_pubkey begin tx: %v", err)
|
|
return
|
|
}
|
|
txStmt := tx.Stmt(stmt)
|
|
for _, b := range batch {
|
|
// Sentinel: "" = scanned-no-pubkey (so the WHERE clause
|
|
// won't keep rescanning this row). hex = real pubkey.
|
|
var val interface{} = ""
|
|
if b.pk != "" {
|
|
val = b.pk
|
|
}
|
|
if _, err := txStmt.Exec(val, b.id); err != nil {
|
|
log.Printf("[backfill] from_pubkey update id=%d: %v", b.id, err)
|
|
}
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
log.Printf("[backfill] from_pubkey commit: %v", err)
|
|
return
|
|
}
|
|
processed += int64(len(batch))
|
|
if progress != nil {
|
|
progress(total, processed, false)
|
|
}
|
|
if len(batch) < chunkSize {
|
|
break
|
|
}
|
|
if yieldDuration > 0 {
|
|
time.Sleep(yieldDuration)
|
|
}
|
|
}
|
|
log.Printf("[backfill] from_pubkey complete: %d rows processed", processed)
|
|
if progress != nil {
|
|
progress(total, processed, true)
|
|
}
|
|
}
|
|
|
|
// extractPubkeyFromAdvertJSON parses an ADVERT decoded_json blob and
|
|
// returns the pubKey field, or "" if absent/invalid.
|
|
func extractPubkeyFromAdvertJSON(s string) string {
|
|
if s == "" {
|
|
return ""
|
|
}
|
|
var m map[string]interface{}
|
|
if err := json.Unmarshal([]byte(s), &m); err != nil {
|
|
return ""
|
|
}
|
|
if v, ok := m["pubKey"].(string); ok {
|
|
return v
|
|
}
|
|
return ""
|
|
}
|