
    D                         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\R"                  5      rg)z8Implementation of gcloud dataflow metrics list command.
    )absolute_import)division)unicode_literalsN)apis)
exceptions)arg_parsers)base)dataflow_util)	job_utils)timesc                   Z    \ rS rSrSrSrSr\S 5       rS r	S r
S rS	 rS
 rS rS rSrg)List!   a  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                    [         R                  " U 5        [        R                  R	                  U 5        [        R
                  R	                  U 5        [        R                  R	                  U 5        U R                  S[        R                  R                  SS9  U R                  SSSSS9  U R                  S	S
S9  U R                  SSSSS.SSS9  U R                  SSSSS9  g)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Args	List.ArgsH   s    F#((0&&v.MM""6*
!!'''  ) .	  0
 J  L +A9

 &  ( 1	  3    c                   ^ ^ [         R                  " T5      nTR                  =(       a     [        R                  " TR                  5      n/ nTR
                  (       d'  TR                  (       a  [        R                  " S5      eTR
                  (       d'  TR                  (       d  UR                  U 4S j5        O7TR
                  (       a&  TR                  (       a  UR                  U 4S j5        UR                  UU 4S j5        UR                  UU 4S j5        TR                  (       a  UR                  U4S j5        [        R                  R                  UR                  UR                  UR                  US9nUR                    VVs/ s H<  n[#        U Vs/ s H
  ow" U5      PM     sn5      (       d  M+  T R%                  U5      PM>     snn$ s  snf s  snnf )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                 .   > TR                  U S5      S:g  $ N	tentativetrue_GetContextValuemselfs    r#   <lambda>List.Run.<locals>.<lambda>       T221kBfLr&   c                 .   > TR                  U S5      S:H  $ r)   r,   r.   s    r#   r1   r2      r3   r&   c                 <   > TR                  U TR                  5      $ N)_FilterBySourcesourcer/   argsr0   s    r#   r1   r2      s    4//4;;?r&   c                 <   > TR                  U TR                  5      $ r6   )_FilterByTransform	transformr9   s    r#   r1   r2      s    4221dnnEr&   c                 ^   > [         R                  " U R                  5      TR                  :  $ r6   )r   ParseDateTime
updateTimechanged_after)r/   r:   s    r#   r1   r2      s    E''58J8JJr&   )
project_id	region_id
start_time)r   ExtractJobRefrA   r   FormatDateTimer*   hide_committedr   InvalidExclusionExceptionappendr   MetricsGetjobId	projectIdlocationmetricsr   _Format)r0   r:   job_refrD   predsresponser/   preds   ``      r#   RunList.Runo   sY    %%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./ DLLO%5 1 1.1s   G #G4
G G G c                 X   [         R                  R                  UR                  R                  5      (       d  g[         R
                  R                  UR                  R                  5      (       d  gUR                  R                  S:H  =(       d    UR                  R                  S:H  $ )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r0   metrics     r#   _IsSentinelWatermarkList._IsSentinelWatermark   sz     ,,226;;3E3EFF..44V[[5E5EFFMM''2- .MM''2-/r&   c                 >    UR                   R                  nUS:X  a  gg)az  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.
rX   zUnknown watermarkzMax watermark)r_   r`   )r0   rb   values      r#    _GetWatermarkSentinelDescription%List._GetWatermarkSentinelDescription   s      MM''E{ r&   c                     U R                  U5      (       a;  U R                  U5      UR                  l        UR                  R	                  S5        U$ )zPerforms extra formatting for sentinel values or otherwise.

Args:
  metric: A single UpdateMetric returned from the API.
Returns:
  The formatted metric.
r`   )rc   rg   r_   string_valueresetra   s     r#   rP   List._Format   sC       ((#'#H#H#Pfmm mm/*Mr&   c                    U R                  US5      =(       d    SnU R                  US5      =(       d    Sn[        R                  " U=(       d    S5      nUR                  U5      (       d  UR                  U5      (       a  gg)Noutput_user_name stepTF)r-   recompiler[   )r0   rb   r=   rn   rp   s        r#   r<   List._FilterByTransform   sh    ,,V5GHNB  06BD

9?+I'((IOOD,A,Ar&   c                     X R                   :X  a  UR                  R                  S:H  $ X R                  :X  a  UR                  R                  S:H  $ g)Nr   zdataflow/v1b3T)USER_SOURCEr\   r]   SERVICE_SOURCE)r0   rb   r8   s      r#   r7   List._FilterBySource   sG    !!![[6))	&&	&[[?22r&   c                     UR                   R                  (       aE  UR                   R                  R                   H!  nUR                  U:X  d  M  UR                  s  $    g r6   )r\   contextadditionalPropertieskeyrf   )r0   rb   r{   props       r#   r-   List._GetContextValue   sC    {{++%%::$88s?
 ; r&    N)__name__
__module____qualname____firstlineno____doc__ru   rv   staticmethodr$   rU   rc   rg   rP   r<   r7   r-   __static_attributes__r~   r&   r#   r   r   !   sK    !F +.$3 $3L(1T/$"r&   r   )r   
__future__r   r   r   rq   googlecloudsdk.api_lib.dataflowr   r   googlecloudsdk.callioper   r	   #googlecloudsdk.command_lib.dataflowr
   r   googlecloudsdk.core.utilr   ListCommandr   r~   r&   r#   <module>r      sA    '  ' 	 0 6 / ( = 9 *|4 |r&   