Mj-8dZdZddlZddlZddlZddlZddlmZddlZ ddl m Z ddl m Z ddlmZmZd d lmZd d lmZd d lmZd d lmZmZdZGdde Zedkr7eejdejeZedSdS)z* This module contains DecisionMaker class z ssa.modulesN)isfile)setup_database)Common)iter_domains_dataiter_domain_url_data) load_tunables)ssa_tunables_schema) report_path)previous_day_date sentry_initic \eZdZdZd fd ZdZedefdZe defdZ e de fdZ e de fd Zed Zde fd Zd edefd ZdedefdZdedefdZededededefdZdZde fdZde fdZdedefdZdZdededefdZxZS)! DecisionMakerz, SSA Decision maker implementation. Ncttjd|_|jdt |r|n t|_dS)Ndecision_makerzDecisionMaker enabled: %s) super__init__logging getLoggerloggerinfo __package__rengine)selfr __class__s Q/opt/cloudlinux/venv/lib64/python3.11/site-packages/ssa/modules/decision_maker.pyrzDecisionMaker.__init__,s_ '(899  4kBBB &z1DecisionMaker.data_processing..sXXXAuuXXXrT)reverser4)r- slow_urls slow_reqs total_reqsurlsrK list_to_sort leave_topkey_for_sorting)#r6 isinstance urls_numberintrrlistr domain_name is_ignoredrr non_url_fieldsis_throttling_suitablegetget_correlationdomain_total_reqserrorstrrequest_number_exceededcorrelation_conditionsnpmeansumr5MAX_REPORT_URL_LENfloatlenheapqheappush heapreplacesortedappendreport_sortingdomains_number)rr&rP domain_data urls_dataorderdomain_slow_reqsurl durationsurl_datacorrelation_valueaverage_duration_calculationsum_url_slow_reqsrGitem sorted_urlss rr$zDecisionMaker.data_processing[s1#)343CS(I(IidN^abNbNbD$$hi ,T[99: : KIE ,@kNe,f,f! ! (Y??3''K%%lC888$---22LL!5tQC"H~~FFQaHb$($8$8BR9SU`Ur$s$s! K%%&FCQQQ//0IJJtOjOj%PP46793E3E0(+H_,E(F(F%$(99$  !4"4!45!2),-I)J)J$)->*D*D$E$E E.vu=D9~~ 11y$7777 ! ,,))T:::QJE YXyRV8W8W8WXXX y!(((4"%k"2"2"2#&{'D#E#E(  )   $ 3 3#I.$:M_j!4!!F9  rrAcn|jpdfdtdtDS)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' r?cDg|]}t||zSr9)rc)rEitimerAs rrHz@DecisionMaker.list_handling_considering_time..s.]]]QM!dQh,/00]]]rr)r}rangerf)rrAr}s `@rlist_handling_considering_timez,DecisionMaker.list_handling_considering_timesC yB]]]]]%3}CUCUW[:\:\]]]]rurl_slow_reqs_by_timec,|D]}||jkrdSdS)z_ This functions will check if any of elements is greater than "request_number" TF)request_number)rrr|s r$compare_elements_with_request_numberz2DecisionMaker.compare_elements_with_request_numbers2'  AD'''tt(urr@r\cb|jsdStjtj||S)z} Calculates the correlation coefficient using the "url_total_reqs" and the "domain_total_reqs" lists r)r8raamincorrcoef)rr@r\s rr[zDecisionMaker.get_correlations1  1wr{>3DEEFFFrrOrPrQcT|fdd|r |d|S|S)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 c|SNr9)dict_rQs rz.DecisionMaker.report_sorting..s E/,BrT)keyrIN)sortrNs `rrlzDecisionMaker.report_sortingsE BBBBDQQQ    + + rc|j}t|rt|5} tj|}|dddd}n#tj$rd}YnwxYwdddn #1swxYwYd|d}||}tj ||dSdS) z# Rename old report r3z dd.mm.yyyy.rFunknownNreport__z.json) r1ropenjsonloadrZreplaceJSONDecodeErrorr.r*rename)r old_report json_dataddate_from_reportnew_report_name new_reports rrename_old_reportzDecisionMaker.rename_old_reports@- *   .j!! UYU ),,A()uuV\'B'B'J'J3PS'T'T$$+111'0$$$1 U U U U U U U U U U U U U U UA)9@@@O**?;;J Ij* - - - - - . .s3B A'+B 'A;8B :A;;B  BBr&c|t|jdd5}tj||ddddddS#1swxYwYdS)z# Makes json report wzutf-8)encodingF) ensure_asciiindentN)rrr1rdump)rr&fs rr%zDecisionMaker.add_json_reports    $*C' B B B ?a IfaeA > > > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sAAAcd} t|j5}tj||}dddn #1swxYwYn##ttjf$r |j}YnwxYw|S)zT Return contents of current report or empty report in case of error N) object_hook)rr1rrOSErrorrr6)r_filtering_hookr& report_dicts rget_json_reportzDecisionMaker.get_json_reports -d.// M6"iOLLL  M M M M M M M M M M M M M M M-. - - -,KKK -s.A; A?A?AA'&A'ruc*|jsdS||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)r8correlation_coefficient)rrus rr`z$DecisionMaker.correlation_conditionss" 4 4#???rcV||}||S)z At least one element from the received list (url_slow_reqs_by_time) must be greater than request_number )rr)rrArs rr_z%DecisionMaker.request_number_exceeded s- !% C CM R R889NOOOrr>ct|t|z dz}|jd|||jddkS)zY Check that percent of throttled requests per URL passes given threshold dzCalculated throttled percent %sallowed_throttling_percentager)rcrr r#rZ)rr>r@throttled_percents rrYz$DecisionMaker.is_throttling_suitables_!!344s>7J7JJcQ ;=NOOO D$:$>$>?^`a$b$bbbrr)__name__ __module__ __qualname____doc__rr' staticmethodr^r.propertyr1r5r6setr:r"r$rUrboolrr[rTrlrr%rr`r_rY __classcell__)rs@rrr's======/c///\/ 0S000X0 :t:::X: sX>>\>PPPPPd^D^T^^^^$SWGdGtGGGG  T  c  C  TX    \  ..."?d????      @ @ @ @ @ @PPPcctcX\ccccccccrr__main__zdecision_maker_standalone.log)filenamelevel) rrrgrrr*os.pathrnumpyrassa.dbrcommonrstoragerr configurationr configuration.schemesr internal.constantsr internal.utilsr rrdrr basicConfigINFOdmr9rrrs  !!!!!!*)))))777777,,,,,,;;;;;;;; pcpcpcpcpcFpcpcpcf zKMMMG!@ UUUU BBDDDDD r