o
    ·Vj$  ã                
   @   s  d Z ddlZddlmZmZmZmZ dedefdd„Zdeeef d	edeeef fd
d„Z	dedefdd„Z
deeef d	edefdd„Zdeeef d	edeeeef  fdd„Zdeeef deeeef  fdd„Zdedeeeef  deee eeef ef fdd„ZdS )uX   Deterministic PCA metric answers from panel_data â€” exact DB-backed numbers before LLM.é    N)ÚAnyÚDictÚOptionalÚTupleÚtextÚreturnc                 C   s   t  ddt| pdƒ ¡ ¡ ¡ S )Nz\s+Ú Ú )ÚreÚsubÚstrÚlowerÚstrip)r   © r   ú9/home/aiteam/pcaa-dev/dashboard-backend/mcube_ai_tools.pyÚ
_normalize   s   r   Ú
panel_dataÚ
period_keyc                 C   sv   |   d¡pi }|  |¡pi }|  d¡pi }|r|S |   d¡pi }|  d¡|ks2|dkr9|  d¡dv r9|  d¡p8i S i S )NÚperiodsÚstatsÚquestion_focusÚlabelÚtoday)r   ©Úget)r   r   r   Úbundler   Úfocusr   r   r   Ú_period_stats   s   r   Úmessagec                 C   s\   t | ƒ}t d|¡rdS t d|¡rdS t d|¡rdS t d|¡r$dS t d	|¡r,d
S d
S )Nz	\btoday\br   z\byesterday\bÚ	yesterdayz+\bthis week\b|\blast 7 days\b|\bpast week\bÚlast_7_daysz \bthis month\b|\bcurrent month\bÚ
this_monthz2\ball[- ]time\b|\btotal\b|\boverall\b|\blifetime\bÚall_time)r   r
   Úsearch)r   r   r   r   r   Ú_pick_period   s   r$   c                 C   sn   |   d¡pi }|  d¡pd}d|› ddddd	d
œ}|   d¡p i }|dkr1|  d¡s/|  d¡r1dS |  ||¡S )NÚ	referenceÚ	today_istr   ztoday (z, IST)r   zthe last 7 daysz
this monthzall time)r   r   r    r!   r"   Úfiltersr"   Ú	date_fromÚdate_toz!the selected dashboard date ranger   )r   r   Úrefr   Úlabelsr'   r   r   r   Ú_period_label(   s   
ûr,   c                 C   sV   |   d¡pi }|  |¡pi }|  d¡p|   d¡pg }|  d¡r#|d S |r)|d S d S )Nr   Úagent_call_countsÚtop_agent_by_call_volumer   r   )r   r   r   r   Úagentsr   r   r   Ú_top_agent_by_calls8   s   
r0   c                 C   s0   |   d¡}|r	|S |   d¡pg }|r|d S d S )NÚtop_agent_by_quality_scoreÚagent_quality_leaderboardr   r   )r   ÚtopÚboardr   r   r   Ú_top_agent_by_qualityA   s
   
r5   c              	   C   s  i }|rt | pdƒ ¡ sd|dfS t| ƒ}t| ƒ}t||ƒ}|s+|dkr+t|dƒ}d}|s4| d¡p3i }|s;d|dfS t||ƒ}| d¡pFi }||| d¡pOd| d	¡|d
œ}t d|¡rGt d|¡rt d|¡st	| d¡pqdƒ}d|d< ||d< d|› d|› d|dkrˆdnd› d|dfS t d|¡rÅt	| d¡pdƒt	| d¡p¥dƒ }	d|d< |	|d< d|› d|	› d|	dkr½dnd› d|dfS t d |¡rñt	| d!¡pÒdƒ}d"|d< ||d< d|› d|› d#|dkrédnd› d|d$fS t d%|¡rt	| d&¡pÿdƒ}d'|d< ||d< d|› d|› d(|dkrdnd› d|d$fS t	| d)¡p'dƒ}d)|d< ||d< d|› d|› d*|dkr?dnd› d|dfS t d+|¡r–t
||ƒ}
|
s_d,|› d-|d.fS |
 d/¡pfd0}t	|
 d)¡podƒ}d1|d< ||d/< ||d< d|› d2|› d3|› d4|dkrŽdnd› d	|d5fS t d6|¡rét|ƒ}
