
    D                         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  G d dej"                        Zy)z8Implementation of gcloud dataflow metrics list command.
    )absolute_import)division)unicode_literalsN)apis)
exceptions)arg_parsers)base)dataflow_util)	job_utils)timesc                   R    e Zd ZdZdZdZed        Zd Zd Z	d Z
d Zd	 Zd
 Zd Zy)Lista  Retrieves the metrics from a specific job.

  This command can be used to explore the job's metrics at a fine-grained level.

  ## EXAMPLES

  Filter metrics with the given name:

    $ {command} JOB --filter="name=ElementCount"

  Filter child metrics with matching transforms:

    $ {command} JOB --transform=WordCount

  Filter child output metrics:

    $ {command} JOB --transform=WordCount/Write.*out

  Filter all output metrics:

    $ {command} JOB --transform=.*out

  Filter all custom-defined user metrics

    $ {command} JOB --source=user

  Filter metrics with a scalar value greater than a threshold.

    $ {command} JOB --filter="scalar > 50"

  List metrics that have changed in the last 2 weeks:

    $ {command} JOB --changed-after=-P2W
  userservicec                    t        j                  |        t        j                  j	                  |        t        j
                  j	                  |        t        j                  j	                  |        | j                  dt        j                  j                  d       | j                  dddd       | j                  d	d
       | j                  ddddddd       | j                  dddd       y)z Register flags for this command.z--changed-afterzOnly display metrics that have changed after the given time. See $ gcloud topic datetimes for information on time formats. For example, `2018-01-01` is the first day of the year, and `-P2W` is 2 weeks ago.)typehelpz--hide-committedF
