o
    QD©iH  ã                   @   st   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
 zd dlZW n ey/   dZY nw G dd„ de
ƒZdS )	é    )Úperf_counter)Úrender_to_string)Ústatic)Úgettext_lazy)ÚPanelNc                       s€   e Zd ZdZdZdd„ ZeduZedƒZ	dZ
edd	„ ƒZe‡ fd
d„ƒZ‡ fdd„Zdd„ Zdd„ Zdd„ Zedd„ ƒZ‡  ZS )Ú
TimerPanelzG
    Panel that displays the time a response took in milliseconds.
    Tc                 C   s\   |   ¡ }| d¡r | d¡}| d¡}tdƒ|| |d dœ S d|v r,tdƒ|d  S dS )NÚutimeÚstimez"CPU: %(cum)0.2fms (%(total)0.2fms)Ú
total_time)ÚcumÚtotalzTotal: %0.2fmsÚ )Ú	get_statsÚgetÚ_)ÚselfÚstatsr   r	   © r   úS/var/www/Datamplify/venv/lib/python3.10/site-packages/debug_toolbar/panels/timer.pyÚnav_subtitle   s   


þzTimerPanel.nav_subtitleNÚTimezdebug_toolbar/panels/timer.htmlc                 C   sv   |   ¡ }tdƒtdƒ| ftdƒtdƒ| ftdƒtdƒ| ftdƒtdƒ| ftd	ƒtd
ƒ| ff}t| jd|iƒS )NúUser CPU timez%(utime)0.3f msecúSystem CPU timez%(stime)0.3f msecúTotal CPU timez%(total)0.3f msecúElapsed timez%(total_time)0.3f mseczContext switchesz)%(vcsw)d voluntary, %(ivcsw)d involuntaryÚrows)r   r   r   Útemplate)r   r   r   r   r   r   Úcontent*   s   
þû
zTimerPanel.contentc                    s   t ƒ j}| tdƒ¡ |S )Nzdebug_toolbar/js/timer.js)ÚsuperÚscriptsÚappendr   )r   r   ©Ú	__class__r   r   r   9   s   zTimerPanel.scriptsc                    s(   t ƒ | _| jrt tj¡| _tƒ  |¡S ©N)	r   Ú_start_timeÚhas_contentÚresourceÚ	getrusageÚRUSAGE_SELFÚ_start_rusager   Úprocess_request)r   Úrequestr!   r   r   r*   ?   s   zTimerPanel.process_requestc                    s   g d¢}‡ fdd„|D ƒS )N)Úru_utimeÚru_stimeÚru_nvcswÚ	ru_nivcswÚ	ru_minfltÚ	ru_majfltc                    s   i | ]}|t ˆ |ƒ“qS r   )Úgetattr)Ú.0Úfield©Údatar   r   Ú
<dictcomp>N   s    z/TimerPanel.serialize_rusage.<locals>.<dictcomp>r   )r   r6   Úfields_to_serializer   r5   r   Úserialize_rusageE   s   zTimerPanel.serialize_rusagec                 C   sÌ   i }t | dƒrtƒ | j d |d< | jr_t tj¡| _|  | j	¡}|  | j¡}| 
d|  ||d¡ d|  ||d¡ |  ||d¡|  ||d¡|  ||d¡|  ||d	¡d
œ¡ |d |d  |d< |  |¡ d S )Nr$   iè  r
   r,   r-   r.   r/   r0   r1   )r   r	   ÚvcswÚivcswÚminfltÚmajfltr   r	   r   )Úhasattrr   r$   r%   r&   r'   r(   Ú_end_rusager9   r)   ÚupdateÚ_elapsed_ruÚrecord_stats)r   r+   Úresponser   ÚstartÚendr   r   r   Úgenerate_statsP   s$   
úÿ
zTimerPanel.generate_statsc                 C   sd   |   ¡ }|  dd| dd¡¡ |  dd| dd¡¡ |  dd| dd¡¡ |  dd	| dd¡¡ d S )
Nr   r   r   r	   r   r   r   r
   r   )r   Úrecord_server_timingr   )r   r+   rC   r   r   r   r   Úgenerate_server_timingr   s   ÿz!TimerPanel.generate_server_timingc                 C   s   |  |¡|   |¡ S r#   )r   )rD   rE   Únamer   r   r   rA   |   s   zTimerPanel._elapsed_ru)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úis_asyncr   r&   r%   r   Útitler   Úpropertyr   r   r*   r9   rF   rH   ÚstaticmethodrA   Ú__classcell__r   r   r!   r   r      s"    
"
r   )Útimer   Údjango.template.loaderr   Údjango.templatetags.staticr   Údjango.utils.translationr   r   Údebug_toolbar.panelsr   r&   ÚImportErrorr   r   r   r   r   Ú<module>   s    ÿ