Mj dZddlZddlmZddlmZmZmZmZddl Z ddl m Z m Z m Z ddl mZmZeGddZd eefd Zd Zd eeeeffd Zd ZdS)zF Represents storage where ssa data is collected, stored and extracted N) dataclass)ListIteratorTupleDict)funccastdistinct) session_scope RequestResultcDeZdZUeed<eeed<eed<eed<dS) DomainData domain_namedomain_total_reqsis_a_wordpress_domain urls_numberN)__name__ __module__ __qualname__str__annotations__rintboolJ/opt/cloudlinux/venv/lib64/python3.11/site-packages/ssa/modules/storage.pyrrsFCy   rrreturnc #zKt|5}|tjt jdtjt jtjt j tj t j ttj tjt jdtjtjt jdtj}t!j|d}|D]^\}}t%|}d}dgdz}|D]*\} } } } } | |t'| <t|| }+t)||| |V_ ddddS#1swxYwYdS)z7 Iterates data from database domain-by-domain. %Hc|dSNrritems rz#iter_domains_data../s VZ[\V]rkeyr)rrrrN)r queryr domainrstrftime created_atCountidmax wordpresscountr pathgroup_byorder_by itertoolsgroupbytuplerr)enginedbresults_by_hourresults_by_hour_groupedrgroupdomain_results_by_hourrrequests_number_by_hour_hour requests_num is_wordpressurlss riter_domains_datarCs v  !"((  M$ 8 9 9 J}' ( ( H], - - Jx 233 4 4   (  M$ 8 9 9   (  M$ 8 9 9   #,"3OI]I]"^"^"^"9   K%*5\\ "K()cBh #=S 5 594|T5A'D 2!+t44 '"9&2'      #!!!!!!!!!!!!!!!!!!sFF00F47F4c # Kt|5}|tjt jdtjt jttj tj  dt j tj dt jttjtj  dtj|ktj|tjt jdtjtjt jdtj}d}dgdzdgdzdgdz}}}|D]q\} } } } } |r/|| kr)|t+||||fVdgdzdgdzdgdz}}}| |t-| <| |t-| <| |t-| <| }r| t+| |||fVddddS#1swxYwYdS) z6 Iterates urls data from database url-by-url. rurl_throttled_reqsurl_total_reqs url_slow_reqsNrr'r1rErFrG)r r(r r1rr*r+Sumr hitting_limits sqlalchemyIntegerlabelr,r-is_slow_requestfilterr)in_r2r3dictr)r7r all_pathsr8 urls_data previous_pathrErFrGr1r?url_throttled_req url_total_req url_slow_reqs riter_urls_datarXCs v  1 "HH   M$ 8 9 9 HT,j.@  u)** J   e$%% HT-z/ACC  eO$$    &  K /  &   " "9 - -  (   dM4L M M  (   dM4L M M  % , C"HqcBhb-:NKT ! ! FD$)=, 1$!6!6#T&'9#1"/ &&&C"HqcBhb5BN"-> s4yy )(5N3t99 %'3M#d)) $ MMD1)'        Y1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 sI$JJ Jc#Kt|5}|tjtjtj|ktj}tj |d}|D]\}}|d|DfV ddddS#1swxYwYdS)zA Get information about durations of requests url-by-url. c|dSr!rr"s rr$z#get_url_durations..s DGrcg|]\}}|Srr).0r>durations r z%get_url_durations..s:::[Q:::rN) r r(r r1r]rOr)r3r4r5)r7rr8rSdurations_by_pathr&r;s rget_url_durationsr`{s# v  ;"HH    "   &  K /  (    &- ++--+ ; ;JC::E:::: : : : : ;;;;;;;;;;;;;;;;;;;sBB77B;>B;c #Kt|5}|tjt jdtjt jttj tj  dt j tj dt jttjtj  dtj|ktjt jdtjtjt jdtj}|tjtjtj|ktj}t+j|d}t+j|d}t/|d}t/|d}|||\} } |\} } | | krd gd z} d gd z}d gd z}| D]>\}}}}}|| t1|<||t1|<||t1|<?d | D}| |t3| | || fVt/|d}t/|d}n'| | krt/|d}nt/|d}||ddddS#1swxYwYdS) u Stream per-URL data for a single domain. Yields ``(path, durations, url_data)`` per path, where: - ``path`` is the URL path (str); - ``durations`` is a list of raw request durations for that path; - ``url_data`` is a dict with per-hour aggregates (``url_throttled_reqs``, ``url_total_reqs``, ``url_slow_reqs`` as 24-element lists) plus ``path``. Merges two ``ORDER BY path`` streams in lock-step instead of materialising the per-domain ``{path: [durations...]}`` dict up front, which caused OOM on large databases (CLPRO-3077). A second benefit: this drops the redundant ``WHERE path IN (...)`` clause — both queries already filter by ``domain``, so the path list adds no selectivity, only memory pressure on the SQL parser. rrErFrGc|dSr!rrs rr$z&iter_domain_url_data.. 1rr%c|dSr!rrcs rr$z&iter_domain_url_data..rerNrr'cg|]\}}|Srr)r\r>ds rr^z(iter_domain_url_data..s55541aQ555rrH)r r(r r1rr*r+rIr rJrKrLrMr,r-rNrOr)r2r3r]r4r5nextrrQ)r7rr8 agg_query dur_query agg_by_path dur_by_pathagg_itemdur_itemagg_path agg_groupdur_path dur_grouprErFrGr>r? throttledtotalslow durationss riter_domain_url_datarxs$ v  E3"HH   M$ 8 9 9 HT,j.@  u)** J   e$%% HT-z/ACC  eO$$    &  K /  (   M$ 8 9 9   (   M$ 8 9 9  # ,HH    "   &  K /  (     ' ~~FFF ' ~~FFF  T** T**"x';"* Hi"* Hi8##&'S2X""#r!"b 7@443AtYt4=&s4yy105N3t99-/3M#d)),,559555  4!'9#1"/ ,,, T22 T22H$$  T22 T22?"x';ME3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3sL"MMM)__doc__r4 dataclassesrtypingrrrrrKrr r ssa.dbr r rrCrXrrr`rxrrrr}s@!!!!!!............++++++++++////////  %*!5%%%%N5 5 5 p;d3c ?.C;;;;*W3W3W3W3W3r