
    z*                     d   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m	Z	m
Z
 ddlmZ dd	lmZ ddlZd
Zd Zd Z ej(                  d       G d de             Zde	eeej0                     f   de	eeej0                     f   defdZd Zdej6                  dej6                  defdZy)zAWrapper for Cloud Run InstanceSplits messages in spec and status.    )absolute_import)division)print_function)unicode_literalsN)ListMappingOptional)instance_split)run_v1_messages-c                 B    | t         k(  rt         S dj                  |       S )Nz{}%)_MISSING_PERCENTformatpercents    5lib/googlecloudsdk/api_lib/run/instance_split_pair.py_FormatPercentager   !   s       <<      c                 &    t        d | D              S )z&Sums the percents of the given splits.c              3   N   K   | ]  }|j                   s|j                     y w)Nr   ).0is     r   	<genexpr>z_SumPercent.<locals>.<genexpr>*   s     41!))QYYs   %%)sum)splitss    r   _SumPercentr   (   s    	44	44r   T)frozenc                       e Zd ZU dZeej                     ed<   eej                     ed<   eed<   e	ed<   dZ
ee   ed<   ed        Zed	        Zed
        Zed        Zed        Zed        Zed        Zy)InstanceSplitPaira  Holder for InstanceSplit status information.

  The representation of the status of instance splits for a worker pool
  includes:
    o User requested assignments (spec.instanceSplits)
    o Actual assignments (status.instanceSplits)

  Each of spec.instanceSplits and status.instanceSplits may contain multiple
  instance splits that reference the same revision, either directly by name or
  indirectly by referencing the latest ready revision.

  The spec and status instance splits for a revision may differ after a failed
  split update or during a successful one. A InstanceSplitPair holds all
  spec and status InstanceSplits that reference the same revision by name or
  reference the latest ready revision. Both the spec and status instance splits
  can be empty.

  The latest revision can be included in the spec instance splits
  two ways
    o by revisionName
    o by setting latestRevision to True.

  Managed cloud run provides a single combined status instance split
  for both spec entries with:
    o revisionName set to the latest revision's name
    o percent set to combined percentage for both spec entries
    o latestRevision not set

  In this case both spec targets are paired with the combined status
  target and a status_percent_override value is used to allocate the
  combined instance split.

  Attributes:
    spec_splits: The spec instance splits for the referenced revision.
    status_splits: The status instance splits for the referenced revision.
    revision_name: The name of the referenced revision.
    latest: Boolean indicating if the instance splits reference the latest ready
      revision.
    status_percent_override: The percent of splits allocated to the referenced
      revision in the worker pool's status.
  spec_splitsstatus_splitsrevision_namelatestNstatus_percent_overridec                 d    | j                   rt        j                  S t        j                  |       S )z,Returns the key for the instance split pair.)latestRevisionr
   LATEST_REVISION_KEYGetKeyselfs    r   keyzInstanceSplitPair.keyi   s2    
  	** ""4(r   c                     | j                   S )zBReturns true if the instance splits reference the latest revision.)r#   r)   s    r   r&   z InstanceSplitPair.latestRevisionr   s     ;;r   c                     | j                   S )z,Returns the name of the referenced revision.)r"   r)   s    r   revisionNamezInstanceSplitPair.revisionNamew   s     r   c                 v    | j                   r(t        j                  t        | j                               S t        S )z7Returns the sum of the spec instance split percentages.)r    six	text_typer   r   r)   s    r   specPercentzInstanceSplitPair.specPercent|   s-     ]];t'7'7899r   c                     | j                   t        j                  | j                         S | j                  r(t        j                  t	        | j                              S t
        S )z9Returns the sum of the status instance split percentages.)r$   r0   r1   r!   r   r   r)   s    r   statusPercentzInstanceSplitPair.statusPercent   sN     ##/]]47788			]];t'9'9:;;r   c                     | j                   | j                  k(  rt        | j                         S dj                  t        | j                        t        | j                               S )z(Returns human readable revision percent.z{:4} (currently {}))r4   r2   r   r   r)   s    r   displayPercentz InstanceSplitPair.displayPercent   sX     T---t1122"))
D,,
-
D..
/ r   c                 |    | j                   r%t        j                  |       d| j                  dS | j                  S )z+Returns human readable revision identifier.z (currently ))r&   r
   r(   r.   r)   s    r   displayRevisionIdz#InstanceSplitPair.displayRevisionId   s<     



