{ "cells": [ { "cell_type": "markdown", "id": "g4-md-intro", "metadata": {}, "source": [ "# 04 \u2014 Export for the report pipeline\n", "\n", "Build the structured JSON envelope consumed by the html2docx report\n", "generation pipeline (Peitho). Output goes to `exports/export.json`." ] }, { "cell_type": "code", "execution_count": null, "id": "04-bootstrap", "metadata": {}, "outputs": [], "source": [ "import sys, pathlib # path shim: works on a fresh kernel\n", "for _p in [pathlib.Path.cwd(), *pathlib.Path.cwd().parents]:\n", " if (_p / \"pyproject.toml\").exists():\n", " sys.path.insert(0, str(_p)); break\n", "\n", "from core.bootstrap import init\n", "\n", "pal = init(study=\"202512_GenesysCX\")\n", "client, seed, config = pal.client, pal.seed_data, pal.config\n", "\n", "STUDY = pal.root / 'studies' / '202512_GenesysCX'\n", "ROOT = pal.root\n" ] }, { "cell_type": "code", "execution_count": null, "id": "g4-code-imports", "metadata": {}, "outputs": [], "source": [ "import json\n", "from datetime import datetime, timezone\n", "from core import __version__\n", "from core.calculations import apply_scenario\n", "from core.export.report_data import _compute_summary\n", "from core.notebook_helpers import display" ] }, { "cell_type": "markdown", "id": "g4-md-build", "metadata": {}, "source": [ "## Build the envelope\n", "\n", "Two paths:\n", "\n", "* **Live** \u2014 `core.export.build_report_data(client, public_id)` pulls\n", " authoritative values + summary from Athena and stamps it.\n", "* **Local** \u2014 when no `TOOL_PUBLIC_ID` is configured, build the envelope\n", " directly from `seed_data` so this notebook is always runnable." ] }, { "cell_type": "code", "execution_count": null, "id": "g4-code-build", "metadata": {}, "outputs": [], "source": [ "if config.TOOL_PUBLIC_ID:\n", " from core.export import build_report_data\n", " from core.tei_client import TEIClient\n", "\n", " client = TEIClient()\n", " envelope = build_report_data(\n", " client,\n", " config.TOOL_PUBLIC_ID,\n", " include_scenarios=True,\n", " study_slug=config.STUDY_SLUG,\n", " )\n", " source = 'live (Athena)'\n", "else:\n", " summary = _compute_summary(\n", " seed.BENEFITS, seed.COSTS, config.DISCOUNT_RATE, config.ANALYSIS_YEARS\n", " )\n", " summary['roi'] = summary.get('roi_pct')\n", " scenarios = {}\n", " for name in ('conservative', 'moderate', 'aggressive'):\n", " sb = apply_scenario(seed.BENEFITS, name, table='benefits')\n", " sc = apply_scenario(seed.COSTS, name, table='costs')\n", " scenarios[name] = _compute_summary(sb, sc, config.DISCOUNT_RATE, config.ANALYSIS_YEARS)\n", " envelope = {\n", " 'metadata': {\n", " 'study_slug': config.STUDY_SLUG,\n", " 'tool_public_id': '',\n", " 'tool_name': 'CX Cloud (Genesys + Salesforce) TEI (local seed)',\n", " 'report_name': 'Total Economic Impact\u2122 Of CX Cloud \u2014 Genesys + Salesforce',\n", " 'report_vendor': 'Genesys',\n", " 'report_version': '1.0',\n", " 'generated_at': datetime.now(timezone.utc).isoformat(),\n", " 'generator': f'palladium core {__version__} (offline)',\n", " },\n", " 'report': {\n", " 'name': 'Total Economic Impact\u2122 Of CX Cloud \u2014 Genesys + Salesforce',\n", " 'vendor': 'Genesys',\n", " 'version': '1.0',\n", " 'discount_rate': config.DISCOUNT_RATE,\n", " 'analysis_period_years': config.ANALYSIS_YEARS,\n", " },\n", " 'values': {'benefits': seed.BENEFITS, 'costs': seed.COSTS},\n", " 'summary': summary,\n", " 'scenarios': scenarios,\n", " 'assumptions': seed.ASSUMPTIONS,\n", " }\n", " source = 'offline seed data'\n", "\n", "display.alert(f'Envelope built from {source}.', 'info')" ] }, { "cell_type": "code", "execution_count": null, "id": "g4-code-write", "metadata": {}, "outputs": [], "source": [ "out_path = STUDY / 'exports' / 'export.json'\n", "out_path.parent.mkdir(parents=True, exist_ok=True)\n", "out_path.write_text(json.dumps(envelope, indent=2, default=str))\n", "size_kb = out_path.stat().st_size / 1024\n", "display.alert(f'Wrote {out_path.relative_to(ROOT)} ({size_kb:.1f} KB).', 'success')" ] }, { "cell_type": "markdown", "id": "g4-md-shape", "metadata": {}, "source": [ "## Envelope shape\n", "\n", "Top-level keys consumed by the report pipeline:" ] }, { "cell_type": "code", "execution_count": null, "id": "g4-code-shape", "metadata": {}, "outputs": [], "source": [ "for key in envelope:\n", " sub = envelope[key]\n", " if isinstance(sub, dict):\n", " print(f' {key}: dict with keys {list(sub.keys())}')\n", " elif isinstance(sub, list):\n", " print(f' {key}: list[{len(sub)}]')\n", " else:\n", " print(f' {key}: {type(sub).__name__}')" ] }, { "cell_type": "markdown", "id": "g4-md-done", "metadata": {}, "source": [ "Done. Hand off `exports/export.json` to **Peitho** / **html2docx** to produce the final Word report.\n", "\n", "**CLI alternative:** `python -m palladium export $PALLADIUM_GENESYSCX_TOOL_PUBLIC_ID -o studies/202512_GenesysCX/exports/export.json`" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.7" } }, "nbformat": 4, "nbformat_minor": 5 }