U
    /e                     @  s4   d dl mZ d dlZd dlmZ G dd deZdS )    )annotationsN)SchedulerPluginc                   @  s2   e Zd ZdZdd ZdddZdd Zd	d
 ZdS )GraphLayoutaK  Dynamic graph layout during computation

    This assigns (x, y) locations to all tasks quickly and dynamically as new
    tasks are added.  This scales to a few thousand nodes.

    It is commonly used with distributed/dashboard/components/scheduler.py::TaskGraph, which
    is rendered at /graph on the diagnostic dashboard.
    c                 C  s   dt   | _i | _i | _i | _|| _i | _i | _d| _	d| _
d| _g | _g | _g | _g | _g | _| jjrdd |j D }dd |j D }| j| j| jj||d d S )Nzgraph-layout-r   c                 S  s"   i | ]\}}|d d |j D qS )c                 S  s   g | ]
}|j qS  )key).0Zdsr   r   H/tmp/pip-unpacked-wheel-g426oqom/distributed/diagnostics/graph_layout.py
<listcomp>%   s     z3GraphLayout.__init__.<locals>.<dictcomp>.<listcomp>)dependenciesr   ktsr   r   r   
<dictcomp>$   s    z(GraphLayout.__init__.<locals>.<dictcomp>c                 S  s   i | ]\}}||j qS r   priorityr   r   r   r   r   (   s      )tasksr
   r   )uuidZuuid4namexy	collision	schedulerindex
index_edgenext_y
next_indexnext_edge_indexnew	new_edgesstate_updatesvisible_updatesvisible_edge_updatesr   itemsupdate_graph)selfr   r
   r   r   r   r   __init__   s4    zGraphLayout.__init__Nc                   s  t | fdddd}|r| }|jks|jkr:q||d}|rtfdd|D s|| |t | fdddd qqtfd	d|D tfd
d|D }	t	fdd|D d }
nd}
j
}	 j
d7  _
|
|	fjkr6|
|	 }}j|
|	f \}
}	|	d7 }	|
|	fj||f< n|
|	fj|
|	f< |
j|< |	j|< jj|< jd _j| |D ]4}||f}jj|<  jd7  _j| qqd S )Nc                   s     | dS Nr   getr   r   r   r   <lambda>3       z*GraphLayout.update_graph.<locals>.<lambda>T)r   reverser   c                 3  s   | ]}| j kV  qd S N)r   r   depr$   r   r   	<genexpr>:   s     z+GraphLayout.update_graph.<locals>.<genexpr>c                   s     | dS r&   r'   r)   r   r   r   r*   =   r+   c                 3  s   | ]}t  j| jV  qd S r-   )lenr   
dependentsr.   )r   r   r   r1   A   s    c                 3  s,   | ]$}j | t j| j  V  qd S r-   )r   r2   r   r3   r.   )r   r$   
total_depsr   r   r1   D   s   c                 3  s   | ]} j | V  qd S r-   )r   r.   r0   r   r   r1   H   s        r   g?)sortedpopr   r   r(   allappendextendsummaxr   r   r   r   r   r   r   r   r   )r$   r   r
   r   r   kwargsstackr   depsr   r   Zold_xZold_yr/   edger   )r   r   r$   r4   r   r#   0   sN    



zGraphLayout.update_graphc           
      O  s   |dkr | j | j| |f n| j| j| df | jj| }|jD ]&}||jf}| j| j	
|df qH|jD ]"}| j| j	
|j|fdf qvz| j| j| | j| f= W n tk
r   Y nX | j| j| jfD ]
}	|	|= qd S )NZ	forgottenFalse)r   r9   r   r    r   r   r3   r   r!   r   r7   r
   r   r   r   KeyError)
r$   r   startfinishargsr=   Ztaskr/   r@   Z
collectionr   r   r   
transitiona   s"    


zGraphLayout.transitionc                 C  s   g | _ g | _g | _g | _g | _i | _d| _i | _d| _| j	D ]n}| j| j|< |  jd7  _| j 
| | jj| jD ]4}|j|f}| j| j|< |  jd7  _| j
| qtq<dS )zReset the index and refill new and new_edges

        From time to time TaskGraph wants to remove invisible nodes and reset
        all of its indices.  This helps.
        r   r5   N)r   r   r    r   r!   r   r   r   r   r   r9   r   r   r
   r   )r$   r   r/   r@   r   r   r   reset_indexw   s$    

zGraphLayout.reset_index)NNN)__name__
__module____qualname____doc__r%   r#   rF   rG   r   r   r   r   r      s   	     
1r   )
__future__r   r   Zdistributed.diagnostics.pluginr   r   r   r   r   r   <module>   s   