o
    ci"                     @   s   d dl mZmZ d dlmZ d dlmZmZmZm	Z	m
Z
 d dlmZ dZdZe
dZe
ded	ZG d
d deZG dd deZG dd deee ZdS )    )ABCabstractmethod)Enum)DictGenericOptionalTupleTypeVar)Gaugeray_train_run_nameray_train_run_idTE)boundc                
   @   st   e Zd Zdedededeeef fddZedefdd	Zed
efddZ	edd Z
d
eedf fddZdS )Metricnamedefaultdescription	base_tagsc                 C   s$   || _ || _t|||  d| _dS )a  
        Initialize a new metric.

        Args:
            name: The name of the metric.
            default: The default value of the metric.
            description: The description of the metric.
            base_tags: The base tags for the metric.
        )r   tag_keysN)_default
_base_tagsr
   _get_tag_keys_gauge)selfr   r   r   r    r   W/home/ubuntu/.local/lib/python3.10/site-packages/ray/train/v2/_internal/metrics/base.py__init__   s   zMetric.__init__valuec                 C      dS )zhUpdate the metric value.

        Args:
            value: The value to update the metric with.
        Nr   r   r   r   r   r   record'   s   zMetric.recordreturnc                 C   r   )zGet the value of the metric.

        Returns:
            The value of the metric. If the metric has not been recorded,
            the default value is returned.
        Nr   r   r   r   r   	get_value0   s   zMetric.get_valuec                 C   r   )z$Reset values and clean up resources.Nr   r#   r   r   r   reset:   s   zMetric.reset.c                 C   s   t | j S N)tupler   keysr#   r   r   r   r   ?   s   zMetric._get_tag_keysN)__name__
__module____qualname__strr   r   r   r   r!   r$   r%   r   r   r   r   r   r   r      s"    

	
r   c                       sZ   e Zd ZdZdededeeef f fddZdefdd	Zd
efddZ	dd Z
  ZS )
TimeMetricz#A metric for tracking elapsed time.r   r   r   c                    s   d| _ t j|d||d d S )Ng        r   r   r   r   )_current_valuesuperr   )r   r   r   r   	__class__r   r   r   F   s   
zTimeMetric.__init__r   c                 C   s$   |  j |7  _ | j| j | j dS )zUpdate the time metric value by accumulating the time.

        Args:
            value: The time value to increment the metric by.
        N)r/   r   setr   r    r   r   r   r!   T   s   zTimeMetric.recordr"   c                 C   s   | j S r&   )r/   r#   r   r   r   r$   ]   s   zTimeMetric.get_valuec                 C   s   | j | _| j| j | j d S r&   )r   r/   r   r3   r   r#   r   r   r   r%   `   s   zTimeMetric.reset)r)   r*   r+   __doc__r,   r   r   floatr!   r$   r%   __classcell__r   r   r1   r   r-   C   s    
	r-   c                       s   e Zd ZdZdZdZdededeeef def fdd	Zd
e	fddZ
d
e	defddZdd Zdeedf fddZd
e	deeef fddZ  ZS )
EnumMetricz"A metric for tracking enum values.r      r   r   r   enum_tag_keyc                    s&   || _ d | _t j|| j||d d S )Nr.   )_enum_tag_keyr/   r0   r   DEFAULT_VALUE)r   r   r   r   r9   r1   r   r   r   k   s   
zEnumMetric.__init__
enum_valuec                 C   sX   || j krdS | j dur| | j }| j| j| | |}| j| j| || _ dS )zRecord a specific enum value.

        The metric will be reset to 0 for the previous value and set to 1 for the new value.

        Args:
            enum_value: The enum value to record for.
        N)r/   	_get_tagsr   r3   r   RECORDED_VALUE)r   r<   previous_tagscurrent_tagsr   r   r   r!   {   s   



zEnumMetric.recordr"   c                 C   s   t || jkS )zGet the value for a specific enum value.

        Args:
            enum_value: The enum value to get the value for

        Returns:
            The value for the enum value
        )intr/   )r   r<   r   r   r   r$      s   	zEnumMetric.get_valuec                 C   s0   | j d ur| | j }| j| j| d | _ d S r&   )r/   r=   r   r3   r   )r   tagsr   r   r   r%      s   

zEnumMetric.reset.c                 C   s   t | j | jf S r&   )r'   r   r(   r:   r#   r   r   r   r      s   zEnumMetric._get_tag_keysc                 C   s   | j  }|j|| j< |S r&   )r   copyr   r:   )r   r<   rB   r   r   r   r=      s   
zEnumMetric._get_tags)r)   r*   r+   r4   r;   r>   r,   r   r   r   r!   rA   r$   r%   r   r   r=   r6   r   r   r1   r   r7   e   s$    
"r7   N)abcr   r   enumr   typingr   r   r   r   r	   ray.util.metricsr
   RUN_NAME_TAG_KEYRUN_ID_TAG_KEYr   r   r   r-   r7   r   r   r   r   <module>   s    5"