
    9Yf3                         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e      Zd Zy)	    N)api)report_errorsreport_warnings	print_err)pretty_json)datetimec                       e Zd Zed        Zed        Zed        Zed        Zedd       Zed        Z	ed        Z
ed        Zed	        Zed
        Zed        Zed        Zed        Zy)TimeboardClientc                 z   |j                  dd      }|j                  dddd       |j                  d	d
      }d|_        |j                  dd      }|j                  dd       |j                  dd       |j                  ddd       |j                  dt        g d       |j                  | j                         |j                  dd      }|j                  dd       |j                  dd       |j                  dd       |j                  dd d       |j                  dt        g d       |j                  | j                         |j                  d!d"      }|j                  dd#       |j                  | j                         |j                  d$d%      }|j                  | j                         |j                  d&d'      }|j                  dd(       |j                  d)d*       |j                  | j                         |j                  d+d,      }	|	j                  d-d.       |	j                  | j                         |j                  d/d0      }
|
j                  d1dd2d3       |
j                  d4d5d6t        j                  d7      8       |
j                  | j                         |j                  d9d:      }|j                  d)d;       |j                  ddd       |j                  | j                         |j                  d<d=      }|j                  d4d>t        j                  d7      ?       |j                  | j                          |j                  d@dA      }|j                  ddB       |j                  | j"                         y )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_parsers                 Z/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/datadog/dogshell/timeboard.pysetup_parserzTimeboardClient.setup_parser   s    &&{9^&_M	 	 	
 ,,7,H $"--f;N-O  /L M  5Y Z  _gj 	! 	
 	  "$+	 	! 	
 	  cii 0$//?[/\"">8i"j""71N"O""=7["\""^fi 	# 	
 	"""$+	 	# 	
 	"""4"--f;X-Y  6I J  cii 0&11*Cb1c$$#--$8"--f;p-q  6O P  2O P  cii 0&11*Cu1v$$Z6Y$Z$$#--$8"--W . 
 	   #E	 	! 	
 	  @RZRcRcdgRh 	! 	
 	  cii 0&11W 2 
 	$$Z6c$d$$_gj 	% 	
 	$$#--$8&11*Ch1i$$V2BIZIZ[^I_$`$$#--$8$//?R/S"">8M"N"""4    c                     | j                  |j                  |j                  |j                  |j                  |j
                         y N)_write_dash_to_filer   r"   timeoutformatr   )r?   argss     rM   r7   zTimeboardClient._pullu   s4     1 14==$,,PTP[P[]a]l]lmrO   c           	         |j                   t        _        d }|j                  }t        j                  j                         }t        |       t        |       t        j                  j                  |j                        s t        j                  |j                  d       t               }|d   D ]  } ||d         }||v r|dz   |d   z   }|j                  |       | j                  |d   t        j                  j!                  |j                  |dz         |j                   ||j"                          |dk(  rLt%        d	j                  t'        |      t        j                  j)                  |j                                     y y )
Nc                     dj                  | j                         D cg c]  }|j                         s|dv s| c}      }|j                  dd      j                  dd      j	                  d      S c c}w )N ) _-rY   rZ   r[   )joinlowerisalnumreplacestrip)r   cno_puncts      rM   _title_to_filenamez5TimeboardClient._pull_all.<locals>._title_to_filename}   sb    ww5;;=`aAIIK1P_K_`aH##C-55c3?EEcJJ  as
   A4A4i  dashesr   r[   idz.jsonprettyz%
### Total: {0} dashboards to {1} ###)rS   r   _timeoutrT   	Timeboardget_allr   r   ospathexistsr$   mkdirsetaddrR   r\   r   printlenrealpath)r?   rU   rc   rT   resused_filenamesdash_summaryr"   s           rM   r8   zTimeboardClient._pull_ally   s?   ||	K mm##%cww~~dmm,HHT]]E*M 	L),w*?@H>)#c>L,>>x(##T"T]]Hw,>?	 X9@@^ATVXV]V]VfVfgkgtgtVuv rO   c                 v   |j                   t        _        |j                  }|j                  }|j                  t
        j                  j                         }t        j                  |      }t        j                  j                  |j                  dj                  |j                        |g      }t        |       t        |       | j                  |d   d   |j                  |j                   ||j                          |dk(  rt#        t%        |             y t#        t        j&                  |             y )NzDescription for {0})r   r   r   dashre   rf   )rS   r   rg   rT   r   sysstdinreadjsonloadsrh   creater"   r   r   rR   r   rp   r   dumpsr?   rU   rT   r   rs   s        rM   r<   zTimeboardClient._new_file   s    ||;;YY^^%FF#mm""---B-I-I$---Xbhai # 
 	cFD 14==$,,PVX\XgXghX+c"#$**S/"rO   c                    t        |d      5 }t        j                  j                  |      }t	        |       t        |       |d   }d|v r|d= d|v r|d= |rt        |d         |d<   |j                  d      s|j                  dd        t        j                  ||d       |d	k(  rt        d
j                  ||             nt        dj                  ||             d d d        y # 1 sw Y   y xY w)Nwrw   resourceurlre   template_variables   )indentrf   z$Downloaded dashboard {0} to file {1}z{0} {1})openr   rh   getr   r   strpopr{   dumprp   rT   )	r?   dash_idr"   rS   rT   r   frs   dash_objs	            rM   rR   z#TimeboardClient._write_dash_to_file   s    (C  	<A--##G,CC #6{HX%Z( UO!$Xd^!4<< 45148IIh!,!=DDWhWXj'':;-	< 	< 	<s   CC))C2c                 @   |j                   t        _        |j                  D ]  }	 t	        j
                  |      }|j                  rct        j                         j                  d      }dj                  ||j                  |d   t        j                               }|dxx   |z  cc<   |j                  dg       }d|v rBt!        |d         |d<   t        j"                  j%                  |d   |d   |d   |d   |	      }n,t        j"                  j'                  |d   |d   |d   |	      }d
