U
    ¿dL                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd eD ZeedZedddZdS )z>
Appraise evaluation framework

See LICENSE for usage details
    )defaultdict)datetime)floor)sqrt)login_required)CommandError)HttpResponse)_get_logger)_get_campaign_instance)DataAssessmentResult)DirectAssessmentDocumentResult) PairwiseAssessmentDocumentResult)PairwiseAssessmentResult)seconds_to_timedelta)TASK_DEFINITIONSc                 C   s   i | ]}|d  j |d qS )      )__name__).0tup r   =/var/www/rival/public_html/translation-eval/Campaign/views.py
<dictcomp>   s      r   )namer   c           '         s  t d| jjpd dkr dzt|}W n* tk
rV   d|}t|dd Y S X g }|j	 D ]}|j
	 D ]
}zt|  }W nH tk
r }	 z*t d| |j t |	 W Y qvW 5 d}	~	X Y nX |jj|d	|jd
}
|tk s|tkr
|
jd	d}
|tks|tkr6|
ddddddd}
n|
ddddddd}
ttdd |
D }dd |
D }dd |
D }dd |
D }tdd |
D |pd |d  d} dkrtt fdd|
D }t|dkrd}tt}tt}|
D ]|}|d d kr|}n|d d!kr |}nq |d  | }|t krXt!|d" }nd#|d$ |d" }|| "| q |rt#t$|nd}|rt#t%|nd}|rt|nd}g }g }t&t|' t|' D ]V}|"t|| t(t|| pd  |"t|| t(t|| p*d  qd}|r|rz&dd%l)m*} |||d&d'\}}|}W n2 t+k
r   t,d( Y n t-k
r   Y nX |rt.d)dd| }t!|/d*d }nd+}|rt.d)dd| }t!|/d*d }nd+}|r6tt0|d, }tt0|d, d- } d.|| }nd/}|rLd0|}nd/}|j|j1||||f}!| jj2rx|!|f7 }!|"|! qvqf|j3fd1d2d3 d4}"| jj2r|"d57 }"d64|"g}#|D ]0}$d7}%| jj2r|%d87 }%|%j|$ }&|#"|& qtd94|#ddS ):z7
    Campaign status view with completion details.
    z-Rendering campaign status view for user "%s".Z	AnonymousNr   z Failure to identify campaign {0}z
text/plain)content_typez(Invalid campaign type %s for campaign %sT)	createdBy	completedtask__campaign)item__isCompleteDocument
start_timeend_timescore1item__itemIDitem__target1IDitem__itemTypeitem__idscoreitem__targetIDc                 S   s   g | ]}|d  qS )   r   r   xr   r   r   
<listcomp>m   s     z#campaign_status.<locals>.<listcomp>c                 S   s   g | ]}|d  qS )r   r   r)   r   r   r   r+   n   s     c                 S   s   g | ]}|d  qS )r   r   r)   r   r   r   r+   o   s     c                 S   s   g | ]}|d  |d  qS )r   r   r   r)   r   r   r   r+   p   s     c                 S   s   g | ]}|d  qS )r   r   r)   r   r   r   r+   r   s     r   r   c                 3   s"   | ]}|d   d    V  qdS )r   Nr   r)   )_cs
_user_meanr   r   	<genexpr>w   s     z"campaign_status.<locals>.<genexpr>TGTBAD   z{0}-{1}   )mannwhitneyuZless)alternativezscipy is not installedi  .ZNeveri  <   z{0:0>2d}h{1:0>2d}mzn/az{0:1.6f}c                    s   | t   S )N)int)r*   )sort_keyr   r   <lambda>       z!campaign_status.<locals>.<lambda>)key)usernameactiveannotationsZfirst_modifiedlast_modifiedZannotation_time)random	z{0:>20}	{1:3}	{2}	{3}	{4}	{5}z	{6}
)5LOGGERinfouserr=   r
   r   formatr   teamsallmembersRESULT_TYPE_BY_CLASS_NAMEget_campaign_typeKeyErrordebugcampaignNameerrorobjectsfilteridr   r   excluder   values_listlensetsumr   r8   r   listr   strappendr   minmaxintersectionkeysfloatZscipy.statsr4   ImportErrorprint
ValueErrorr   splitr   	is_activeis_staffsortjoin)'requestcampaign_namer9   campaign_msg_outZteamrF   result_typeexc_dataZ_annotationsZ_start_timesZ
_end_timesZ
_durationsZ_user_stdevZ_tgtZ_badZ_x_dstZ_z_score_keyZ_first_modifiedZ_last_modifiedZ_annotation_timeZ_yZ	_reliabler4   _tZpvalueZ_date_modified_hours_minutesZ_itemZ_headerZ_txtZ_rowZ
_local_fmtZ
_local_outr   )r,   r-   r9   r   campaign_status    s   


  


 (,




rv   N)r   )__doc__collectionsr   r   mathr   r   django.contrib.auth.decoratorsr   django.core.management.baser   django.httpr   Appraise.utilsr	   ZCampaign.utilsr
   EvalData.modelsr   r   r   r   r   r   rK   r   rD   rv   r   r   r   r   <module>   s&   
