Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cleanup db schema + identifier types + remove action key #589

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions frontend/src/client/schemas.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,12 @@ export const $ActionRead = {
type: 'object',
title: 'Inputs'
},
key: {
type: 'string',
title: 'Key'
},
control_flow: {
'$ref': '#/components/schemas/ActionControlFlow'
}
},
type: 'object',
required: ['id', 'type', 'title', 'description', 'status', 'inputs', 'key'],
required: ['id', 'type', 'title', 'description', 'status', 'inputs'],
title: 'ActionRead'
} as const;

Expand Down Expand Up @@ -143,14 +139,10 @@ export const $ActionReadMinimal = {
status: {
type: 'string',
title: 'Status'
},
key: {
type: 'string',
title: 'Key'
}
},
type: 'object',
required: ['id', 'workflow_id', 'type', 'title', 'description', 'status', 'key'],
required: ['id', 'workflow_id', 'type', 'title', 'description', 'status'],
title: 'ActionReadMinimal'
} as const;

Expand Down
2 changes: 0 additions & 2 deletions frontend/src/client/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export type ActionRead = {
inputs: {
[key: string]: unknown;
};
key: string;
control_flow?: ActionControlFlow;
};

Expand All @@ -42,7 +41,6 @@ export type ActionReadMinimal = {
title: string;
description: string;
status: string;
key: string;
};

export type ActionRetryPolicy = {
Expand Down
10 changes: 2 additions & 8 deletions tracecat/db/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ class Resource(SQLModel):
surrogate_id: int | None = Field(default=None, primary_key=True, exclude=True)
owner_id: OwnerID
created_at: datetime = Field(
sa_type=TIMESTAMP(timezone=True), # UTC Timestamp
sa_type=TIMESTAMP,
sa_column_kwargs={
"server_default": text("(now() AT TIME ZONE 'utc'::text)"),
"nullable": False,
},
)
updated_at: datetime = Field(
sa_type=TIMESTAMP(timezone=True), # UTC Timestamp
sa_type=TIMESTAMP,
sa_column_kwargs={
"server_default": text("(now() AT TIME ZONE 'utc'::text)"),
"onupdate": text("(now() AT TIME ZONE 'utc'::text)"),
Expand Down Expand Up @@ -399,12 +399,6 @@ class Action(Resource, table=True):
back_populates="actions", sa_relationship_kwargs=DEFAULT_SA_RELATIONSHIP_KWARGS
)

@computed_field
@property
def key(self) -> str:
"""Workflow-relative key for an Action."""
return action.key(self.workflow_id, self.id)

@property
def ref(self) -> str:
"""Slugified title of the action. Used for references."""
Expand Down
7 changes: 0 additions & 7 deletions tracecat/identifiers/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from pydantic import StringConstraints
from slugify import slugify

from tracecat.identifiers.resource import ResourcePrefix

ActionID = Annotated[str, StringConstraints(pattern=r"act-[0-9a-f]{32}")]
"""A unique ID for an action. e.g. 'act-77932a0b140a4465a1a25a5c95edcfb8'"""

Expand All @@ -20,8 +18,3 @@
def ref(text: str) -> ActionRef:
"""Return a slugified version of the text."""
return slugify(text, separator="_")


def key(workflow_id: str, action_ref: str) -> ActionKey:
"""Identifier key for an action, using the workflow ID and action ref."""
return f"{ResourcePrefix.ACTION}:{workflow_id}:{action_ref}"
6 changes: 2 additions & 4 deletions tracecat/identifiers/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
"""Resource identifier pattern. e.g. 'wf-77932a0b140a4465a1a25a5c95edcfb8'"""


def generate_resource_id(prefix: ResourcePrefix, *, sep: str = "-") -> ResourceID:
def generate_resource_id(prefix: str, *, sep: str = "-") -> ResourceID:
"""Generate a short unique identifier with a prefix."""

return prefix + sep + uuid4().hex


def id_factory(
prefix: ResourcePrefix, *, sep: str = "-"
) -> Callable[[str], ResourceID]:
def id_factory(prefix: str, *, sep: str = "-") -> Callable[[], ResourceID]:
"""Factory function to generate a short unique identifier with a prefix."""

# Assert that the prefix is a valid resource class identifier.
Expand Down
2 changes: 0 additions & 2 deletions tracecat/workflow/actions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class ActionRead(BaseModel):
description: str
status: str
inputs: dict[str, Any]
key: str # Computed field
control_flow: ActionControlFlow = Field(default_factory=ActionControlFlow)


Expand All @@ -34,7 +33,6 @@ class ActionReadMinimal(BaseModel):
title: str
description: str
status: str
key: str


class ActionCreate(BaseModel):
Expand Down
11 changes: 6 additions & 5 deletions tracecat/workflow/actions/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ async def list_actions(
title=action.title,
description=action.description,
status=action.status,
key=action.key,
)
for action in actions
]
Expand All @@ -51,13 +50,18 @@ async def create_action(
session: AsyncDBSession,
) -> ActionReadMinimal:
"""Create a new action for a workflow."""
if role.workspace_id is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Workspace ID is required",
)
action = Action(
owner_id=role.workspace_id,
workflow_id=params.workflow_id,
type=params.type,
title=params.title,
description="", # Default to empty string
)
) # type: ignore
# Check if a clashing action ref exists
statement = select(Action).where(
Action.owner_id == role.workspace_id,
Expand All @@ -82,7 +86,6 @@ async def create_action(
title=action.title,
description=action.description,
status=action.status,
key=action.key,
)
return action_metadata

Expand Down Expand Up @@ -115,7 +118,6 @@ async def get_action(
description=action.description,
status=action.status,
inputs=action.inputs,
key=action.key,
control_flow=ActionControlFlow(**action.control_flow),
)

Expand Down Expand Up @@ -163,7 +165,6 @@ async def update_action(
description=action.description,
status=action.status,
inputs=action.inputs,
key=action.key,
)


Expand Down
Loading