U
    /eL                     @  sb  d dl mZ d dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
 d dlmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZ d dl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% ej&'drd dl(m)Z) ndZ)ej&'dZ*ej+j,e*ddZ*G dd de!Z-G dd de!Z.G dd de!Z/G dd de!Z0G dd de!Z1dS )    )annotationsN)without_property_validation)columnrow)	ButtonColumnDataSourceDataRange1d	HoverToolLabelSetNumeralTickFormatterRange1dSelectTitle)	Spectral9)figure)gen)profile)WINDOWS)DashboardComponentupdate)
log_errorsz!distributed.dashboard.export-tool)
ExportToolz#distributed.worker.profile.intervalms)defaultc                   @  s8   e Zd ZdZdd Zeedd Zeedd Z	dS )	
ProcessingzProcessing and distribution per core

    This shows how many tasks are actively running on each worker and how many
    tasks are enqueued for each worker and how many are in the common pool
    c                 K  s   |  i i d}t|| _tdd}tf dd|d|}|j| jddtd d	d
d d|j_d|j	_
d|j_
t }|| |t}d|_d|_|| _d S )N)
processingnthreads   zProcessing and Pending )titletoolsx_ranger   righttopbottom)sourceleftr$   colorr%   r&   Fa  
        <div>
            <span style="font-size: 14px; font-weight: bold;">Host:</span>&nbsp;
            <span style="font-size: 10px; font-family: Monaco, monospace;">@name</span>
        </div>
        <div>
            <span style="font-size: 14px; font-weight: bold;">Processing:</span>&nbsp;
            <span style="font-size: 10px; font-family: Monaco, monospace;">@processing</span>
        </div>
        Zfollow_mouse)processing_updater   r'   r   r   Zquadr   ZxaxisZminor_tick_line_alphayaxisvisibleZygridr	   Z	add_toolsselectZtooltipsZpoint_policyroot)selfkwargsdatar#   ZfigZhover r2   K/tmp/pip-unpacked-wheel-g426oqom/distributed/dashboard/components/shared.py__init__1   s6    

	


zProcessing.__init__c                 C  s   |d }| dsd S | |}| jj}t|d }t|d }|j|k rV|d |_n&|jd| | kr||jd |d  |_t| j| d S )Nr   r   r$      gffffff?g?)getr*   r.   r#   maxendr   r'   )r/   messagesmsgr1   r#   Z	max_rightZcoresr2   r2   r3   r   Z   s    


zProcessing.updatec              	     s   t | d }t |}| d fdd|D | d   fdd|D  t|}t|ttt|ddtt|d dd d	}d
g| |d< |S )Nr   c                   s   g | ]} | qS r2   r2   .0name)r   r2   r3   
<listcomp>q   s     z0Processing.processing_update.<locals>.<listcomp>r   c                   s   g | ]} | qS r2   r2   r;   )r   r2   r3   r>   s   s     r   r   r   )r=   r   r$   r%   r&   r   gffffff?alpha)sortedlenlistrange)r:   namesndr2   )r   r   r3   r*   k   s     	zProcessing.processing_updateN)
__name__
__module____qualname____doc__r4   r   r   r   staticmethodr*   r2   r2   r2   r3   r   *   s   )r   c                   @  s(   e Zd ZdZdd Zeedd ZdS )ProfilePlotTime plots of the current resource usage on the cluster

    This is two plots, one for CPU and Memory and another for Network I/O
    c                   s^   t  }t |t}|d _t j|f|\ _ _t	t
 fdd} jjd| d S )Nstatesc                   sn   z|j d }W n tk
