
    .hG              	           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m	Z	 d dl
mZmZ d dlmZmZmZmZmZmZmZmZ d dlmZ d dlmZ dd	ed
ededdfdZ G d d      Z e       Zy)    N)Path)Thread)Requesturlopen)SETTINGS__version__)ARGVENVIRONMENTGITIS_PIP_PACKAGEONLINEPYTHON_VERSIONRANKTESTS_RUNNING)GITHUB_ASSETS_NAMES)get_cpu_infourldatatimeoutreturnc                     	 t        j                  |d      j                         }t        | |ddi      }t	        ||      j                          y# t        $ r Y yw xY w)z"Send a one-shot JSON POST request.),:)
separatorszContent-Typezapplication/json)r   headers)r   N)jsondumpsencoder   r   close	Exception)r   r   r   bodyreqs        V/var/www/html/ai-service/venv/lib/python3.12/site-packages/ultralytics/utils/events.py_postr$      sY    zz$:6==?c~?Q.RSW%++- s   AA 	AAc                   &    e Zd ZdZdZddZdddZy)Eventsa  
    Collect and send anonymous usage analytics with rate-limiting.

    Event collection and transmission are enabled when sync is enabled in settings, the current process is rank -1 or 0,
    tests are not running, the environment is online, and the installation source is either pip or the official
    Ultralytics GitHub repository.

    Attributes:
        url (str): Measurement Protocol endpoint for receiving anonymous events.
        events (list[dict]): In-memory queue of event payloads awaiting transmission.
        rate_limit (float): Minimum time in seconds between POST requests.
        t (float): Timestamp of the last transmission in seconds since the epoch.
        metadata (dict): Static metadata describing runtime, installation source, and environment.
        enabled (bool): Flag indicating whether analytics collection is active.

    Methods:
        __init__: Initialize the event queue, rate limiter, and runtime metadata.
        __call__: Queue an event and trigger a non-blocking send when the rate limit elapses.
    zihttps://www.google-analytics.com/mp/collect?measurement_id=G-X8NCJYTQXM&api_secret=QLQrATrNSwGRFRLE-cbHJwNc           
         g | _         d| _        d| _        t        t        d         j
                  dk(  t        j                  rdn	t        rdndt        j                  dd	      d   t               t        t        t        t        j                         d
z        dd| _        t"        d   xr6 t$        dv xr, t&         xr# t(        xr t        xs t        j*                  dk(  | _        y)zRInitialize the Events instance with queue, rate limiter, and environment metadata.g      >@g        r   yologitpipother.   g  4&kCi  )cliinstallpythonCPUversionenv
session_idengagement_time_msecsync>   r   z.https://github.com/ultralytics/ultralytics.gitN)events
rate_limittr   r	   namer   is_repor   r   rsplitr   r   r
   roundrandommetadatar   r   r   r   originenabled)selfs    r#   __init__zEvents.__init__1   s    Q=%%/ #u.g$++C3A6>"$ 67$(

 V cc!!c c  a3::1a#a 	    c                 x   | j                   syt        | j                        dk  ri | j                  |j                  |j
                  t        v r|j
                  ndt        |      d}|j                  dk(  r|j                  |d<   | j                  j                  |j                  |d       t        j                         }|| j                  z
  | j                  k  ryt        | j                        }t        t         | j"                  t$        d   |d	fd
      j'                          g | _        || _        y)a3  
        Queue an event and flush the queue asynchronously when the rate limit elapses.

        Args:
            cfg (IterableSimpleNamespace): The configuration object containing mode and task information.
            device (torch.device | str, optional): The device type (e.g., 'cpu', 'cuda').
        N   custom)taskmodeldeviceexportformat)r;   paramsuuid)	client_idr8   T)targetargsdaemon)rB   lenr8   r@   rI   rJ   r   strmoderM   appendtimer:   r9   listr   r$   r   r   start)rC   cfgrK   rN   r:   payload_eventss         r#   __call__zEvents.__call__I   s    || t{{b --&)ii3F&FHf+	F xx8##&::x KKFCD IIKJ$//) dkk*(((6*:nUV	
 %' rE   )r   N)N)__name__
__module____qualname____doc__r   rD   r]    rE   r#   r&   r&      s    ( vC
0'rE   r&   )g      @) r   r?   rX   pathlibr   	threadingr   urllib.requestr   r   ultralyticsr   r   ultralytics.utilsr	   r
   r   r   r   r   r   r   ultralytics.utils.downloadsr   ultralytics.utils.torch_utilsr   rU   dictfloatr$   r&   r8   rb   rE   r#   <module>rl      sb         + - q q q ; 6s $   V Vr 
rE   