store_truezIf true, hide committed values.)defaultactionr   z--transformz;Filters only the metrics that prefix match the given regex.)r   z--sourcezRetrieves all metrics.z(Retrieves only dataflow service metrics.z#Retrieves only custom user metrics.)allr   r   r   zSet the metrics source.)choicesr   r   z--tentativez"If true, display tentative values.N)r   ArgsForJobRefr	   PAGE_SIZE_FLAGRemoveFromParserSORT_BY_FLAGURI_FLAGadd_argumentr   DatetimeParse)parsers    $lib/surface/dataflow/metrics/list.pyArgsz	List.ArgsH   s    F#((0&&v.MM""6*
!!'''  ) .	  0
 J  L +A9

 &  ( 1	  3    c                 Z    t        j                        }j                  xr t        j                  j                        }g }j
                  s!j                  rt        j                  d      j
                  s!j                  s|j                   fd       n,j
                  r j                  r|j                   fd       |j                   fd       |j                   fd       j                  r|j                  fd       t        j                  j                  |j                  |j                  |j                  |      }|j                   D cg c]2  }t#        |D cg c]
  } ||       c}      r j%                  |      4 c}}S c c}w c c}}w )a)  This is what gets called when the user runs this command.

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

    Returns:
      List of metric values.

    Raises:
      exceptions.InvalidExclusionException: If the excluded metrics are not
        valid.
    z4Cannot exclude both tentative and committed metrics.c                 .    j                  | d      dk7  S N	tentativetrue_GetContextValuemselfs    r"   <lambda>zList.Run.<locals>.<lambda>       T221kBfLr$   c                 .    j                  | d      dk(  S r'   r*   r,   s    r"   r/   zList.Run.<locals>.<lambda>   r0   r$   c                 <    j                  | j                        S N)_FilterBySourcesourcer-   argsr.   s    r"   r/   zList.Run.<locals>.<lambda>   s    4//4;;?r$   c                 <    j                  | j                        S r3   )_FilterByTransform	transformr6   s    r"   r/   zList.Run.<locals>.<lambda>   s    4221dnnEr$   c                 \    t        j                  | j                        j                  kD  S r3   )r   ParseDateTime
updateTimechanged_after)r-   r7   s    r"   r/   zList.Run.<locals>.<lambda>   s    E''58J8JJr$   )
project_id	region_id
start_time)r   ExtractJobRefr>   r   FormatDateTimer(   hide_committedr   InvalidExclusionExceptionappendr   MetricsGetjobId	projectIdlocationmetricsr   _Format)r.   r7   job_refrA   predsresponser-   preds   ``      r"   RunzList.Runo   sW    %%d+G##P(<(<T=O=O(PJE>>d1100
@B B^^D$7$7llLM	D//llLM	LL?@	LLEFll
JL ||$$""	   H &.%5%5 1%5.DG./ LLO%5 1 1.1s   'F'5F"F'"F'c                 :   t         j                  j                  |j                  j                        syt         j
                  j                  |j                  j                        sy|j                  j                  dk(  xs |j                  j                  dk(  S )zThis returns true if the metric is a watermark with a sentinel value.

    Args:
      metric: A single UpdateMetric returned from the API.
    Returns:
      True if the metric is a sentinel value, false otherwise.
    F)r
   DATAFLOW_METRICS_REmatchnameoriginWINDMILL_WATERMARK_REscalarinteger_valuer.   metrics     r"   _IsSentinelWatermarkzList._IsSentinelWatermark   st     ,,226;;3E3EF..44V[[5E5EFMM''2- .MM''2-/r$   c                 <    |j                   j                  }|dk(  ryy)a  This method gets the description of the watermark sentinel value.

    There are only two watermark sentinel values we care about, -1 represents a
    watermark at kInt64Min. -2 represents a watermark at kInt64Max. This runs
    on the assumption that _IsSentinelWatermark was called first.

    Args:
      metric: A single UpdateMetric returned from the API.
    Returns:
      The sentinel description.
    rT   zUnknown watermarkzMax watermark)r[   r\   )r.   r^   values      r"    _GetWatermarkSentinelDescriptionz%List._GetWatermarkSentinelDescription   s      MM''E{ r$   c                     | j                  |      r;| j                  |      |j                  _        |j                  j	                  d       |S )zPerforms extra formatting for sentinel values or otherwise.

    Args:
      metric: A single UpdateMetric returned from the API.
    Returns:
      The formatted metric.
    r\   )r_   rb   r[   string_valueresetr]   s     r"   rM   zList._Format   s@       (#'#H#H#Pfmm mm/*Mr$   c                     | j                  |d      xs d}| j                  |d      xs d}t        j                  |xs d      }|j                  |      s|j                  |      ryy)Noutput_user_name stepTF)r+   recompilerW   )r.   r^   r:   rg   ri   s        r"   r9   zList._FilterByTransform   sb    ,,V5GHNB  06BD

9?+I'(IOOD,Ar$   c                     || j                   k(  r|j                  j                  dk(  S || j                  k(  r|j                  j                  dk(  S y)Nr   zdataflow/v1b3T)USER_SOURCErX   rY   SERVICE_SOURCE)r.   r^   r5   s      r"   r4   zList._FilterBySource   sK    !!![[6))	4&&	&[[?22r$   c                     |j                   j                  rB|j                   j                  j                  D ]  }|j                  |k(  s|j                  c S  y r3   )rX   contextadditionalPropertieskeyra   )r.   r^   rr   props       r"   r+   zList._GetContextValue   sC    {{++%%::$88s?
 ; r$   N)__name__
__module____qualname____doc__rm   rn   staticmethodr#   rR   r_   rb   rM   r9   r4   r+    r$   r"   r   r   !   sK    !F +.$3 $3L(1T/$"r$   r   )rw   
__future__r   r   r   rj   googlecloudsdk.api_lib.dataflowr   r   googlecloudsdk.callioper   r	   #googlecloudsdk.command_lib.dataflowr
   r   googlecloudsdk.core.utilr   ListCommandr   ry   r$   r"   <module>r      sA    '  ' 	 0 6 / ( = 9 *|4 |r$   