HEX
Server: Apache
System: Linux zacp120.webway.host 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64
User: govancoz (1003)
PHP: 8.3.26
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/ssa/modules/__pycache__/stat_sender.cpython-37.pyc
B

V�d��
@sddZdZddlZddlZddlZddlmZddlmZddl	m
Z
mZddlm
Z
ddlmZdd	lmZmZdd
lmZddlmZddlmZd
dlmZd
dlmZddlmZddlm Z ddl!m"Z"m#Z#ddl$m%Z%m&Z&m'Z'm(Z(Gdd�de�Z)e*dk�r`e&�ej+dej,d�ye)��-�Wn4e k
�r^Z.ze/e.�e0d
��WddZ.[.XYnXdS)zd
This module contains SSA classes for sending e-mails with report
and sending reports to ClickHouse
zssa.modules�N)�EmailMessage)�partial)�Optional�Any)�get_admin_email)�is_cl_solo_edition)�Session�Response)�HTTPAdapter)�RequestException)�Retry�)�Common)�
DecisionMaker�)�stat_server)�SSAError)�Mailer�render_report_table)�read_sys_id�sentry_init�
duration_cast�format_datecs�eZdZdZ�fdd�Zdedd�dd�Zeeed�d	d
�Z	ee
d�dd
�Zee
d�dd�Zee
d�dd�Zeeed�dd��Zeed�dd�Zee
d�dd�Z�ZS)�StatisticsSenderz4
    Send report to ClickHouse and over e-mails
    cs�t���t�d�|_|j�dt�t�|_dt	�d�|_
t�|_t
dtdg�tddd	g�dd
�}t|d�}t�|_|j�d|�t|jjdd
�|j_dS)N�stat_senderzStatisticsSender enabled: %szhttps://z
/api/clos-ssa��POSTi�i�i�)�total�method_whitelist�status_forcelist�backoff_factor)�max_retries�
)�timeout)�super�__init__�logging�	getLogger�logger�info�__package__r�sys_idr�ch_endpointr�mail_senderr�	frozensetr
r�session�mountr�request)�selfZ
retry_conf�adapter)�	__class__��H/opt/alt/python37/lib/python3.7/site-packages/ssa/modules/stat_sender.pyr%*s

zStatisticsSender.__init__N)�report�returncCs0|dkrt���}|jr"|�|�|�|�dS)zi
        Send given report over e-mail
        If no report given, get it from DecisionMaker API
        N)r�get_json_report�summary_notification_enabled�email_report�clickhouse_report)r2r7r5r5r6�send;s


zStatisticsSender.send)�report_viewr8cCs�|��}|r�|�d�r�t|�\}}|jj|dt|d�|d�}|jt�|�dd|d�d�d�|j|d	d|d�d
�d�|j�	|�|SdS)z4
        Create and send e-mail with report
        �domainsZ
ssa_report�date)Z	recipient�templater@�html�jsonZreport_z.json)�subtype�filenamerBz.htmlN)
�get_mail_recipient�getrr-�_messager�add_attachmentrC�dumpsZ_send)r2r>Zmail_toZreport_table�mail�msgr5r5r6r;Gs 
zStatisticsSender.email_report)r8c
CsHy|��pt�Stk
rB}z|j�dt|��Wdd}~XYnXdS)z�
        Retrieve a recipient's e-mail:
        1. get address from a wmt-api utility
        2. if command failed or address is empty, get address of server admin
        zget_admin_email failed with: %sN)�wmt_api_report_emailr�	Exceptionr(�error�str)r2�er5r5r6rF`sz#StatisticsSender.get_mail_recipientcCs�tdd�rdnd}y&tjd|��dgdddd�j��}Wn>tjtttfk
rv}z|j	�
dt|��d	Sd	}~XYnXyt�
|��d
��d�Stjk
r�}z|j	�
dt|��Wd	d	}~XYn"tk
r�|j	�
d
|�YnXd	S)zD
        Retrieve a recipient's e-mail address from WMT API
        T)�skip_jwt_checkzwmt-api-solozwmt-apiz&/usr/share/web-monitoring-tool/wmtbin/z--config-get)�check�text�capture_outputzwmt-api utility failed: %sN�configZreport_emailz!wmt-api returned invalid json: %sz(wmt-api returned unexpected response: %s)r�
subprocess�run�stdout�strip�CalledProcessError�AttributeError�OSError�
ValueErrorr(rOrPrC�loadsrG�JSONDecodeError)r2�_utilZapi_responserQr5r5r6rMls(
z%StatisticsSender.wmt_api_report_emailc
Cs�|�d�r�|j�d|j�y"|jj|j|�|�|��d�}WnNtk
r�}z0|jj	d|d|jid�t
dt|����|�Wdd}~XYnX|�|�S|j�d	�d
SdS)z+
        Send report to ClickHouse
        r?zSending POST request to %s)rCzPOST failed with %s�endpoint)�extraz'Failed to POST data to SSA API server: Nz*Report is empty, not sending to ClickHouseF)
rGr(r)r,r/�post�_ch_pack�clickhouse_formatrrOrrP�_process_response)r2r>�resprQr5r5r6r<�s

$
z"StatisticsSender.clickhouse_report)�valuer8cCs
t|d�S)z<
        Pack given value into data field of a dict
        )�data)�dict)rir5r5r6re�szStatisticsSender._ch_pack)�original_reportr8cCs`t�}xT|�d�D]F}|�|j|�d�|�d�|�d�|�d�dd�|�d�D�d	��qW|S)
zd
        Format local report for sending to ClickHouse
        (required structures differ)
        r?�name�	slow_urls�	slow_reqs�
total_reqsc
Ss<g|]4}|�d�|�d�t|�d��t|�dd��d��qS)rm�reqs_num�average_duration�correlationr)�urlZcount_requests�avg_durationrs)rGr�float)�.0�ur5r5r6�
<listcomp>�sz6StatisticsSender.clickhouse_format.<locals>.<listcomp>�urls)�	system_id�domainZcount_slow_urlsZcount_slow_requestsZtotal_requests�details)�listrG�appendr+)r2rlZ	ch_reportr|r5r5r6rf�sz"StatisticsSender.clickhouse_format)�responser8cCs�|js(|jjd|j|jd|jid�dS|j�d|j|j|j�|��}|ddkrr|jjd|dd	|id�dS|j�d
�dS)z�
        Check received response
        :param response: a requests.Response object
        :return: True in case of success, False otherwise
        z&Unable to connect to server with %s:%sZ	resp_text)rcFz[%s:%s] Response received %s�status�okz Received response with status %sr�zSent to ClickHouse successfullyT)	r�r(rO�status_code�reasonrTr)rtrC)r2r��resultr5r5r6rg�sz"StatisticsSender._process_response)N)�__name__�
__module__�__qualname__�__doc__r%rkr=rrr;rPrFrM�boolr<�staticmethodrrer~rfr	rg�
__classcell__r5r5)r4r6r%sr�__main__zstat_sender_standalone.log)rE�level)1r�r*rCr&rW�
email.messager�	functoolsr�typingrr�clcommon.cpapir�clcommon.lib.cleditionr�requestsrr	Zrequests.adaptersr
Zrequests.exceptionsrZ$requests.packages.urllib3.util.retryr�commonr�decision_makerr�internal.constantsr�internal.exceptionsrZinternal.mailerrr�internal.utilsrrrrrr��basicConfig�INFOr=�exc�print�
SystemExitr5r5r5r6�<module>s<0