
    hRi                     `    d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
 d dlmZ  G d d      Zy)	    )action)Responsestatus)get_company_usersaggregate_user_callsformat_best_worstformat_user_performance)ValidationErrorc                       e Zd ZdZd Zd Z eddgd      d        Z eddgd	      d
        Z eddgd      d        Z	y)UserPerformanceMixinz6
    Enterprise-grade user performance endpoints.
    c                 J    t        |j                        }|t        d      |S )NzNo active company found)r   userr   )selfrequestuserss      K/var/www/html/dp2/backend_v2.1/server/apps/calls/mixins/user_performance.py_get_company_users_or_400z.UserPerformanceMixin._get_company_users_or_400   s&    !',,/=!";<<    c                 |    | j                  | j                               }| j                  |      }t        ||      S )N)calls_qscompany_users)filter_querysetget_querysetr   r   )r   r   r   r   s       r   
_aggregatezUserPerformanceMixin._aggregate   s>    ''(9(9(;<66w?#'
 	
r   Fgetzuser-performance)detailmethodsurl_pathc                     |j                   j                  dd      }| j                  |      \  }}t        t	        |||      t
        j                        S )Nshow_allF)r!   r   )query_paramsr   r   r   r
   r   HTTP_200_OKr   r   r!   statsr   s        r   user_performancez%UserPerformanceMixin.user_performance!   sU    ''++J>w/u#!
 %%
 	
r   zbest-performing-usersc           	          |j                   j                  dd      }| j                  |      \  }}t        dt	        ||d|      it
        j                        S )Nr!   Fdataavg_attended_valueorder_byr!   r   r"   r   r   r   r	   r   r#   r$   s        r   best_performing_usersz*UserPerformanceMixin.best_performing_users.   s_    ''++J>w/u!2)	"
 
 %%' 	'r   zworst-performing-usersc           	          |j                   j                  dd      }| j                  |      \  }}t        dt	        ||d|      it
        j                        S )Nr!   Fr(   avg_missed_valuer*   r   r,   r$   s        r   worst_performing_usersz+UserPerformanceMixin.worst_performing_users9   s_    ''++J>w/u!2'	"
 
 %%' 	'r   N)
__name__
__module____qualname____doc__r   r   r   r&   r-   r0    r   r   r   r      st    
 55'4FG

 H

 55'4KL' M' 55'4LM' N'r   r   N)rest_framework.decoratorsr   rest_framework.responser   rest_frameworkr   apps.calls.servicesr   r   r	   r
   rest_framework.exceptionsr   r   r5   r   r   <module>r;      s'    , , !  64' 4'r   