+
    7iio)                       R t ^ RIHt ^ RIt^ RIt^ RIHt ^ RIHt ^ RI	H
t
Ht ^ RIHt ^ RIHtHt ^ RIHt ^ R	IHt ^ R
IHtHtHtHtHtHtHt R R ltR^ RR/R R llt]! R.4      ]! ].4      R 4       4       t]! R.4      ]! ].4      R 4       4       t ]! R.4      ]! ].4      R 4       4       t!]! R.4      ]! ].4      R 4       4       t"]! R.4      ]! ].4      R 4       4       t#R R lt$R t%RR^ R^ R^ R^ R ^ R!^ R"R#R$^ R%^ /R&R$^ R%^ /R'R$^ R%^ ///R(R)^ //t&]! R.4      ]! ].4      R* 4       4       t']! R.4      ]! ].4      R+ 4       4       t(]! R.4      ]! ].4      R, 4       4       t)]! R.4      ]! ].4      R- 4       4       t*]! R.4      ]! ].4      R. 4       4       t+]! R.4      ]! ].4      R/ 4       4       t,]! R.4      ]! ].4      R0 4       4       t-R# )1zV
Dashboard, call history, and related endpoints expected by the React app (/api/...).
)annotationsN)Any)connections)HttpResponseForbiddenStreamingHttpResponse)Token)api_viewpermission_classes)IsAuthenticatedResponse)_legacy_profile_for_user_admin_rolecluster_table_existsfetch_active_calls_countfetch_call_history_pagefetch_dashboard_chartsfetch_dashboard_statsc                   V ^8  d   QhRR/# )   returnzdict[str, Any] | None )formats   "DE:\live-kit-agent\livekit_voicebot\backend\config\reporting_views.py__annotate__r      s     2 2 5 2    c                ,    \        V P                  4      # N)r   userrequests   &r   _resolve_legacyr!      s    #GLL11r   min_vmax_vi'  c               0    V ^8  d   QhRRRRRRRRRRR	R/# )
r   qdictkeystrdefaultintr"   r#   r   r   )r   s   "r   r   r   !   s7     % %$ %S %3 %# %# %[^ %r   c                    \        V P                  W4      4      p\	        V\        WT4      4      #   \        \        3 d    Tp L*i ; ir   )r*   get	TypeError
ValueErrormaxmin)r%   r'   r)   r"   r#   vs   &&&$$ r   
_parse_intr2   !   sH    c#$ uc!m$$ z" s   1 AAGETc                    \        V 4      pV'       d   VP                  R 4      f   \        RR/RR7      # \        \        V4      4      # )business_iddetail(No business profile found for this user.  status)r!   r,   r   r   )r    legacys   & r   dashboard_statsr<   )   sK     W%FVZZ.6AB
 	
 )&122r   c                   \        V 4      pV'       d   VP                  R 4      f   \        RR/RR7      # V P                  P                  R4      ;'       g    RP	                  4       ;'       g    RpV P                  P                  R4      ;'       g    RP	                  4       ;'       g    Rp\        \        WV4      4      # )	r5   Nr6   r7   r8   r9   	date_from date_to)r!   r,   r   query_paramsstripr   )r    r;   r>   r@   s   &   r   dashboard_chartsrC   5   s     W%FVZZ.6AB
 	
 %%))+6<<"CCEMMI##''	288b??AIITG*6gFGGr   c                    \        V 4      pV'       d   VP                  R 4      f   \        RRR^ RR/RR7      # \        V P                  R^^R	R
7      p\        W4      p\        RRRV/4      # )r5   successFcountmessageNo business profiler8   r9   recent_minutesi  r"   r#   T)r!   r,   r   r2   rA   r   )r    r;   recentrF   s   &   r   active_callsrL   C   ss     W%FVZZ.6E7AyBWXadee,,.>RVWF$V4EYgu566r   c                	   \        V 4      pV'       d   VP                  R 4      f   \        RRR. R^ RR/RR	7      # \        VR ,          4      p\	        V P
                  R
^d^RR7      p\	        V P
                  R^ ^ RR7      p\        R4      '       g   \        RRR. R^ R
VRV/4      # Rp. p\        VP                  R4      4      '       g)   VP                  R4      e   Rp\        VR,          4      .pV P
                  P                  R4      ;'       g    RP                  4       ;'       g    RpV P
                  P                  R4      ;'       g    RP                  4       ;'       g    RpV P
                  P                  R4      ;'       g    RP                  4       ;'       g    Rp	Rp
