
œùQ`  ã               @   sÈ   d  d l  m Z m Z d  d l m Z m Z d  d l m Z d  d l Z d  d l	 Z	 d  d l
 Z d  d l Z d  d l m Z d  d l	 m Z d  d l Z Gd d „  d e ƒ Z Gd	 d
 „  d
 e ƒ Z d S)é    )ÚResourceÚreqparse)ÚjsonifyÚmake_response)ÚdbN)ÚOrderedDict)Údatec               @   s   e  Z d  Z d d „  Z d S)ÚGetCallLogsc       '      C   s¯  t  j ƒ  } | j d ƒ | j d d t j j ƒ  j d ƒ ƒ| j d d t j j ƒ  j d ƒ ƒ| j d d d ƒ| j ƒ  } | d } | d } | d } | d } d d	 d
 d d d d d d d d d d d g } i  } t ƒ  } i  }	 g  }
 g  } g  } g  } g  } g  } g  } g  } g  } g  } g  } g  } g  } t	 j
 j t j j | | | ƒ ƒ } xj| D]b\ } } } } } } } } }  }! }" }# }$ |
 j t | ƒ ƒ | j t | ƒ ƒ | j t | ƒ ƒ | j t | ƒ ƒ | j t | ƒ ƒ | j t | ƒ ƒ | j t | ƒ ƒ | j t | ƒ ƒ | j t |  ƒ ƒ | j t |! ƒ ƒ | j t |" ƒ ƒ | j t |# ƒ ƒ |$ d k r•| j d ƒ qh|$ d k r±| j d ƒ qh|$ d k rh| j d ƒ qhW|
 | d	 <| | d
 <| | d <| | d <| | d <| | d <| | d <| | d <| | d <| | d <| | d <| | d <| | d <t | ƒ t j | ƒ }% t j t j d ƒ }& |% j |& d d d d  ƒ|& j ƒ  t d! ƒ d  S)"NÚaccount_numberÚ
start_dateÚdefaultz%Y-%m-%dÚend_dateÚcall_directionr   ÚIDÚSourcezIncoming DIDÚDestinationz
Start Datez
Start TimezAnswered TimezEnd TimezDuration (in sec)zCall Rate / min (in GBP)zCall Cost (in GBP)z	Area CodezDestination DetailszCall Directioné   ZINBOUNDé   ZOUTBOUNDé   Z	EXTENSIONzoutput.xlsxÚindexFÚ
sheet_namez	Call Logsz-Call logs successfully written to Excel File.)r   ÚRequestParserÚadd_argumentÚdatetimer   ÚtodayÚstrftimeÚ
parse_argsr   r   ÚsessionÚexecuteÚconstÚSQL_CALL_LOGS_XLSÚformatÚappendÚstrÚprintÚpdZ	DataFrameÚExcelWriterÚCALL_LOG_XL_PATHÚto_excelÚsave)'ÚselfÚparserÚargsr
   r   r   r   ÚcolumnsZinbound_callsZoutbound_callsZl_sourceZl_incoming_didZl_destinationZl_start_dateZl_start_timeZl_answered_timeZ
l_end_timeZ
l_durationZl_call_rateZl_call_costZl_area_codeZl_destination_detailsZl_call_directionZ	call_logsZ	call_fromZcall_to_didZcall_toZcall_start_dateZcall_get_timeZcall_start_timeZcall_end_timeZcall_durationZcall_rate_per_minZ	call_costZcall_area_codeZcall_locationÚstatusZdf_call_logsÚwriter© r0   ú3/var/www/html/sampleapi.quiits.com/get_call_logs.pyÚpost   sŠ    %%



	$1














zGetCallLogs.postN)Ú__name__Ú
__module__Ú__qualname__r2   r0   r0   r0   r1   r	      s   r	   c               @   s   e  Z d  Z d d „  Z d S)ÚGetCallLogsUsingPandasc             C   s  t  j ƒ  } | j d ƒ | j d d t j j ƒ  j d ƒ ƒ| j d d t j j ƒ  j d ƒ ƒ| j d d d ƒ| j ƒ  } | d } | d } | d } | d } t t	 | ƒ ƒ d d	 d
 d d d d d d d d d d d g } t d ƒ t
 t j ƒ  ƒ } t j ƒ  }	 d t	 |	 ƒ t	 | ƒ d }
 t t	 t j j | | | d ƒ ƒ ƒ t j t j j | | | d ƒ d t j ƒ} t j t j |
 d d ƒ} | j | d d d d  d! d" ƒ| j } | j d" } | j d# d$ i ƒ } | j d% d& | ƒ | j d' d& | ƒ | j d( d& | ƒ | j d) d& | ƒ | j d* d& | ƒ | j d# d+ i ƒ } | j d, d- | ƒ | j d. d- | ƒ | j d/ d- | ƒ | j d# d0 i ƒ } | j d1 d- | ƒ | j d# d2 i ƒ } | j d3 d4 | ƒ | j d# d5 i ƒ } | j d6 d& | ƒ | j d# d7 i ƒ } | j d8 d& | ƒ | j d9 d& | ƒ | j ƒ  t d: ƒ t d; d< d= d> d? d@ dA |
 i ƒ S)BNr
   r   r   z%Y-%m-%dr   r   r   r   r   zIncoming DIDr   z
Start Datez
Start TimezAnswered TimezEnd TimezDuration (in sec)zCall Rate / min (in GBP)zCall Cost (in GBP)z	Area CodezDestination DetailszCall Directionz"Preparing to fetch data from tableZcdr_z.xlsxÚ ÚconÚengineZ
xlsxwriterÚheaderTr   Fr   z	Call LogsZ
num_formatÚ0zA1:Aé   zB1:BzC1:CzH1:HzK1:Kzhh:mm:sszE1:Eé   zF1:FzG1:Gz
yyyy/mm/ddzD1:Dz0.000zI1:Ié   z0.00000zJ1:Jú@zL1:LzM1:Mz-Call logs successfully written to Excel File.r.   r   ÚmessageZSuccessÚresultZ	file_linkz!https://api.quiits.com/templates/)r   r   r   r   r   r   r   r   r$   r#   ÚintÚtimer   r    r!   r%   Zread_sqlr   r9   r&   r'   r(   ZbookZsheetsZ
add_formatZ
set_columnr)   r   )r*   r+   r,   r
   r   r   r   r-   Z
epoch_timer   Ú	file_nameZsql_dfr/   ZworkbookZ	worksheetZsource_formatÚtime_formatZdate_formatZrate_per_min_formatZcall_cost_formatZtext_formatr0   r0   r1   r2   `   sf    %%





%		

zGetCallLogsUsingPandas.postN)r3   r4   r5   r2   r0   r0   r0   r1   r6   _   s   r6   )Úflask_restfulr   r   Úflaskr   r   Úappr   Úrequestsr   Ú	constantsr   Zpandasr%   Úcollectionsr   r   rC   r	   r6   r0   r0   r0   r1   Ú<module>   s   R