On-demand per-fund reports for standalone Symbol-box funds

Any symbol that stands alone in the Symbol box (not a comma-joined
portfolio component) now gets a full report, built on demand and cached
in reports/report_data_adhoc.json. Rendered at the top of the Fund Lab
Summary as group 'A'; pointer at the top of the page. Rendering logic
extracted into render_fund_report() shared by pre-built and on-demand
entries. ~0.3-0.9 s per new fund after one-time panel warmup; instant
afterwards (memory + disk cache).
This commit is contained in:
gmp
2026-08-30 18:35:42 -04:00
parent e28f8fcf85
commit d2ff607c79
4 changed files with 166 additions and 77 deletions
+14
View File
@@ -1149,3 +1149,17 @@ first point before handing it to the widget (per-series 1.0 start, as
the widget expects). Also removed the "expand all" checkbox (24 expanded
sections made the page unusable and hid the sections below) and added a
"rest of the Fund Lab continues below" marker.
**On-demand reports for any Symbol-box fund.** `reportdata.ensure_adhoc(syms)`
builds the same full report entry (narrative, chart, period table, drivers,
reference, tax, peers) for ARBITRARY symbols, cached in memory + on disk
(reports/report_data_adhoc.json, gitignored) so repeat loads are instant and
survive restarts; build time ~0.3-0.9 s per new fund after the one-time
~30-60 s driver-panel warmup. The app triggers it for symbols that stand
ALONE in the Symbol box (a parsed item with a single component) - portfolio
components (comma-joined) are explicitly excluded, per user request. Ad-hoc
entries render at the top of the Fund Lab Summary as group "A" (before the
pre-built 24), the per-fund rendering was extracted into render_fund_report()
shared by both, and a pointer at the top of the page tells the user where
the generated reports are. ETFs with no name on file render as "A01 · TLT"
(no duplicated name).
+37
View File
@@ -183,6 +183,10 @@ def _tax(sym: str) -> str:
def _peers(sym: str, fr: dict) -> dict:
# no return history: a zero loading vector would park the fund in the
# cash cluster - no peer table for a fund with no measured returns
if _r.price(sym) is None:
return {}
if sym in _r.MEMBER:
cid, clabel, cn = _r.MEMBER[sym]
members = _r.KMEANS["clusters"][cid]["syms"]
@@ -272,6 +276,39 @@ def _peers(sym: str, fr: dict) -> dict:
"rows": rows, "proscons": pc}
ADHOC_FILE = HERE.parent / "reports" / "report_data_adhoc.json"
_ADHOC_MEM: dict = None # lazy-loaded disk cache
def _adhoc_store() -> dict:
global _ADHOC_MEM
if _ADHOC_MEM is None:
try:
_ADHOC_MEM = json.loads(ADHOC_FILE.read_text())
except Exception:
_ADHOC_MEM = {"funds": {}}
return _ADHOC_MEM
def ensure_adhoc(syms: list[str]) -> dict[str, dict]:
"""Build report entries for ARBITRARY funds (any symbol the user puts
in the portfolio box), cached in memory + on disk so repeat loads are
instant and survive server restarts. The one-time ~30-60 s driver-panel
build happens on the first call in a fresh process."""
store = _adhoc_store()
funds = store["funds"]
missing = [s for s in syms
if s not in funds
and _r.price(s) is not None]
if missing:
for s in missing:
funds[s] = build_fund(s, "adhoc",
order=len(funds) + 1)
ADHOC_FILE.parent.mkdir(exist_ok=True)
ADHOC_FILE.write_text(json.dumps(store))
return {s: funds[s] for s in syms if s in funds}
def build_fund(sym: str, group: str, order: int) -> dict:
t0 = time.time()
fr = _r.factor_row(sym)