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

edC+@sdZdZddlZddlZddlZddlmZddlZddl	m
Z
ddlmZddl
mZdd	lmZmZmZd
dlmZd
dlmZd
d
lmZd
dlmZmZGdddeZedkreejdejdeZ e dS)z*
This module contains DecisionMaker class
zssa.modulesN)isfile)is_cl_solo_edition)setup_database)Common)iter_domains_dataiter_urls_dataget_url_durations)
load_tunables)ssa_tunables_schema)report_path)previous_day_datesentry_initcs"eZdZdZd/fdd	ZddZeeddd	Ze	edd
dZ
e	eddd
Ze	e
dddZeddZedddZeedddZeedddZeedddZeeeeedddZd d!Zed"d#d$Zedd%d&Zeed'd(d)Zd*d+Zeeed,d-d.ZZS)0
DecisionMakerz,
    SSA Decision maker implementation.
    Ncs8ttd|_|jdt|r,|nt|_dS)NZdecision_makerzDecisionMaker enabled: %s)	super__init__logging	getLoggerloggerinfo__package__rengine)selfr)	__class__K/opt/alt/python37/lib/python3.7/site-packages/ssa/modules/decision_maker.pyr's
zDecisionMaker.__init__cCsZ|jd|jd|j||_|jd|j|}|||jd||S)NzDecisionMaker startedzDecisionMaker loaded config: %sz!DecisionMaker loaded tunables: %szDecisionMaker report: %s)rrdebugconfigload_external_confexternal_tunablesdata_processingadd_json_report)rreportrrr__call__.s

zDecisionMaker.__call__)returncCstjt|S)zE
        Full path to given filename in DM reports directory
        )ospathjoinr
)namerrr_report_file9szDecisionMaker._report_filecCs
|dS)zU
        Full path to current DM report: report.json in DM reports directory
        zreport.json)r*)rrrrcurrent_report_file@sz!DecisionMaker.current_report_filecCsttgdS)z&
        Returns empty report
        )datedomains)dictr)rrrr
_empty_reportGszDecisionMaker._empty_reportcCsdhS)Ncorrelationr)rrrrsolo_filtered_optionsNsz#DecisionMaker.solo_filtered_optionscCs
tdtS)z"Load external configuration valueszssa.json)rrrrrrrRsz DecisionMaker.load_external_confc
Cs|j}xnt|jD]^}t}d}tt|j|j}xt|j|jt|D]\}}|	|rt|j
d|qR||jkrR|
|dtdgd|dsqR||d|j}||}	|	dkr|j
dt|qR||drR||rRt|	}
t|d}||7}|t||t|
t|d	d
qRW|r|j||jdd}|d
t|jt||t|j|dqW|d
r|j|d
|jdd|d
<|S)z
        Going through the list of domains, for each domain we go through
        the list of urls. During data processing, we will form
        the resulting dictionary.
        rz
%s ignoredurl_throttled_reqsurl_total_reqsNzUnable to get durations for %s
url_slow_reqsz.2f)r)reqs_numZaverage_durationr0r6)list_to_sort	leave_topkey_for_sortingr-)r)Z	slow_urls	slow_reqs
total_reqsurlsr:)r/rrlistr.r	domain_namerkeys
is_ignoredrrnon_url_fieldsis_throttling_suitablegetget_correlationdomain_total_reqserrorstrrequest_number_exceededcorrelation_conditionsnpmeansumappendintfloatreport_sortingurls_numberlendomains_number)
rr#domain_data	urls_dataZdomain_slow_reqsdomain_url_durationsZdomain_data_keyZdomain_data_valuecorrelation_value	durationsZaverage_duration_calculationZsum_url_slow_reqssorted_urlsrrrr!Ws`












zDecisionMaker.data_processing)r5r%cs*|jpdfddtdtDS)z
        Based on the 'url_slow_reqs' list, a new list will be formed,
        where the elements of the original list will be iteratively
        summed by the number of elements equal to 'time'
        r3cs g|]}t||qSr)rL).0i)timer5rr
