
    -`i                         d Z ddlZddlmZ ddlmZ ddlmZmZ  ee	          Z
dedeeeef                  deeef         dz  fd	Zdeeeef                  fd
Zdedeeef         dz  fdZdS )zK
Utility functions that create ORCA endpoint load report response headers.
    N)Mapping)init_logger)Gaugeget_metrics_snapshotmetrics_formatnamed_metricsreturnc                    |                                  dvr"t                              dt                     dS i }dd |D             i}|                                  dk    r(d                    d |D                       }d	| |d
<   n2|                                  dk    rdt          j        |           |d
<   t                              d|           |S )a  
    Creates ORCA headers named 'endpoint-load-metrics' in the specified format
    and adds custom metrics to named_metrics.
    ORCA headers format description: https://docs.google.com/document/d/1C1ybMmDKJIVlrbOLbywhu9iRYo4rilR-cT50OTtOFTs/edit?tab=t.0
    ORCA proto https://github.com/cncf/xds/blob/main/xds/data/orca/v3/orca_load_report.proto

    Parameters:
    - metrics_format (str): The format of the header ('TEXT', 'JSON').
    - named_metrics (List[Tuple[str, float]]): List of tuples with metric names
    and their corresponding double values.

    Returns:
    - Optional[Mapping[str,str]]: A dictionary with header key as
    'endpoint-load-metrics' and values as the ORCA header strings with
    format prefix and data in  with named_metrics in.
    )textjsonzAWarning: `%s` format is not supported in the ORCA response headerNr   c                 n    i | ]2\  }}t          |t                    t          |t                    /||3S  
isinstancestrfloat.0metric_namevalues      x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/entrypoints/openai/orca_metrics.py
<dictcomp>z&create_orca_header.<locals>.<dictcomp>-   sR     
 
 
"U+s++
 1;5%0H0H

 
 
    r   z, c                 x    g | ]7\  }}t          |t                    t          |t                    /d | d| 8S )znamed_metrics.=r   r   s      r   
<listcomp>z&create_orca_header.<locals>.<listcomp>7   s`       &Kk3// 5?ue4L4L666u66  r   zTEXT zendpoint-load-metricsr   zJSON zCreated ORCA header %s)lowerloggerwarningformatjoinr   dumpsinfo)r   r   headerorca_reportnative_http_headers        r   create_orca_headerr'      s   ( %555O	
 	
 	
 tF 
 
&3
 
 
K ''!YY *7  
 
 +G2D*F*F&'' 
				6	)	)*K$*[2I2I*K*K&'
KK(&111Mr   c                     g } ddd}t                      }|D ]i}|                    |j                  }|Kt          |t                    r6|                     t          |          t          |j                  f           j| S )a  
    Collects current metrics from Prometheus and returns some of them
    in the form of the `named_metrics` list for `create_orca_header()`.

    Parameters:
    - None

    Returns:
    - list[tuple[str, float]]: List of tuples of metric names and their values.
    kv_cache_usage_percnum_requests_waiting)zvllm:kv_cache_usage_perczvllm:num_requests_waiting)	r   getnamer   r   appendr   r   r   )r   prometheus_to_orca_metricsmetricsmetric	orca_names        r   !get_named_metrics_from_prometheusr2   I   s     .0M %:%;" " #$$G H H.226;??	  Z%>%>   #i..%2E2E!FGGGr   c                 F    | sdS t                      }t          | |          S )a  
    Creates ORCA headers named 'endpoint-load-metrics' in the specified format.
    Metrics are collected from Prometheus using `get_named_metrics_from_prometheus()`.

    ORCA headers format description: https://docs.google.com/document/d/1C1ybMmDKJIVlrbOLbywhu9iRYo4rilR-cT50OTtOFTs/edit?tab=t.0
    ORCA proto https://github.com/cncf/xds/blob/main/xds/data/orca/v3/orca_load_report.proto

    Parameters:
    - metrics_format (str): The format of the header ('TEXT', 'JSON').

    Returns:
    - Optional[Mapping[str,str]]: A dictionary with header key as
    'endpoint-load-metrics' and values as the ORCA header strings with
    format prefix and data in  with named_metrics in.
    N)r2   r'   )r   r   s     r   metrics_headerr4   d   s,       t577Mnm<<<r   )__doc__r   collections.abcr   vllm.loggerr   vllm.v1.metrics.readerr   r   __name__r   r   listtupler   r'   r2   r4   r   r   r   <module>r<      s     # # # # # # # # # # # # > > > > > > > >	X		66(,U3:->(?6S#X6 6 6 6r4c5j0A+B    6=3 =738+<t+C = = = = = =r   