• File: stat_sender.cpython-37.pyc
  • Full Path: /home3/farmfre2/public_html/ALFA_DATA/alfasymlink/root/opt/alt/python37/lib/python3.7/site-packages/ssa/modules/__pycache__/stat_sender.cpython-37.pyc
  • Date Modified: 06/05/2023 12:25 PM
  • File size: 7.09 KB
  • MIME-type: text/x-bytecode.python
  • Charset: 8 bit
B

ed
@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(GdddeZ)e*dkr`e&ej+dej,dye)-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.modulesN)EmailMessage)partial)OptionalAny)get_admin_email)is_cl_solo_edition)SessionResponse)HTTPAdapter)RequestException)Retry)Common)
DecisionMaker)stat_server)SSAError)Mailerrender_report_table)read_sys_idsentry_init
duration_castformat_datecseZdZdZfddZdeddddZeeedd	d
Z	ee
ddd
Zee
dddZee
dddZeeedddZeedddZee
dddZZS)StatisticsSenderz4
    Send report to ClickHouse and over e-mails
    csttd|_|jdtt|_dt	d|_
t|_t
dtdgtddd	gdd
}t|d}t|_|jd|t|jjdd
|j_dS)Nstat_senderzStatisticsSender enabled: %szhttps://z
/api/clos-ssaPOSTiii)totalmethod_whiteliststatus_forcelistbackoff_factor)max_retries
)timeout)super__init__logging	getLoggerloggerinfo__package__rsys_idrch_endpointrmail_senderr	frozensetr
rsessionmountrrequest)selfZ
retry_confadapter)	__class__H/opt/alt/python37/lib/python3.7/site-packages/ssa/modules/stat_sender.pyr%*s

zStatisticsSender.__init__N)reportreturncCs0|dkrt}|jr"||||dS)zi
        Send given report over e-mail
        If no report given, get it from DecisionMaker API
        N)rget_json_reportsummary_notification_enabledemail_reportclickhouse_report)r2r7r5r5r6send;s


zStatisticsSender.send)report_viewr8cCs|}|r|drt|\}}|jj|dt|d|d}|jt|dd|ddd|j|d	d|dd
d|j	||SdS)z4
        Create and send e-mail with report
        domainsZ
ssa_reportdate)Z	recipienttemplater@htmljsonZreport_z.json)subtypefilenamerBz.htmlN)
get_mail_recipientgetrr-_messageradd_attachmentrCdumpsZ_send)r2r>Zmail_toZreport_tablemailmsgr5r5r6r;Gs 
zStatisticsSender.email_report)r8c
CsHy|ptStk
rB}z|jdt|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(errorstr)r2er5r5r6rF`sz#StatisticsSender.get_mail_recipientcCstddrdnd}y&tjd|dgddddj}Wn>tjtttfk
rv}z|j	
dt|d	Sd	}~XYnXyt
|d
dSt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)checktextcapture_outputzwmt-api utility failed: %sNconfigZreport_emailz!wmt-api returned invalid json: %sz(wmt-api returned unexpected response: %s)r
subprocessrunstdoutstripCalledProcessErrorAttributeErrorOSError
ValueErrorr(rOrPrCloadsrGJSONDecodeError)r2_utilZapi_responserQr5r5r6rMls(
z%StatisticsSender.wmt_api_report_emailc
Cs|dr|jd|jy"|jj|j|||d}WnNtk
r}z0|jj	d|d|jidt
dt||Wdd}~XYnX||S|jd	d
SdS)z+
        Send report to ClickHouse
        r?zSending POST request to %s)rCzPOST failed with %sendpoint)extraz'Failed to POST data to SSA API server: Nz*Report is empty, not sending to ClickHouseF)
rGr(r)r,r/post_ch_packclickhouse_formatrrOrrP_process_response)r2r>resprQr5r5r6r<s

$
z"StatisticsSender.clickhouse_report)valuer8cCs
t|dS)z<
        Pack given value into data field of a dict
        )data)dict)rir5r5r6reszStatisticsSender._ch_pack)original_reportr8cCs`t}xT|dD]F}||j|d|d|d|ddd|dDd	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|dt|dt|dddqS)rmreqs_numaverage_durationcorrelationr)urlZcount_requestsavg_durationrs)rGrfloat).0ur5r5r6
<listcomp>sz6StatisticsSender.clickhouse_format.<locals>.<listcomp>urls)	system_iddomainZcount_slow_urlsZcount_slow_requestsZtotal_requestsdetails)listrGappendr+)r2rlZ	ch_reportr|r5r5r6rfsz"StatisticsSender.clickhouse_format)responser8cCs|js(|jjd|j|jd|jiddS|jd|j|j|j|}|ddkrr|jjd|dd	|iddS|jd
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 %sstatusokz Received response with status %srzSent to ClickHouse successfullyT)	rr(rOstatus_codereasonrTr)rtrC)r2rresultr5r5r6rgsz"StatisticsSender._process_response)N)__name__
__module____qualname____doc__r%rkr=rrr;rPrFrMboolr<staticmethodrrer~rfr	rg
__classcell__r5r5)r4r6r%sr__main__zstat_sender_standalone.log)rElevel)1rr*rCr&rW
email.messager	functoolsrtypingrrclcommon.cpapirclcommon.lib.cleditionrrequestsrr	Zrequests.adaptersr
Zrequests.exceptionsrZ$requests.packages.urllib3.util.retryrcommonrdecision_makerrinternal.constantsrinternal.exceptionsrZinternal.mailerrrinternal.utilsrrrrrrbasicConfigINFOr=excprint
SystemExitr5r5r5r6<module>s<0