o
    wid6                     @  s  U d Z ddlmZ ddlmZmZmZmZmZm	Z	 ddl
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 ddlmZmZmZmZ ddlm Z m!Z! ddl"m#Z# ddl$m%Z%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1 erddl2m3Z3 G dd de&Z4G dd deZ5G dd deZ6G dd deZ7G dd deZ8G dd deZ9G dd deZ:G d d! d!e#Z;G d"d# d#eZ<G d$d% d%e<Z=G d&d' d'e=Z>G d(d) d)e=Z?G d*d+ d+e=Z@G d,d- d-e<ZAG d.d/ d/eAZBG d0d1 d1eAZCee	e>e?e@eBeCf ed2d3f ZDeeDjEZFd4eGd5< G d6d7 d7ZHG d8d9 d9ZIeJ  e9J  e5J  e>J  e?J  e@J  eBJ  dgd:d; eFD d7d9d<d=d>ZKd?S )@z$Events that trigger W&B Automations.    )annotations)TYPE_CHECKING	AnnotatedAnyLiteralOptionalUnion)AfterValidatorField)get_args)GQLBasemodel_validatorpydantic_isinstancenameof   )AndMongoLikeFilter)FilterableField)MetricChangeFilterMetricThresholdFilter	MetricValMetricZScoreFilter)StateFilterStateOperand)FilterEventFields)JsonEncodedLenientStrEnumensure_jsonwrap_mutation_event_filterwrap_run_event_run_filter)InputActionInputActionTypesSavedActionTypes)ArtifactCollectionScopeAutomationScopeProjectScopeNewAutomationc                   @  s0   e Zd Zd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_CHANGE	RUN_STATERUN_METRIC_ZSCOREN)__name__
__module____qualname____doc__r*   r+   r,   LINK_ARTIFACTRUN_METRIC_THRESHOLDr/   r0   r1    r8   r8   L/home/ubuntu/.local/lib/python3.10/site-packages/wandb/automations/events.pyr)   &   s    r)   c                   @     e Zd ZU e Zded< dS )_WrappedSavedEventFilterzJsonEncoded[MongoLikeFilter]filterNr2   r3   r4   r   r<   __annotations__r8   r8   r8   r9   r;   A   s   
 r;   c                   @  <   e Zd ZU ejZded< ded< eddedd
dZ	dS )_WrappedMetricThresholdFilterzSAnnotated[Literal[EventType.RUN_METRIC_THRESHOLD], Field(exclude=True, repr=False)]
event_typer   threshold_filterbeforemodevr   returnc                 C     t |tr
| |dS |S )N)rB   )r   r   clsrF   r8   r8   r9   _nest_inner_filterM      

z0_WrappedMetricThresholdFilter._nest_inner_filterNrF   r   rG   r   )
r2   r3   r4   r)   r7   rA   r>   r   classmethodrK   r8   r8   r8   r9   r@   E      
 
r@   c                   @  r?   )_WrappedMetricChangeFilterzPAnnotated[Literal[EventType.RUN_METRIC_CHANGE], Field(exclude=True, repr=False)]rA   r   change_filterrC   rD   rF   r   rG   c                 C  rH   )N)rQ   )r   r   rI   r8   r8   r9   rK   ^   rL   z-_WrappedMetricChangeFilter._nest_inner_filterNrM   )
r2   r3   r4   r)   r/   rA   r>   r   rN   rK   r8   r8   r8   r9   rP   V   rO   rP   c                   @  r?   )_WrappedMetricZScoreFilterzPAnnotated[Literal[EventType.RUN_METRIC_ZSCORE], Field(exclude=True, repr=False)]rA   r   zscore_filterrC   rD   rF   r   rG   c                 C  rH   )N)rS   )r   r   rI   r8   r8   r9   rK   o   s   

