o
    i3                     @   s   d dl Z d dl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
mZmZ d dlmZ d dlmZ G dd deZdd	 ZdS )
    N)api)report_errorsreport_warnings	print_err)pretty_json)datetimec                   @   s   e Zd Zedd Zedd Zedd Zedd ZedddZedd Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd ZdS )TimeboardClientc                 C   s~  |j ddd}|jddddd |jd	d
d}d|_|j ddd}|jddd |jddd |jdddd |jdtg dd |j| jd |j ddd}|jddd |jddd |jddd |jdd dd |jdtg dd |j| jd |j d!d"d}|jdd#d |j| jd |j d$d%d}|j| j	d |j d&d'd}|jdd(d |jd)d*d |j| j
d |j d+d,d}	|	jd-d.d |	j| jd |j d/d0d}
|
jd1dd2d3d |
jd4d5d6td7d8 |
j| jd |j d9d:d}|jd)d;d |jdddd |j| jd |j d<d=d}|jd4d>td7d? |j| jd |j d@dAd}|jddBd |j| jd d S )CN	timeboardz#Create, edit, and delete timeboards)helpz--string_ids
store_true
string_idsz:Represent timeboard IDs as strings instead of ints in JSON)actiondestr
   Verbsverb)titler   TpostzCreate timeboardsr   ztitle for the new timeboarddescriptionz"short description of the timeboardgraphsz?graph definitions as a JSON string. if unset, reads from stdin.?)r
   nargsz--template_variableszma json list of template variable dicts, e.g. [{'name': 'host', 'prefix': 'host', 'default': 'host:my-host'}]')typedefaultr
   )funcupdatezUpdate existing timeboardstimeboard_idz,timeboard to replace with the new definitionznew title for the timeboardz>graph definitions as a JSON string. if unset, reads from stdinshowzShow a timeboard definitionztimeboard to showshow_allzShow a list of all timeboardspullz0Pull a timeboard on the server into a local filezID of timeboard to pullfilenamezfile to pull timeboard intopull_allz-Pull all timeboards into files in a directorypull_dirz!directory to pull timeboards intopushz9Push updates to timeboards from local files to the serverz--append_auto_textappend_auto_textzbWhen pushing to the server, appends filename and timestamp to the end of the timeboard descriptionfilez%timeboard files to push to the server+r)r
   r   r   new_filez5Create a new timeboard and put its contents in a filez+name of file to create with empty timeboardweb_viewz#View the timeboard in a web browserztimeboard file)r
   r   deletezDelete timeboardsztimeboard to delete)
add_parseradd_argumentadd_subparsersrequired_template_variablesset_defaults_post_update_show	_show_all_pull	_pull_allargparseFileType_push	_new_file	_web_view_delete)cls
subparsersparserverb_parserspost_parserupdate_parsershow_parsershow_all_parserpull_parserpull_all_parserpush_parsernew_file_parserweb_view_parserdelete_parser rJ   N/home/ubuntu/.local/lib/python3.10/site-packages/datadog/dogshell/timeboard.pysetup_parser   s   	zTimeboardClient.setup_parserc                 C   s    |  |j|j|j|j|j d S N)_write_dash_to_filer   r   timeoutformatr   )r<   argsrJ   rJ   rK   r4   u   s    zTimeboardClient._pullc              	   C   s   |j t_dd }|j}tj }t| t| tj	
|js&t|jd t }|d D ].}||d }||v rA|d |d  }|| | |d tj	|j|d |j ||j q-|d	krqtd
t|tj	|j d S d S )Nc                 S   s2   d dd |  D }|dddddS )N c                 S   s    g | ]}|  s|d v r|qS )) _-)isalnum).0crJ   rJ   rK   
<listcomp>   s     zITimeboardClient._pull_all.<locals>._title_to_filename.<locals>.<listcomp>rS   rT   rU   )joinlowerreplacestrip)r   no_punctrJ   rJ   rK   _title_to_filename}   s   z5TimeboardClient._pull_all.<locals>._title_to_filenamei  dashesr   rU   idz.jsonprettyz%
### Total: {0} dashboards to {1} ###)rO   r   _timeoutrP   	Timeboardget_allr   r   ospathexistsr!   mkdirsetaddrN   rZ   r   printlenrealpath)r<   rQ   r_   rP   resused_filenamesdash_summaryr   rJ   rJ   rK   r5   y   s4   

zTimeboardClient._pull_allc                 C   s   |j t_|j}|j}|jd u rtj }t	|}tj
j|jd|j|gd}t| t| | |d d |j|j ||j |dkrLtt| d S tt| d S )NzDescription for {0})r   r   r   dashra   rb   )rO   r   rc   rP   r   sysstdinreadjsonloadsrd   creater   r   r   rN   r   rl   r   dumpsr<   rQ   rP   r   ro   rJ   rJ   rK   r9      s   


 zTimeboardClient._new_filerawFc           	      C   s   t |dc}tj|}t| t| |d }d|v r|d= d|v r&|d= |r0t|d |d< |ds;|dd  tj	||dd |d	krPt
