Handle disconnected dashboard clients and batch EOD quote writes

This commit is contained in:
PrimeThai Automation committed 2026-10-07 07:07:33 +07:00
1 parent 16e04271b3
commit 7a8ee40fbb
4 files changed
+151 -9

No files matched your search

+11 -6
View File
@@ -102,12 +102,17 @@ def read_latest_eod_quotes(archive_path: str | Path) -> tuple[str, dict[str, flo
def update_price_marks(as_of: str, quotes: dict[str, float], *,
database: str | Path = DEFAULT_BOOK) -> None:
with closing(_connect(database)) as connection:
connection.executemany("""
INSERT INTO price_marks(symbol, as_of, close) VALUES (?, ?, ?)
ON CONFLICT(symbol) DO UPDATE SET as_of=excluded.as_of, close=excluded.close
WHERE excluded.as_of >= price_marks.as_of
""", [(symbol.upper(), as_of, float(close)) for symbol, close in quotes.items()
if _SYMBOL.fullmatch(symbol.upper()) and math.isfinite(float(close)) and float(close) > 0])
# The connection uses autocommit. Group the market-wide update so a
# dashboard read does not incur a separate disk commit for each ticker.
with connection:
connection.execute("BEGIN IMMEDIATE")
connection.executemany("""
INSERT INTO price_marks(symbol, as_of, close) VALUES (?, ?, ?)
ON CONFLICT(symbol) DO UPDATE SET as_of=excluded.as_of, close=excluded.close
WHERE excluded.as_of > price_marks.as_of
OR (excluded.as_of = price_marks.as_of AND excluded.close != price_marks.close)
""", [(symbol.upper(), as_of, float(close)) for symbol, close in quotes.items()
if _SYMBOL.fullmatch(symbol.upper()) and math.isfinite(float(close)) and float(close) > 0])
def add_cash_event(kind: str, amount: float, note: str = "", *,
+41 -3
View File
@@ -6,6 +6,7 @@ from datetime import datetime, timedelta, timezone
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from http.cookies import SimpleCookie
import csv
import errno
import hmac
import json
import os
@@ -66,6 +67,7 @@ _LOGIN_LOCK = threading.Lock()
_LOGIN_FAILURES: dict[str, tuple[int, float, float]] = {}
_LOGIN_MAX_FAILURES = 5
_LOGIN_WINDOW_SECONDS = 15 * 60
_DISCONNECT_ERRNOS = {errno.EPIPE, errno.ECONNRESET, errno.ECONNABORTED, errno.ETIMEDOUT}
_STATE: dict[str, object] = {
"running": False,
"stage": "idle",
@@ -705,6 +707,42 @@ def _handler_factory(archives: tuple[Path, ...], report_dir: Path,
start: str, initial_equity: float,
auth: AuthConfig) -> type[BaseHTTPRequestHandler]:
class DashboardHandler(BaseHTTPRequestHandler):
def handle_one_request(self) -> None:
self._request_started_at = time.perf_counter()
super().handle_one_request()
def _request_log(self, event: str, **details: object) -> None:
started = getattr(self, "_request_started_at", time.perf_counter())
payload = {
"client": self.client_address[0],
"method": self.command,
"path": self._request_path(),
"elapsed_ms": round((time.perf_counter() - started) * 1000, 1),
**details,
}
print("[webui] " + event + " " + json.dumps(payload, ensure_ascii=False), flush=True)
def _finish_response(self, status: int, body: bytes = b"") -> None:
try:
self.end_headers()
if body:
self.wfile.write(body)
except OSError as exc:
if (not isinstance(exc, (BrokenPipeError, ConnectionResetError,
ConnectionAbortedError, TimeoutError))
and exc.errno not in _DISCONNECT_ERRNOS):
raise
self.close_connection = True
self._request_log(
"client_disconnected", intended_status=status,
response_bytes=len(body), error_type=type(exc).__name__,
error=str(exc), errno=exc.errno,
)
return
started = getattr(self, "_request_started_at", time.perf_counter())
if time.perf_counter() - started >= 2:
self._request_log("slow_response", status=status, response_bytes=len(body))
server_version = "PrimeThaiPortfolio/1.0"
def _request_path(self) -> str:
@@ -796,8 +834,7 @@ def _handler_factory(archives: tuple[Path, ...], report_dir: Path,
)
for name, value in (headers or {}).items():
self.send_header(name, value)
self.end_headers()
self.wfile.write(body)
self._finish_response(status, body)
def _redirect(self, location: str, *, cookie: str | None = None) -> None:
self.send_response(303)
@@ -807,7 +844,7 @@ def _handler_factory(archives: tuple[Path, ...], report_dir: Path,
self.send_header("X-Content-Type-Options", "nosniff")
if cookie:
self.send_header("Set-Cookie", cookie)
self.end_headers()
self._finish_response(303)
def _send_login_page(self) -> None:
page = Path(__file__).with_name("login.html")
@@ -893,6 +930,7 @@ def _handler_factory(archives: tuple[Path, ...], report_dir: Path,
body = json.dumps(payload, ensure_ascii=False, default=_json_default).encode("utf-8")
self._send(200, "application/json; charset=utf-8", body)
except Exception as exc:
self._request_log("portfolio_failed", error_type=type(exc).__name__, error=str(exc))
body = json.dumps({"error": f"{type(exc).__name__}: {exc}"}, ensure_ascii=False).encode("utf-8")
self._send(500, "application/json; charset=utf-8", body)
return
+21
View File
@@ -1,5 +1,7 @@
import unittest
import os
import sqlite3
from contextlib import closing
from datetime import datetime, timedelta
from pathlib import Path
@@ -97,5 +99,24 @@ class PortfolioValuationDateTests(unittest.TestCase):
database.unlink(missing_ok=True)
class PriceMarkBatchTests(unittest.TestCase):
def test_batch_preserves_newer_quotes_and_accepts_same_day_corrections(self):
database = Path(__file__).with_name(f".portfolio_batch_test_{os.getpid()}.sqlite3")
try:
update_price_marks("2026-10-06", {"AAA": 10, "BBB": 20}, database=database)
update_price_marks("2026-10-05", {"AAA": 9}, database=database)
update_price_marks("2026-10-06", {"AAA": 11}, database=database)
with closing(sqlite3.connect(database)) as connection:
rows = connection.execute("SELECT symbol, as_of, close FROM price_marks ORDER BY symbol").fetchall()
self.assertEqual(rows, [("AAA", "2026-10-06", 11), ("BBB", "2026-10-06", 20)])
# Bad input after a valid row must leave the previous snapshot intact.
with self.assertRaises(ValueError):
update_price_marks("2026-10-07", {"AAA": 12, "BBB": "invalid"}, database=database)
with closing(sqlite3.connect(database)) as connection:
self.assertEqual(connection.execute("SELECT close FROM price_marks WHERE symbol='AAA'").fetchone()[0], 11)
finally:
database.unlink(missing_ok=True)
if __name__ == "__main__":
unittest.main()
+78
View File
@@ -0,0 +1,78 @@
"""Regression coverage for clients disconnecting while responses are sent."""
from email.message import Message
import errno
import io
from pathlib import Path
import time
import unittest
from unittest.mock import Mock, patch
from primethai.auth import AuthConfig
from primethai.webui import _handler_factory
class DashboardDisconnectTests(unittest.TestCase):
def make_handler(self, writer):
handler_type = _handler_factory(
(), Path("reports"), "2026-01-01", 10_000,
AuthConfig("test", "test", b"test-secret"),
)
handler = handler_type.__new__(handler_type)
handler.client_address = ("127.0.0.1", 12345)
handler.command = "GET"
handler.path = "/api/portfolio"
handler.requestline = "GET /api/portfolio HTTP/1.1"
handler.request_version = "HTTP/1.1"
handler.headers = Message()
handler.wfile = writer
handler.close_connection = False
handler._request_started_at = time.perf_counter()
handler._authenticated = Mock(return_value=True)
handler.log_message = Mock()
handler._request_log = Mock()
handler.send_response = Mock(wraps=handler.send_response)
return handler
def test_portfolio_disconnect_does_not_attempt_a_second_response(self):
for error_type in (BrokenPipeError, ConnectionResetError, ConnectionAbortedError):
for failing_write in (1, 2): # Header flush or response body.
with self.subTest(error=error_type, failing_write=failing_write):
writer = Mock()
writer.write.side_effect = (
[error_type(errno.EPIPE, "connection closed")]
if failing_write == 1
else [100, error_type(errno.ECONNRESET, "connection closed")]
)
handler = self.make_handler(writer)
with patch("primethai.webui._portfolio_data", return_value={"cash": 100}):
handler.do_GET()
handler.send_response.assert_called_once_with(200)
self.assertTrue(handler.close_connection)
self.assertEqual(handler._request_log.call_args.args, ("client_disconnected",))
def test_redirect_handles_closed_connection(self):
writer = Mock()
writer.write.side_effect = BrokenPipeError(errno.EPIPE, "connection closed")
handler = self.make_handler(writer)
handler._redirect("/login")
handler.send_response.assert_called_once_with(303)
self.assertTrue(handler.close_connection)
def test_success_and_actual_portfolio_failure_remain_distinct(self):
for failed in (False, True):
with self.subTest(failed=failed):
handler = self.make_handler(io.BytesIO())
with patch("primethai.webui._portfolio_data", return_value={"cash": 100},
side_effect=ValueError("invalid portfolio") if failed else None):
handler.do_GET()
handler.send_response.assert_called_once_with(500 if failed else 200)
response = handler.wfile.getvalue()
self.assertIn(b"invalid portfolio" if failed else b'"cash": 100', response)
self.assertFalse(handler.close_connection)
if failed:
self.assertEqual(handler._request_log.call_args.args, ("portfolio_failed",))
if __name__ == "__main__":
unittest.main()