+
    }i$                        ^ RI Ht ^ RIHt ^ RIHt ^ RIHt ^ RIH	t	 ^ RI
HtHtHt ^ RIHt ]'       d   ^ RIHt ]! ]4      t]! R	4       ! R
 R4      4       tR# )    )annotations)	timedelta)	getLogger)TYPE_CHECKING)pairwise)LoadRatioInfoSnapshot
SystemInfo)
docs_group)SnapshotterAutoscalingc            
          ] tR t^tRtR]! ^R7      RRRRRR	R
R/R R lltR R ltR R ltRR/R R llt	R!R R llt
R!R R lltR!R R lltR!R R lltR R ltR tR# )"SystemStatusa  Provides a simple interface for evaluating system resource usage from snapshots collected by `Snapshotter`.

This class aggregates and interprets snapshots from a Snapshotter instance to evaluate the current and historical
status of system resources like CPU, memory, event loop, and client API usage. It exposes two methods
`get_current_system_info` and `get_historical_system_info`. The system information is computed using a weighted
average of overloaded messages in the snapshots, with the weights being the time intervals between the snapshots.
Each resource is computed separately, and the system is considered as overloaded whenever at least one resource
is overloaded.

`get_current_system_info` returns a `SystemInfo` data structure that represents the current status
of the system. The length of the current timeframe in seconds is configurable by the `max_snapshot_age` option
and represents the max age of snapshots to be considered for the computation.

`SystemStatus.get_historical_system_info` returns a `SystemInfo` that represents the long-term status of the system.
It considers the full snapshot history available in the `Snapshotter` instance.
max_snapshot_age)secondscpu_overload_thresholdg?memory_overload_thresholdg?event_loop_overload_thresholdg333333?client_overload_thresholdg333333?c               4    V ^8  d   QhRRRRRRRRRRR	RR
R/# )   snapshotterr   r   r   r   floatr   r   r   returnNone )formats   "t/Users/ahmad/.openclaw/workspace/my-crawler/.venv/lib/python3.14/site-packages/crawlee/_autoscaling/system_status.py__annotate__SystemStatus.__annotate__'   sY     D D D $	D
 !&D $)D (-D $)D 