r$   Y d S X t j| t} jd d =  j|d t j	| | j	_
d S )Nr   rN   )indices
IndexErrorr   	plot_datarN   profile_intervalextendpopr   r'   selected)attroldnewindr1   r/   r2   r3   cb   s    z ProfilePlot.__init__.<locals>.cbrO   )r   createrQ   rR   rT   rN   plot_figurer.   r'   r   r   rU   	on_change)r/   r0   stater1   r[   r2   rZ   r3   r4      s    zProfilePlot.__init__c                 C  s0   || _ t| j t}|d| _t| j| d S NrN   r_   r   rQ   rR   rT   rN   r   r'   r/   r_   r1   r2   r2   r3   r      s    zProfilePlot.updateN)rG   rH   rI   rJ   r4   r   r   r   r2   r2   r2   r3   rL      s
   rL   c                   @  s:   e Zd ZdZd
ddZeedddZeddd	ZdS )ProfileTimePlotrM   Nc              	     s  |d k	rt |_z|jjjdd _W n tk
rF   d _Y nX t	jt
r`jd _t	jtrxj _jrdjgndg_nd _dg_|_d _d _g g d_t _tjt}|d_tj|f|\__dg t fdd}jjd	| tg g d
_ t!dddddddd_"j"j#ddj d j"j$ddj d dd dj"j%_&dj"j'_&t(fdd}j jd	| t)ddd_*j*+fdd t)ddd_,j,+j- t.jd  jd!_/fd"d#}j/d$| t0t1j/j*j,d%d&d'jj"f|_2t3d(d)d*_4j5j4d t6j7d+sd,j4_8d-j/_9d-j*_9d-j,_9d S ).Nkeyr   AllcounttimerN   Fc              	     s    d st |dkrd S t ` tj|d  t}jd d = j|d d d< tj	| |j	j
_d d< W 5 Q R X d S Nr   rN   TF)rA   r   r   rQ   rN   rR   rS   rT   r   r'   rU   rO   rV   rW   rX   r1   Zchangingr/   r2   r3   r[      s    
z$ProfileTimePlot.__init__.<locals>.cbrO   rh   rg   Activity over time   datetimexbox_select"xpan,xwheel_zoom,xbox_select,resetstretch_widthabover!   heightx_axis_typeZactive_dragr"   sizing_modetoolbar_locationrh   rg   r'   oranger'   r)   Zselection_colorc                   sv    j jj}|rZ j jd t| d } j jd t| d }t||t||  _ _nd   _ _ jdd d S )Nrh     Fupdate_metadata		ts_sourcerU   rO   r1   minr7   startstoptrigger_updaterV   rW   rX   rU   r   r   rZ   r2   r3   	ts_change   s    
z+ProfileTimePlot.__init__.<locals>.ts_changeResetsuccesslabelZbutton_typec                     s      jS Nr   r_   r2   rZ   r2   r3   <lambda>       z*ProfileTimePlot.__init__.<locals>.<lambda>Updater   )valueoptionsc                   s"   |dkrd }| _  jdd d S )Nre   Fr}   )rd   r   )rV   rW   rX   rZ   r2   r3   	select_cb   s    z+ProfileTimePlot.__init__.<locals>.select_cbr   scale_width   )rw   ru    italictextZtext_font_style"distributed.worker.profile.enabledProfiling is disabled.T):weakrefrefdocZsession_contextrequest	argumentsr6   rd   AttributeError
isinstancerB   bytesdecode
task_namesserverr   r   tsr   r\   r_   rQ   rR   rT   rN   r]   profile_plotr'   r   rU   r^   r   r   r   ts_plotlinecircler+   r,   gridr   r   reset_buttonon_clickupdate_buttonr   r   r-   r   r   r.   r   subtitle
add_layoutdaskconfigr   disabled)r/   r   r   r0   r1   r[   r   r   r2   rk   r3   r4      s    
	    



zProfileTimePlot.__init__c                 C  s   || _ t| j t}|d| _t| j| |d k	r|d rdgt|d  | _	| j	| j
_| jr~| j|d kr~|d | j }n|d }t| \}}|dd |D d| _| jj| j d S )NrN   countsre   keysc                 S  s   g | ]}|d  qS r|   r2   )r<   tr2   r2   r3   r>   )  s     z*ProfileTimePlot.update.<locals>.<listcomp>rf   )r_   r   rQ   rR   rT   rN   r   r'   r@   r   r-   r   rd   zipr   r   r1   )r/   r_   metadatar1   r   timesr   r2   r2   r3   r     s    
zProfileTimePlot.updateTc                   s$   t  fdd} jj| d S )Nc                     sx   j jjjjdI d H r4j  I d H  nd  ttjrZt	
 I d H \   fdd d S )N)rd   r   r   c                     s     S r   r   r2   )r   profr/   r2   r3   r   :  r   z<ProfileTimePlot.trigger_update.<locals>.cb.<locals>.<lambda>)r   get_profilerd   r   r   Zget_profile_metadatar   r   ZFutureasyncioZgatherr   Zadd_next_tick_callbackr2   r/   r~   )r   r   r3   r[   /  s      z*ProfileTimePlot.trigger_update.<locals>.cb)r   r   ZloopZadd_callback)r/   r~   r[   r2   r   r3   r   -  s    zProfileTimePlot.trigger_update)N)N)T	rG   rH   rI   rJ   r4   r   r   r   r   r2   r2   r2   r3   rc      s   