z-_WrappedMetricZScoreFilter._nest_inner_filterNrM   )
r2   r3   r4   r)   r1   rA   r>   r   rN   rK   r8   r8   r8   r9   rR   g   rO   rR   c                   @  sN   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	dS )RunMetricFiltermAnnotated[JsonEncoded[MongoLikeFilter], AfterValidator(wrap_run_event_run_filter), Field(alias='run_filter')]runzAnnotated[Union[_WrappedMetricThresholdFilter, _WrappedMetricChangeFilter, _WrappedMetricZScoreFilter], Field(alias='run_metric_filter')]metricNzfAnnotated[Optional[JsonEncoded[MetricThresholdFilter]], Field(alias='metric_filter', deprecated=True)]legacy_metric_filterrC   rD   rF   r   rG   c                 C  s   t |tttfr| |dS |S )N)rW   )r   r   r   r   rI   r8   r8   r9   _nest_metric_filter   s
   

z#RunMetricFilter._nest_metric_filterrM   )
r2   r3   r4   r   rV   r>   rX   r   rN   rY   r8   r8   r8   r9   rT   w   s   
 

rT   c                   @  sD   e Zd ZU dZe Zded< 	 ded< 	 eddedddZ	dS )RunStateFilterzIRepresents a filter for triggering events based on changes in run states.rU   rV   z7Annotated[StateFilter, Field(alias='run_state_filter')]staterC   rD   rF   r   rG   c                 C  rH   )N)r[   )r   r   rI   r8   r8   r9   _nest_state_filter   s   

z!RunStateFilter._nest_state_filterNrM   )
r2   r3   r4   r5   r   rV   r>   r   rN   r\   r8   r8   r8   r9   rZ      s   
 
rZ   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)]rA   zMJsonEncoded[Union[_WrappedSavedEventFilter, RunMetricFilter, RunStateFilter]]r<   N)r2   r3   r4   r5   r>   r8   r8   r8   r9   r]      s
   
 r]   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)   rA   r%   scopezJsonEncoded[Any]r<   actionr!   rG   r(   c                 C  s<   ddl m} t|ttfr|| |dS tdtt|)zFDefine a new Automation in which this event triggers the given action.r   r'   )eventr`   zExpected a valid action, got: )automationsr(   
isinstancer"   r#   	TypeErrorr   type)selfr`   r(   r8   r8   r9   then   s   z_BaseEventInput.thenotherc                 C  s
   |  |S )z4Implement `event >> action` to define an automation.)rg   )rf   rh   r8   r8   r9   
