B
    F.\                 @   s4   d dl mZmZmZ ddlmZ G dd deZdS )    )print_functiondivisionabsolute_import   )SchedulerPluginc               @   s2   e Zd ZdZdd ZdddZdd Zd	d
 ZdS )GraphLayouta=   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/bokeh/scheduler.py::GraphPlot, which
    is rendered at /graph on the diagnostic dashboard.
    c             C   s   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||d d S )Nr   c             S   s"   i | ]\}}d d |j D |qS )c             S   s   g | ]
}|j qS  )key).0Zdsr   r   Clib/python3.7/site-packages/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   )priority)r
   r   r   r   r   r   r   $   s    )r   r   )xy	collision	schedulerindex
index_edgenext_y
next_indexnext_edge_indexnew	new_edgesstate_updatesvisible_updatesvisible_edge_updatesZ
add_plugintasksitemsupdate_graph)selfr   r   r   r   r   r   __init__   s*    

zGraphLayout.__init__Nc                s  t | fdddd}x|r| }|jks|jkr>q|| }|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| x<|D ]4}||f}jj|<  jd7  _j| qW qW d S )Nc                s     | dS )Nr   )get)r   )r   r   r   <lambda>*   s    z*GraphLayout.update_graph.<locals>.<lambda>T)r	   reversec             3   s   | ]}| j kV  qd S )N)r   )r
   dep)r#   r   r   	<genexpr>1   s    z+GraphLayout.update_graph.<locals>.<genexpr>c                s     | dS )Nr   )r%   )r   )r   r   r   r&   3   s    c             3   s   | ]}t  j| jV  qd S )N)lenr    
dependents)r
   r(   )r   r   r   r)   7   s   c             3   s,   | ]$}j | t j| j  V  qd S )N)r   r*   r    r+   )r
   r(   )r   r#   
total_depsr   r   r)   9   s   c             3   s   | ]} j | V  qd S )N)r   )r
   r(   )r#   r   r   r)   <   s    r   r   g?)sortedpopr   r    allappendextendsummaxr   r   r   r   r   r   r   r   r   )r#   r   r   r   kwargsstackr	   Zdepsr   r   Zold_xZold_yr(   edger   )r   r   r#   r,   r   r"   (   sH    








zGraphLayout.update_graphc       
      O   s   |dkr | j | j| |f n| j| j| df | jj| }x6|jD ],}||jf}| j| j	
||jfdf qJW x,|jD ]"}| j| j	
|j|fdf qW y| j| j| | j| f= W n tk
r   Y nX x| j| j| jgD ]
}	|	|= qW d S )NZ	forgottenFalse)r   r0   r   r   r   r    r+   r	   r   r   r.   r   r   r   r   KeyError)
r#   r	   startZfinishargsr4   Ztaskr(   r6   Z
collectionr   r   r   
transitionU   s    
""zGraphLayout.transitionc             C   s   g | _ g | _g | _g | _g | _i | _d| _i | _d| _x|| j	D ]r}| j| j|< |  jd7  _| j 
| xF| jj| jD ]4}|j|f}| j| j|< |  jd7  _| j
| qxW q>W dS )z Reset the index and refill new and new_edges

        From time to time GraphPlot wants to remove invisible nodes and reset
        all of its indices.  This helps.
        r   r   N)r   r   r   r   r   r   r   r   r   r   r0   r   r    r   r	   )r#   r	   r(   r6   r   r   r   reset_indexi   s$    
zGraphLayout.reset_index)NN)__name__
__module____qualname____doc__r$   r"   r;   r<   r   r   r   r   r      s
   
-r   N)Z
__future__r   r   r   Zpluginr   r   r   r   r   r   <module>   s   