
                             d 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dlmZ ddl	m
Z
 dd	l	mZ d
Zd Zd Zd Zd ZddZy)z+Helper functions for the log read commands.    )absolute_import)division)print_function)unicode_literalsN)util)arg_parsers)basezFilter expression that specifies the log entries to return. Detailed information about filters can be found at: https://cloud.google.com/logging/docs/view/logging-query-languagec                 4    | j                  dt        d       y)z Add a log filter positional arg.
log_filter?)helpnargsNadd_argument_LOG_FILTER_HELP_TEXTparsers    +lib/googlecloudsdk/command_lib/logs/read.pyLogFilterPositionalArgsr   "   s    l)>cJ    c                 2    | j                  dt               y)zAdd a log filter arg.z--log-filter)r   Nr   r   s    r   LogFilterArgsr   '   s    n+@Ar   c                     t         j                  j                  |        t        j                  ddddd      }|j                  |        | j	                  dt        j                         dd	
       y)z%Arguments common to all log commands.z--order)descascFr   z:Ordering of returned log entries based on timestamp field.)choicesrequireddefaulthelp_strz--freshnesszReturn entries that are not older than this value. Works only with DESC ordering and filters without a timestamp. See $ gcloud topic datetimes for information on duration formats.1d)typer   r   N)r	   
LIMIT_FLAGAddToParserChoiceArgumentr   r   Duration)r   	order_args     r   LoggingReadArgsr'   ,   sp    //f%!!KM) !    r   c                    | j                   dk(  rt| j                  rd| j                  vrZt        j                  | j                        }t        j                  j                         |z
  }dt        j                  |      z  gS g S )ag  Create filters for the minimum log timestamp.

  This function creates an upper bound on the timestamp of log entries.
  A filter clause is returned if order == 'desc' and timestamp is not in
  the log-filter argument.

  Args:
    args: An argparse namespace object.

  Returns:
    A list of strings that are clauses in a Cloud Logging filter expression.
  r   	timestamp)secondsztimestamp>="%s")orderr   datetime	timedelta	freshnessutcnowr   FormatTimestamp)argsr.   last_timestamps      r   MakeTimestampFiltersr3   B   sm     jjF??k@""4>>:I&&--/);N 4 4^ DDEEIr   c                 0    d|z   dz   j                  |       S )aL  Join the clauses with the operator.

  This function surrounds each clause with a set of parentheses and joins the
  clauses with the operator.

  Args:
    clauses: List of strings. Each string is a clause in the filter.
    operator: Logical operator used to join the clauses

  Returns:
    The clauses joined by the operator.
   )join)clausesoperators     r   JoinFiltersr9   [   s     .3
	$	$W	--r   )AND)__doc__
__future__r   r   r   r   r,   googlecloudsdk.api_lib.loggingr   googlecloudsdk.callioper   r	   r   r   r   r'   r3   r9    r   r   <module>r@      sE    2 &  % '  / / (2 K
B
,2.r   