
    z*                     b   S 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SKJrJ	r	J
r
  SSKJr  SS	KJr  SSKrS
rS rS r\R(                  " SS9 " S S\5      5       rS\	\\\R0                     4   S\	\\\R0                     4   S\4S jrS rS\R6                  S\R6                  S\4S jrg)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                 D    U [         :X  a  [         $ SR                  U 5      $ )Nz{}%)_MISSING_PERCENTformatpercents    5lib/googlecloudsdk/api_lib/run/instance_split_pair.py_FormatPercentager   !   s       <<      c                 &    [        S U  5       5      $ )z&Sums the percents of the given splits.c              3   ^   #    U  H#  oR                   (       d  M  UR                   v   M%     g 7f)Nr   ).0is     r   	<genexpr>_SumPercent.<locals>.<genexpr>*   s     41))YQYYs   --)sum)splitss    r   _SumPercentr   (   s    	44	44r   T)frozenc                       \ rS rSr% Sr\\R                     \S'   \\R                     \S'   \	\S'   \
\S'   Sr\\   \S'   \S	 5       r\S
 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rSrg)InstanceSplitPair-   a  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                 p    U R                   (       a  [        R                  $ [        R                  " U 5      $ )z,Returns the key for the instance split pair.)latestRevisionr
   LATEST_REVISION_KEYGetKeyselfs    r   keyInstanceSplitPair.keyi   s2    
  	** ""4(r   c                     U R                   $ )zBReturns true if the instance splits reference the latest revision.)r%   r+   s    r   r(    InstanceSplitPair.latestRevisionr   s     ;;r   c                     U R                   $ )z,Returns the name of the referenced revision.)r$   r+   s    r   revisionNameInstanceSplitPair.revisionNamew   s     r   c                     U R                   (       a)  [        R                  " [        U R                   5      5      $ [        $ )z7Returns the sum of the spec instance split percentages.)r"   six	text_typer   r   r+   s    r   specPercentInstanceSplitPair.specPercent|   s-     ]];t'7'7899r   c                     U R                   b   [        R                  " U R                   5      $ U R                  (       a)  [        R                  " [	        U R                  5      5      $ [
        $ )z9Returns the sum of the status instance split percentages.)r&   r5   r6   r#   r   r   r+   s    r   statusPercentInstanceSplitPair.statusPercent   sN     ##/]]47788			]];t'9'9:;;r   c                     U R                   U R                  :X  a  [        U R                   5      $ SR                  [        U R                  5      [        U R                   5      5      $ )z(Returns human readable revision percent.z{:4} (currently {}))r:   r7   r   r   r+   s    r   displayPercent InstanceSplitPair.displayPercent   sX     T---t1122"))
D,,
-
D..
/ r   c                     U R                   (       a(  [        R                  " U 5      < SU R                  < S3$ U R                  $ )z+Returns human readable revision identifier.z (currently ))r(   r
   r*   r2   r+   s    r   displayRevisionId#InstanceSplitPair.displayRevisionId   s<     



%


 
 r    )__name__
__module____qualname____firstlineno____doc__r   messagesInstanceSplit__annotations__strboolr&   r	   intpropertyr-   r(   r2   r7   r:   r=   rA   __static_attributes__rC   r   r   r    r    -   s    (h H**++h,,--
,+/8C=/             r   r    	spec_dictstatus_dictlatest_ready_revision_namec                     Sn[         R                  U ;   aC  [         R                  U;  a/  X!;   a*  X   nXA[         R                  '   X ;   a  [        U5      nU$ X	 U$ )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)rQ   rR   rS   combined_status_splits_idlatest_status_splitss        r   _SplitManagedLatestStatusSplitsrX      sk    . #((I5

,
,K
?
$
3&B6J223!."$%9": 
#" 
1	""r   c                     Sn[        U5      U:X  aN  [        U[        R                     5      n[        U5      n[	        XV5      nU [        R                  :X  a  UnU$ Xg-
  nU$ )zFComputes the optional override percent to apply to the status percent.N)rU   r   r
   r)   min)r-   rQ   r#   rV   percent_overridespec_by_latest_percentstatus_percentstatus_by_latest_percents           r   _PercentOverrider_      st    33(.445 !/N"#9J
n0001 
 (B	r   
spec_splitstatus_splitc           
         [        U 5      n[        U5      n[        X4U5      n/ n[        U5      R                  U5       Hl  nUR	                  U/ 5      nUR	                  U/ 5      n	[        XsX5      n
U[        R                  :X  a  UnSnOUnSnUR                  [        XXU
5      5        Mn     [        U[        R                  S9$ )aD  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-   )dictrX   setuniongetr_   r
   r)   appendr    sortedSortKeyFromSplit)r`   ra   rS   rQ   rR   rV   resultkr"   r#   r[   r$   r%   s                r   GetInstanceSplitPairsrl      s    P :)\"+=8 &y>,a--2&KOOAr*M'	m 	N...0mfmf
MM?O	
 -$ 
N;;	<<r   )rH   
__future__r   r   r   r   dataclassestypingr   r   r	   googlecloudsdk.api_lib.runr
   ,googlecloudsdk.generated_clients.apis.run.v1r   rI   r5   r   r   r   	dataclassobjectr    rL   rJ   rX   r_   InstanceSplitsrl   rC   r   r   <module>ru      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   