d
|| nt
d|| W d    d S W d    d S 1 skw   Y  d S )Nwrr   resourceurlra   template_variables   )indentrb   z$Downloaded dashboard {0} to file {1}z{0} {1})openr   rd   getr   r   strpoprv   dumprl   rP   )	r<   dash_idr   rO   rP   r   fro   dash_objrJ   rJ   rK   rN      s(   
"z#TimeboardClient._write_dash_to_filec           	      C   sn  |j t_|jD ]}zt|}W n ty% } z	td|j|d }~ww |j	rEt
 d}d||j|d t }|d  |7  < |dg }d|v rkt|d |d< tjj|d |d |d |d |d	}ntjj|d |d |d |d	}d
|v rtd|d |j t| t| tdkrtt| ntt| |jdkrtd|j|d  qd S )NzCould not parse {0}: {1}z%x %Xz*<br/>
Updated at {0} from {1} ({2}) on {3}ra   r   r   r   r   r   r   r   r   errorsz-Upload of dashboard {0} from file {1} failed.rb   z!Uploaded file {0} (dashboard {1}))rO   r   rc   r$   rv   load	ExceptionrP   namer#   r   nowstrftimeplatformnoder   intrd   r   rx   r   r   r   rl   r   ry   )	r<   rQ   r   r   errdatetime_str	auto_texttpl_varsro   rJ   rJ   rK   r8      sR   

zTimeboardClient._pushc                 C   s   |j t_|j}|j}|jd u rtj }t	|}tj
j|j|j|g|jd}t| t| |dkr;tt| d S tt| d S Nr   rb   )rO   r   rc   rP   r   rs   rt   ru   rv   rw   rd   rx   r   r   r   r   r   rl   r   ry   rz   rJ   rJ   rK   r0      s   


zTimeboardClient._postc                 C   s   |j t_|j}|j}|jd u rtj }t	|}tj
j|j|j|j||jd}t| t| |dkr<tt| d S tt| d S r   )rO   r   rc   rP   r   rs   rt   ru   rv   rw   rd   r   r   r   r   r   r   r   rl   r   ry   rz   rJ   rJ   rK   r1     s$   


zTimeboardClient._updatec                 C   st   |j t_|j}tj|j}t| t| |j	r%t
|d d |d d< |dkr1tt| d S tt| d S )Nrr   ra   rb   )rO   r   rc   rP   rd   r   r   r   r   r   r   rl   r   rv   ry   )r<   rQ   rP   ro   rJ   rJ   rK   r2   (  s   zTimeboardClient._showc                 C   s   |j t_|j}tj }t| t| |jr&|d D ]
}t	|d |d< q|dkr2t
t| d S |dkr?t
t| d S |d D ]}t
d|d |d |d | |d g qCd S )	Nr`   ra   rb   r{   	r}   r   r   )rO   r   rc   rP   rd   re   r   r   r   r   rl   r   rv   ry   rZ   _escape)r<   rQ   rP   ro   drJ   rJ   rK   r3   8  s   
.zTimeboardClient._show_allc                 C   s6   |j t_tj|j}|d urt| t| d S d S rM   )rO   r   rc   rd   r)   r   r   r   )r<   rQ   ro   rJ   rJ   rK   r;   L  s   zTimeboardClient._deletec                 C   s.   t |jd }tjd| }t| d S )Nra   z/dash/dash/{0})rv   r   r$   r   	_api_hostrP   
webbrowserr   )r<   rQ   r   r~   rJ   rJ   rK   r:   T  s   zTimeboardClient._web_viewc                 C   s$   |r| dd dd ddS dS )Nz\r
z\nr   z\trR   )r\   )r<   srJ   rJ   rK   r   Z  s   $zTimeboardClient._escapeN)r{   F)__name__
__module____qualname__classmethodrL   r4   r5   r9   rN   r8   r0   r1   r2   r3   r;   r:   r   rJ   rJ   rJ   rK   r      s6    
^

$

0





r   c                 C   sB   d| vrdd |  dD S zt| W S  ty    tdw )N[c                 S   s   g | ]}|  qS rJ   )r]   )rW   vrJ   rJ   rK   rY   a  s    z'_template_variables.<locals>.<listcomp>,z$bad template_variable json parameter)splitrv   rw   r   r6   ArgumentTypeError)tpl_var_inputrJ   rJ   rK   r.   _  s   
r.   )rv   os.pathrf   r   rs   r   r6   datadogr   datadog.dogshell.commonr   r   r   datadog.util.formatr   r   objectr   r.   rJ   rJ   rJ   rK   <module>   s     L