
    [,h                     `    d Z ddlmZ ddlmZ ddlmZmZ ddlmZ ddlm	Z	 dZ
 G d d	      Zy
)z"Contains the mutlihar parse object    )stdev)mean)UnionList)cached_property   )	HarParserc                   x   e Zd ZdZdefdZdedefdZdede	e
ef   fdZeded   fd	       Zedefd
       Zede	e
ef   fd       Zede	e
ef   fd       Zede	e
ef   fd       Zede	e
ef   fd       Zede	e
ef   fd       Zede	e
ef   fd       Zede	e
ef   fd       Zede	e
ef   fd       Zy)MultiHarParserz
    An object that represents multiple HAR files OF THE SAME CONTENT.
    It is used to gather overall statistical data in situations where you have
    multiple runs against the same web asset, which is common in performance
    testing.
    Nc                 .    || _         || _        || _        y)a3  
        :param har_data: A list of dict representing the JSON
        of a HAR file. See the docstring of HarParser.__init__ for more detail.
        :type har_data: List[dict]
        :param page_id: If a page ID is provided, the
        multiparser will return aggregate results for this specific page. If
        not, it will assume that there is only one page in the run (this was
        written specifically for that use case).
        :type page_id: str
        :param decimal_precision: The precision of the.
        :type decimal_precision: int
        N)har_datapage_iddecimal_precision)selfr   r   r   s       N/var/www/html/trade_iq/venv/lib/python3.12/site-packages/haralyzer/multihar.py__init__zMultiHarParser.__init__   s     !!2    
asset_typereturnc                 x    g }| d}| j                   D ]#  }t        ||d      }||j                  |       % |S )z
        Just a list of the load times of a certain asset type for each page

        :param asset_type: The asset type to return load times for
        :type asset_type: str
        :return: List of load times
        :rtype: list
        
_load_timeN)pagesgetattrappend)r   r   
load_times
search_strhar_pagevals         r   get_load_timeszMultiHarParser.get_load_times$   sQ     
"|:.


 	'H(J5C!!#&	' r   c                    g }|dk(  r:| j                   D ]*  }|j                  |j                  |j                         , nZ|| j                  vr;|dk7  r6t	        dd j                  dj                  | j                                    | j                  |      }|rt        |      syt        t        |      | j                        S )aW  
        Returns the standard deviation for a set of a certain asset type.

        :param asset_type: The asset type to calculate standard deviation for.
        :type asset_type: str
        :returns: Standard deviation, which can be an `int` or `float`
            depending on the self.decimal_precision
        :rtype: int, float
        ttfbpagez asset_type must be one of:
ttfb
r   
)r   time_to_first_byter   asset_types
ValueErrorformatjoinr   sumroundr   r   )r   r   r   r"   s       r   	get_stdevzMultiHarParser.get_stdev5   s     


 ?**6%%d&=&=>? t///J&4H4QC8??IId../  ,,Z8JZU:&(>(>??r   HarPagec                     g }| j                   D ]f  }t        |      }| j                  r=|j                  D ]-  }|j                  | j                  k(  s|j	                  |       / X||j                  z   }h |S )z
        Aggregate pages of all the parser objects.

        :return: All the pages from parsers
        :rtype: List[haralyzer.assets.HarPage]
        )r   )r   r	   r   r   r   )r   r   har_dict
har_parserr"   s        r   r   zMultiHarParser.pagesR   sw      	1H"H5J||&,, +D||t||3T*+ 
 0 00	1 r   c                 4    | j                   d   j                  S )zy
        Mimic the asset types stored in HarPage

        :return: Asset types from HarPage
        :rtype: dict
        r   )r   r%   )r   s    r   r%   zMultiHarParser.asset_typese   s     zz!}(((r   c                     g }| j                   D ]*  }|j                  |j                  |j                         , t        t	        |      | j
                        S )z
        :returns: The aggregate time to first byte for all pages.
            Can be an `int` or `float` depending on the self.decimal_precision
        :rtype: int, float
        )r   r$   r   r*   r   r   )r   r!   r"   s      r   r$   z!MultiHarParser.time_to_first_byteo   sS     JJ 	5D&&2D334	5 T$Z!7!788r   c                 b    | j                  d      }t        t        |      | j                        S )z
        :returns: Average total load time for all runs (not weighted).
            Can be an `int` or `float` depending on the self.decimal_precision
        :rtype: int, float
        r"   r   r*   r   r   r   r   s     r   page_load_timezMultiHarParser.page_load_time|   ,     ((0
T*%t'='=>>r   c                 b    | j                  d      }t        t        |      | j                        S )z
        :returns: Aggregate javascript load time.
            Can be an `int` or `float` depending on the self.decimal_precision
        :rtype: int, float
        jsr3   r4   s     r   js_load_timezMultiHarParser.js_load_time   s,     ((.
T*%t'='=>>r   c                 b    | j                  d      }t        t        |      | j                        S )z
        :returns: Aggregate css load time for all pages.
            Can be an `int` or `float` depending on the self.decimal_precision
        :rtype: int, float
        cssr3   r4   s     r   css_load_timezMultiHarParser.css_load_time   s,     ((/
T*%t'='=>>r   c                 b    | j                  d      }t        t        |      | j                        S )z
        :returns: Aggregate image load time for all pages.
            Can be an `int` or `float` depending on the self.decimal_precision
        :rtype: int, float
        imager3   r4   s     r   image_load_timezMultiHarParser.image_load_time   ,     ((1
T*%t'='=>>r   c                 b    | j                  d      }t        t        |      | j                        S )z
        :returns: Aggregate html load time for all pages.
            Can be an `int` or `float` depending on the self.decimal_precision
        :rtype: int, float
        htmlr3   r4   s     r   html_load_timezMultiHarParser.html_load_time   r6   r   c                 b    | j                  d      }t        t        |      | j                        S )z
        :returns: Aggregate audio load time for all pages.
            Can be an `int` or `float` depending on the self.decimal_precision
        :rtype: int, float
        audior3   r4   s     r   audio_load_timezMultiHarParser.audio_load_time   r@   r   c                 b    | j                  d      }t        t        |      | j                        S )z
        :returns: Aggregate video load time for all pages.
            Can be an `int` or `float` depending on the self.decimal_precision
        :rtype: int, float
        videor3   r4   s     r   video_load_timezMultiHarParser.video_load_time   r@   r   )__name__
__module____qualname____doc__DECIMAL_PRECISIONr   strlistr   r   intfloatr+   propertyr   r   r   dictr%   r$   r5   r9   r<   r?   rC   rF   rI    r   r   r   r      s    *.AR 3"  "@C @E#u*,= @: tI  $ )T ) ) 
9E#u*$5 
9 
9 ?c5j 1 ? ? ?eCJ/ ? ? ?uS%Z0 ? ? ?sEz!2 ? ? ?c5j 1 ? ? ?sEz!2 ? ? ?sEz!2 ? ?r   r   N)rM   
statisticsr   r   typingr   r   r   assetsr	   rN   r   rU   r   r   <module>rY      s)    (    +  u? u?r   