o
    -wi(                     @  sb  U d Z ddlmZ ddlmZmZmZmZmZ ddl	m
Z
 ddlmZmZmZ ddlmZmZmZmZmZmZ ddlmZmZmZ dd	lmZ dd
lmZmZmZ ddl m!Z! ddl"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z( ddl)m*Z*m+Z+m,Z, er{ddl-m.Z. G dd de#Z/G dd deZ0G dd deZ1G dd deZ2G dd de!Z3G dd deZ4G dd de4Z5G dd de5Z6G d d! d!e5Z7G d"d# d#e5Z8G d$d% d%e4Z9G d&d' d'e9Z:eee6e7e8e:f e
d(d)f Z;ee;j<Z=d*e>d+< G d,d- d-Z?G d.d/ d/Z@eA  e2A  e0A  e6A  e7A  e8A  e:A  dgd0d1 e=D d-d/d2d3ZBd4S )5z$Events that trigger W&B Automations.    )annotations)TYPE_CHECKINGAnyLiteralOptionalUnion)Field)	AnnotatedSelfget_args)GQLBaseSerializedToJsonensure_jsonfield_validatormodel_validatorpydantic_isinstance   )AndMongoLikeFilterOr)FilterableField)MetricChangeFilterMetricThresholdFilter	MetricVal)FilterEventFields)LenientStrEnumsimplify_op)InputActionInputActionTypesSavedActionTypes)ArtifactCollectionScopeAutomationScopeProjectScopeNewAutomationc                   @  s(   e Zd ZdZdZdZdZdZdZdZ	dS )		EventTypez.The type of event that triggers an automation.UPDATE_ARTIFACT_ALIASCREATE_ARTIFACTADD_ARTIFACT_ALIAS
LINK_MODEL
RUN_METRICRUN_METRIC_CHANGEN)
__name__
__module____qualname____doc__r&   r'   r(   LINK_ARTIFACTRUN_METRIC_THRESHOLDr+    r2   r2   U/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/automations/events.pyr%       s    r%   c                   @  s   e Zd ZU e Zded< dS )_WrappedSavedEventFilter!SerializedToJson[MongoLikeFilter]filterN)r,   r-   r.   r   r6   __annotations__r2   r2   r2   r3   r4   8   s   
 r4   c                   @  s`   e Zd ZU dZded< dZded< eddedddZedddddZ	e
dddZdS )_WrappedMetricFilterNzOptional[MetricThresholdFilter]threshold_filterzOptional[MetricChangeFilter]change_filterbeforemodevr   returnc                 C  s,   t |tr
| |dS t |tr| |dS |S )N)r9   )r:   r   r   r   clsr>   r2   r2   r3   _wrap_metric_filter@   s
   



z(_WrappedMetricFilter._wrap_metric_filterafterr
   c                 C  sd   dd | D }|sd ttt| j}td| t|dkr0d tt|}td| | S )Nc                 S  s   g | ]
\}}|d ur|qS Nr2   ).0namevalr2   r2   r3   
<listcomp>K   s    z@_WrappedMetricFilter._ensure_exactly_one_set.<locals>.<listcomp>z, zExpected one of: r   z)Expected exactly one metric filter, got: )joinmapreprtypemodel_fields
ValueErrorlen)self
set_fields	all_names	set_namesr2   r2   r3   _ensure_exactly_one_setI   s   z,_WrappedMetricFilter._ensure_exactly_one_setr%   c                 C  s(   | j d urtjS | jd urtjS td)Nz6Expected one of: `threshold_filter` or `change_filter`)r9   r%   r1   r:   r+   RuntimeError)rQ   r2   r2   r3   
event_typeW   s
   

z_WrappedMetricFilter.event_typer>   r   r?   r   )r?   r
   )r?   r%   )r,   r-   r.   r9   r7   r:   r   classmethodrC   rU   propertyrW   r2   r2   r2   r3   r8   <   s   
 r8   c                   @  s`   e Zd ZU e Zded< ded< dZded< 	 edd	edddZ	e
ddd	dddZdS )RunMetricFilterzGAnnotated[SerializedToJson[MongoLikeFilter], Field(alias='run_filter')]runzAAnnotated[_WrappedMetricFilter, Field(alias='run_metric_filter')]metricNzkAnnotated[Optional[SerializedToJson[MetricThresholdFilter]], Field(alias='metric_filter', deprecated=True)]legacy_metric_filterr;   r<   r>   r   r?   c                 C  s   t |ttfr| |dS |S )N)r]   r@   rA   r2   r2   r3   rC   n   s   
z#RunMetricFilter._wrap_metric_filterrD   r   c                 C  s"   t |}t|tr|S t|gdS )Nand_)r   r   r   rB   r>   v_newr2   r2   r3   _wrap_run_filtery   s   z RunMetricFilter._wrap_run_filterrX   )r>   r   r?   r   )r,   r-   r.   r   r\   r7   r^   r   rY   rC   r   rc   r2   r2   r2   r3   r[   `   s   
 

	r[   c                   @  s"   e Zd ZU dZded< ded< dS )
SavedEventz+A triggering event from a saved automation.z(Annotated[EventType, Field(frozen=True)]rW   zBSerializedToJson[Union[_WrappedSavedEventFilter, RunMetricFilter]]r6   N)r,   r-   r.   r/   r7   r2   r2   r2   r3   rd      s
   
 rd   c                   @  s<   e Zd ZU ded< ded< 	 ded< dddZdddZdS )_BaseEventInputr%   rW   r!   scopezSerializedToJson[Any]r6   actionr   r?   r$   c                 C  s:   ddl m} t|ttfr|| |dS tdt|j)zFDefine a new Automation in which this event triggers the given action.r   r#   )eventrg   zExpected a valid action, got: )automationsr$   
