
    %                     X   S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSK	J
r  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKrSrS rS rS rS rS r \RB                  \RD                  " \RF                  RH                  5       " S S\RJ                  5      5       5       r&\RD                  " \RF                  RN                  5       " S S\&5      5       r(\RD                  " \RF                  RR                  5       " S S\(5      5       r*g)z8Displays log entries produced by Google Cloud Functions.    )absolute_import)division)unicode_literalsN)util)client)common)arg_parsers)base)parser_extensions)flags)log)
properties)	resourcesz+table(level,name,execution_id,time_utc,log)c                    U (       d  g [         R                  R                  U [        R                  R
                  R                  R                  5       [        R                  R                  R                  R                  5       S.SS9$ )N)
projectsIdlocationsIdz+cloudfunctions.projects.locations.functions)params
collection)
r   REGISTRYParser   VALUEScoreproject	GetOrFail	functionsregion)names    "lib/surface/functions/logs/read.py_GetFunctionRefr   (   so    					!	!
"))..66@@B#**44;;EEG ? 
" 
     c                     SSR                  U5      S/nU (       a,  U R                  nUR                  SR                  U5      5        SR                  U5      $ )z(Generates Gen1-specific log filter base.zresource.type="cloud_function"zresource.labels.region="{}"zlogName:"cloud-functions"z"resource.labels.function_name="{}" )formatfunctionsIdappendjoin)function_refr   
log_filterfunction_ids       r   _CreateGen1LogFilterBaser*   7   sX     '#**62!* **K:AA+NO	*	r    c                     SSR                  U5      SS/nU (       aJ  U R                  R                  5       R                  SS5      nUR	                  SR                  U5      5        SR                  U5      $ )	z(Generates Gen2-specific log filter base.z"resource.type="cloud_run_revision"zresource.labels.location="{}"zlogName:"run.googleapis.com"z)labels."goog-managed-by"="cloudfunctions"_-z!resource.labels.service_name="{}"r"   )r#   r$   lowerreplacer%   r&   )r'   r   r(   service_names       r   _CreateGen2LogFilterBaser1   F   ss     +%,,V4$1	*   ++113;;CEL9@@NO	*	r    c                 .   [        U R                  5      n[        R                  R                  R
                  R                  5       n[        R                  " 5       (       a  [        X5      /nOL[        R                  " 5       (       a  [        X5      /nO%SR                  [        X5      [        X5      5      /nU R                  (       a*  UR                  SR                  U R                  5      5        U R                  (       a8  UR                  SR                  U R                  R!                  5       5      5        U R"                  (       a>  UR                  SR                  [$        R&                  " U R"                  5      5      5        UR                  SR                  [$        R&                  " U R(                  =(       d3    [*        R*                  R-                  5       [*        R.                  " SS9-
  5      5      5        SR1                  U5      $ )	zCreates the filter for retrieving function logs based on the given args.


Args:
  args: The arguments that were provided to this command invocation.

Returns:
z({}) OR ({})zlabels.execution_id="{}"zseverity>={}ztimestamp<="{}"ztimestamp>="{}"   )daysr"   )r   r   r   r   r   r   r   r   ShouldUseGen1r*   ShouldUseGen2r1   r#   execution_idr%   min_log_levelupperend_timelogging_utilFormatTimestamp
start_timedatetimeutcnow	timedeltar&   )argsr'   r   r(   s       r   _CreateLogFilterrB   Y   s    !+,&&--779&
*<@AJ*<@AJ 	$\:$\:	
J 
0778I8IJK	n++D,>,>,D,D,FGH	]]  !=!=dmm!LM 

&
&oo I""))+h.@.@a.HH 
*	r    c              #     #    U  GH  nUR                   nUR                  (       at  UR                  R                   Vs/ s H!  nUR                  S:X  d  M  UR                  PM#     nn[        U5      S:X  a#  [        US   S5      (       a  US   R                  nSU0nUR                  (       aA  [        R                  " UR                  5      nU[        R                  ;   a	  US   US'   OXeS'   UR                  (       ac  UR                  R                  (       aH  UR                  R                  R                   H$  nUR                  S;   d  M  UR                  US'   M&     UR                  (       a>  UR                  R                   H$  nUR                  S	:X  d  M  UR                  US	'   M&     UR                  (       a#  [         R"                  " UR                  5      US
'   Uv   GM     gs  snf 7f)zProcesses the given entries to yield rows.

Args:
  entries: the log entries to process.

Yields:
  Rows with level, name, execution_id, time_utc, and log properties.
message   r   string_valuer   level)function_namer0   r   r7   time_utcN)textPayloadjsonPayloadadditionalPropertieskeyvaluelenhasattrrF   severitysix	text_typer   
SEVERITIESresourcelabels	timestampr   r<   )entriesentryrD   proppropsrowrQ   labels           r   _YieldLogEntriesr^      s     eG ''<<<dXX" $**<  
 
UqWU1X~>>('''
C~~u~~.h	U%%	%{G  G~~%..//>>((==%9999#f+ > ||<<44%99& %#n
 5 ,,U__=c*o
I; s%   <G6G1G1&C'G6AG6 AG6c                   L    \ rS rSrSr\S 5       r\R                  S 5       r	Sr
g)GetLogs   7Display log entries produced by Google Cloud Functions.c                 4   [         R                  " U SS9  [        R                  R	                  U 5        U R                  SSSS9  U R                  SSS	9  U R                  S
S[        R                  R                  SS9  U R                  SS[        R                  R                  SS9  U R                  SS[        R                  " SS5      SSS9  [         R                  " U 5        U R                  R                  S5        [         R                  " U 5        g)z Register flags for this command.z4Only show logs generated by functions in the region.)	help_textr   ?ztName of the function which logs are to be displayed. If no name is specified, logs from all functions are displayed.)nargshelpz--execution-idz0Execution ID for which logs are to be displayed.)rg   z--start-timeFzReturn only log entries in which timestamps are not earlier than the specified time. If *--start-time* is not specified, a default start time of 1 week ago is assumed. See $ gcloud topic datetimes for information on time formats.)requiredtyperg   z
--end-timea  Return only log entries which timestamps are not later than the specified time. If *--end-time* is specified but *--start-time* is not, the command returns *--limit* latest log entries which appeared before --end-time. See *$ gcloud topic datetimes* for information on time formats.z--limitrE   i     zNumber of log entries to be fetched; must not be greater than 1000. Note that the most recent entries in the specified time range are returned, rather than the earliest.)rh   ri   defaultrg   N)r   AddRegionFlagr
   
LIMIT_FLAGRemoveFromParseradd_argumentr	   Datetimer   
BoundedIntAddMinLogLevelFlagdisplay_infoAddCacheUpdaterAddGen2Flag)parsers    r   ArgsGetLogs.Args   s*    
H 	OO$$V,
C	   ?   !!''?  
 !!''J   ##At,<  
 
V$
''-	fr    c                     UR                  S5      (       d  [        Ul        [        U5      n[	        [
        R                  " USUR                  S95      nUR                  (       a  U(       d  [        R                  " U R                  5       5      n[        UR                  5      nUR                  UR                  5       5      (       d:  [        R                   " SR                  UR"                  UR$                  5      5        ['        U5      $ )zThis is what gets called when the user runs this command.

Args:
  args: an argparse namespace. All the arguments that were provided to this
    command invocation.

Returns:
  A generator of objects representing log entries.
r#   DESC)order_bylimitzThere is no function named `{}` in region `{}`. Perhaps you meant to specify `--region` or update the `functions/region` configuration property?)IsSpecified_DEFAULT_TABLE_FORMATr#   rB   listlogging_common	FetchLogsr|   r   	client_v2FunctionsClientReleaseTrackr   GetFunctionRelativeNamer   warningr$   r   r^   )selfrA   r(   rX   r   r'   s         r   RunGetLogs.Run   s     H%%)dk!$'J  fDJJOG yy(():):)<=f$TYY/l 9 9 ;<<&&,f((,*B*B'	
 G$$r     N)__name__
__module____qualname____firstlineno____doc__staticmethodrw   util_v1 CatchHTTPErrorRaiseHTTPExceptionr   __static_attributes__r   r    r   r`   r`      s5     @9 9v ++ % , %r    r`   c                       \ rS rSrSrSrg)GetLogsBetai  rb   r   Nr   r   r   r   r   r   r   r    r   r   r         ?r    r   c                       \ rS rSrSrSrg)GetLogsAlphai  rb   r   Nr   r   r    r   r   r     r   r    r   )+r   
__future__r   r   r   r>   #googlecloudsdk.api_lib.functions.v1r   r   #googlecloudsdk.api_lib.functions.v2r   r   googlecloudsdk.api_lib.loggingr   r   r;   googlecloudsdk.callioper	   r
   r   $googlecloudsdk.command_lib.functionsr   googlecloudsdk.corer   r   r   rR   r~   r   r*   r1   rB   r^   DefaultUniverseOnlyReleaseTracksr   GAListCommandr`   BETAr   ALPHAr   r   r    r   <module>r      s   ? &  '  ? C C ? / ( 5 6 5 # * ) 
E &+\&R D%%(()`%d `% * `%F D%%**+@' @ ,@ D%%++,@; @ -@r    