File size: 2,749 Bytes
9c6594c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# ruff: noqa: UP007  # Avoid using `X | Y` for union fields, as this can cause issues with pydantic < 2.6

from __future__ import annotations

from datetime import datetime
from typing import Optional

from pydantic import Field
from typing_extensions import Annotated

from wandb._pydantic import GQLBase, GQLId

from ._generated import TriggerFields
from .actions import InputAction, SavedAction
from .events import InputEvent, SavedEvent
from .scopes import AutomationScope


# ------------------------------------------------------------------------------
# Saved types: for parsing response data from saved automations
class Automation(TriggerFields):
    """A local instance of a saved W&B automation."""

    id: GQLId

    created_at: Annotated[datetime, Field(repr=False, frozen=True, alias="createdAt")]
    """The date and time when this automation was created."""

    updated_at: Annotated[
        Optional[datetime], Field(repr=False, frozen=True, alias="updatedAt")
    ] = None
    """The date and time when this automation was last updated, if applicable."""

    name: str
    """The name of this automation."""

    description: Optional[str]
    """An optional description of this automation."""

    enabled: bool
    """Whether this automation is enabled.  Only enabled automations will trigger."""

    event: SavedEvent
    """The event that will trigger this automation."""

    scope: AutomationScope
    """The scope in which the triggering event must occur."""

    action: SavedAction
    """The action that will execute when this automation is triggered."""


class NewAutomation(GQLBase, extra="forbid", validate_default=False):
    """A new automation to be created."""

    name: Optional[str] = None
    """The name of this automation."""

    description: Optional[str] = None
    """An optional description of this automation."""

    enabled: Optional[bool] = None
    """Whether this automation is enabled.  Only enabled automations will trigger."""

    event: Optional[InputEvent] = None
    """The event that will trigger this automation."""

    # Ensure that the event and its scope are always consistent, if the event is set.
    @property
    def scope(self) -> Optional[AutomationScope]:
        """The scope in which the triggering event must occur."""
        return self.event.scope if self.event else None

    @scope.setter
    def scope(self, value: AutomationScope) -> None:
        if self.event is None:
            raise ValueError("Cannot set `scope` for an automation with no `event`")
        self.event.scope = value

    action: Optional[InputAction] = None
    """The action that will execute when this automation is triggered."""


__all__ = [
    "Automation",
    "NewAutomation",
]