o
    PDi                     @   s   d dl Z d dlZ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 ed	ZG d
d dZG dd dZe dd Zedkr]e  dS dS )    Nwraps)apps)settings)timezone)DataCollector)SilkyConfig)_time_takenzsilk.profiling.profilerc                       sD   e Zd ZdZ fddZedd Zdd Zdd	 Zd
d Z	  Z
S )silk_meta_profilerz%Used in the profiling of Silk itself.c                    s   t    d | _d S N)super__init__
start_timeself	__class__ P/var/www/Datamplify/venv/lib/python3.10/site-packages/silk/profiling/profiler.pyr      s   

zsilk_meta_profiler.__init__c                 C   s   t  jS r   )r   
SILKY_METAr   r   r   r   _should_meta_profile   s   z'silk_meta_profiler._should_meta_profilec                 C   s   | j r
t | _d S d S r   )r   r   nowr   r   r   r   r   	__enter__   s   zsilk_meta_profiler.__enter__c                 C   sn   | j r3t }|d u}|rtd t||| tt j	dd }|r5|j
p'd}|t| j| |_
d S d S d S )Nz=Exception when performing meta profiling, dumping trace belowrequestr   )r   r   r   loggererror	tracebackprint_exceptiongetattrr   local	meta_timer	   r   )r   exc_typeexc_valexc_tbend_timeexception_raisedr   currr   r   r   __exit__"   s   

zsilk_meta_profiler.__exit__c                    s   | j r fdd}|S  S )Nc                     sZ   t  j}|r$t } | i |}t }|jpd}|t|| |_|S  | i |}|S )Nr   )r   r   r   r   r    r	   )argskwargsr   r   resultr$   r&   targetr   r   wrapped_target0   s   
z3silk_meta_profiler.__call__.<locals>.wrapped_target)r   )r   r,   r-   r   r+   r   __call__.   s   zsilk_meta_profiler.__call__)__name__
__module____qualname____doc__r   propertyr   r   r'   r.   __classcell__r   r   r   r   r
      s    
r
   c                       sn   e Zd Zd f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  ZS )silk_profileNFc                    s,   t    || _d | _d | _d | _|| _d S r   )r   r   nameprofile_queries_before_queries_after_dynamic)r   r6   r:   r   r   r   r   B   s   

zsilk_profile.__init__c                 C   s   dd t  jD S )Nc                 S   s   g | ]}|qS r   r   .0xr   r   r   
<listcomp>K   s    zBsilk_profile._query_identifiers_from_collector.<locals>.<listcomp>)r   queriesr   r   r   r   !_query_identifiers_from_collectorJ   s   z.silk_profile._query_identifiers_from_collectorc                 C      |   | _dS )z=record queries that have been executed before profiling beganN)r@   r8   r   r   r   r   _start_queriesM      zsilk_profile._start_queriesc                 C   rA   )zCrecord queries that have been executed after profiling has finishedN)r@   r9   r   r   r   r   _end_queriesQ   rC   zsilk_profile._end_queriesc                 C   s   |   rP|  rPt ; |   | jstdt }t|}|d }|d }|d }t	 j
}| j||| j|t d| _W d    d S 1 sIw   Y  d S td d S )Nz7silk_profile used as a context manager must have a name      )r6   	file_pathline_numdynamicr   r   ?Cannot execute silk_profile as silk is not installed correctly.)_silk_installed_should_profiler
   rB   r6   
ValueErrorinspectcurrentframegetouterframesr   r   r:   r   r   r7   r   warning)r   frameframesouter_framepathrH   r   r   r   r   r   U   s(   

"zsilk_profile.__enter__c                 C   sL   t  }|   | jsJ dt| jt| j}|| jd< || j d S )Nzno profile was createdr?   )r   rD   r7   setr9   
differencer8   register_profile)r   	collectordiffr   r   r   _finalise_queriesl   s   
zsilk_profile._finalise_queriesc                 C   sl   |   r2|  r4t  |d u}|| jd< t | jd< |   W d    d S 1 s+w   Y  d S d S d S )Nr%   r$   )rK   rL   r
   r7   r   r   r[   )r   r!   r"   r#   r%   r   r   r   r'   u   s   

"zsilk_profile.__exit__c                 C   s.   t tdg }|s
g }t j|v }tdo|S )N
MIDDLEWAREsilk)r   r   r   SILKY_MIDDLEWARE_CLASSr   is_installed)r   middlewaresmiddleware_installedr   r   r   rK   }   s
   zsilk_profile._silk_installedc                 C   s   t  jd uS r   )r   r   r   r   r   r   rL      s   zsilk_profile._should_profilec                    s,      r fdd}|S td S )Nc                    s   t |  fdd}|S )Nc                     sJ  t  A zj}W n ty   tdjj w |j}|j}j} js(| _| j|| j	t
 t jd _   W d    n1 sGw   Y  z9z	| i |}W n tyc   d jd<  w W t   t
  jd<    W d    |S 1 sw   Y  |S t   t
  jd<    W d    w 1 sw   Y  w )Nz+Profile not implemented to decorate type %s)	func_namer6   rG   rH   rI   r   r   Tr%   r$   )r
   __code__AttributeErrorNotImplementedErrorr   r/   co_firstlinenoco_filenamer6   r:   r   r   r   r   r7   rB   	Exceptionr[   )r(   r)   	func_coderH   rG   rb   r*   r   r,   r   r   r-      sN   

	



z@silk_profile.__call__.<locals>.decorator.<locals>.wrapped_targetr   )	view_funcr-   rj   r   r   	decorator   s    z(silk_profile.__call__.<locals>.decoratorrJ   )rK   r   rQ   )r   r,   rl   r   rj   r   r.      s
   $
zsilk_profile.__call__c                    s    fdd j D S )Nc                    s   g | ]	}| j vr|qS r   )r8   r;   r   r   r   r>      s    z1silk_profile.distinct_queries.<locals>.<listcomp>)r9   r   r   r   r   distinct_queries   s   zsilk_profile.distinct_queries)NF)r/   r0   r1   r   r@   rB   rD   r   r[   r'   rK   rL   r.   rm   r4   r   r   r   r   r5   A   s    	+r5   c                   C   s   t d d S )NrE   )timesleepr   r   r   r   blah   rC   rp   __main__)rN   loggingrn   r   	functoolsr   django.appsr   django.confr   django.utilsr   silk.collectorr   silk.configr   silk.modelsr	   	getLoggerr   r
   r5   rp   r/   r   r   r   r   <module>   s&    
.u