<listcomp>sz@DecisionMaker.list_handling_considering_time.<locals>.<listcomp>r)r\rangerR)rr5r)r\r5rlist_handling_considering_times
z,DecisionMaker.list_handling_considering_time)url_slow_reqs_by_timer%cCs x|D]}||jkrdSqWdS)z_
        This functions will check if any of elements is greater than "request_number"
        TF)request_number)rr`r[rrr$compare_elements_with_request_numbers

z2DecisionMaker.compare_elements_with_request_number)r4rEcCs|js
dStt||S)z}
        Calculates the correlation coefficient using the "url_total_reqs" and
        the "domain_total_reqs" lists
        r)r0rJamincorrcoef)rr4rErrrrDszDecisionMaker.get_correlation)r7r8r9r%cs.|jfdddd|r&|d|S|SdS)a
        Will sort the domain list by "slow_reqs", the goal is to leave only
        "domains_number" of uppers, also per each domain will sort urls by
        "reqs_num", the goal is to leave only "urls_number" of uppers.
        leave_top == 0 allows to keep the full list
        cs|S)Nr)dict_)r9rr<lambda>z.DecisionMaker.report_sorting.<locals>.<lambda>T)keyreverseN)sort)r7r8r9r)r9rrPs
	zDecisionMaker.report_sortingc	Cs|j}t|rt|D}yt|}Wntjk
r@d}YnX|dddd}WdQRXd|d}||}t	
||dS)	z#
        Rename old report
        unknownr,z
dd.mm.yyyy._NZreport__z.json)r+ropenjsonloadJSONDecodeErrorrCreplacer*r&rename)rZ
old_reportZ	json_datadZdate_from_reportZnew_report_nameZ
new_reportrrrrename_old_reports


zDecisionMaker.rename_old_report)r#c	Cs:|t|jddd}tj||dddWdQRXdS)z#
        Makes json report
        wzutf-8)encodingF)ensure_asciiindentN)rurnr+rodump)rr#frrrr"szDecisionMaker.add_json_reportc	sjtddrfdd}nd}y(tj}tj||d}WdQRXWn ttjfk
rdj}YnX|S)zT
        Return contents of current report or empty report in case of error
        T)skip_jwt_checkcsfdd|DS)z
                Filter decoded object by fields, given in solo_filtered_options
                property, e.g. 'correlation'
                cs i|]\}}|jkr||qSr)r1)rZkv)rrr
<dictcomp>szJDecisionMaker.get_json_report.<locals>._filtering_hook.<locals>.<dictcomp>)items)value)rrr_filtering_hooksz6DecisionMaker.get_json_report.<locals>._filtering_hookN)object_hook)rrnr+rorpOSErrorrqr/)rrr#Zreport_dictr)rrget_json_reports
zDecisionMaker.get_json_report)rWr%cCs|js
dS||jkS)a
        If correlation flag is enabled - we'll compare correlation_coefficient
        from configuration with calculated correlation coefficient.
        If the calculated value exceeds the configuration value - we return
        True otherwise False. At the same time if correlation flag is disabled -
        we'll also return "True" since in this case the correlation coefficient
        is not checked and its value is specified as zero in final report.
        T)r0correlation_coefficient)rrWrrrrIs	z$DecisionMaker.correlation_conditionscCs||}||S)z
        At least one element from the received list (url_slow_reqs_by_time)
        must be greater than request_number
        )r_rb)rr5r`rrrrHsz%DecisionMaker.request_number_exceeded)r2r4r%cCs4t|t|d}|jd|||jddkS)zY
        Check that percent of throttled requests per URL passes given threshold
        dzCalculated throttled percent %sallowed_throttling_percentager)rLrrr rC)rr2r4throttled_percentrrrrBs
z$DecisionMaker.is_throttling_suitable)N)__name__
__module____qualname____doc__rr$staticmethodrGr*propertyr+r.r/setr1rr!r=r_boolrbrDrNrPrur"rrIrHrB
__classcell__rr)rrr"s4<		
	r__main__zdecision_maker_standalone.log)filenamelevel)!rrrorr&os.pathrnumpyrJclcommon.lib.cleditionrssa.dbrcommonrstoragerrr	
configurationrZconfiguration.schemesrinternal.constantsr
internal.utilsrrrrbasicConfigINFOdmrrrr<module>
s,p