
    j
                     ^    d dl Z d dlZd dlmZmZ ddlmZ dZ G d dej                        Z	y)    N)ListCallable   )messages2   c            	       *   e Zd Zefdeej                  gdf   dededefdZ	ddZ
defd	Zdefd
Zej                  deej                     fd       Zej                  dej                  ddfd       Zdeej                  gef   ddfdZdefdZy)BaseBatcherflush_callbackNmax_batch_sizeflush_interval_secondsbatch_memory_limit_mbc                 |    || _         || _        g | _        || _        || _        t        j
                         | _        y N)_flush_interval_seconds_flush_callback_accumulated_messages_max_batch_size_batch_memory_limit_mbtime _last_time_flush_callback_called)selfr
   r   r   r   s        /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/message_processing/batching/base_batcher.py__init__zBaseBatcher.__init__   s;     /E$GUAC"$2+@#7;yy{-    returnc                     t        | j                        dkD  r/| j                         }g | _        |D ]  }| j                  |        t	        j                         | _        y Nr   )lenr   )_create_batches_from_accumulated_messagesr   r   r   )r   batch_messagesbatch_messages      r   flushzBaseBatcher.flush   sQ    t))*Q.!KKMN)+D&!/$$]3 "004		-r   c                 b    t        j                          | j                  z
  }|| j                  k\  S r   )r   r   r   )r   elapseds     r   is_ready_to_flushzBaseBatcher.is_ready_to_flush$   s)    ))+ E EE$6666r   c                 2    t        | j                        dk(  S r   r   r   r   s    r   is_emptyzBaseBatcher.is_empty(   s    4--.!33r   c                      y r    r(   s    r   r   z5BaseBatcher._create_batches_from_accumulated_messages+   s     &)r   messagec                     | j                   j                  |       t        | j                         | j                  k\  r| j	                          y y r   )r   appendr   r   r"   )r   r,   s     r   addzBaseBatcher.add0   s>    ""))'2t))*d.B.BBJJL Cr   filter_funcc                 T    t        t        fd| j                              | _        y)z
        Remove messages from _accumulated_messages that match the provided filter function.

        Args:
            filter_func: A function that takes a BaseMessage and returns True if the message should be removed
        c                      |        S r   r+   )xr0   s    r   <lambda>z7BaseBatcher._remove_matching_messages.<locals>.<lambda>@   s    Q/r   N)listfilterr   )r   r0   s    `r   _remove_matching_messagesz%BaseBatcher._remove_matching_messages6   s#     &*/1K1KL&
"r   c                 ,    t        | j                        S )ap  
        Gets the total number of accumulated messages in the current instance.

        The method calculates and retrieves the count of accumulated messages
        maintained within an internal structure. Thread safety is guaranteed
        through the usage of a lock mechanism.

        Returns:
            int: The total number of accumulated messages.
        r'   r(   s    r   sizezBaseBatcher.sizeC   s     4--..r   )r   N)__name__
__module____qualname__BASE_BATCH_MEMORY_LIMIT_MBr   r   BaseMessageintfloatr   r"   boolr%   r)   abcabstractmethodr   r   r/   r7   r9   r+   r   r   r	   r	      s     &@C ("6"6!7!=>C C !&	C
  #C<74 74$ 4 	)	h""	#) ) 	8// D  

#X%9%9$:D$@A
	
/c /r   r	   )
r   rB   typingr   r    r   r=   ABCr	   r+   r   r   <module>rG      s+     
 !    C/#'' C/r   