o
    QDi                     @   s   d dl 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 d dlmZ d dlmZ G dd	 d	ZG d
d deZdS )    N)
hsv_to_rgb)Stats)settings)format_html)gettext_lazy)Panelc                   @   sx   e Zd Z	dddZdd Zdd	 Zd
d Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )FunctionCallr   Nr   g      ?   c                 C   sR   || _ || _|r|| _n
|j| d d | _|| _|| _|pg | _|| _d| _d S )N   F)statobjfuncstatsdepthid
parent_idshsvhas_subfuncs)selfr   r   r   r   r   r   r    r   W/var/www/Datamplify/venv/lib/python3.10/site-packages/debug_toolbar/panels/profiling.py__init__   s   

zFunctionCall.__init__c                 C   s   | j S N)parent_classesr   r   r   r   r      s   zFunctionCall.parent_classesc                 C   s:   t | j \}}}d|d dd|d dd|d ddS )Nzrgb(d   fz%,z%))r   r   )r   rgbr   r   r   
background!   s   *zFunctionCall.backgroundc                    s^   t tdr-| j\ }}ttj}tj  tj|} |o,t	 fdddD  S dS )z
        Check if the function is from the project code.

        Project code is identified by the BASE_DIR setting
        which is used in Django projects by default.
        BASE_DIRc                 3   s"    | ]}|  tjjv V  qd S r   )splitospathsep).0	directory	file_namer   r   	<genexpr>3   s
    
z/FunctionCall.is_project_func.<locals>.<genexpr>)zsite-packageszdist-packagesN)
hasattrr   r   strr!   r#   r$   normpath
startswithany)r   _base_dirr   r(   r   is_project_func%   s   


zFunctionCall.is_project_funcc           	      C   s   | j }|d d dkr%|d }|dr#|dr#d|dd  dS |S | j \}}}|d	}|dkr<||d
 d  }|tjd}t|dkrS|tjd\}}nd}td||||S )N   )~r   <>{r
   }z/site-packages/   z<module>z<span class="djdt-path">{0}/</span><span class="djdt-file">{1}</span> in <span class="djdt-func">{3}</span>(<span class="djdt-lineno">{2}</span>))	r   r.   endswithfindrsplitr#   r%   lenr   )	r   	func_namenamer)   line_nummethodidx
split_path	file_pathr   r   r   func_std_string9   s*   
zFunctionCall.func_std_stringc           
      c   s    | j \}}}t| jj| j }t| jj| j  D ]H\}\}}||d | | jd   }| jd dkr7dn
||d | jd   }	t	| j|| jd |t
| jd t
| | j| jg ||	dfdV  qd S )Nr
      r   r0   )r   r   r   r   )r   r>   r   all_calleesr   	enumerateitemsr   r   r   r,   r   r   )
r   hsvcountir   r   h1s1r   r   r   subfuncsY   s    "(
zFunctionCall.subfuncsc                 C   
   | j d S )Nr
   r   r   r   r   r   rN   i      
zFunctionCall.countc                 C   rS   )Nr3   rT   r   r   r   r   tottimel   rU   zFunctionCall.tottimec                 C   s   | j \}}}}| j d S )NrG   rT   r   ccncttctr   r   r   cumtimeo   s   
zFunctionCall.cumtimec                 C   s"   | j \}}}}|dkrdS || S Nr   rT   rW   r   r   r   tottime_per_calls      zFunctionCall.tottime_per_callc                 C   s"   | j \}}}}|dkrdS || S r]   rT   rW   r   r   r   cumtime_per_call{   r_   zFunctionCall.cumtime_per_callc                 C   s
   d| j  S )N   r   r   r   r   r   indent   rU   zFunctionCall.indentc                 C   sB   | j | j| j|  |  |  |  |  |  | 	 | 
 dS )Nr   r   r   r2   rc   rF   r\   r`   rV   r^   rN   rd   r   r   r   r   	serialize   s   zFunctionCall.serialize)r   Nr   Nr	   )__name__
__module____qualname__r   r   r    r2   rF   rR   rN   rV   r\   r^   r`   rc   re   r   r   r   r   r      s    
 r   c                       sP   e Zd ZdZdZedZdZe	 d Z
 fddZdd	 Z fd
dZ  ZS )ProfilingPanelz4
    Panel that displays profiling information.
    F	Profilingz#debug_toolbar/panels/profiling.htmlPROFILER_CAPTURE_PROJECT_CODEc                    s   t  | _| jt j|S r   )cProfileProfileprofilerruncallsuperprocess_request)r   request	__class__r   r   rq      s   
zProfilingPanel.process_requestc                 C   sj   | | |j|k r1| D ]$}|jd |ks%| jr0| r0|jd dkr0d|_| |||| qd S d S )NrG   r   T)appendr   rR   r   capture_project_coder2   r   add_node)r   	func_listr   	max_depthcum_timesubfuncr   r   r   rw      s   

zProfilingPanel.add_nodec                    s   t | dsd S | j  t| j| _| j  tt j	j
}|| jjv rTt| j|dd}g }|jd t d  }| ||t d | | ddd	 |D i d S d S )
Nrn   r   rb   rG   PROFILER_THRESHOLD_RATIOPROFILER_MAX_DEPTHrx   c                 S   s   g | ]}|  qS r   )re   )r&   r   r   r   r   
<listcomp>   s    z1ProfilingPanel.generate_stats.<locals>.<listcomp>)r+   rn   create_statsr   r   calc_calleesrl   labelrp   rq   __code__r   dt_settings
get_configrw   record_stats)r   rr   response	root_funcrootrx   cum_time_thresholdrs   r   r   generate_stats   s&   



zProfilingPanel.generate_stats)rf   rg   rh   __doc__is_asyncr0   titletemplater   r   rv   rq   rw   r   __classcell__r   r   rs   r   ri      s    ri   )rl   r#   colorsysr   pstatsr   django.confr   django.utils.htmlr   django.utils.translationr   r0   debug_toolbarr   debug_toolbar.panelsr   r   ri   r   r   r   r   <module>   s     	