o
    i!                     @   s   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 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 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e!Z"G dd deeef Z#dS )    )Any)Optional)test)ITR_SKIPPING_LEVEL)TestModuleId)
TestStatus)TestVisibilityParentItem)TestVisibilitySessionSettings)TestVisibilityModule)
SESSION_ID)SESSION_TYPE)SUITE)TEST)TEST_EFD_ABORT_REASON)TEST_EFD_ENABLED)TEST_MANAGEMENT_ENABLED)EVENT_TYPES)record_event_created)record_event_finished)
get_logger)EFDTestStatusc                       s(  e Zd ZdZeZejZ	d4de	de
eeef  ddf fddZdeeef fdd	Zde	fd
dZdd Zdd Zdeddf fddZdd Zdd Zdd ZdefddZdefddZdd  Zd!d" Zd#efd$d%Zd&d' Zd(d) Z defd*d+Z!defd,d-Z"d.d/ Z#d0d1 Z$d2d3 Z%  Z&S )5TestVisibilitySessionzThis class represents a Test session and is the top level in the hierarchy of Test visibility items.

    It does not access its skip-level descendents directly as they are expected to be managed through their own parent
    instances.
    Nsession_settingsinitial_tagsreturnc                    sT   t d t d||j| | jj| _d | _d | _	d| _
d| _| tj|j d S )Nz$Initializing Test Visibility sessiontest_visibility_sessionFr   )logdebugsuper__init__session_operation_name_session_settingstest_command_test_command_efd_abort_reason_efd_is_faulty_session_efd_has_efd_failed_tests_atr_total_retriesset_tagr   ITR_TEST_CODE_COVERAGE_ENABLEDcoverage_enabled)selfr   r   	__class__ _/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/ci_visibility/api/_session.pyr   &   s   


zTestVisibilitySession.__init__c                 C   s   t t|  iS N)r   strget_span_idr+   r.   r.   r/   _get_hierarchy_tags9   s   z)TestVisibilitySession._get_hierarchy_tagsc                 C   s   | j S r0   )r!   r3   r.   r.   r/   get_session_settings>   s   z*TestVisibilitySession.get_session_settingsc                 C   sN   | j jjr| td | jd ur| t| j d S |  r%| td d S d S )NTfaulty)r!   efd_settingsenabledr(   r   r$   r   efd_is_faulty_sessionr3   r.   r.   r/   _set_efd_tagsA   s   

z#TestVisibilitySession._set_efd_tagsc                 C   s   |  td d S )NT)r(   r   r3   r.   r.   r/   _set_test_management_tagsJ      z/TestVisibilitySession._set_test_management_tagsitr_enabledc                    sb   t  | | tj| jj |r/| jjtj	krt	nt
}| tj| | tj| jdk dS dS )z5Set session-level tags based in ITR enablement statusr   N)r   _set_itr_tagsr(   r   ITR_TEST_SKIPPING_ENABLEDr!   itr_test_skipping_enableditr_test_skipping_levelr   r   r   ITR_TEST_SKIPPING_TYPEITR_DD_CI_ITR_TESTS_SKIPPED_itr_skipped_count)r+   r=   skipping_levelr,   r.   r/   r>   M   s   z#TestVisibilitySession._set_itr_tagsc                 C   s$   t | j| jj| jd u| jjd d S )N)
event_typetest_frameworkhas_codeownersis_unsupported_ci)r   _event_type_metric_namer!   test_framework_metric_name_codeownersrI   r3   r.   r.   r/   _telemetry_record_event_createdY   s   
z5TestVisibilitySession._telemetry_record_event_createdc                 C   s2   t | j| jj| jd u| jj|  rdnd d d S )Nr6   )rF   rG   rH   rI   "early_flake_detection_abort_reason)r   rJ   r!   rK   rL   rI   r9   r3   r.   r.   r/    _telemetry_record_event_finisheda   s   
z6TestVisibilitySession._telemetry_record_event_finishedc                 O   s   t d)Nz)Coverage data cannot be added to session.)NotImplementedError)r+   argskwargsr.   r.   r/   add_coverage_dataj   s   z'TestVisibilitySession.add_coverage_dataskipped_countc                 C   s   || _ | | jj d S r0   )rD   r>   r!   r@   )r+   rT   r.   r.   r/   set_skipped_countm   s   z'TestVisibilitySession.set_skipped_countcoverage_pctc                 C   s   |  tj| d S r0   )r(   r   TEST_LINES_PCT)r+   rV   r.   r.   r/   set_covered_lines_pctq      z+TestVisibilitySession.set_covered_lines_pctc                 C   s   | S r0   r.   r3   r.   r.   r/   get_sessiont   s   z!TestVisibilitySession.get_sessionc                 C   
   | j jjS r0   )r!   r7   r8   r3   r.   r.   r/   efd_is_enabledz      
