o
    zKh                     @   s   d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	Z	d dl
Z
d dlmZ d dlmZ e
eZdd	 Zeed
gdd Zeedgdd ZdS )    )JsonResponseHttpResponseBadRequest)csrf_exempt)require_http_methods)settingsN)datetime)Elasticsearchc                   C   s   t dtj dtj dddS )Nhttps://elastic:@,  Trequest_timeoutretry_on_timeout)r   r   ES_PASSWORDES_URL r   r   2/var/www/html/livechatnow/livechatnow_api/views.pyget_elasticsearch_client   s   r   POSTc              
   C   sZ  z\t | jd}t  |d< t  |d< d|d< |ds2t	
d tdd	d
ddW S d}t }|j||d}t	d|d d|  tdd||d|ddW S  t jy } zt	d|  tddd
ddW  Y d }~S d }~w ty } z!t	dt|  tddtjrt|nd dddW  Y d }~S d }~ww )Nzutf-8z
@timestampZprocessed_atlivehelpnow_webhooksource
event_typez#Received payload without event_typeerrorzMissing event_type in payload)statusmessagei  r   Zlivehelpnow)indexbodyz(Successfully indexed LiveHelpNow event: z to successT_idresult)r   Zindexedr   Zdocument_idr    z!Invalid JSON in webhook payload: zInvalid JSON formatz&Error processing LiveHelpNow webhook: zInternal server error)r   r   detailsi  )jsonloadsr   decoder   utcnow	isoformatnowgetloggerwarningr   r   r   infoJSONDecodeErrorr   	Exceptionstrr   DEBUG)requestpayload
index_nameesresponseer   r   r   r      sX   


r   GETc              
   C   s   d}i }zOt dtj dtj ddd}| }|rNz| }|d|di d	d
}W n tyJ } zddt| i}W Y d }~nd }~ww d}nddi}W n tyk } zdt|i}W Y d }~nd }~ww t	|dkrsdnd|d|dS )Ni  r	   r
   r   Tr   cluster_nameversionnumber)clusterr8   r*   zping ok, info error:    r   zping returned FalseokZ	unhealthy)r   elasticsearchr   )
r   r   r   r   pingr+   r(   r-   r.   r   )r0   r   r!   r3   r<   r+   r5   r   r   r   webhook_health_check?   s*   "r?   )django.httpr   r   django.views.decorators.csrfr   Zdjango.views.decorators.httpr   django.confr   r"   loggingr   r=   r   	getLogger__name__r)   r   r   r?   r   r   r   r   <module>   s     
-