__rshift__   s   
z_BaseEventInput.__rshift__N)r`   r!   rG   r(   )rh   r!   rG   r(   )r2   r3   r4   r>   rg   ri   r8   r8   r8   r9   r^      s   
 
	r^   c                   @  r:   )_BaseMutationEventInputzSAnnotated[JsonEncoded[MongoLikeFilter], AfterValidator(wrap_mutation_event_filter)]r<   Nr=   r8   r8   r8   r9   rj      s   
 
rj   c                   @      e Zd ZU dZejZded< dS )OnLinkArtifacta  A new artifact is linked to a collection.

    Examples:
    Define an event that triggers when an artifact is linked to the
    collection "my-collection" with the alias "prod":

    ```python
    from wandb import Api
    from wandb.automations import OnLinkArtifact, ArtifactEvent

    api = Api()
    collection = api.artifact_collection(name="my-collection", type_name="model")

    event = OnLinkArtifact(
        scope=collection,
        filter=ArtifactEvent.alias.eq("prod"),
    )
    ```
    z Literal[EventType.LINK_ARTIFACT]rA   N)r2   r3   r4   r5   r)   r6   rA   r>   r8   r8   r8   r9   rl         
 rl   c                   @  rk   )OnAddArtifactAliasa  A new alias is assigned to an artifact.

    Examples:
    Define an event that triggers whenever the alias "prod" is assigned to
    any artifact in the collection "my-collection":

    ```python
    from wandb import Api
    from wandb.automations import OnAddArtifactAlias, ArtifactEvent

    api = Api()
    collection = api.artifact_collection(name="my-collection", type_name="model")

    event = OnAddArtifactAlias(
        scope=collection,
        filter=ArtifactEvent.alias.eq("prod"),
    )
    ```
    z%Literal[EventType.ADD_ARTIFACT_ALIAS]rA   N)r2   r3   r4   r5   r)   r,   rA   r>   r8   r8   r8   r9   rn     rm   rn   c                   @  (   e Zd ZU dZejZded< ded< dS )OnCreateArtifacta  A new artifact is created.

    Examples:
    Define an event that triggers when a new artifact is created in the
    collection "my-collection":

    ```python
    from wandb import Api
    from wandb.automations import OnCreateArtifact

    api = Api()
    collection = api.artifact_collection(name="my-collection", type_name="model")

    event = OnCreateArtifact(scope=collection)
    ```
    z"Literal[EventType.CREATE_ARTIFACT]rA   r$   r_   N)r2   r3   r4   r5   r)   r+   rA   r>   r8   r8   r8   r9   rp     s
   
 rp   c                   @  s   e Zd ZU ded< dS )_BaseRunEventInputr&   r_   N)r2   r3   r4   r>   r8   r8   r8   r9   rq   3  s   
 rq   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 )OnRunMetrica  A run metric satisfies a user-defined condition.

    Examples:
    Define an event that triggers for any run in project "my-project" when
    the average of the last 5 values of metric "my-metric" exceeds 123.45:

    ```python
    from wandb import Api
    from wandb.automations import OnRunMetric, RunEvent

    api = Api()
    project = api.project(name="my-project")

    event = OnRunMetric(
        scope=project,
        filter=RunEvent.metric("my-metric").avg(5).gt(123.45),
    )
    ```
    zaLiteral[EventType.RUN_METRIC_THRESHOLD, EventType.RUN_METRIC_CHANGE, EventType.RUN_METRIC_ZSCORE]rA   zJsonEncoded[RunMetricFilter]r<   rC   rD   datar   rG   c                 C  s>   t |tr|d }rtt|}i |d|jjiS |S )zInfer the event type from the inner filter during validation.

        This supports both "threshold" and "change" metric filters, which can
        only be determined after parsing and validating the inner JSON data.
        r<   rA   )rc   dictgetrT   model_validate_jsonr   rW   rA   )rJ   rs   
raw_filterparsed_filterr8   r8   r9   _infer_event_typeV  s   zOnRunMetric._infer_event_typeN)rs   r   rG   r   )r2   r3   r4   r5   r>   r   rN   ry   r8   r8   r8   r9   rr   8  s   
 rr   c                   @  ro   )
OnRunStatea  A run state changes.

    Examples:
    Define an event that triggers for any run in project "my-project" when
    its state changes to "finished" (i.e. succeeded) or "failed":

    ```python
    from wandb import Api
    from wandb.automations import OnRunState

    api = Api()
    project = api.project(name="my-project")

    event = OnRunState(
        scope=project,
        filter=RunEvent.state.in_(["finished", "failed"]),
    )
    ```
    zLiteral[EventType.RUN_STATE]rA   zJsonEncoded[RunStateFilter]r<   N)r2   r3   r4   r5   r)   r0   rA   r>   r8   r8   r8   r9   rz   f  s
   
 rz   rA   )discriminatorztuple[type, ...]InputEventTypesc                   @  s*   e Zd ZeddZe Zed
ddZd	S )RunEventdisplay_name)server_namenamestrrG   r   c                 C  s
   t | dS )z!Define a metric filter condition.r   )r   r   r8   r8   r9   rW     s   
zRunEvent.metricN)r   r   rG   r   )	r2   r3   r4   r   r   r   r[   staticmethodrW   r8   r8   r8   r9   r}     s
    
r}   c                   @  s   e Zd Ze ZdS )ArtifactEventN)r2   r3   r4   r   aliasr8   r8   r8   r9   r     s    
r   c                 c  s    | ]}t |V  qd S )Nr   ).0rJ   r8   r8   r9   	<genexpr>  s    r   r   r   r   N)Lr5   
__future__r   typingr   r   r   r   r   r   pydanticr	   r
   typing_extensionsr   wandb._pydanticr   r   r   wandb._strutilsr   _filtersr   r   _filters.expressionsr   _filters.run_metricsr   r   r   r   _filters.run_statesr   r   
_generatedr   _validatorsr   r   r   r   r    actionsr!   r"   r#   scopesr$   r%   r&   rb   r(   r)   r;   r@   rP   rR   rT   rZ   r]   r^   rj   rl   rn   rp   rq   rr   rz   
InputEvent
__origin__r|   r>   r}   r   model_rebuild__all__r8   r8   r8   r9   <module>   s     (.