
    W8i5                     p    d dl Z d dlZd dlZd dlmZ d dlmZmZ  ej                  e	      Z
 G d d      Zy)    N)ThreadPoolExecutor)ListTuplec                       e Zd Z ed      Zeddededefd       Ze	 	 ddedede	j                  deded	eeef   fd
       Ze	 	 ddedededed	ee   f
d       Zeddefd       Zy)CatchPhraseReportService   )max_workerstextphraseuse_word_boundariesc                 T   | r|s|dfS 	 t        j                  |j                               }|rd| dn|}t        j                  |      }t	        |j                  | j                                     }||fS # t        $ r&}t        j                  d|        |dfcY d }~S d }~ww xY w)NFz\bzPhrase preparation failed )	reescapelowercompileboolsearch	Exceptionloggerwarning)r
   r   r   phrase_escapedpatternregexfoundexcs           I/var/www/html/dp2/backend_v2.1/server/apps/calls/services/catch_phrase.py_phrase_present_syncz-CatchPhraseReportService._phrase_present_sync   s    65= 
	!YYv||~6N2EN+2.>GJJw'Edjjl34E5=  	!NN7u=>5= 	!s   A-A8 8	B'B"B'"B'	semaphoretimeoutreturnc           	      @   K   |4 d {    t        j                         }	 t        j                  |j                  t        j
                   fd      |       d {   cd d d       d {    S 7 l7 7 	# t         j                  $ r2 t        j                  d d       dfcY cd d d       d {  7   S t        $ r;}t        j                  d d|        dfcY d }~cd d d       d {  7   S d }~ww xY w# 1 d {  7  sw Y   y xY ww)Nc                  2    t         j                         S )N)r   r   )r   r
   r   s   r   <lambda>z=CatchPhraseReportService.count_phrase_async.<locals>.<lambda>.   s     8 M M "/!    )r   z&Phrase counting timed out for phrase ''r   z"Unexpected error counting phrase 'z': )asyncioget_running_loopwait_forrun_in_executorr   	_executorTimeoutErrorr   r   r   error)r
   r   r   r   r   loopes   ``  `  r   count_phrase_asyncz+CatchPhraseReportService.count_phrase_async   s      9++-D!$--((0:: $
 
	 99
	  '' !!GxqQRqy # 99$  !A&QCPQqy ) 99$!% 99s   DA9DD	=A?#A;$A?'D3A=4D;A?=D?0D/D	0D<B?=DDD)D*D	.D:C=;DDD		DDDDphrasesmax_concurrencyc           	        K   | sg S t        j                  |      }|D cg c]%  }t        j                  | |j                  ||      ' }}t        j
                  |  d{   }|D 	cg c]
  \  }}	|	s	| c}	}S c c}w 7 !c c}	}w w)zC
        Count all phrases in a transcript asynchronously.
        r   N)r&   	Semaphorer   r/   r   gather)
r
   r0   r1   r   r   ptasksresultsr   r   s
             r   count_phrases_in_textz.CatchPhraseReportService.count_phrases_in_text=   s      I%%o6	 
  %77$7	 8   	 
  ..,3=G=65uG==
 /=s4   B*A>
B!B"	B+
B6B:
BBc                    K   | j                   }|s| j                  }|sg S t        j                  |||       d {   S 7 w)Nr3   )twilio_recording_text
transcriptr   r9   )callr0   r   r<   s       r   build_call_phrase_report_asyncz7CatchPhraseReportService.build_call_phrase_report_async[   sQ     //
J	-CC 3 D 
 
 	
 
s   :AAAN)F)   F)   F)__name__
__module____qualname__r   r*   staticmethodstrr   r   r&   r4   intr   r/   r   listr9   r>    r$   r   r   r      s    "r2I!3 ! !$ ! !  
 $)!!! $$! 	!
 "! 
sCx! !:   "$)	>>> > "	>
 
c> >: 
QU 
 
r$   r   )r   r&   loggingconcurrent.futuresr   typingr   r   	getLoggerrA   r   r   rH   r$   r   <module>rM      s4    	   1  
		8	$\
 \
r$   