""" 🇬🇧 Module: metrics.py Purpose: Provides async utilities to fetch and display portfolio metrics as a DataFrame. 🇷🇺 Модуль: metrics.py Назначение: предоставляет асинхронные функции для получения и отображения метрик портфеля в виде DataFrame. """ import pandas as pd import asyncio from services.output_api import extract_portfolio_id, fetch_metrics_async def show_metrics_table(portfolio_input: str): """Fetch portfolio metrics and return them as a DataFrame for Gradio.""" pid = extract_portfolio_id(portfolio_input) if not pid: return "❌ Invalid portfolioId format." try: df = asyncio.run(_get_metrics_df(pid)) return df except Exception as e: return f"❌ Error fetching metrics: {e}" async def _get_metrics_df(portfolio_id: str) -> pd.DataFrame: """Internal helper to asynchronously get metrics.""" metrics = await fetch_metrics_async(portfolio_id) if not metrics: raise ValueError("No metrics found for given portfolio.") df = pd.DataFrame(list(metrics.items()), columns=["Metric", "Value"]) return df