
    \                     x    S 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	r	Sr
SS jrS	 rSS
 jrS rS rS rg)zJHelper functions for the ml-engine  client to use command_lib.logs.stream.    N)encoding)jobs)
properties)	resourcesz]value(severity,timestamp.date("%Y-%m-%d %H:%M:%S %z",tz="LOCAL"), task_name,trial_id,message)c                 z    SSR                  U 5      /nU(       a   UR                  SR                  U5      5        U$ )zReturns filters for log fetcher to use.

Args:
  job_id: String id of job.
  task_name: String name of task.

Returns:
  A list of filters to be passed to the logging API.
z7(resource.type="ml_job" OR resource.type="cloudml_job")zresource.labels.job_id="{0}"z;(resource.labels.task_name="{0}" OR labels.task_name="{0}"))formatappend)job_id	task_namefilterss      5lib/googlecloudsdk/command_lib/ml_engine/log_utils.py
LogFiltersr   '   sF     @$++F3' NNELL	 
.    c                    ^^ [         R                  " 5       m[        R                  R                  R
                  R                  SS9n[        R                  R                  SXS9mUU4S jnU$ )zReturns a function to decide if log fetcher should continue polling.

Args:
  job_id: String id of job.

Returns:
  A one-argument function decides if log fetcher should continue.
T)requiredzml.projects.jobs)jobsId
projectsIdc                 L   > U S::  a  gTR                  T5      R                  SL $ )aj  Returns whether to continue polling the logs.

Returns False only once we've checked the job and it is finished; we only
check whether the job is finished once we've gone >1 interval without
getting any new logs.

Args:
  periods_without_logs: integer number of empty polls.

Returns:
  True if we haven't tried polling more than once or if job is not finished.
   TN)GetendTime)periods_without_logsjob_refjobs_clients    r   ShouldContinue,MakeContinueFunction.<locals>.ShouldContinueI   s*     q ??7#++t33r   )
r   
JobsClientr   VALUEScoreprojectr   r   REGISTRYCreate)r
   
project_idr   r   r   s      @@r   MakeContinueFunctionr$   <   sd     !+  %%--1141@*%% & @'4  
r   c              #      #    U  H_  n[        U5      nU(       a  Uv   M  US   R                  5       nU(       d  S/nU H!  n[        R                  " U5      nXVS'   Uv   M#     Ma     g7f)zSplits the dict output of logs into multiple lines.

Args:
  log_generator: iterator that returns a an ml log in dict format.
  allow_multiline: Tells us if logs with multiline messages are okay or not.

Yields:
  Single-line ml log dictionaries.
message N)_EntryToDict
splitlinescopydeepcopy)log_generatorallow_multilineloglog_dictmessagesr&   single_line_logs          r   SplitMultiliner2   \   sf      cC Hn)$//1h4'--1%,	"  s   A'A)c                 H   0 nU R                   (       a  U R                   R                  OSUS'   U R                  US'   [        U 5      nUS   US'   SU;   a  US   US'   SUS'   U R                  bG  [        U R                  5      nSU;   a  US   (       a  US==   US   -  ss'   US	 SU;   a  US	 X1S	'   U$ U R                  b-  US==   [        R                  " U R                  5      -  ss'   U$ U R                  b#  [        R                  " U R                  5      US	'   U$ )
z%Converts a log entry to a dictionary.DEFAULTseverity	timestampr   trial_idr'   r&   	levelnamejson)r5   namer6   _GetLabelAttributesjsonPayload_ToDicttextPayloadsix	text_typeprotoPayloadr   MessageToDict)	log_entryoutputlabel_attributes	json_datas       r   r(   r(   t   sE   &/8/A/AI&&++y 	!++&(3(5&##)*5F:&&	--.I I	9	yYy11
I
 i
K
 6N
 
-	 (
9y'<'<== 
- )++I,B,BCF6N	-r   c                    SS0n[        U R                  5      nU R                  (       d  0 O[        U R                  R                  5      nUR                  S5      b	  US   US'   O5UR                  S5      b	  US   US'   OUR                  S5      b  US   US'   UR                  S5      b
  US   US'   U$ UR                  S5      b  US   US'   U$ )z2Reads the label attributes of the given log entry.r   unknown_taskzml.googleapis.com/task_namer7   zml.googleapis.com/trial_id)r=   labelsresourceget)rC   rE   rI   resource_labelss       r   r;   r;      s    !>29##$&'00Bg7!/%1$3K$@[!zz+*$*;$7[!zz/0<$*+H$I[!ZZ
'#)*#5Z  
 zz./;#)*F#GZ 	r   c                 n    U (       d  0 $ [        U [        5      (       a  U $ [        R                  " U 5      $ N)
isinstancedictr   rB   )r&   s    r   r=   r=      s-    	IN!!'**r   rN   )F)__doc__r*   apitools.base.pyr    googlecloudsdk.api_lib.ml_enginer   googlecloudsdk.corer   r   r?   
LOG_FORMATr   r$   r2   r(   r;   r=    r   r   <module>rW      sE    Q  % 1 * ) 

*@0>,+r   