File size: 1,202 Bytes
b2d5b74
 
 
 
 
 
 
 
 
 
7574047
b2d5b74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
🇬🇧 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