o
    wOi                     @   s*  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	 edg dZ
G dd dZG d	d
 d
e jZG dd deZG dd deZG dd dZi Ze ad(dedefddZdefddZdd Zd)defddZd(ddZd*deded efd!d"Zd ededefd#d$Zd%efd&d'ZdS )+    N)
namedtuplewraps)DictOptional
MetricData)	timestamp
group_namenamevaluec                   @   s.   e Zd ZdgZddeeeef  fddZdS )MetricsConfigparamsNc                 C   s   || _ | j d u ri | _ d S d S N)r   )selfr    r   L/home/ubuntu/.local/lib/python3.10/site-packages/torchelastic/metrics/api.py__init__   s   

zMetricsConfig.__init__r   )__name__
__module____qualname__	__slots__r   r   strr   r   r   r   r   r      s     r   c                   @   s    e Zd ZejdefddZdS )MetricHandlermetric_datac                 C      d S r   r   r   r   r   r   r   emit   s   zMetricHandler.emitN)r   r   r   abcabstractmethodr   r   r   r   r   r   r      s    r   c                   @      e Zd ZdefddZdS )ConsoleMetricHandlerr   c                 C   s    t d|j|j|j|j d S )Nz[{}][{}]: {}={})printformatr   r	   r
   r   r   r   r   r   r   $   s   zConsoleMetricHandler.emitNr   r   r   r   r   r   r   r   r   r    #       r    c                   @   r   )NullMetricHandlerr   c                 C   r   r   r   r   r   r   r   r   0   s   zNullMetricHandler.emitNr#   r   r   r   r   r%   /   r$   r%   c                   @   s0   e Zd ZdedefddZdedefddZd	S )
MetricStreamr	   handlerc                 C   s   || _ || _d S r   )r	   r'   )r   r	   r'   r   r   r   r   5   s   
zMetricStream.__init__metric_namemetric_valuec                 C   s    | j tt | j|| d S r   )r'   r   r   timer	   )r   r(   r)   r   r   r   	add_value9   s   zMetricStream.add_valueN)r   r   r   r   r   r   intr+   r   r   r   r   r&   4   s    r&   r'   groupc                 C   s   |d u r| a d S | t|< d S r   )_default_metrics_handler_metrics_map)r'   r-   r   r   r   	configureD   s   r0   c                 C   s    | t v r	t |  }nt}t| |S r   )r/   r.   r&   )r-   r'   r   r   r   	getStreamN   s   

r1   c                 C   sL   | j }|d}t|dkr$| j}|r |dd d |d  S |d S |S )N.   r   )r   splitlenr   )fnqualnamer5   moduler   r   r   _get_metric_nameV   s   
r:   torchelasticc                    s    fdd}| r|| S |S )a  
    @profile decorator publishes duration.ms, count, success, failure
    metrics for the function that it decorates. The metric name defaults
    to the qualified name (``class_name.def_name``) of the function.
    If the function does not belong to a class, it uses the leaf module name
    instead.

    Usage

    ::

     @metrics.prof
     def x():
         pass

     @metrics.prof(group="agent")
     def y():
         pass
    c                       t   fdd}|S )Nc               
      s   t  }z6zt } | i |}t| dd W n ty,   t| dd  w W t| dt| |S t| dt| w )Nz.successr3   z.failurez.duration.ms)r:   r*   
put_metric	Exceptionget_elapsed_time_ms)argskwargskeystartresult)fr-   r   r   wrappery   s   z#prof.<locals>.wrap.<locals>.wrapperr   )rE   rF   r-   )rE   r   wrapx   s   zprof.<locals>.wrapr   )r7   r-   rH   r   rG   r   profc   s   rI   c                    s   t dt  fdd}|S )z
    @profile decorator adds latency and success/failure metrics to any given function.

    Usage

    ::

     @metrics.profile("my_metric_group")
     def some_function(<arguments>):
    zDeprecated, use @prof insteadc                    r<   )Nc                     s   z9zt   } | i |}td jd W n ty*   td jd  w W td jt| |S td jt| w )Nz
{}.successr3   z
{}.failurez{}.duration.ms)r*   publish_metricr"   r   r>   r?   )r@   rA   
start_timerD   )funcr-   r   r   rF      s(   

z&profile.<locals>.wrap.<locals>.wrapperr   )rL   rF   rG   )rL   r   rH      s   zprofile.<locals>.wrap)warningswarnDeprecationWarning)r-   rH   r   rG   r   profile   s   rP   r(   r)   metric_groupc                 C   s   t || | dS )z
    Publishes a metric data point.

    Usage

    ::

     put_metric("metric_name", 1)
     put_metric("metric_name", 1, "metric_group_name")
    N)r1   r+   )r(   r)   rQ   r   r   r   r=      s   r=   c                 C   s"   t d t| }||| d S )NzKDeprecated, use put_metric(metric_group)(metric_name, metric_value) instead)rM   rN   r1   r+   )rQ   r(   r)   metric_streamr   r   r   rJ      s
   rJ   start_time_in_secondsc                 C   s   t   }t||  d S )zG
    Returns the elapsed time in millis from the given start time.
    i  )r*   r,   )rS   end_timer   r   r   r?      s   r?   r   )Nr;   )r;   )r   r*   rM   collectionsr   	functoolsr   typingr   r   r   r   ABCr   r    r%   r&   r/   r.   r   r0   r1   r:   rI   rP   r,   r=   rJ   floatr?   r   r   r   r   <module>   s,   	

,$