"""InsightUX report adapter for Microlink multi-page gaze heatmaps.""" from __future__ import annotations import base64 import os import re import shutil from pathlib import Path from core import analysis as _legacy_analysis load_session = _legacy_analysis.load_session session_summary = _legacy_analysis.session_summary summarize_mouse = _legacy_analysis.summarize_mouse def _generate_without_legacy_screenshots(session_dir: str | os.PathLike) -> str: session = Path(session_dir) screenshots = session / "screenshots" hidden = session / ".microlink_legacy_screenshots_hidden" moved = False try: if screenshots.exists(): if hidden.exists(): shutil.rmtree(hidden, ignore_errors=True) screenshots.rename(hidden) moved = True return _legacy_analysis.generate_report(str(session)) finally: if moved and hidden.exists(): if screenshots.exists(): shutil.rmtree(screenshots, ignore_errors=True) hidden.rename(screenshots) def _gallery_html(heatmaps: list[dict]) -> str: """Create one gallery with one exact PNG per visited URL.""" images = [] for i, item in enumerate(heatmaps, 1): path = Path(item["composite_path"]) encoded = base64.b64encode(path.read_bytes()).decode("ascii") images.append( f'Webpage {i}' ) n = len(images) return f'''
1 / {n}
''' def _inject_export_override(html: str) -> str: """Replace the old exported heatmap with the same Microlink gallery images.""" script = r'''''' pos=html.lower().rfind('') return html[:pos]+script+html[pos:] if pos>=0 else html+script def attach_microlink_heatmaps_to_report(report_path: str | os.PathLike, heatmaps: list[dict]) -> bool: """Embed all URL-scoped Microlink composites as one interactive gallery.""" report=Path(report_path) if not report.exists() or not heatmaps: return False try: for item in heatmaps: if not Path(item.get("composite_path", "")).exists(): print(f"[MICROLINK] ERROR: missing composite for {item.get('url','')}") return False html=report.read_text(encoding="utf-8") start=html.find("// ---------- Attention Heatmaps panel:") end=html.find("// ---------- Element Attention Analysis:",start+1) if start>=0 and end>start: html=html[:start]+"// ---------- Microlink interactive gallery ----------\n"+html[end:] html=re.sub(r'