o
    gi$                     @   s   d Z ddlZddlZddlZddlZddlZddlZddlZddlm	Z	 ddl
Z
ddlmZ ddlmZ ddlmZ G dd dZdS )	z
This class implements the basics for sending usage statistics to the borb server(s).
These kinds of checks are useful to get an idea of which functionality is most often used,
where development effort needs to be spent, etc
    N)Decimal)License)PersistentRandomUserID)Versionc                   @   s   e Zd ZU dZG dd dZdZeed< dZe	ed< g Z
eje ed< d	Zeed
< dZeed< edejd defddZedeje	 fddZed!ddZed!ddZed!ddZed!ddZed"de	dejd ddfdd ZdS )#AsyncUsageStatisticsz
    This class implements the basics for sending usage statistics to the borb server(s).
    These kinds of checks are useful to get an idea of which functionality is most often used,
    where development effort needs to be spent, etc
    c                   @   sP   e Zd ZdZdededefddZdefdd	Zdefd
dZdefddZ	dS )zAsyncUsageStatistics.Eventzl
        This class represents something that happened that needs to be sent to the borb server(s).
        count
event_namenumber_of_pagesc                 C   s*   || _ tt d | _|| _|g| _d S )N  )_countinttime_creation_timestamp_in_ms_event_name_number_of_pages)selfr   r   r	    r   W/home/ubuntu/.local/lib/python3.10/site-packages/borb/license/async_usage_statistics.py__init__$   s   z#AsyncUsageStatistics.Event.__init__returnc                 C   s&   t | jdkr	dS t| jt | j S )a&  
            This function returns the average number of pages in this Event.
            e.g.    2 PDF documents have been read, one of 10 pages and one of 2 pages.
                    This function would return 6.
            :return:    the average number of pages in this Event
            r   )lenr   sumr   r   r   r   get_avg_number_of_pages/   s   z2AsyncUsageStatistics.Event.get_avg_number_of_pagesc                 C      t | jdkr	dS t| jS )a'  
            This function returns the maximum number of pages in this Event.
            e.g.    2 PDF documents have been read, one of 10 pages and one of 2 pages.
                    This function would return 10.
            :return:    the maximum number of pages in this Event
            r   )r   r   maxr   r   r   r   get_max_number_of_pages:      
z2AsyncUsageStatistics.Event.get_max_number_of_pagesc                 C   r   )a&  
            This function returns the minimum number of pages in this Event.
            e.g.    2 PDF documents have been read, one of 10 pages and one of 2 pages.
                    This function would return 2.
            :return:    the minimum number of pages in this Event
            r   )r   r   minr   r   r   r   get_min_number_of_pagesE   r   z2AsyncUsageStatistics.Event.get_min_number_of_pagesN)
__name__
__module____qualname____doc__r   strr   r   r   r   r   r   r   r   Event   s    
r%   T_ENABLEDzEhttps://cztmincfqq4fobtt6c7ect7gli0isbwx.lambda-url.us-east-1.on.aws/_ENDPOINT_URL_EVENT_QUEUEi  !_MAX_NUMBER_OF_MS_BETWEEN_SENDINGF_REGISTERED_FOR_SYS_EXIT_EVENTdocumentDocumentr   c                 C   s$   | d u rdS t |   ptdS )Nr   )r   get_document_infoget_number_of_pagesr   )r+   r   r   r   _get_number_of_pages`   s   z)AsyncUsageStatistics._get_number_of_pagesc                   C   s   t  pt S )N)r   get_user_idr   getr   r   r   r   _get_user_idf   s   z!AsyncUsageStatistics._get_user_idNc                  C   s   t jsd S t j} g t _| D ]O}t  t |j|jt t	 |
 | | tjtt d t d}ddd}ztjt j|t|d W q ty\ } zW Y d }~qd }~ww d S )Nr
   )anonymous_user_idcompanyr   eventlicense_valid_from_in_mslicense_valid_until_in_msr	   min_number_of_pagesmax_number_of_pagessys_platformutc_time_in_msversionzapplication/jsonz
text/plain)zContent-typeAccept)headersdata)r   r&   r(   r2   r   get_companyr   r   get_valid_from_in_msget_valid_until_in_msr   r   r   sysplatformr   r   r   get_versionrequestspostr'   jsondumps	Exception)
queue_copyevtjson_payloadr>   er   r   r   _process_event_queuej   s<   

z)AsyncUsageStatistics._process_event_queuec               
   C   s   t jsd S tt jdkrd S tt d } | tdd t jD  }|t jk r)d S ztj	t j
d  W d S  tyK } z
dt _W Y d }~d S d }~ww )Nr   r
   c                 S   s   g | ]}|j qS r   )r   .0xr   r   r   
<listcomp>   s    zKAsyncUsageStatistics._send_usage_statistics_immediately.<locals>.<listcomp>)targetF)r   r&   r   r(   r   r   r   r)   	threadingThreadrO   startrJ   )	now_in_msdelta_in_msrN   r   r   r   "_send_usage_statistics_immediately   s(   
z7AsyncUsageStatistics._send_usage_statistics_immediatelyc                   C   
   dt _dS )za
        This function disables the sending of usage statistics
        :return:    None
        FNr   r&   r   r   r   r   disable      
zAsyncUsageStatistics.disablec                   C   r[   )z`
        This function enables the sending of usage statistics
        :return:    None
        TNr\   r   r   r   r   enable   r^   zAsyncUsageStatistics.enable r   c                    s  t jsdS t js)dt _t t u r)tt j t		t	j
t j t		t	jt j t |}tt jdksAt fddt jD sSt jt jd |d t   dS z*tdtt jD ]}t j| j kr{t j|  jd7  _t j| j|  nq\W n   Y t   dS )a   
        This method sends (anonymous) usage statistics to the borb server
        :param event_name:  the name of the event (e.g. "PDF.dumps")
        :param document:    the document to which the event is applied
        :return:            None
        NTr   c                    s   g | ]}|j  kqS r   )r   rP   r   r   r   rS      s    z>AsyncUsageStatistics.send_usage_statistics.<locals>.<listcomp>   )r   r   r	   )r   r&   r*   rU   current_threadmain_threadatexitregisterrO   signalSIGINTSIGTERMr/   r   r(   anyappendr%   rZ   ranger   r   r   )r   r+   r	   ir   ra   r   send_usage_statistics   sD   

z*AsyncUsageStatistics.send_usage_statistics)r   N)r`   N)r    r!   r"   r#   r%   r&   bool__annotations__r'   r$   r(   typingListr)   r   r*   staticmethodOptionalr/   r2   rO   rZ   r]   r_   rn   r   r   r   r   r      s,   
 2/ $r   )r#   re   rH   rg   rC   rU   r   rq   decimalr   rF   borb.license.licenser   &borb.license.persistent_random_user_idr   borb.license.versionr   r   r   r   r   r   <module>   s   