
    j                      Z    d dl Z d dlmZ d dlmZ d dlmZ  G d de      Z G d d      Zy)	    N)Enum)Optional)connection_probec                       e Zd ZdZdZdZdZy)ConnectionStatusaO  
    Represents the connection status for a system.

    Provides an enumeration of various states a connection can be in, such as
    successful connection, connection failure, and restored connection.
    Used primarily to indicate the status of a network, server, or other connectivity-dependent services.

    Attributes:
        connection_ok: Indicates the connection is currently active and functioning properly.
        connection_failed: Indicates the connection has failed.
        connection_restored: Indicates the connection, which was previously failed, has been restored.
             N)__name__
__module____qualname____doc__connection_okconnection_failedconnection_restored     |/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/healthcheck/connection_monitor.pyr   r      s     Mr   r   c                       e Zd ZdZdededej                  fdZede	fd       Z
dd	Zd
ee   ddfdZdefdZde	d
ee   defdZy)OpikConnectionMonitora  
    Monitors the connection to the Opik server and tracks its status.

    This class is responsible for checking the server connectivity using a probe object
    at regular intervals. It maintains the status of the server connection, including
    disconnection details and reasons.

    Attributes:
        ping_interval: Interval in seconds between server connection pings.
        disconnect_time: Time in seconds when the server connection was lost.
            Defaults to 0 if the connection has not been lost yet.
        disconnect_reason: Reason for the disconnection, if applicable.
            Defaults to None if there is no disconnection.
    ping_intervalcheck_timeoutprobec                 x    || _         d| _        || _        t        d      | _        || _        d| _        d| _        y)a  
        Initializes an instance of the class to handle connection probing and monitoring.

        Args:
            ping_interval: The interval, in seconds, at which pings are sent to probe
                the connection.
            check_timeout: The threshold duration, in seconds, before a connection is
                considered lost if no response is received.
            probe: An instance of the ``connection_probe.ConnectionProbe`` class used for
                sending probe requests and handling connectivity checks.
        Tz-inf        N)_check_timeout_has_server_connection_probefloat	last_beatr   disconnect_timedisconnect_reason)selfr   r   r   s       r   __init__zOpikConnectionMonitor.__init__+   s>    " ,&*#v*"04r   returnc                     | j                   S )N)r   r#   s    r   has_server_connectionz+OpikConnectionMonitor.has_server_connectionE   s    ***r   Nc                 .    d| _         d | _        d| _        y )Nr   T)r!   r"   r   r'   s    r   resetzOpikConnectionMonitor.resetI   s    "!%&*#r   failure_reasonc                 j    | j                   r t        j                         | _        || _        d| _         y)a  
        Handles the procedure when the connection to the server is determined to have failed.

        This method processes the failure by capturing the disconnection time and the reason for the failure
        (if provided). It also updates the state to reflect that the connection to the server is no longer active.

        Args:
            failure_reason: Optional reason for the connection failure. Defaults to None if no reason
                is provided.
        FN)r   timer!   r"   )r#   r+   s     r   r   z'OpikConnectionMonitor.connection_failedN   s+     &&#'99;D %3D"&+#r   c                    | j                   | j                  z   }t        j                         }||k  rf| j                  j	                  | j
                        }t        j                         | _         | j                  |j                  |j                        S | j                  rt        j                  S t        j                  S )a  
        Checks and updates the connection status during a timed heartbeat event.

        This method ensures that the connection is actively monitored, sends probe
        requests to check the connection status if necessary, and updates the last
        heartbeat timestamp.

        Returns:
            ConnectionStatus: Indicates the current status of the connection. It may
            return one of the following statuses:
            - `ConnectionStatus.connection_ok`: The connection is active.
            - `ConnectionStatus.connection_failed`: The connection is not active.
            - Result based on the probe check indicating whether the connection is
              healthy or not.
        )timeout)r+   )r    r   r-   r   check_connectionr   _on_ping_result
is_healthyerror_messager(   r   r   r   )r#   	next_beatnowresults       r   tickzOpikConnectionMonitor.tick`   s      NNT%7%77	iik[[11$:M:M1NF!YY[DN''!!&2F2F (   ''#111#555r   successc                     |r6| j                   st        j                  }nt        j                  }d| _        |S t        j
                  }| j                  |       |S )z)Invoked to process a received ping resultT)r(   r   r   r   r   r   )r#   r8   r+   statuss       r   r1   z%OpikConnectionMonitor._on_ping_result}   sU     --)==)77*.D'
  &77F"">2r   )r%   N)r   r   r   r   r   r   ConnectionProber$   propertyboolr(   r*   r   strr   r   r7   r1   r   r   r   r   r      s    55 5  //	54 +t + ++
, ,$ ,$6& 6:-5c]	r   r   )	r-   enumr   typingr   opik.healthcheckr   r   r   r   r   r   <module>rB      s)       -t &q qr   