Add #loser command — worst packet delivery record over directed sends (v0.21.9)
CI / build + unit (pull_request) Successful in 48s
CI / build + unit (pull_request) Successful in 48s
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
// pure formatting helper (no cycle: battery → mesh only)
|
||||
import { mention } from '../battery/battery.service.js';
|
||||
import { BROADCAST_ADDR } from '../mesh/codec.js';
|
||||
// type-only: the instances come from DI (command.module.ts factory) or direct construction in tests — no runtime import cycles with src/mesh / src/rss
|
||||
import type { MeshService } from '../mesh/mesh.service.js';
|
||||
@@ -43,10 +45,12 @@ export class CommandService {
|
||||
rss?: RssService,
|
||||
) {
|
||||
this.register('ping', () => '#pong!');
|
||||
this.register('loser', () => this.loserReply());
|
||||
// single multi-line string = ONE packet — not an array, which would drip-feed one spaced packet per line
|
||||
this.register('help', () =>
|
||||
[
|
||||
'#ping - alive check',
|
||||
'#loser - worst delivery record',
|
||||
'#rss help - rss subcommands',
|
||||
'#rss add <url> <name> - add feed with AI tags',
|
||||
'#rss remove <name> - drop feed',
|
||||
@@ -57,6 +61,52 @@ export class CommandService {
|
||||
if (rss) this.registerPrefix('rss', (ctx) => rss.handle(ctx));
|
||||
}
|
||||
|
||||
/**
|
||||
* #loser — worst packet delivery record, over directed sends only. Meshtastic shares no mesh-wide
|
||||
* loss stats, so the only real data is our own tx history: group by destination, count delivered vs
|
||||
* failed per node; broadcasts are excluded (firmware strips want_ack pre-TX, they stay pending — and
|
||||
* the broadcast addr is never a real destination even if a NACK ever lands on it). Loser = most
|
||||
* failures, tie-break worse failure ratio. One reply line, ≤ ~180 chars.
|
||||
*/
|
||||
private async loserReply(): Promise<string> {
|
||||
const mesh = this.mesh;
|
||||
if (!mesh)
|
||||
return 'not enough data yet - only directed sends count (broadcasts have no acks)';
|
||||
const stats = new Map<number, { failed: number; delivered: number }>();
|
||||
for (const m of mesh.messages()) {
|
||||
if (m.dir !== 'tx' || m.to === undefined || m.to === BROADCAST_ADDR)
|
||||
continue; // directed sends only
|
||||
if (m.status !== 'delivered' && m.status !== 'failed') continue; // pending = no final status yet
|
||||
const s = stats.get(m.to) ?? { failed: 0, delivered: 0 };
|
||||
if (m.status === 'failed') s.failed += 1;
|
||||
else s.delivered += 1;
|
||||
stats.set(m.to, s);
|
||||
}
|
||||
if (stats.size === 0)
|
||||
return 'not enough data yet - only directed sends count (broadcasts have no acks)';
|
||||
let worst: { num: number; failed: number; total: number } | undefined;
|
||||
for (const [num, s] of stats) {
|
||||
if (s.failed === 0) continue; // zero-failure nodes can't be the loser
|
||||
const total = s.failed + s.delivered;
|
||||
// most failures first, then worse failure ratio on a tie
|
||||
if (
|
||||
!worst ||
|
||||
s.failed > worst.failed ||
|
||||
(s.failed === worst.failed &&
|
||||
s.failed / total > worst.failed / worst.total)
|
||||
) {
|
||||
worst = { num, failed: s.failed, total };
|
||||
}
|
||||
}
|
||||
if (!worst) return 'no failed sends yet - everyone delivers :)';
|
||||
const names = new Map(
|
||||
(await mesh.nodes()).map((n) => [n.num, n.longName ?? n.shortName]),
|
||||
);
|
||||
const name = names.get(worst.num);
|
||||
const who = name ? `${name} (${mention(worst.num)})` : mention(worst.num);
|
||||
return `worst delivery: ${who} - ${worst.failed} failed / ${worst.total - worst.failed} delivered`;
|
||||
}
|
||||
|
||||
/** Register a command handler (names are case-sensitive; the leading `#` is stripped before lookup). */
|
||||
register(name: string, fn: CommandHandler): void {
|
||||
this.commands.set(name, fn);
|
||||
|
||||
Reference in New Issue
Block a user