server: remove diff switch, always send diffs
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+2
-5
@@ -6,9 +6,10 @@ from datetime import datetime, timezone
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from . import log_config, network, we_connect
|
from . import log_config, network, we_connect
|
||||||
|
from jaydiff.diff import diff_full as jay_diff_full
|
||||||
|
|
||||||
from .data_model import ALL_DOMAINS, apply_procedural, extract_all
|
from .data_model import ALL_DOMAINS, apply_procedural, extract_all
|
||||||
from .storage_helpers import auto_out_path, load_last_24h_records, load_store, save_store
|
from .storage_helpers import auto_out_path, load_last_24h_records, load_store, save_store
|
||||||
from jaydiff.diff import diff_full as jay_diff_full
|
|
||||||
|
|
||||||
from volkswagencarnet.vw_exceptions import AuthenticationError
|
from volkswagencarnet.vw_exceptions import AuthenticationError
|
||||||
|
|
||||||
@@ -46,7 +47,6 @@ def run(args: argparse.Namespace) -> None:
|
|||||||
out = auto_out_path(logs_dir, vin)
|
out = auto_out_path(logs_dir, vin)
|
||||||
|
|
||||||
push_clients, push_lock, push_store_ref = network.start_push_server(args.host, args.port)
|
push_clients, push_lock, push_store_ref = network.start_push_server(args.host, args.port)
|
||||||
push_store_ref["diff_mode"] = args.diff
|
|
||||||
push_store_ref["preloaded"] = load_last_24h_records(logs_dir, vin)
|
push_store_ref["preloaded"] = load_last_24h_records(logs_dir, vin)
|
||||||
for _r in push_store_ref["preloaded"]:
|
for _r in push_store_ref["preloaded"]:
|
||||||
if "procedural" not in _r:
|
if "procedural" not in _r:
|
||||||
@@ -89,10 +89,7 @@ def run(args: argparse.Namespace) -> None:
|
|||||||
snapshot = apply_procedural(snapshot)
|
snapshot = apply_procedural(snapshot)
|
||||||
store["records"].append(snapshot)
|
store["records"].append(snapshot)
|
||||||
save_store(out, store, args.max_records)
|
save_store(out, store, args.max_records)
|
||||||
if args.diff:
|
|
||||||
payload = jay_diff_full(last_broadcast, snapshot, combine_upd_add=True)
|
payload = jay_diff_full(last_broadcast, snapshot, combine_upd_add=True)
|
||||||
else:
|
|
||||||
payload = snapshot
|
|
||||||
last_broadcast = snapshot
|
last_broadcast = snapshot
|
||||||
network.broadcast(payload, push_clients, push_lock)
|
network.broadcast(payload, push_clients, push_lock)
|
||||||
log.info("Record #%d saved at %s", len(store["records"]), snapshot["ts"])
|
log.info("Record #%d saved at %s", len(store["records"]), snapshot["ts"])
|
||||||
|
|||||||
@@ -120,12 +120,6 @@ def main() -> None:
|
|||||||
help="Directory for rotating log files (overrides credentials file, default: ./logs)",
|
help="Directory for rotating log files (overrides credentials file, default: ./logs)",
|
||||||
)
|
)
|
||||||
p.add_argument("-v", "--verbose", action="store_true", help="Enable debug logging")
|
p.add_argument("-v", "--verbose", action="store_true", help="Enable debug logging")
|
||||||
p.add_argument(
|
|
||||||
"--diff",
|
|
||||||
action="store_true",
|
|
||||||
default=None,
|
|
||||||
help="Send diffs instead of full snapshots over the push server (overrides credentials file, default: off)",
|
|
||||||
)
|
|
||||||
p.add_argument(
|
p.add_argument(
|
||||||
"--dry",
|
"--dry",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
@@ -176,9 +170,6 @@ def main() -> None:
|
|||||||
args.port = int(creds_data.get("port", 9999))
|
args.port = int(creds_data.get("port", 9999))
|
||||||
if args.log_dir is None:
|
if args.log_dir is None:
|
||||||
args.log_dir = creds_data.get("log_dir", "./logs")
|
args.log_dir = creds_data.get("log_dir", "./logs")
|
||||||
if args.diff is None:
|
|
||||||
args.diff = bool(creds_data.get("diff", False))
|
|
||||||
|
|
||||||
if not args.dry and (not args.username or not args.password):
|
if not args.dry and (not args.username or not args.password):
|
||||||
p.error(
|
p.error(
|
||||||
"Username and password are required. "
|
"Username and password are required. "
|
||||||
|
|||||||
+1
-5
@@ -31,15 +31,11 @@ def _accept_loop(
|
|||||||
records = in_mem
|
records = in_mem
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if store_ref.get("diff_mode"):
|
|
||||||
prev: dict = {}
|
prev: dict = {}
|
||||||
for record in records:
|
for record in records:
|
||||||
diff = jay_diff_full(prev, record, combine_upd_add=True)
|
diff = jay_diff_full(prev, record, combine_upd_add=True)
|
||||||
prev = record
|
prev = record
|
||||||
conn.sendall((json.dumps(diff, default=str) + "\n").encode())
|
conn.sendall((json.dumps(diff, default=str) + "\n").encode())
|
||||||
else:
|
|
||||||
for record in records:
|
|
||||||
conn.sendall((json.dumps(record, default=str) + "\n").encode())
|
|
||||||
if records:
|
if records:
|
||||||
log.info("Sent %d record(s) to %s", len(records), addr)
|
log.info("Sent %d record(s) to %s", len(records), addr)
|
||||||
except OSError:
|
except OSError:
|
||||||
@@ -73,7 +69,7 @@ def start_push_server(host: str, port: int) -> tuple:
|
|||||||
server_sock.listen()
|
server_sock.listen()
|
||||||
clients: list = []
|
clients: list = []
|
||||||
lock = threading.Lock()
|
lock = threading.Lock()
|
||||||
store_ref: dict = {"current": None, "preloaded": [], "diff_mode": False}
|
store_ref: dict = {"current": None, "preloaded": []}
|
||||||
t = threading.Thread(
|
t = threading.Thread(
|
||||||
target=_accept_loop,
|
target=_accept_loop,
|
||||||
args=(server_sock, clients, lock, store_ref),
|
args=(server_sock, clients, lock, store_ref),
|
||||||
|
|||||||
Reference in New Issue
Block a user