|
s©d7|d.fS |
 d/¡p°d0}|
 d8|
 d9¡¡}t	|
 d)¡pÂdƒ}d:|d< ||d/< ||d8< d;|› d<|› d=|› d>|dkrádnd› d	|dfS t d?|¡r| d@¡}|du rdA|› dB|dCfS d@|d< ||d< d|› dD|› dE|d$fS t dF|¡rƒ| dG¡p%i }|s:| dH¡p0g }|r8|d ni }|sFdI|› d|dJfS | dK¡pMdL}| dM¡p[| dN¡p[d0}| dO¡pcd}dG|d< ||dP< dQ|› dR|› d;|r}dS|› dnd |dTfS d|dfS )Uz˜
    Return (direct_answer, computed_facts, confidence).
    direct_answer is set for high-confidence metric questions answered from DB panel_data.
    r	   Ng        r"   Údashboard_statsr%   ÚtimezonezAsia/Kolkatar&   )ÚperiodÚperiod_labelr7   r&   r   z](how many|total|number of|count of).*(call|calls)|(call|calls).*(how many|total|count|number)z\banswered\bz\bmissed\b|\bnot answered\bÚanswered_totalr   Úanswered_callsÚmetricÚvaluezFor z, this business has **z** answered callé   ÚsÚ.gffffffî?z)\bmissed\b|\bnot answered\b|\bno answer\bÚinbound_not_answeredÚoutbound_not_answeredÚmissed_callsz** missed / not-answered callgq=
×£pí?z\binbound\bÚinbound_totalÚinbound_callsz** inbound callgÃõ(\Âí?z\boutbound\bÚoutbound_totalÚoutbound_callsz** outbound callÚtotal_callsz** total callz¦(which|who).*(agent|agents).*(most|max|highest|top|maximum).*(call|calls)|(most|max|highest|top).*(call|calls).*(agent|agents)|agent.*(most|max|highest).*(call|calls)z'I don't have agent call-count data for z in the current filters.g333333ë?Ú
agent_nameÚUnknownÚtop_agent_by_callsz, **z&** has the highest call volume with **z** callg®Gázî?z˜(best|top|highest).*(agent|agents).*(quality|score|perform)|(quality|score).*(leader|leaderboard|ranking|best)|agent.*(leaderboard|ranking|best perform)zHI don't have agent quality leaderboard data for the current filters yet.Úperformance_scoreÚ	avg_scoreÚtop_agent_by_qualityz**z=** leads the agent leaderboard with a performance score of **z** across **z** analyzed callz1(average|avg|mean).*(quality|score)|quality scoreÚavg_quality_scorez+Average quality score is not available for z in the current data.gš™™™™™é?z!, the average quality score is **z**.z'(latest|last|most recent).*(call|calls)Úlatest_callÚrecent_callszNo recent calls found for g)\Âõ(ì?Úcallidu   â€”Ú	agentnameÚagent_callinfoÚcall_starttimeÚcall_idzThe latest call is **z** by **z at gÍÌÌÌÌÌì?)r   r   r   r$   r   r   r,   r
   r#   Úintr0   r5   )r   r   Úfactsr   r   r   Úperiod_namer*   ÚvalÚmissedr3   ÚnameÚcountÚscoreÚcallsÚlatestrR   ÚagentÚwhenr   r   r   Úcompute_panel_answerI   s   




û	ý"ýÿ"ý"ý$ý$ýü

ý*ýüýÿÿü


ýý
ýÿü
rc   )Ú__doc__r
   Útypingr   r   r   r   r   r   r   r$   r,   r0   r5   Úfloatrc   r   r   r   r   Ú<module>   s     &*&	ÿÿþ