V.pV'       d   V
R,          p
VP                  V4       V'       d   V
R,          p
VP                  V4       V	'       d   V
R,          p
VP                  RV	 R24       RV
 RV R2pRV
 RV R 2p \        R!,          P                  4       ;_uu_ 4       pVP                  V. VOVO4       VP                  4       pV'       d   \        V^ ,          ;'       g    ^ 4      M^ pVP                  V. VOVOVNVN4       VP                   Uu. uF  pV^ ,          NK  	  ppVP                  4        Uu. uF  p\!        \#        VV4      4      NK  	  ppRRR4       . pX EFr  pTP                  R"4      ;'       g    / p\'        T\(        4      '       d    \*        P,                  ! T4      p\'        T\         4      '       g   / pTP                  R#4      pTP                  R$TP                  R$4      ;'       g    TP                  R%4      R&TP                  R&4      ;'       g+    TP                  R'4      ;'       g    TP                  R(4      R)T'       d   TP/                  4       MRR*TP                  R*4      ;'       g    TP                  R+4      R,TP                  R,4      ;'       g    TP                  R-4      R.TP                  R.4      /4       EKu  	  \        RRRTRXR
TRT/4      # u upi u upi   + '       g   i     EL; i  \$         d    \        RRR. R^ R
TRT/4      u # i ; i  \$         d    / p ELi ; i)/r5   NrE   FdatatotalrG   rH   r8   r9   limit  rJ   offset@B business_id_callhistoryTr?   roleagent_idz AND user_id = %s
start_dateend_datecustomer_numberz AND call_started_at >= %sz3 AND call_started_at < DATE_ADD(%s, INTERVAL 1 DAY)z# AND CAST(metadata AS CHAR) LIKE %s%zU
        SELECT COUNT(*) FROM business_id_callhistory
        WHERE business_id = %s  z
    z
        SELECT user_id, call_started_at, call_ended_at, duration_seconds, status, metadata, created_at
        FROM business_id_callhistory
        WHERE business_id = %s zA
        ORDER BY created_at DESC
        LIMIT %s OFFSET %s
    clustermetadatacall_started_at	agentname
agent_namecalltocall_toto	starttimeansweredtimeduration_seconds
dialstatusr:   	direction)r!   r,   r   r*   r2   rA   r   r   rB   appendr   cursorexecutefetchonedescriptionfetchallr&   zip	Exception
isinstancer(   jsonloads	isoformat)r    r;   bidrP   rR   agent_filter_sqlagent_paramsrW   rX   custwhere_extraparams_prefix	count_sqllist_sqlcur	total_rowrO   ccolsrrowsoutmdstarteds   &                       r   attempted_call_historyr   N   s    W%FVZZ.6vr7AyBWX
 	
 f]#
$Cw++Wc#NE,,h)TF 9::D&"gq'5RZ\bcdd Lvzz&)**vzz*/E/Q.F:./0&&**<8>>BEEGOO4J$$((4::AACKKtH  $$%67==2DDFNN$DK #uM33Z(LLX&<<qa[)  +}A.>-? @I  !,}A.>-? @H	e#**,,KK	#B]#B\#BCI.7C	!))*QEKK"PM"PL"P%"P"PQ"%//2/QAaDD/D203?1DT1&D? - !#CUU:$$"b#ZZ^ "d##B%%)*

RVVK0HHBFF<4H"&&*OObffY.?OO266$<GW..0~ 6 S S!%%@R:Sbff\2EEaeeHoRVVK0		
 * 	D&#wwxQWX 7 3? -,,  eD&"gq'5RZ\bcdde  sg   -#S AR-1R-R#R-+R(	R-S S'#
R--R>	8S >S  S$#S$'S87S8c                   \        V 4      pV'       d   VP                  R 4      f   \        RRRR. R^ /RR/R	R
7      # \        V P                  R^d^RR7      p\        V P                  R^ ^ RR7      p\        TTTV P                  P                  R4      ;'       g    RP                  4       ;'       g    RV P                  P                  R4      ;'       g    RP                  4       ;'       g    RV P                  P                  R4      ;'       g    RP                  4       ;'       g    RV P                  P                  R4      ;'       g    RP                  4       ;'       g    RV P                  P                  R4      ;'       g    RP                  4       ;'       g    RV P                  P                  R4      ;'       g    RP                  4       ;'       g    RR7	      w  rE\        RRRRVRV//4      # )r5   NrE   FrN   conversationsrO   rG   rH   r8   r9   rP   rQ   rJ   rR   rS   rW   r?   rX   rV   rY   campaign_idconversation_id)rP   rR   rW   rX   rV   rY   r   r   T)r!   r,   r   r2   rA   r   rB   )r    r;   rP   rR   r   rO   s   &     r   call_historyr      s    W%FVZZ.6vWa'H)Ujk
 	
 w++Wc#NE,,h)TF2((,,\:@@bGGIQQT&&**:6<<"CCEMM&&**:6<<"CCEMM --112CDJJQQS[[W[))--m<BBIIKSSt --112CDJJQQS[[W[