lrc   c                   @  s6   e Zd ZdZd	ddZeedd Zedd ZdS )
ProfileServerrM   Nc              	     s  |d k	rt |_|_jjj_d _d _g g d_	t
j_tjt}|d_tj|f|\__dg tt fdd}jjd| tg g d_tdd	d
ddddd_jjddjd jjddjd dd djj_djj_tfdd}jjd| t ddd_!j!"fdd t ddd_#j#"j$ t%t&j!j#ddjjf|_'t(dd d!_)j*j)d t+j,-d"sd#j)_.d$j!_/d$j#_/d S )%Nrf   rN   Fc                   sv    d st |dkrd S tj|d  t}jd d = j|d d d< tj| |jj	_
d d< d S ri   )rA   r   rQ   rN   rR   rS   rT   r   r'   rU   rO   rj   rk   r2   r3   r[   T  s    
z"ProfileServer.__init__.<locals>.cbrO   rl   rm   rn   ro   rp   rq   rr   rs   rt   rh   rg   ry   rz   r{   c                   sr    j jj}|rZ j jd t| d } j jd t| d }t||t||  _ _nd   _ _   d S )Nrh   r|   r   r   rZ   r2   r3   r   u  s    
z)ProfileServer.__init__.<locals>.ts_changer   r   r   c                     s      jS r   r   r2   rZ   r2   r3   r     r   z(ProfileServer.__init__.<locals>.<lambda>r   r   )rw   r   r   r   r   r   T)0r   r   r   r   Zio_loopr   logr   r   r   r   r_   rQ   rR   rT   rN   r]   r   r'   r   r   rU   r^   r   r   r   r   r   r   r+   r,   r   r   r   r   r   r   r   r   r.   r   r   r   r   r   r6   r   r   )r/   r   r   r0   r1   r[   r   r2   rk   r3   r4   E  sp    	    