z$TestVisibilitySession.efd_is_enabledabort_reasonc                 C   s
   || _ d S r0   )r$   )r+   r^   r.   r.   r/   set_efd_abort_reason}   r]   z*TestVisibilitySession.set_efd_abort_reasonc                 C   s   | j dur| j S | jjjdu rdS d}d}| j D ]}|j D ]}|j D ]}|d7 }| r6|d7 }q(q!q|| jjjkrBdS d||  }|| jjjk| _ | j S )a!  A session is considered "EFD faulty" if the percentage of tests considered new is greater than the
        given threshold, and the total number of news tests exceeds the threshold.

        NOTE: this behavior is cached on the assumption that this method will only be called once
        NFr      d   )r%   r!   r7   r8   	_childrenvaluesis_newfaulty_session_threshold)r+   total_tests_countnew_tests_count_module_suite_testnew_tests_pctr.   r.   r/   r9      s(   
z+TestVisibilitySession.efd_is_faulty_sessionc                 C   sj   | j jjr	|  rdS | j D ]"}|j D ]}|j D ]}| r0| tj	kr0   dS qqqdS NFT)
r!   r7   r8   r9   rb   rc   efd_has_retriesefd_get_final_statusr   ALL_FAILr+   rh   ri   rj   r.   r.   r/   efd_has_failed_tests   s   
z*TestVisibilitySession.efd_has_failed_testsc                 C   r[   r0   )r!   atr_settingsr8   r3   r.   r.   r/   atr_is_enabled   r]   z$TestVisibilitySession.atr_is_enabledc                 C   s   | j | jjjkS r0   )r'   r!   rr   max_session_total_retriesr3   r.   r.   r/   atr_max_retries_reached   r<   z-TestVisibilitySession.atr_max_retries_reachedc                 C   s   |  j d7  _ d S )Nr`   )r'   r3   r.   r.   r/   _atr_count_retry   rY   z&TestVisibilitySession._atr_count_retryc                 C   sl   | j jjsdS | j D ]'}|j D ]}|j D ]}| r!q| r1| tj	kr1   dS qqqdS rl   )
r!   rr   r8   rb   rc   is_quarantinedatr_has_retriesatr_get_final_statusr   FAILrp   r.   r.   r/   atr_has_failed_tests   s   

z*TestVisibilitySession.atr_has_failed_testsc                 C   st   | j jjsdS | j D ]+}|j D ]#}|j D ]}| s$| r%q| r5| t	j
kr5   dS qqqdS rl   )r!   test_management_settingsr8   rb   rc   rw   is_disabledis_attempt_to_fixattempt_to_fix_get_final_statusr   rz   rp   r.   r.   r/   attempt_to_fix_has_failed_tests   s   

z5TestVisibilitySession.attempt_to_fix_has_failed_testsr0   )'__name__
__module____qualname____doc__r   _event_typer   SESSIONrJ   r	   r   dictr1   r   r   r4   r5   r:   r;   boolr>   rM   rO   rS   intrU   floatrX   rZ   r\   r_   r9   rq   rs   ru   rv   r{   r   __classcell__r.   r.   r,   r/   r      s@    		r   N)$typingr   r   ddtrace.extr   ddtrace.ext.test_visibilityr   1ddtrace.ext.test_visibility._test_visibility_baser   "ddtrace.ext.test_visibility.statusr   (ddtrace.internal.ci_visibility.api._baser   r	   *ddtrace.internal.ci_visibility.api._moduler
   (ddtrace.internal.ci_visibility.constantsr   r   r   r   r   r   r   2ddtrace.internal.ci_visibility.telemetry.constantsr   /ddtrace.internal.ci_visibility.telemetry.eventsr   r   ddtrace.internal.loggerr   ,ddtrace.internal.test_visibility._efd_mixinsr   r   r   r   r.   r.   r.   r/   <module>   s.    