|v r(t)        dj                  |d   |j                               t+        |       t-        |       t        dk(  rt/        t1        |             nt/        t	        j2                  |             |j                  dk(  st/        dj                  |j                  |d                 y # t        $ r*}t        dj                  |j                  |            d }~ww xY w)NzCould not parse {0}: {1}z%x %Xz*<br/>
Updated at {0} from {1} ({2}) on {3}re   r   r   r   r   r   r   r   r   errorsz-Upload of dashboard {0} from file {1} failed.rf   z!Uploaded file {0} (dashboard {1}))rS   r   rg   r'   r{   load	ExceptionrT   namer&   r   nowstrftimeplatformnoder   intrh   r   r}   r   r   r   rp   r   r~   )	r?   rU   r   r   errdatetime_str	auto_texttpl_varsrs   s	            rM   r;   zTimeboardClient._push   s   || ,	ZAP99Q< $$'||~66w?IPP !&&(4.(--/	 '94'||$8"=Hx!$Xd^!4mm**TN"7+ ( 7#H-'/ +  mm**"7+ ( 7#H-'/	 +  3IPPQYZ^Q_abagaghiC #!k#&'djjo&{{h&9@@RVXYY,	Z  P : A A!&&# NOOPs   G**	H3%HHc                    |j                   t        _        |j                  }|j                  }|j                  t
        j                  j                         }t        j                  |      }t        j                  j                  |j                  |j                  |g|j                        }t        |       t!        |       |dk(  rt#        t%        |             y t#        t        j&                  |             y Nr   rf   )rS   r   rg   rT   r   rx   ry   rz   r{   r|   rh   r}   r   r   r   r   r   rp   r   r~   r   s        rM   r3   zTimeboardClient._post   s    ||;;YY^^%FF#mm""**$*:*:F8`d`w`w # 
 	cX+c"#$**S/"rO   c                    |j                   t        _        |j                  }|j                  }|j                  t
        j                  j                         }t        j                  |      }t        j                  j                  |j                  |j                  |j                  ||j                        }t!        |       t#        |       |dk(  rt%        t'        |             y t%        t        j(                  |             y r   )rS   r   rg   rT   r   rx   ry   rz   r{   r|   rh   r   r   r   r   r   r   r   rp   r   r~   r   s        rM   r4   zTimeboardClient._update  s    ||;;YY^^%FF#mm""**((#66 # 
 	cX+c"#$**S/"rO   c                 z   |j                   t        _        |j                  }t        j                  j                  |j                        }t        |       t        |       |j                  rt        |d   d         |d   d<   |dk(  rt        t        |             y t        t        j                  |             y )Nrw   re   rf   )rS   r   rg   rT   rh   r   r   r   r   r   r   rp   r   r{   r~   )r?   rU   rT   rs   s       rM   r5   zTimeboardClient._show(  s    ||mm 1 12c?? #CK$5 6CKX+c"#$**S/"rO   c                    |j                   t        _        |j                  }t        j                  j                         }t        |       t        |       |j                  r|d   D ]  }t        |d         |d<    |dk(  rt        t        |             y |dk(  rt        t        j                  |             y |d   D ];  }t        dj                  |d   |d   |d   | j                  |d         g             = y )	Nrd   re   rf   raw	r   r   r   )rS   r   rg   rT   rh   ri   r   r   r   r   rp   r   r{   r~   r\   _escape)r?   rU   rT   rs   ds        rM   r6   zTimeboardClient._show_all8  s    ||mm##%c??] 'ag,$' X+c"#u_$**S/"] ldii!D'Qz]aj3;;WXYfWgKh ijklrO   c                     |j                   t        _        t        j                  j	                  |j
                        }|t        |       t        |       y y rQ   )rS   r   rg   rh   r,   r   r   r   )r?   rU   rs   s      rM   r>   zTimeboardClient._deleteL  sA    ||mm""4#4#45?C # rO   c                     t        j                  |j                        d   }t        j                  dj                  |      z   }t        j                  |       y )Nre   z/dash/dash/{0})r{   r   r'   r   	_api_hostrT   
webbrowserr   )r?   rU   r   r   s       rM   r=   zTimeboardClient._web_viewT  s?    ))DII&t,mm.55g>>rO   c                 n    |r2|j                  dd      j                  dd      j                  dd      S dS )Nz\r
z\nr   z\trX   )r_   )r?   ss     rM   r   zTimeboardClient._escapeZ  s5    STqyyu%--dE:BB4O\Z\\rO   N)r   F)__name__
__module____qualname__classmethodrN   r7   r8   r<   rR   r;   r3   r4   r5   r6   r>   r=   r    rO   rM   r
   r
      s   \5 \5| n n " "H # #* < <2 .Z .Z` # #" # #, # # l l&    
 ] ]rO   r
   c                     d| vr-| j                  d      D cg c]  }|j                          c}S 	 t        j                  |       S c c}w # t        $ r t        j                  d      w xY w)N[,z$bad template_variable json parameter)splitr`   r{   r|   r   r9   ArgumentTypeError)tpl_var_inputvs     rM   r1   r1   _  sk    
-#0#6#6s#;<a	<<	U::m,, =  	U,,-STT	Us   AA A,)r{   os.pathrj   r   rx   r   r9   datadogr   datadog.dogshell.commonr   r   r   datadog.util.formatr   r   objectr
   r1   r   rO   rM   <module>r      sC   
    
    M M + G]f G]T
UrO   