zProfileServer.__init__c                 C  s0   || _ t| j t}|d| _t| j| d S r`   ra   rb   r2   r2   r3   r     s    zProfileServer.updatec                 C  s   t j| j| j| jd| _t | jt}|d| _	t
| j| dd | jD }ttdtd| j}| jj
||d d S )N)r   r   rN   c                 S  s   g | ]\}}|d  qS r   r2   )r<   r   _r2   r2   r3   r>     s     z0ProfileServer.trigger_update.<locals>.<listcomp>rg   r   rl   )r   r   r   r   r   r_   rQ   rR   rT   rN   r   r'   rB   toolzZpluckr   r1   )r/   r1   r   r   r2   r2   r3   r     s    zProfileServer.trigger_update)Nr   r2   r2   r2   r3   r   ?  s   
Qr   c                   @  s.   e Zd Zd	ddZdd Zeedd ZdS )
SystemMonitorrn   Nc           
      K  s  || _ |jj}d| _|d k	r0|jj|d}|| _tdd |D | _tdgd ddd	gd
ddgdddgd| _t| j| 	  t
dddd}d}tf dd||d|d|| _| jj| jddd d| jj_| jtdddddd | jd! tf d"d||d|d|| _| jj| jdd#d d$| jj_| jtddddd#d | jd! tf d%d|||dd&|| _| jj| jdd'd(d)d* | jj| jdd+d,d-d* d.| jj_td/d0| jjd _td/d0| jjd _| j| j| jg}tstf d1d|||dd&|| _| jj| jdd2d || j d3|kr6d3|d3 i}	ni }	tsJd| jj_d| jj_d| jj_d| jj_t||	| _| j j  d S )4Nr   r   c                 S  s   i | ]
}|g qS r2   r2   r;   r2   r2   r3   
<dictcomp>  s      z*SystemMonitor.__init__.<locals>.<dictcomp>      F   7   (   zmax: 45%zmin: 45%z	mean: 45%zmax: 133.5MiBzmin: 23.6MiBzmean: 115.4MiB)xycpumemoryr8   i N  )followZfollow_intervalZrange_paddingzreset,xpan,xwheel_zoomZCPUro   rs   )r!   rv   ru   r"   rx   r#   rh   r   )r'   r   r   Z
Percentager   r   ZscreenZ1em)r   r   Zx_unitsZy_unitsr   Ztext_font_sizer'   ZMemoryr   ZBytesZ	Bandwidth)r!   rv   ru   r#   r"   rx   zhost_net_io.read_bpsZredread)r'   r   r   r)   Zlegend_labelzhost_net_io.write_bpsZbluewritezBytes / secondz0.0b)formatzNumber of File Descriptorsnum_fdsrw   )workermonitorZ
quantities
last_countrange_queryr   r'   label_sourcer   get_datar   r   r   r   r+   Z
axis_labelr   r
   Zmem	bandwidthr   	formatterr   r   appendZy_ranger   r   r.   )
r/   r   ru   r   r0   rD   r#   r"   Zplotskwr2   r2   r3   r4     s    	
	
	






zSystemMonitor.__init__c                 C  s8   | j jj| jd}dd |d D |d< | j jj| _|S )Nr   c                 S  s   g | ]}|d  qS r   r2   )r<   r   r2   r2   r3   r>   *  s     z*SystemMonitor.get_data.<locals>.<listcomp>rh   )r   r   r   r   rg   )r/   rF   r2   r2   r3   r   (  s    zSystemMonitor.get_datac                   s^   dd } j   d  fddtt|fD  jjd<  fddtt|fD  jjd< d S )	Nc                 S  s   t | t|  S r   )sumrA   )r   r2   r2   r3   mean1  s    z"SystemMonitor.update.<locals>.meanr|   c                   s&   g | ]}d  |j| jjd qS )z{}: {:.1f}%r   )r   rG   r'   r1   r<   frZ   r2   r3   r>   5  s   z(SystemMonitor.update.<locals>.<listcomp>r   c              
     s.   g | ]&}d  |jtj| jjd qS )z{}: {}r   )r   rG   r   utilsZformat_bytesr'   r1   r   rZ   r2   r3   r>   9  s
    r   )r'   streamr   r   r7   r   r1   )r/   r   r2   rZ   r3   r   .  s    

zSystemMonitor.update)rn   N)rG   rH   rI   r4   r   r   r   r   r2   r2   r2   r3   r     s
   
~r   )2
__future__r   r   r   Ztlzr   Zbokeh.core.propertiesr   Zbokeh.layoutsr   r   Zbokeh.modelsr   r   r   r	   r
   r   r   r   r   Zbokeh.palettesr   Zbokeh.plottingr   Ztornador   r   Zdistributedr   Zdistributed.compatibilityr   Z distributed.dashboard.componentsr   Zdistributed.dashboard.utilsr   Zdistributed.utilsr   r   r6   Z!distributed.dashboard.export_toolr   rR   r   Zparse_timedeltar   rL   rc   r   r   r2   r2   r2   r3   <module>   s4   ,Y$ j