o
    `۷iG                     @   sj   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ G dd	 d	Zd
S )    )defaultdict)DictList)AutoscalerPrometheusMetrics)InstanceUtil)NodeTypeConfig)NodeType)Instancec                   @   sZ   e Zd ZdeddfddZdee deee	f fdd	Z
dee deee	f fd
dZdS )AutoscalerMetricsReporterprom_metricsreturnNc                 C   s
   || _ d S N)_prom_metrics)selfr    r   X/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/autoscaler/v2/metrics_reporter.py__init__   s   
z"AutoscalerMetricsReporter.__init__	instancesnode_type_configsc                 C   sD  i }|  D ]}ddddd||< q|D ]I}t|jr(||j d  d7  < qt|jr:||j d  d7  < q|jtjkrL||j d  d7  < q|jtjkr]||j d  d7  < q|	 D ]=\}}| j
jj| j
j|d|d  | j
jj| j
j|d|d  | j
jj| j
j|d|d  | j
j|d  qbd	S )
a3  
        Record autoscaler metrics for:
            - pending_nodes: Nodes that are launching/pending ray start
            - active_nodes: Active nodes (nodes running ray)
            - recently_failed_nodes: Nodes that are being terminated.
            - stopped_nodes: Nodes that are terminated.
        r   )pendingrunningterminating
terminatedr      r   r   r   )SessionNamer   N)keysr   is_ray_pendingstatusinstance_typeis_ray_running
IMInstanceTERMINATING
TERMINATEDitemsr   pending_nodeslabelssession_namesetactive_nodesrecently_failed_nodesstopped_nodesinc)r   r   r   status_count_by_typer   instancestatus_countr   r   r   report_instances   s@   z*AutoscalerMetricsReporter.report_instancesc           	      C   s   t t}t t}dd }|D ]}t|jr||||jd qt|jr-||||jd q| D ]\}}| jj	j
| jj|d| q2| D ]\}}| jjj
| jj|d| qIdS )z
        Record autoscaler metrics for:
            - pending_resources: Pending resources
            - cluster_resources: Cluster resources (resources running on the cluster)
        c                 S   s4   || j }| D ]\}}| |  || 7  < q	d S r   )	resourcesr#   )resource_mapr   	node_typecountnode_resourcesresource_nameresource_valuer   r   r   _add_resourcesM   s   
zBAutoscalerMetricsReporter.report_resources.<locals>._add_resourcesr   )r   resourceN)r   floatr   r   r   r   r   r#   r   pending_resourcesr%   r&   r'   cluster_resources)	r   r   r   r:   r;   r7   r-   r5   r6   r   r   r   report_resources?   s0   



z*AutoscalerMetricsReporter.report_resources)__name__
__module____qualname__r   r   r   r    r   r   r   r/   r<   r   r   r   r   r
      s    

0
r
   N)collectionsr   typingr   r   $ray.autoscaler._private.prom_metricsr   )ray.autoscaler.v2.instance_manager.commonr   )ray.autoscaler.v2.instance_manager.configr   ray.autoscaler.v2.schemar   'ray.core.generated.instance_manager_pb2r	   r    r
   r   r   r   r   <module>   s    