from __future__ import annotations import json import os import tempfile from pathlib import Path from datetime import date from typing import Any import httpx from dotenv import load_dotenv from fastapi import Depends, FastAPI, HTTPException, Query, Request from fastapi.responses import JSONResponse, RedirectResponse from fastapi.staticfiles import StaticFiles from hockey_data import ( HockeyDataService, HockeyDatabase, HockeySettingsStore, create_hockey_router, ) from hockey_data.router import create_hockey_vmix_router from hockey_data.vmix_portable import ( HockeyVmixPortable, configure_hockey_vmix_settings, create_hockey_vmix_settings_router, ) from hockey_data.async_work import run_db from hockey_data.auth_bridge import HockeyAuthDependencies, create_auth_adapter from hockey_data.auth_router import create_hockey_auth_router from hockey_data.agent_bridge import VmixAgentHub, create_hockey_agent_router from hockey_data.idle_matches import IdleMatchReaper from ui_builder import install_ui_builder from khl_site.khl_data_center import APP as khl_site_app, configure_storage, sync_storage from hockey_data.khl_career import KhlCareerService BASE_DIR = Path(__file__).resolve().parent BUILD_VERSION = "2026.09.29.3" # compatibility: BUILD_VERSION = "2026.09.24.2" # compatibility: BUILD_VERSION = "2026.09.23.8" # compatibility: BUILD_VERSION = "2026.09.23.7" # compatibility: BUILD_VERSION = "2026.09.23.6" # compatibility: BUILD_VERSION = "2026.08.26.1" # compatibility: BUILD_VERSION = "2026.08.25.1" # compatibility: BUILD_VERSION = "2026.08.24.15" # compatibility: BUILD_VERSION = "2026.08.24.14" # compatibility: BUILD_VERSION = "2026.08.24.13" # compatibility: BUILD_VERSION = "2026.08.24.8" # compatibility: BUILD_VERSION = "2026.08.24.7" # compatibility: BUILD_VERSION = "2026.08.24.6" # compatibility: BUILD_VERSION = "2026.08.24.5" # compatibility: BUILD_VERSION = "2026.08.24.4" # compatibility: BUILD_VERSION = "2026.08.24.2" # compatibility: BUILD_VERSION = "2026.08.24.1" # compatibility: BUILD_VERSION = "2026.08.21.1" # compatibility: BUILD_VERSION = "2026.08.20.17" # compatibility: BUILD_VERSION = "2026.08.20.16" # compatibility: BUILD_VERSION = "2026.08.20.15" # compatibility: BUILD_VERSION = "2026.08.20.14" # compatibility: BUILD_VERSION = "2026.08.20.13" # compatibility: BUILD_VERSION = "2026.08.19.28" # compatibility: BUILD_VERSION = "2026.08.19.24" # compatibility: BUILD_VERSION = "2026.08.19.22" load_dotenv(BASE_DIR / ".env.local", override=False) load_dotenv(BASE_DIR / ".env", override=False) # Production can keep secrets outside the Git working tree. systemd already # injects EnvironmentFile=/mnt/khl/.env, but this fallback also makes a manual # `python -m uvicorn app:app` launch read the same file. Existing process # environment variables always win because override=False. _external_env = Path(os.getenv("HOCKEY_ENV_FILE", "/mnt/khl/.env")) if _external_env.is_file(): load_dotenv(_external_env, override=False) VMIX_API_URL = os.getenv("VMIX_API_URL", "http://127.0.0.1:8088/api/") configure_hockey_vmix_settings(BASE_DIR) app = FastAPI(title=f"Хоккейная панель управления / Stat2TV — build {BUILD_VERSION}") print(f"[Hockey Build] {BUILD_VERSION}") @app.get("/api/health", include_in_schema=False) async def hockey_public_health() -> dict[str, Any]: """Unauthenticated liveness probe for systemd/deploy scripts.""" return {"ok": True, "build": BUILD_VERSION} hockey_settings = HockeySettingsStore(BASE_DIR / "settings") hockey_database = HockeyDatabase(BASE_DIR) hockey_career = KhlCareerService(hockey_database) configure_storage(hockey_career.import_store) hockey_auth = HockeyAuthDependencies(create_auth_adapter(hockey_database)) hockey_agent_hub = VmixAgentHub(hockey_database, settings=hockey_settings) hockey_idle_matches = IdleMatchReaper(hockey_database, hockey_agent_hub) PLAYER_PANEL_BUTTONS_PATH = BASE_DIR / "settings" / "hockey_player_panel_buttons.json" def _normalise_player_panel_title_buttons(payload: Any) -> dict[str, Any]: """Normalise operator-configured Preview buttons for player-selection panels. This deliberately lives in a standalone JSON file rather than app.js / the main UI Builder document. A studio can therefore change title assignments without editing application code or rebuilding the whole layout. """ source = payload if isinstance(payload, dict) else {} rows = source.get("buttons") if isinstance(source.get("buttons"), list) else [] result: list[dict[str, Any]] = [] used_ids: set[str] = set() for index, raw in enumerate(rows[:128]): if not isinstance(raw, dict): continue panel_id = str(raw.get("panel_id") or "").strip()[:64] label = str(raw.get("label") or "").strip()[:80] input_ref = str(raw.get("input") or raw.get("input_ref") or "").strip()[:300] if not panel_id or not label: continue button_id = str(raw.get("id") or f"panel_title_{index + 1}").strip() button_id = "".join(ch if ch.isascii() and (ch.isalnum() or ch in "_-") else "_" for ch in button_id)[:64].strip("_") if not button_id: button_id = f"panel_title_{index + 1}" base_id = button_id suffix = 2 while button_id in used_ids: ending = f"_{suffix}" button_id = f"{base_id[:64 - len(ending)]}{ending}" suffix += 1 used_ids.add(button_id) try: sort_order = int(raw.get("sort_order", index * 10)) except (TypeError, ValueError): sort_order = index * 10 result.append({ "id": button_id, "panel_id": panel_id, "label": label, "description": str(raw.get("description") or "").strip()[:300], "icon": str(raw.get("icon") or "▶").strip()[:32] or "▶", "icon_only": raw.get("icon_only") is True, "input": input_ref, "enabled": raw.get("enabled") is not False, "sort_order": max(0, min(100000, sort_order)), }) result.sort(key=lambda item: (str(item["panel_id"]), int(item["sort_order"]), str(item["label"]).casefold())) return {"version": 1, "buttons": result} def _load_player_panel_title_buttons() -> dict[str, Any]: PLAYER_PANEL_BUTTONS_PATH.parent.mkdir(parents=True, exist_ok=True) if not PLAYER_PANEL_BUTTONS_PATH.exists(): payload = {"version": 1, "buttons": []} try: with PLAYER_PANEL_BUTTONS_PATH.open("x", encoding="utf-8") as target: target.write(json.dumps(payload, ensure_ascii=False, indent=2) + "\n") except FileExistsError: return _load_player_panel_title_buttons() return payload try: raw = json.loads(PLAYER_PANEL_BUTTONS_PATH.read_text(encoding="utf-8")) except (OSError, ValueError, TypeError) as error: raise HTTPException(status_code=500, detail="Не удалось прочитать настройки кнопок титров. Проверьте settings/hockey_player_panel_buttons.json.") from error return _normalise_player_panel_title_buttons(raw) def _save_player_panel_title_buttons(payload: Any) -> dict[str, Any]: normalised = _normalise_player_panel_title_buttons(payload) PLAYER_PANEL_BUTTONS_PATH.parent.mkdir(parents=True, exist_ok=True) with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=PLAYER_PANEL_BUTTONS_PATH.parent, prefix=".panel-buttons-", suffix=".tmp", delete=False) as target: tmp_path = Path(target.name) target.write(json.dumps(normalised, ensure_ascii=False, indent=2) + "\n") try: tmp_path.replace(PLAYER_PANEL_BUTTONS_PATH) finally: tmp_path.unlink(missing_ok=True) return normalised @app.on_event("startup") async def hockey_mapping_auto_refresh_startup() -> None: await run_db(sync_storage) # BUILD151: isolated per-second schedule polling writes only to schedule_live. await run_db(hockey_service.ensure_schedule_live_table) await run_db(hockey_agent_hub.mapping_data.ensure_defaults) hockey_agent_hub.start_auto_refresh() hockey_idle_matches.start() # BUILD143: schedules, standings and authoritative per-game scores refresh # on the server even when no browser/operator is open. hockey_service.start_schedule_auto_refresh(interval_seconds=3600) @app.on_event("shutdown") async def hockey_mapping_auto_refresh_shutdown() -> None: await hockey_idle_matches.stop() await hockey_agent_hub.stop_auto_refresh() await hockey_service.stop_schedule_auto_refresh() @app.middleware("http") async def hockey_runtime_auth(request: Request, call_next): """Protect runtime/editor and prevent stale operator-interface assets.""" path = request.url.path editor_path = ( path == "/editor" or path.startswith("/api/ui-builder/editor/") or path.startswith("/api/ui-builder/auth/") ) khl_admin_path = path.startswith("/khl-site") protected_path = path == "/" or khl_admin_path or editor_path if protected_path: try: user = await hockey_auth.optional_user(request) except HTTPException as error: return JSONResponse({"detail": error.detail}, status_code=error.status_code) if user is None: if path in {"/", "/editor"}: return RedirectResponse("/login?reason=expired", status_code=303) return JSONResponse({"detail": "Требуется вход"}, status_code=401) if (khl_admin_path or editor_path) and not user.is_admin: if path == "/editor": return RedirectResponse("/", status_code=303) return JSONResponse({"detail": "Раздел доступен только администратору"}, status_code=403) request.state.wfl_user = user response = await call_next(request) if ( path in {"/", "/editor"} or path.startswith("/ui-builder-assets/") or path.startswith("/hockey-assets/") or path.startswith("/api/ui-builder/runtime/") or path.startswith("/khl-site") ): response.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0" response.headers["Pragma"] = "no-cache" response.headers["Expires"] = "0" return response hockey_service = HockeyDataService( base_dir=BASE_DIR, database=hockey_database, settings=hockey_settings, ) hockey_vmix_portable = HockeyVmixPortable(hockey_service) HOCKEY_BOOTSTRAP = hockey_service.bootstrap() _backfill = HOCKEY_BOOTSTRAP.get("normalized_backfill", {}) if isinstance(HOCKEY_BOOTSTRAP, dict) else {} if any(int(value or 0) for value in _backfill.values() if isinstance(value, (int, float))): print( "[Hockey DB] normalized backfill updated " + " ".join(f"{key}={value}" for key, value in _backfill.items()) ) HOCKEY_SCHEMA_STATUS = hockey_database.schema_status() print( "[Hockey DB] schema ok=" f"{HOCKEY_SCHEMA_STATUS.get('ok')} " f"repaired={len(HOCKEY_SCHEMA_STATUS.get('repaired', []))} " f"rebuilt={HOCKEY_SCHEMA_STATUS.get('rebuilt', False)}" ) app.mount("/khl-site", khl_site_app) app.mount( "/hockey-assets", StaticFiles(directory=BASE_DIR / "hockey_data" / "static"), name="hockey-assets", ) def hockey_stat2tv_data() -> dict[str, Any]: return hockey_service.data_for_builder() def vmix_demo_data() -> dict[str, Any]: home_players = [ {"id": f"H{i:02d}", "number": number, "name": name, "position": position} for i, (number, name, position) in enumerate([ (1, "Максим Лебедев", "ВР"), (30, "Илья Зорин", "ВР"), (2, "Алексей Титов", "ЗАЩ"), (4, "Егор Савельев", "ЗАЩ"), (7, "Антон Крылов", "НАП"), (9, "Даниил Орехов", "НАП"), (10, "Никита Сергеев", "НАП"), (11, "Михаил Сафонов", "НАП"), (13, "Владимир Комаров", "ЗАЩ"), (15, "Роман Беляев", "НАП"), (17, "Андрей Иванов", "НАП"), (19, "Степан Волков", "НАП"), (21, "Кирилл Панов", "ЗАЩ"), (23, "Олег Миронов", "НАП"), (25, "Арсений Белов", "НАП"), (27, "Матвей Фролов", "ЗАЩ"), (29, "Глеб Мельников", "НАП"), (44, "Павел Кузнецов", "ЗАЩ"), (71, "Иван Ларионов", "НАП"), (77, "Семён Жуков", "НАП"), (81, "Фёдор Егоров", "НАП"), (91, "Артём Петров", "НАП"), ], start=1) ] away_players = [ {"id": f"A{i:02d}", "number": number, "name": name, "position": position} for i, (number, name, position) in enumerate([ (20, "Лев Корнеев", "ВР"), (35, "Тимур Котов", "ВР"), (3, "Ярослав Сорокин", "ЗАЩ"), (5, "Денис Макаров", "ЗАЩ"), (8, "Георгий Гусев", "НАП"), (12, "Виктор Назаров", "НАП"), (14, "Руслан Власов", "НАП"), (16, "Борис Тарасов", "ЗАЩ"), (18, "Марк Селезнёв", "НАП"), (22, "Станислав Никифоров", "ЗАЩ"), (24, "Игорь Соколов", "НАП"), (28, "Вадим Фомин", "НАП"), (32, "Пётр Громов", "ЗАЩ"), (37, "Леонид Васильев", "НАП"), (41, "Константин Ершов", "ЗАЩ"), (55, "Захар Захаров", "ЗАЩ"), (61, "Александр Осипов", "НАП"), (68, "Сергей Орлов", "НАП"), (73, "Дмитрий Богданов", "НАП"), (84, "Валерий Маслов", "НАП"), (88, "Николай Голубев", "НАП"), (97, "Анатолий Чернов", "НАП"), ], start=1) ] return { "vmix": { "api_url": VMIX_API_URL, "function": "OverlayInput4In", "input": 2, }, "hockey": { "home": {"name": "СЕВЕР", "players": home_players}, "away": {"name": "ВОСТОК", "players": away_players}, }, } @app.get("/api/vmix/command", dependencies=[Depends(hockey_auth.require_user)]) async def vmix_command() -> dict[str, Any]: """Legacy server-wide command proxy is deliberately disabled.""" raise HTTPException(status_code=410, detail="Используйте команды выбранного Agent через /api/hockey/vmix/sequence") install_ui_builder( app, build_version=BUILD_VERSION, settings_dir=BASE_DIR / "settings", data_providers={ "hockey_stat2tv": hockey_stat2tv_data, "vmix_demo": vmix_demo_data, }, source_labels={ "hockey_stat2tv": "Stat2TV — хоккей", "vmix_demo": "vMix — тестовые данные", }, editor_url="/editor", runtime_url="/", custom_early_script_urls=[f"/hockey-assets/match-access.js?v={BUILD_VERSION}"], custom_script_urls=[ f"/hockey-assets/session-activity.js?v={BUILD_VERSION}", f"/hockey-assets/tournament-menu.js?v={BUILD_VERSION}", f"/hockey-assets/admin-directories.js?v={BUILD_VERSION}", "/hockey-assets/match-status-header.js", f"/hockey-assets/agent-devices.js?v={BUILD_VERSION}", f"/hockey-assets/agent-diagnostics.js?v={BUILD_VERSION}", ], custom_style_urls=[ f"/hockey-assets/match-access.css?v={BUILD_VERSION}", "/hockey-assets/tournament-menu.css", f"/hockey-assets/admin-directories.css?v={BUILD_VERSION}", "/hockey-assets/match-status-header.css", f"/hockey-assets/agent-devices.css?v={BUILD_VERSION}", f"/hockey-assets/agent-diagnostics.css?v={BUILD_VERSION}", ], ) @app.post( "/api/hockey/ui/prematch-buttons", dependencies=[Depends(hockey_auth.require_admin)], tags=["Hockey UI"], ) async def save_hockey_prematch_buttons(payload: dict[str, Any]) -> dict[str, Any]: """Save operator prematch buttons without replacing the rest of UI Builder config.""" buttons = payload.get("prematch_buttons") groups = payload.get("prematch_groups", []) selectors = payload.get("quick_panel_selectors", []) player_panels = payload.get("player_selection_panels", []) if not isinstance(buttons, list): raise HTTPException(status_code=422, detail="prematch_buttons must be a list") if not isinstance(groups, list): raise HTTPException(status_code=422, detail="prematch_groups must be a list") if not isinstance(selectors, list): raise HTTPException(status_code=422, detail="quick_panel_selectors must be a list") if not isinstance(player_panels, list): raise HTTPException(status_code=422, detail="player_selection_panels must be a list") draft_manager = app.state.ui_builder_draft_manager published_manager = app.state.ui_builder_published_manager draft_config = draft_manager.load() draft_config["prematch_groups"] = groups draft_config["prematch_buttons"] = buttons draft_config["quick_panel_selectors"] = selectors draft_config["player_selection_panels"] = player_panels saved_draft = draft_manager.save(draft_config) published_config = published_manager.load() published_config["prematch_groups"] = saved_draft.get("prematch_groups", []) published_config["prematch_buttons"] = saved_draft.get("prematch_buttons", []) published_config["quick_panel_selectors"] = saved_draft.get("quick_panel_selectors", []) published_config["player_selection_panels"] = saved_draft.get("player_selection_panels", []) saved_published = published_manager.save(published_config, create_backup=True) return { "ok": True, "prematch_groups": saved_published.get("prematch_groups", []), "prematch_buttons": saved_published.get("prematch_buttons", []), "quick_panel_selectors": saved_published.get("quick_panel_selectors", []), "player_selection_panels": saved_published.get("player_selection_panels", []), "config": saved_published, } @app.get( "/api/hockey/ui/player-panel-buttons", dependencies=[Depends(hockey_auth.require_user)], tags=["Hockey UI"], ) async def get_hockey_player_panel_buttons() -> dict[str, Any]: return _load_player_panel_title_buttons() @app.post( "/api/hockey/ui/player-panel-buttons", dependencies=[Depends(hockey_auth.require_admin)], tags=["Hockey UI"], ) async def save_hockey_player_panel_buttons(payload: dict[str, Any]) -> dict[str, Any]: saved = _save_player_panel_title_buttons(payload) return {"ok": True, **saved} @app.post( "/api/hockey/ui/triggers", dependencies=[Depends(hockey_auth.require_admin)], tags=["Hockey UI"], ) async def save_hockey_ui_triggers(payload: dict[str, Any]) -> dict[str, Any]: """Save only UI triggers for an authenticated operator, preserving all other config.""" triggers = payload.get("triggers") if not isinstance(triggers, list): raise HTTPException(status_code=422, detail="triggers must be a list") draft_manager = app.state.ui_builder_draft_manager published_manager = app.state.ui_builder_published_manager draft_config = draft_manager.load() draft_config["triggers"] = triggers saved_draft = draft_manager.save(draft_config) published_config = published_manager.load() published_config["triggers"] = saved_draft.get("triggers", []) saved_published = published_manager.save(published_config, create_backup=True) return { "ok": True, "triggers": saved_published.get("triggers", []), } async def _require_ui_builder_editor(request: Request) -> None: await app.state.ui_builder_auth.require_editor(request) @app.get( "/api/ui-builder/editor/vmix-inventory", dependencies=[Depends(_require_ui_builder_editor)], tags=["UI Builder"], ) async def ui_builder_vmix_inventory() -> dict[str, Any]: return await hockey_agent_hub.latest_vmix_inventory() app.include_router( create_hockey_router( hockey_service, auth_dependency=hockey_auth.require_user, admin_dependency=hockey_auth.require_admin, agent_hub=hockey_agent_hub, ) ) @app.post( "/api/hockey/tournaments/{external_id}/games/online-sync", dependencies=[Depends(hockey_auth.require_user)], tags=["Hockey"], ) async def refresh_tournament_schedule_online( external_id: str, on_date: date = Query(...), language: str = Query("ru", pattern="^(ru|en)$"), mode: str = Query("seed", pattern="^(seed|live|discover)$"), ) -> dict[str, Any]: """Refresh the isolated schedule_live snapshot from per-game JSON. ``seed`` checks every match once, ``live`` checks only rows currently live, and ``discover`` checks non-finished rows so newly-started games are found. The base hockey_games table is never modified by this endpoint. """ if hockey_service.tournament(external_id, language=language) is None: raise HTTPException(status_code=404, detail="Турнир не найден") try: return await hockey_service.refresh_schedule_live_from_match_json( tournament_external_id=external_id, on_date=on_date, language=language, mode=mode, ) except ValueError as error: raise HTTPException(status_code=400, detail=str(error)) from error except Exception as error: raise HTTPException(status_code=502, detail=str(error)) from error @app.get( "/api/hockey/tournaments/{external_id}/games/live-schedule", dependencies=[Depends(hockey_auth.require_user)], tags=["Hockey"], ) async def tournament_live_schedule( external_id: str, on_date: date = Query(...), language: str = Query("ru", pattern="^(ru|en)$"), ) -> dict[str, Any]: """Read score/status from schedule_live without modifying hockey_games.""" if hockey_service.tournament(external_id, language=language) is None: raise HTTPException(status_code=404, detail="Турнир не найден") return hockey_service.schedule_live( tournament_external_id=external_id, on_date=on_date, language=language, ) @app.post( "/api/hockey/tournaments/{external_id}/standings/force-sync", dependencies=[Depends(hockey_auth.require_user)], tags=["Hockey"], ) async def force_sync_tournament_standings( external_id: str, language: str = Query("ru", pattern="^(ru|en)$"), game_id: str = Query("", max_length=64), ) -> dict[str, Any]: """Force a fresh Stat2TV standings download, bypassing the in-memory resource cache.""" if hockey_service.tournament(external_id, language=language) is None: raise HTTPException(status_code=404, detail="Турнир не найден") try: return await hockey_service.sync_standings( external_id, language=language, game_external_id=game_id, force=True, ) except ValueError as error: raise HTTPException(status_code=400, detail=str(error)) from error except Exception as error: raise HTTPException(status_code=502, detail=str(error)) from error app.include_router(create_hockey_auth_router(hockey_auth)) app.include_router( create_hockey_agent_router( hockey_agent_hub, auth_dependency=hockey_auth.require_user, admin_dependency=hockey_auth.require_admin, ) ) app.include_router( create_hockey_vmix_settings_router( hockey_vmix_portable, admin_dependency=hockey_auth.require_admin, ) ) app.include_router(create_hockey_vmix_router(hockey_service, hockey_vmix_portable)) app.state.hockey_service = hockey_service app.state.hockey_database = hockey_database app.state.hockey_settings = hockey_settings app.state.hockey_auth = hockey_auth app.state.hockey_vmix_portable = hockey_vmix_portable app.state.hockey_agent_hub = hockey_agent_hub @app.get("/runtime", include_in_schema=False) async def legacy_runtime_redirect() -> RedirectResponse: return RedirectResponse("/", status_code=302) @app.get("/ui-builder", include_in_schema=False) async def legacy_editor_redirect() -> RedirectResponse: # Legacy/bookmarked UI Builder URLs should always land in the operator runtime. # The editor is opened explicitly from the runtime toolbar after PIN auth. return RedirectResponse("/", status_code=302)