-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Alex Streed <[email protected]>
- Loading branch information
1 parent
bce7345
commit 0f2530c
Showing
9 changed files
with
1,033 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from prefect.client.orchestration.base import BaseAsyncClient, BaseClient | ||
|
||
if TYPE_CHECKING: | ||
from uuid import UUID | ||
|
||
from prefect._experimental.sla.objects import SlaTypes | ||
|
||
|
||
class SlaClient(BaseClient): | ||
def create_sla(self, sla: "SlaTypes") -> "UUID": | ||
""" | ||
Creates a service level agreement. | ||
Args: | ||
sla: The SLA to create. Must have a deployment ID set. | ||
Raises: | ||
httpx.RequestError: if the SLA was not created for any reason | ||
Returns: | ||
the ID of the SLA in the backend | ||
""" | ||
if not sla.owner_resource: | ||
raise ValueError( | ||
"Deployment ID is not set. Please set using `set_deployment_id`." | ||
) | ||
|
||
response = self.request( | ||
"POST", | ||
"/slas/", | ||
json=sla.model_dump(mode="json", exclude_unset=True), | ||
) | ||
response.raise_for_status() | ||
|
||
from uuid import UUID | ||
|
||
return UUID(response.json().get("id")) | ||
|
||
|
||
class SlaAsyncClient(BaseAsyncClient): | ||
async def create_sla(self, sla: "SlaTypes") -> "UUID": | ||
""" | ||
Creates a service level agreement. | ||
Args: | ||
sla: The SLA to create. Must have a deployment ID set. | ||
Raises: | ||
httpx.RequestError: if the SLA was not created for any reason | ||
Returns: | ||
the ID of the SLA in the backend | ||
""" | ||
if not sla.owner_resource: | ||
raise ValueError( | ||
"Deployment ID is not set. Please set using `set_deployment_id`." | ||
) | ||
|
||
response = await self.request( | ||
"POST", | ||
"/slas/", | ||
json=sla.model_dump(mode="json", exclude_unset=True), | ||
) | ||
response.raise_for_status() | ||
|
||
from uuid import UUID | ||
|
||
return UUID(response.json().get("id")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import annotations | ||
|
||
import abc | ||
from typing import Literal, Optional, Union | ||
from uuid import UUID | ||
|
||
from pydantic import Field, PrivateAttr, computed_field | ||
from typing_extensions import TypeAlias | ||
|
||
from prefect._internal.schemas.bases import PrefectBaseModel | ||
|
||
|
||
class ServiceLevelAgreement(PrefectBaseModel, abc.ABC): | ||
"""An ORM representation of a Service Level Agreement.""" | ||
|
||
_deployment_id: Optional[UUID] = PrivateAttr(default=None) | ||
|
||
name: str = Field( | ||
default=..., | ||
description="The name of the SLA. Names must be unique on a per-deployment basis.", | ||
) | ||
severity: Literal["minor", "low", "moderate", "high", "critical"] = Field( | ||
default="moderate", | ||
description="The severity of the SLA.", | ||
) | ||
enabled: Optional[bool] = Field( | ||
default=True, | ||
description="Whether the SLA is enabled.", | ||
) | ||
|
||
def set_deployment_id(self, deployment_id: UUID): | ||
self._deployment_id = deployment_id | ||
return self | ||
|
||
@computed_field | ||
@property | ||
def owner_resource(self) -> Union[str, None]: | ||
if self._deployment_id: | ||
return f"prefect.deployment.{self._deployment_id}" | ||
return None | ||
|
||
|
||
class TimeToCompletionSla(ServiceLevelAgreement): | ||
"""An SLA that triggers when a flow run takes longer than the specified duration.""" | ||
|
||
duration: int = Field( | ||
default=..., | ||
description="The maximum flow run duration allowed before the SLA is violated, expressed in seconds.", | ||
) | ||
|
||
|
||
# Concrete SLA types | ||
SlaTypes: TypeAlias = Union[TimeToCompletionSla] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.