B
    è?F[Ü  ã               @   s@   d dl Z d dlmZmZ d dlmZ d dlZG dd„ deƒZdS )é    N)ÚdatetimeÚ	timedelta)Úparse_tsc               @   sZ   e Zd ZdZddd„Zdd„ Zdd„ Zdd
d„Zddd„Zddd„Z	dd„ Z
ddd„ZdS )ÚResultProcessorzlog.csvNc             C   s^   || _ || _d | _d| _d| _tj| _tj| _	t
j| _t
j| _| j  d¡| _| j  d¡| _d S )Nr   Zoutput_queueZoutput_domain)ÚsdÚbatchÚlog_fpÚ	num_filesÚ
total_timer   ÚmaxÚmin_timeÚminÚmax_timer   Úearliest_timeÚlatest_timeÚget_objÚqueueÚdomain)ÚselfZ
batch_namer   Zmimetype_files© r   ú3lib/python3.7/site-packages/boto/services/result.pyÚ__init__   s    zResultProcessor.__init__c             C   st   t |d ƒ}t |d ƒ}|| }|| jkr0|| _|| jk r@|| _|  j|j7  _|| jk r`|| _|| jkrp|| _d S )NzService-ReadzService-Write)r   r   r   r
   Úsecondsr   r   )r   ÚmsgZ
start_timeZend_timeÚelapsed_timer   r   r   Úcalculate_stats,   s    



zResultProcessor.calculate_statsc             C   sœ   t | ¡ ƒ}| jsDttj || j¡dƒ| _d |¡}| j |d ¡ g }x4|D ],}|| }| 	d¡dkrpd| }| 
|¡ qNW d |¡}| j |d ¡ d S )NÚaú,Ú
r   z"%s")ÚsortedÚkeysr   ÚopenÚosÚpathÚjoinÚLogFileNameÚwriteÚfindÚappend)r   r   r#   r    ÚlineÚvaluesÚkeyÚvaluer   r   r   Úlog_message:   s    


zResultProcessor.log_messageTc       
      C   s²   |   ||¡ |  |¡ |d  d¡}d|kr>t d|d ¡}nt d|d ¡}x^|D ]V}|rœ| d¡d }| |¡}tj ||¡}	td||	f ƒ | 	|	¡ |  j
d	7  _
qTW d S )
NZ	OutputKeyr   ZOutputBucketZs3ZBucketú;r   zretrieving file: %s to %sé   )r-   r   ÚsplitÚbotoÚlookupr"   r#   r$   ÚprintÚget_contents_to_filenamer	   )
r   Úrecordr#   Úget_fileZoutputsÚbucketÚoutputZkey_namer+   Ú	file_namer   r   r   Úprocess_recordI   s    



zResultProcessor.process_recordc             C   sV   | j  ¡ }xF|rPd|krD|d | jkrD|  |||¡ |rD| j  |¡ | j  ¡ }qW d S )NZBatch)r   Úreadr   r:   Zdelete_message)r   r#   r6   Ú
delete_msgÚmr   r   r   Úget_results_from_queueZ   s    
z&ResultProcessor.get_results_from_queuec             C   s2   | j  d| j ¡}x|D ]}|  |||¡ qW d S )Nz['Batch'='%s'])r   Zqueryr   r:   )r   r#   r6   ZrsÚitemr   r   r   Úget_results_from_domainc   s    
z'ResultProcessor.get_results_from_domainc             C   s\   | j  d¡}|rXtdƒ x>|D ]6}tj ||¡}td||f ƒ | |¡ | jd  qW d S )NZoutput_bucketzCNo output queue or domain, just retrieving files from output_bucketzretrieving file: %s to %sr/   )r   r   r3   r"   r#   r$   r4   r	   )r   r#   r7   r+   r9   r   r   r   Úget_results_from_bucketh   s    

z'ResultProcessor.get_results_from_bucketc             C   sô   t j |¡st  |¡ | jr*|  ||¡ n| jr>|  ||¡ n
|  |¡ | j	rX| j	 
¡  td| j ƒ | jdkrðt| jƒ| j | _td| jj ƒ td| jj ƒ td| j ƒ | j| j | _td| jj ƒ d| jjd | j  }td	| ƒ d S )
Nz"%d results successfully retrieved.r   zMinimum Processing Time: %dzMaximum Processing Time: %dzAverage Processing Time: %fzElapsed Time: %dg      ð?g      N@z$Throughput: %f transactions / minute)r"   r#   ÚisdirÚmkdirr   r>   r   r@   rA   r   Úcloser3   r	   Úfloatr
   Zavg_timer   r   r   r   r   r   )r   r#   r6   r<   Ztputr   r   r   Úget_resultsr   s&    



zResultProcessor.get_results)N)T)TT)T)TT)Ú__name__Ú
__module__Ú__qualname__r%   r   r   r-   r:   r>   r@   rA   rF   r   r   r   r   r      s   


	

r   )r"   r   r   Z
boto.utilsr   r1   Úobjectr   r   r   r   r   Ú<module>   s   