automations.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from __future__ import annotations
  2. from datetime import datetime
  3. from typing import Annotated, Optional
  4. from pydantic import Field
  5. from wandb._pydantic import GQLId, GQLInput
  6. from ._generated import TriggerFields
  7. from .actions import InputAction, SavedAction
  8. from .events import InputEvent, SavedEvent
  9. from .scopes import AutomationScope
  10. # ------------------------------------------------------------------------------
  11. # Saved types: for parsing response data from saved automations while allowing
  12. # local editing.
  13. class Automation(TriggerFields, frozen=False):
  14. """A local instance of a saved W&B automation that supports editing."""
  15. id: GQLId
  16. created_at: Annotated[datetime, Field(repr=False, frozen=True, alias="createdAt")]
  17. """The date and time when this automation was created."""
  18. updated_at: Annotated[
  19. Optional[datetime], Field(repr=False, frozen=True, alias="updatedAt")
  20. ] = None
  21. """The date and time when this automation was last updated, if applicable."""
  22. name: str
  23. """The name of this automation."""
  24. description: Optional[str]
  25. """An optional description of this automation."""
  26. enabled: bool
  27. """Whether this automation is enabled. Only enabled automations will trigger."""
  28. event: SavedEvent
  29. """The event that will trigger this automation."""
  30. scope: AutomationScope
  31. """The scope in which the triggering event must occur."""
  32. action: SavedAction
  33. """The action that will execute when this automation is triggered."""
  34. class NewAutomation(GQLInput, extra="forbid", validate_default=False):
  35. """A new automation to be created."""
  36. name: Optional[str] = None
  37. """The name of this automation."""
  38. description: Optional[str] = None
  39. """An optional description of this automation."""
  40. enabled: Optional[bool] = None
  41. """Whether this automation is enabled. Only enabled automations will trigger."""
  42. event: Optional[InputEvent] = None
  43. """The event that will trigger this automation."""
  44. # Ensure that the event and its scope are always consistent, if the event is set.
  45. @property
  46. def scope(self) -> Optional[AutomationScope]:
  47. """The scope in which the triggering event must occur."""
  48. return self.event.scope if self.event else None
  49. @scope.setter
  50. def scope(self, value: AutomationScope) -> None:
  51. if self.event is None:
  52. raise ValueError("Cannot set `scope` for an automation with no `event`")
  53. self.event.scope = value
  54. action: Optional[InputAction] = None
  55. """The action that will execute when this automation is triggered."""
  56. __all__ = [
  57. "Automation",
  58. "NewAutomation",
  59. ]