D    c               N    Wn         W n        W0n        W@n        WPn        W`n        R# )a  Initialize a new instance.

Args:
    snapshotter: The `Snapshotter` instance to be queried for `SystemStatus`.
    max_snapshot_age: Defines max age of snapshots used in the `SystemStatus.get_current_system_info`
        measurement.
    cpu_overload_threshold: Sets the threshold of overloaded snapshots in the CPU sample.
        If the sample exceeds this threshold, the system will be considered overloaded.
    memory_overload_threshold: Sets the threshold of overloaded snapshots in the memory sample.
        If the sample exceeds this threshold, the system will be considered overloaded.
    event_loop_overload_threshold: Sets the threshold of overloaded snapshots in the event loop sample.
        If the sample exceeds this threshold, the system will be considered overloaded.
    client_overload_threshold: Sets the threshold of overloaded snapshots in the Client sample.
        If the sample exceeds this threshold, the system will be considered overloaded.
N)_snapshotter_max_snapshot_age_cpu_overload_threshold_memory_overload_threshold_event_loop_overload_threshold_client_overload_threshold)selfr   r   r   r   r   r   s   &&$$$$$r   __init__SystemStatus.__init__'   s(    2 (!1'=$*C'.K+*C'r!   c                   V ^8  d   QhRR/# r   r   r
   r   )r   s   "r   r   r    G   s     	M 	M 	Mr!   c                :    V P                  V P                  R7      # )a)  Retrieve and evaluates the current status of system resources.

Considers snapshots within the `_max_snapshot_age` timeframe and determines if the system is currently
overloaded based on predefined thresholds for each resource type.

Returns:
    An object representing the current system status.
)sample_duration)_get_system_infor$   r)   s   &r   get_current_system_info$SystemStatus.get_current_system_infoG   s     $$T5K5K$LLr!   c                   V ^8  d   QhRR/# r-   r   )r   s   "r   r   r    R   s     	' 	'J 	'r!   c                "    V P                  4       # )a+  Retrieve and evaluates the historical status of system resources.

Considers the entire history of snapshots from the Snapshotter to assess long-term system performance and
determines if the system has been historically overloaded.

Returns:
    An object representing the historical system status.
)r0   r1   s   &r   get_historical_system_info'SystemStatus.get_historical_system_infoR   s     $$&&r!   r/   Nc                    V ^8  d   QhRRRR/# )r   r/   timedelta | Noner   r
   r   )r   s   "r   r   r    ]   s     
 
3C 
z 
r!   c                   V P                  V4      pV P                  V4      pV P                  V4      pV P                  V4      p\	        VVVVR7      # )am  Get system information based on the overload state of different resources within a specified duration.

Args:
    sample_duration: Specific duration for which to evaluate the system status. If None, evaluates across
        the entire history available in the snapshotter.

Returns:
    Aggregated system status indicating whether the system is idle or overloaded.
)memory_infoevent_loop_infocpu_infoclient_info)_is_memory_overloaded_is_event_loop_overloaded_is_cpu_overloaded_is_client_overloadedr
   )r)   r/   mem_infor<   r=   r>   s   &$    r   r0   SystemStatus._get_system_info]   s]     --o>88I**?;00A +#	
 	
r!   c                    V ^8  d   QhRRRR/# r   r/   r9   r   r   r   )r   s   "r   r   r    s   s     P P2B Pm Pr!   c                n    V P                   P                  V4      pV P                  W P                  4      # )a  Determine if the CPU has been overloaded within a specified time duration.

Args:
    sample_duration: The duration within which to analyze CPU snapshots. If None, evaluates across
        the entire history available in the snapshotter.

Returns:
    CPU load ratio information.
)r#   get_cpu_sample_is_sample_overloadedr%   r)   r/   samples   && r   rA   SystemStatus._is_cpu_overloadeds   s1     ""11/B))&2N2NOOr!   c                    V ^8  d   QhRRRR/# rF   r   )r   s   "r   r   r             S S5E SQ^ Sr!   c                n    V P                   P                  V4      pV P                  W P                  4      # )a  Determine if memory has been overloaded within a specified time duration.

Args:
    sample_duration: The duration within which to analyze memory snapshots. If None, evaluates across
        the entire history available in the snapshotter.

Returns:
    Memory load ratio information.
)r#   get_memory_samplerI   r&   rJ   s   && r   r?   "SystemStatus._is_memory_overloaded   1     ""44_E))&2Q2QRRr!   c                    V ^8  d   QhRRRR/# rF   r   )r   s   "r   r   r       s      W W9I WUb Wr!   c                n    V P                   P                  V4      pV P                  W P                  4      # )a-  Determine if the event loop has been overloaded within a specified time duration.

Args:
    sample_duration: The duration within which to analyze event loop snapshots. If None, evaluates across
        the entire history available in the snapshotter.

Returns:
    Event loop load ratio information.
)r#   get_event_loop_samplerI   r'   rJ   s   && r   r@   &SystemStatus._is_event_loop_overloaded   s1     ""88I))&2U2UVVr!   c                    V ^8  d   QhRRRR/# rF   r   )r   s   "r   r   r       rN   r!   c                n    V P                   P                  V4      pV P                  W P                  4      # )a!  Determine if the client has been overloaded within a specified time duration.

Args:
    sample_duration: The duration within which to analyze client snapshots. If None, evaluates across
        the entire history available in the snapshotter.

Returns:
    Client load ratio information.
)r#   get_client_samplerI   r(   rJ   s   && r   rB   "SystemStatus._is_client_overloaded   rR   r!   c               $    V ^8  d   QhRRRRRR/# )r   rK   zlist[Snapshot]	thresholdr   r   r   r   )r   s   "r   r   r       s'     "] "]N "]u "]Q^ "]r!   c                   V'       g   \        V^ R7      # \        V4      ^8X  d(   \        V\        V^ ,          P                  4      R7      # RpRp\	        V4       Ff  w  rVVP
                  VP
                  ,
          P                  4       pV^ 8  d   \        R4      hVP                  '       d   W7,          pK^  WG,          pKh  	  W4,           ;p^ 8X  d   Rp	MW8,          p	\        V\        V	^4      R7      # )a  Determine if a sample of snapshot data is overloaded based on a specified ratio.

Args:
    sample: A list of snapshot data to analyze.
    threshold: The threshold ratio to use for determining if the sample is overloaded.

Returns:
    An object with an `is_overloaded` property set to `True` if the sample is considered overloaded based
    on the specified threshold ratio. Otherwise, `is_overloaded` is set to `False`.
)limit_ratioactual_ratiog        zLNegative time. Code assumptions are not valid. Expected time sorted samples.)	r   lenr   is_overloadedr   
created_attotal_seconds
ValueErrorround)
r)   rK   r\   overloaded_timenon_overloaded_timepreviouscurrenttime
total_timeoverloaded_ratios
   &&&       r   rI   "SystemStatus._is_sample_overloaded   s      YQGGv;! YU6RS9KbKbEcdd!!)&!1H&&)<)<<KKMDax !opp$$$'#+# "2 *??JAE".;GWYZA[\\r!   )r(   r%   r'   r$   r&   r#   )N)__name__
__module____qualname____firstlineno____doc__r   r*   r2   r6   r0   rA   r?   r@   rB   rI   __static_attributes__r   r!   r   r   r      s    "D '0&:	D
 ),D ,/D 03D ,/D@	M	'
d 
,PSWS"] "]r!   r   N)
__future__r   datetimer   loggingr   typingr   more_itertoolsr   crawlee._autoscaling._typesr   r	   r
   crawlee._utils.docsr   crawlee._autoscalingr   rn   loggerr   r   r!   r   <module>r}      sS    #     # K K *0	8	 Mt] t] t]r!   