mirror of
https://github.com/db9979/wibosim.git
synced 2026-12-06 13:34:09 +01:00
- Marktdaten: Schlüsselbild (market mit seq) + nur geänderte Felder je Takt (mkd), einmal für alle kodiert; Aushandeln per net-Nachricht, Lückenerkennung mit Schlüsselbild-Anforderung, alte Clients bekommen die volle Liste - Orderbuch als Delta und nur bei Änderung (neue Umsätze vorne angefügt); Rangliste und Depot nur bei Änderung - Anzeige-Rundung für Stimmung, Branchenstimmung und Zinskurve - Tab im Hintergrund: seltener Marktdaten, kein Orderbuch/Rangliste, Schlüsselbild beim Zurückkehren - Statische Dateien mit Brotli/gzip (einmal gepackt, ETag/304); WebSocket-Kompression als Einstellung (aus) - Einstellungen netDelta, netKeyEvery, netBgEvery, netDeflate; Kennzahlen in /metrics (auch JS-Heap) - SQLite journal_size_limit 16 MB; NODE_OPTIONS --max-old-space-size=192 in docker-compose.yml - Lasttest: Delta-Client, Bytes je Nachrichtentyp und auf der Leitung, --no-delta/--hidden/--server-env, Korrektur der Datenmenge (zählte Daten nach Messende mit); Messwerte und Pi-Schätzung in docs/BETRIEB.md - Tests: netdelta.test.js (Delta = volle Liste über Auktionen, Vola, Abendhandel, Krypto, Split, Börsengang, Rückzug, Lücken), netserve.test.js (Server-Aushandlung, Hintergrund, Kompression statischer Dateien, WAL) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUhQVoPUusUzxgxLWkK
182 lines
7.4 KiB
JavaScript
182 lines
7.4 KiB
JavaScript
// Delta-Takt (Team Netz): Marktdaten einmal je Takt kodieren – als Schlüsselbild (vollständige Liste, wie bisher
|
||
// „market“, zusätzlich mit Sequenznummer) und als Delta („mkd“: nur geänderte Felder seit dem vorigen Takt).
|
||
// Feste Felder (Name, Branche, Art …) und selten ändernde Teile (Konjunkturdaten, Saison, Politik) stehen damit nur
|
||
// noch im Schlüsselbild bzw. im Delta des Takts, in dem sie sich ändern. Das Format beschreibt public/js/mkdelta.js
|
||
// (dort auch applyPatch für den Client). Alte Clients ohne Aushandlung bekommen weiter die volle Liste.
|
||
|
||
import { MK_KEYS } from '../public/js/mkdelta.js';
|
||
|
||
const MAX_DEPTH = 5;
|
||
const SHORT = new Map(MK_KEYS.map((k, i) => [k, String(i)]));
|
||
|
||
/** gerundete Kopie (n Nachkommastellen) – nur für Anzeigewerte, die sonst in jedem Takt im Rauschen zittern */
|
||
const rn = (x, n) => (typeof x === 'number' && Number.isFinite(x) ? Math.round(x * 10 ** n) / 10 ** n : x);
|
||
function roundDeep(v, n, depth = 0) {
|
||
if (typeof v === 'number') return rn(v, n);
|
||
if (!v || typeof v !== 'object' || depth > 4) return v;
|
||
if (Array.isArray(v)) return v.map((x) => roundDeep(x, n, depth + 1));
|
||
const out = {};
|
||
for (const k of Object.keys(v)) out[k] = roundDeep(v[k], n, depth + 1);
|
||
return out;
|
||
}
|
||
|
||
/**
|
||
* Anzeige-Rundung der Marktansicht: Stimmung, Branchenstimmung und Zinskurve ändern sich sonst in jedem Takt an der
|
||
* sechsten Nachkommastelle. Angezeigt werden sie mit höchstens zwei Stellen (Stimmung ×100 ganzzahlig, Zinskurve in %).
|
||
* Gilt gleichermaßen für volle Liste und Delta – beide Wege ergeben denselben Zustand.
|
||
*/
|
||
export function tidyMarket(view) {
|
||
const m = view?.macro;
|
||
if (!m) return view;
|
||
return {
|
||
...view,
|
||
macro: {
|
||
...m,
|
||
sentiment: rn(m.sentiment, 4),
|
||
sectorMood: m.sectorMood ? roundDeep(m.sectorMood, 4) : m.sectorMood,
|
||
curve: m.curve ? roundDeep(m.curve, 4) : m.curve,
|
||
},
|
||
};
|
||
}
|
||
|
||
const isObj = (v) => v !== null && typeof v === 'object';
|
||
const SET = Symbol('set');
|
||
|
||
/**
|
||
* Unterschied zweier JSON-Zustände (aus JSON.parse, also ohne undefined/NaN). Liefert undefined (gleich), einen
|
||
* Patch oder SET (der Behälter wird besser ganz ersetzt).
|
||
*/
|
||
function diffC(a, b, depth) {
|
||
const arr = Array.isArray(a);
|
||
if (arr !== Array.isArray(b)) return SET;
|
||
const p = {};
|
||
let n = 0;
|
||
if (arr) {
|
||
const k = prepended(a, b);
|
||
if (k) return { $u: b.slice(0, k), ...(b.length !== a.length + k ? { $n: b.length } : {}) };
|
||
if (a.length !== b.length) { p.$n = b.length; n++; }
|
||
for (let i = 0; i < b.length; i++) n += diffSlot(p, String(i), i < a.length, a[i], b[i], depth);
|
||
} else {
|
||
for (const k of Object.keys(b)) {
|
||
const c0 = k.charCodeAt(0);
|
||
if (c0 === 36 || (c0 >= 48 && c0 <= 57)) return SET; // $… und Ziffern sind dem Patch-Format vorbehalten
|
||
n += diffSlot(p, SHORT.get(k) ?? k, Object.hasOwn(a, k), a[k], b[k], depth);
|
||
}
|
||
for (const k of Object.keys(a)) {
|
||
if (Object.hasOwn(b, k)) continue;
|
||
const c0 = k.charCodeAt(0);
|
||
if (c0 === 36 || (c0 >= 48 && c0 <= 57)) return SET;
|
||
(p.$x ??= []).push(k); n++;
|
||
}
|
||
}
|
||
return n ? p : undefined;
|
||
}
|
||
|
||
/** JSON-Werte gleich? (tiefer Vergleich, ohne Umweg über JSON.stringify) */
|
||
function eq(x, y) {
|
||
if (x === y) return true;
|
||
if (!isObj(x) || !isObj(y) || Array.isArray(x) !== Array.isArray(y)) return false;
|
||
const kx = Object.keys(x), ky = Object.keys(y);
|
||
if (kx.length !== ky.length) return false;
|
||
for (const k of kx) if (!Object.hasOwn(y, k) || !eq(x[k], y[k])) return false;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* Wurden vorne k Einträge eingefügt (Umsatzliste: neueste zuerst, hinten abgeschnitten)? b = [k neue, ...a] gekürzt.
|
||
* Liefert k (1–10) oder 0.
|
||
*/
|
||
function prepended(a, b) {
|
||
if (!a.length || !b.length || a.length > 200 || eq(a[0], b[0])) return 0;
|
||
for (let k = 1; k <= Math.min(10, b.length - 1); k++) {
|
||
if (!eq(b[k], a[0])) continue;
|
||
const m = Math.min(a.length, b.length - k);
|
||
if (b.length - k > a.length) continue;
|
||
let ok = true;
|
||
for (let i = 1; i < m && ok; i++) ok = eq(b[i + k], a[i]);
|
||
if (ok) return k;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
function diffSlot(p, k, had, x, y, depth) {
|
||
if (had && x === y) return 0;
|
||
if (had && isObj(x) && isObj(y)) {
|
||
if (depth < MAX_DEPTH) {
|
||
const sub = diffC(x, y, depth + 1);
|
||
if (sub === undefined) return 0;
|
||
if (sub !== SET) { (p.$p ??= {})[k] = sub; return 1; }
|
||
} else if (JSON.stringify(x) === JSON.stringify(y)) return 0;
|
||
}
|
||
p[k] = y;
|
||
return 1;
|
||
}
|
||
|
||
/** Patch von a nach b (JSON-Zustände); null = keine Änderung */
|
||
export function diff(a, b) {
|
||
if (!isObj(a) || !isObj(b)) return a === b ? null : { $set: true };
|
||
const d = diffC(a, b, 0);
|
||
if (d === SET) return { $set: true };
|
||
return d ?? null;
|
||
}
|
||
|
||
/**
|
||
* Takt-Kodierer: next(state) kodiert einmal für alle Clients.
|
||
* – key: Schlüsselbild (vollständig, mit Sequenznummer; für alte Clients die gewohnte Nachricht)
|
||
* – deltaBuf: nur die Änderungen seit dem vorigen Stand, oder null, wenn ein Delta nicht möglich ist (erster Stand)
|
||
* Beide als Buffer; key wird erst beim ersten Zugriff gebaut (in den meisten Takten braucht ihn niemand).
|
||
* skipSame: ohne Änderung bleibt die Sequenznummer stehen und es gibt nichts zu senden (changed = false).
|
||
*/
|
||
export class DeltaFeed {
|
||
/** @param {{keyMsg: (seq: number, json: string) => string, deltaMsg: (seq: number, d: string) => string, tidy?: (v: any) => any, skipSame?: boolean}} o */
|
||
constructor(o) {
|
||
this.o = o;
|
||
this.seq = 0;
|
||
this.prev = null;
|
||
this.json = null;
|
||
this._key = null;
|
||
this.deltaBuf = null;
|
||
this.changed = false;
|
||
this.stats = { keyBytes: 0, deltaBytes: 0 };
|
||
}
|
||
|
||
next(view) {
|
||
const json = JSON.stringify(this.o.tidy ? this.o.tidy(view) : view);
|
||
if (this.o.skipSame && json === this.json) { this.changed = false; return this; }
|
||
const snap = JSON.parse(json);
|
||
let d = this.prev ? diff(this.prev, snap) : { $set: true };
|
||
if (this.o.skipSame && d === null) { this.changed = false; this.prev = snap; this.json = json; this._key = null; return this; }
|
||
if (d && d.$set) d = undefined;
|
||
this.seq++;
|
||
this.changed = true;
|
||
this.prev = snap;
|
||
this.json = json;
|
||
this._key = null;
|
||
this.deltaBuf = d === undefined ? null : Buffer.from(this.o.deltaMsg(this.seq, JSON.stringify(d ?? {})));
|
||
this.stats.deltaBytes = this.deltaBuf?.length ?? 0;
|
||
return this;
|
||
}
|
||
|
||
/** Schlüsselbild zum aktuellen Stand (gleiche Sequenznummer wie das letzte Delta) */
|
||
get key() {
|
||
if (!this._key && this.json) { this._key = Buffer.from(this.o.keyMsg(this.seq, this.json)); this.stats.keyBytes = this._key.length; }
|
||
return this._key;
|
||
}
|
||
}
|
||
|
||
/** Marktdaten: Schlüsselbild „market“ (wie bisher, plus seq), Delta „mkd“ – in jedem Takt (Uhrzeit ändert sich immer) */
|
||
export class MarketFeed extends DeltaFeed {
|
||
constructor() {
|
||
super({ tidy: tidyMarket, keyMsg: (seq, json) => `{"type":"market","seq":${seq},"market":${json}}`, deltaMsg: (seq, d) => `{"type":"mkd","seq":${seq},"d":${d}}` });
|
||
}
|
||
}
|
||
|
||
/** Orderbuch eines Werts: Schlüsselbild „book“ (wie bisher, plus seq), Delta „bkd“ – nur wenn sich etwas ändert */
|
||
export class BookFeed extends DeltaFeed {
|
||
/** @param {string} symbol */
|
||
constructor(symbol) {
|
||
const sym = JSON.stringify(symbol);
|
||
super({ skipSame: true, keyMsg: (seq, json) => `{"type":"book","symbol":${sym},"seq":${seq},${json.slice(1)}`, deltaMsg: (seq, d) => `{"type":"bkd","symbol":${sym},"seq":${seq},"d":${d}}` });
|
||
}
|
||
}
|