%


 
 r   )__name__
__module____qualname____doc__r   messagesInstanceSplit__annotations__strboolr$   r	   intpropertyr+   r&   r.   r2   r4   r6   r9    r   r   r   r   -   s    (h H**++h,,--
,+/8C=/             r   r   	spec_dictstatus_dictlatest_ready_revision_namec                     d}t         j                  | v rBt         j                  |vr0||v r,||   }||t         j                  <   || v rt        |      }|S ||= |S )a:  Splits the fully-managed latest status target.

  For managed the status target for the latest revision is
  included by revisionName only and may hold the combined splits
  percent for both latestRevisionName and latestRevision spec splits.
  Here we adjust keys in status_dict to match with spec_dict.

  Args:
    spec_dict: Dictionary mapping revision name or 'LATEST' to the spec instance
      split referencing that revision.
    status_dict: Dictionary mapping revision name or 'LATEST' to the status
      instance split referencing that revision. Modified by this function.
    latest_ready_revision_name: The name of the latest ready revision.

  Returns:
    Optionally, the id of the list of status splits containing the combined
    instance splits referencing the latest ready revision by name and by latest.
  N)r
   r'   id)rF   rG   rH   combined_status_splits_idlatest_status_splitss        r   _SplitManagedLatestStatusSplitsrM      sw    . #((I5

,
,K
?
$
3&'AB6JK223!Y."$%9": 
#" 0
1	""r   c                     d}t        |      |k(  rOt        |t        j                           }t        |      }t	        ||      }| t        j                  k(  r|}|S ||z
  }|S )zFComputes the optional override percent to apply to the status percent.N)rJ   r   r
   r'   min)r+   rF   r!   rK   percent_overridespec_by_latest_percentstatus_percentstatus_by_latest_percents           r   _PercentOverriderT      sy    33(.445 !/N"#9>J
n0001 
 (*BB	r   
spec_splitstatus_splitc           
         t        |       }t        |      }t        |||      }g }t        |      j                  |      D ]n  }|j	                  |g       }|j	                  |g       }	t        |||	|      }
|t        j                  k(  r|}d}n|}d}|j                  t        ||	|||
             p t        |t        j                        S )av  Returns a list of InstanceSplitPairs for a WorkerPool.

  Given the spec and status instance splits wrapped in a InstanceSplits instance
  for a sevice, this function pairs up all spec and status instance splits that
  reference the same revision (either by name or the latest ready revision) into
  InstanceSplitPairs. This allows the caller to easily see any differences
  between the spec and status split.

  For fully-managed Cloud Run, the status target for the latest revision is
  included by revisionName only and may hold the combined split
  percent for both latestRevisionName and latestRevision spec targets. This
  function splits the fully-managed status target for the latest revision into
  a target for the percent allocated to the latest revision by name and a target
  for the percent allocated to the latest revision because it is latest.

  Args:
    spec_split: A instance_split.InstanceSplits instance wrapping the spec
      split.
    status_split: A instance_split.InstanceSplits instance wrapping the status
      split.
    latest_ready_revision_name: The name of the worker pool's latest ready
      revision.

  Returns:
    A list of InstanceSplitPairs representing the current state of the worker
    pool's
    instance split assignments. The InstanceSplitPairs are sorted by revision
    name,
    with targets referencing the latest ready revision at the end.
  TF)r+   )dictrM   setuniongetrT   r
   r'   appendr   sortedSortKeyFromSplit)rU   rV   rH   rF   rG   rK   resultkr    r!   rP   r"   r#   s                r   GetInstanceSplitPairsra      s    P :)\"+=8 &y>,a--2&KOOAr*M'	9m%> 	N...0mfmf
MMv?O	
 -$ 
N;;	<<r   )r=   
__future__r   r   r   r   dataclassestypingr   r   r	   googlecloudsdk.api_lib.runr
   ,googlecloudsdk.generated_clients.apis.run.v1r   r>   r0   r   r   r   	dataclassobjectr   rA   r?   rM   rT   InstanceSplitsra   rE   r   r   <module>rj      s    H &  % '  * * 5 T 
  !5
 d#t t $tn##sD!7!7889##d8#9#9::;## !$##L A=--A= //A= !$A=r   