
    %                     X   d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddl	m
Z ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlZdZd Zd Zd Zd Zd Z ejB                   ejD                  ejF                  jH                         G d dejJ                                      Z& ejD                  ejF                  jN                         G d de&             Z( ejD                  ejF                  jR                         G d de(             Z*y)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                    | sy t         j                  j                  | t        j                  j
                  j                  j                         t        j                  j                  j                  j                         dd      S )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                     ddj                  |      dg}| r,| j                  }|j                  dj                  |             dj                  |      S )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                     ddj                  |      ddg}| rJ| j                  j                         j                  dd      }|j	                  dj                  |             dj                  |      S )	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           
         t        | j                        }t        j                  j                  j
                  j                         }t        j                         rt        ||      g}nIt        j                         rt        ||      g}n'dj                  t        ||      t        ||            g}| j                  r*|j                  dj                  | j                               | j                  r8|j                  dj                  | j                  j!                                      | j"                  r=|j                  dj                  t%        j&                  | j"                                     |j                  dj                  t%        j&                  | j(                  xs5 t*        j*                  j-                         t+        j.                  d      z
                     dj1                  |      S )	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 	$\6:$\6:	
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              #     K   | D ]  }|j                   }|j                  rh|j                  j                  D cg c]  }|j                  dk(  r|j                   }}t        |      dk(  rt        |d   d      r|d   j                  }d|i}|j                  r?t        j                  |j                        }|t        j                  v r	|d   |d<   n||d<   |j                  rY|j                  j                  rC|j                  j                  j                  D ]   }|j                  dv s|j                  |d<   " |j                  r:|j                  j                  D ]!  }|j                  d	k(  s|j                  |d	<   # |j                  r"t!        j"                  |j                        |d
<   |  yc c}w w)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   7F>"F9CF>,AF>2AF>c                   D    e Zd ZdZed        Zej                  d        Zy)GetLogs7Display log entries produced by Google Cloud Functions.c                 P   t        j                  | d       t        j                  j	                  |        | j                  ddd       | j                  dd	       | j                  d
dt        j                  j                  d       | j                  ddt        j                  j                  d       | j                  ddt        j                  dd      dd       t        j                  |        | j                  j                  d       t        j                  |        y)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.)rf   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typerf   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.)rg   rh   defaultrf   N)r   AddRegionFlagr
   
LIMIT_FLAGRemoveFromParseradd_argumentr	   Datetimer   
BoundedIntAddMinLogLevelFlagdisplay_infoAddCacheUpdaterAddGen2Flag)parsers    r   ArgszGetLogs.Args   s*    
H 	OO$$V,
C	   ?   !!''?  
 !!''J   ##At,<  
 
V$
''-	fr    c                    |j                  d      st        |_        t        |      }t	        t        j                  |d|j                              }|j                  r|st        j                  | j                               }t        |j                        }|j                  |j                               s9t        j                   dj                  |j"                  |j$                               t'        |      S )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	FetchLogsrz   r   	client_v2FunctionsClientReleaseTrackr   GetFunctionRelativeNamer   warningr$   r   r^   )selfrA   r(   rX   r   r'   s         r   RunzGetLogs.Run   s     H%)dk!$'J  fDJJOG yy(():):)<=f$TYY/l 9 9 ;<&&,f((,*B*B'	
 G$$r    N)	__name__
__module____qualname____doc__staticmethodrv   util_v1 CatchHTTPErrorRaiseHTTPExceptionr    r    r   r`   r`      s5     @9 9v ++ % , %r    r`   c                       e Zd ZdZy)GetLogsBetara   Nr   r   r   r   r   r    r   r   r         ?r    r   c                       e Zd ZdZy)GetLogsAlphara   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    