M t_mWeD	
 r   c               $    V ^8  d   QhRRRRRR/# )r   r>   z
str | Noner@   r   zdict[str, Any]r   )r   s   "r   r   r      s!      Z *  r   c                    \        V 4      pV'       d   VP                  R 4      f   RRR. /# R\        V4      R\        W1V4      /# )r5   Nstatscharts)r   r,   r   r   )r   r>   r@   r;   s   &&& r   _stream_payloadr      sN    %d+FVZZ.6x,,&v.(GD r   c                  aaaa V P                   R8w  d   \        R4      # V P                  P                  R4      ;'       g    RP	                  4       pV'       g   \        R4      #  \
        P                  P                  R4      P                  VR7      pTP                  oT P                  P                  R	4      ;'       g    RP	                  4       ;'       g    R
oT P                  P                  R4      ;'       g    RP	                  4       ;'       g    R
o \        T P                  P                  R4      ;'       g    ^
4      o\        ^\        S^<4      4      oTTTT3R lp\        T! 4       RR7      pRTR&   RTR&   T#   \
        P                   d    \        R4      u # i ; i  \         d    ^
o Lri ; i)zUSSE: `event: dashboard` with JSON payload. Token passed as query param (EventSource).r3   zMethod not allowedtokenr?   zmissing tokenr   )r'   zinvalid tokenr>   Nr@   intervalc               3     <"     \        SSS4      p R\        P                  ! V 4       R2x  \        P                  ! S4       KB    \
         d     R# i ; i5i)Tzevent: dashboard
data: z

N)r   rr   dumpstimesleepGeneratorExit)payloadr>   r@   r   r   s    r   event_generator)dashboard_stream.<locals>.event_generator   sR     	)$	7C0G1D0ETJJ

8$ 		s#   AAA AAAAztext/event-stream)content_typezno-cachezCache-ControlnozX-Accel-Buffering)methodr   r3   r,   rB   r   objectsselect_relatedDoesNotExistr   r*   r.   r/   r0   r   )	r    	token_keytokr   respr>   r@   r   r   s	   &    @@@@r   dashboard_streamr      s   ~~$%9::)//R668I$_556mm**626696E 88D-33::<DDI{{y)//R668@@DGw{{z288b9 1c(B'(H  !(D 'D $D	K7  6$_556  s*   (/F	 &F/ F/ 	 F,+F,/F?>F?overview
totalCallscompletedCallsanalyzedCallsavgHandlingTimesatisfactionScoreintentAccuracyintentBreakdownHighrF   
percentageMediumLowperformancecompletionRatec                     \        \        4      # r   )r   _EMPTY_POST_CALLr   s   &r   post_call_analyticsr     s     $%%r   c                    \        . 4      # r   r   r   s   &r   analytics_transfer_reasonsr          B<r   c                    \        . 4      # r   r   r   s   &r   analytics_sentimentr     r   r   c                    \        . 4      # r   r   r   s   &r   analytics_trendsr     r   r   c                    \        . 4      # r   r   r   s   &r   analytics_bots_statsr     r   r   c                     \        R RR/ /4      # )rE   TrN   r   r   s   &r   analytics_call_volume_heatmapr   %  s     Yfb122r   c                    \        . 4      # r   r   r   s   &r   analytics_agent_comparisonr   +  r   r   ).__doc__
__future__r   rr   r   typingr   	django.dbr   django.httpr   r   rest_framework.authtoken.modelsr   rest_framework.decoratorsr   r	   rest_framework.permissionsr
   rest_framework.responser   config.reporting_utilsr   r   r   r   r   r   r   r!   r2   r<   rC   rL   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s   #    ! D 1 B 6 ,  2% %PV % 
5'_%&3 ' 3 
5'_%&	H ' 	H 
5'_%&7 ' 7 
5'_%&S ' Sl 
5'_%& ' 8$P a!1Q!Waq1w<3GQa0
 $a( $ 
5'_%&& ' & 
5'_%& '  
5'_%& '  
5'_%& '  
5'_%& '  
5'_%&3 ' 3 
5'_%& ' r   