isinstancer   r   	TypeErrorrM   r.   )rQ   rg   r$   r2   r2   r3   then   s   z_BaseEventInput.thenotherc                 C  s
   |  |S )zPImplements `event >> action` to define an Automation with this event and action.)rl   )rQ   rm   r2   r2   r3   
__rshift__   s   
z_BaseEventInput.__rshift__N)rg   r   r?   r$   )rm   r   r?   r$   )r,   r-   r.   r7   rl   rn   r2   r2   r2   r3   re      s   
 
	re   c                   @  s4   e Zd ZU e Zded< 	 edddddd	Zd
S )_BaseMutationEventInputr5   r6   rD   r<   r>   r   r?   c                 C  s.   t |}t|tr|nt|gd}t|gdS )zEnsure the given filter is wrapped like: `{"$or": [{"$and": [<original_filter>]}]}`.

        This is awkward but necessary, because the frontend expects this format.
        r_   )or_)r   r   r   r   ra   r2   r2   r3   _wrap_filter   s   z$_BaseMutationEventInput._wrap_filterNrX   )r,   r-   r.   r   r6   r7   r   rq   r2   r2   r2   r3   ro      s
   
 
ro   c                   @      e Zd ZU dZejZded< dS )OnLinkArtifactz)A new artifact is linked to a collection.z Literal[EventType.LINK_ARTIFACT]rW   N)r,   r-   r.   r/   r%   r0   rW   r7   r2   r2   r2   r3   rs         
 rs   c                   @  rr   )OnAddArtifactAliasz'A new alias is assigned to an artifact.z%Literal[EventType.ADD_ARTIFACT_ALIAS]rW   N)r,   r-   r.   r/   r%   r(   rW   r7   r2   r2   r2   r3   ru      rt   ru   c                   @  s(   e Zd ZU dZejZded< ded< dS )OnCreateArtifactzA new artifact is created.z"Literal[EventType.CREATE_ARTIFACT]rW   r    rf   N)r,   r-   r.   r/   r%   r'   rW   r7   r2   r2   r2   r3   rv      s
   
 rv   c                   @  s   e Zd ZU ded< dS )_BaseRunEventInputr"   rf   N)r,   r-   r.   r7   r2   r2   r2   r3   rw      s   
 rw   c                   @  s<   e Zd ZU dZded< ded< 	 eddedddZdS )OnRunMetricz0A run metric satisfies a user-defined condition.zDLiteral[EventType.RUN_METRIC_THRESHOLD, EventType.RUN_METRIC_CHANGE]rW   z!SerializedToJson[RunMetricFilter]r6   r;   r<   datar   r?   c                 C  s>   t |tr|d }rtt|}i |d|jjiS |S )a  Infer the event type at validation time from the inner filter.

        This allows this class to accommodate both "threshold" and "change" metric
        filter types, which are can only be determined after parsing and validating
        the inner JSON data.
        r6   rW   )rj   dictgetr[   model_validate_jsonr   r]   rW   )rB   ry   
raw_filterparsed_filterr2   r2   r3   _infer_event_type   s   	zOnRunMetric._infer_event_typeN)ry   r   r?   r   )r,   r-   r.   r/   r7   r   rY   r   r2   r2   r2   r3   rx      s   
 rx   rW   )discriminatorztuple[type, ...]InputEventTypesc                   @  s$   e Zd ZeddZed
ddZd	S )RunEventdisplay_name)server_namerG   strr?   r   c                 C  s
   t | dS )z!Define a metric filter condition.rG   )r   r   r2   r2   r3   r]     s   
zRunEvent.metricN)rG   r   r?   r   )r,   r-   r.   r   rG   staticmethodr]   r2   r2   r2   r3   r      s    
r   c                   @  s   e Zd Ze ZdS )ArtifactEventN)r,   r-   r.   r   aliasr2   r2   r2   r3   r   	  s    
r   c                 c  s    | ]}|j V  qd S rE   )r,   )rF   rB   r2   r2   r3   	<genexpr>  s    r   r   r   N)Cr/   
__future__r   typingr   r   r   r   r   pydanticr   typing_extensionsr	   r
   r   wandb._pydanticr   r   r   r   r   r   _filtersr   r   r   _filters.expressionsr   _filters.run_metricsr   r   r   
_generatedr   _validatorsr   r   actionsr   r   r   scopesr    r!   r"   ri   r$   r%   r4   r8   r[   rd   re   ro   rs   ru   rv   rw   rx   
InputEvent
__origin__r   r7   r   r   model_rebuild__all__r2   r2   r2   r3   <module>   sx     	$
