Skip to content

Commit

Permalink
feat(engine): Add workflow alias to backend (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
daryllimyt authored Dec 28, 2024
1 parent b06a1d7 commit 8cad761
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 3 deletions.
40 changes: 40 additions & 0 deletions alembic/versions/8be6393b2ee0_add_workflow_alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Add workflow alias
Revision ID: 8be6393b2ee0
Revises: 3bc0a0970817
Create Date: 2024-12-24 18:10:17.717675
"""
from collections.abc import Sequence

import sqlalchemy as sa
import sqlmodel.sql.sqltypes

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "8be6393b2ee0"
down_revision: str | None = "3bc0a0970817"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"workflow",
sa.Column("alias", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
)
op.create_index(op.f("ix_workflow_alias"), "workflow", ["alias"], unique=False)
op.create_unique_constraint(
"uq_workflow_alias_owner_id", "workflow", ["alias", "owner_id"]
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint("uq_workflow_alias_owner_id", "workflow", type_="unique")
op.drop_index(op.f("ix_workflow_alias"), table_name="workflow")
op.drop_column("workflow", "alias")
# ### end Alembic commands ###
33 changes: 33 additions & 0 deletions frontend/src/client/schemas.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3661,6 +3661,17 @@ export const $WorkflowRead = {
type: 'null'
}
]
},
alias: {
anyOf: [
{
type: 'string'
},
{
type: 'null'
}
],
title: 'Alias'
}
},
type: 'object',
Expand Down Expand Up @@ -3732,6 +3743,17 @@ export const $WorkflowReadMinimal = {
}
],
title: 'Tags'
},
alias: {
anyOf: [
{
type: 'string'
},
{
type: 'null'
}
],
title: 'Alias'
}
},
type: 'object',
Expand Down Expand Up @@ -3875,6 +3897,17 @@ export const $WorkflowUpdate = {
type: 'null'
}
]
},
alias: {
anyOf: [
{
type: 'string'
},
{
type: 'null'
}
],
title: 'Alias'
}
},
type: 'object',
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/client/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ export type WorkflowRead = {
} | null;
returns: unknown;
config: DSLConfig_Output | null;
alias?: string | null;
};

export type WorkflowReadMinimal = {
Expand All @@ -1139,6 +1140,7 @@ export type WorkflowReadMinimal = {
updated_at: string;
version: number | null;
tags?: Array<TagRead> | null;
alias?: string | null;
};

export type WorkflowTagCreate = {
Expand All @@ -1163,6 +1165,7 @@ export type WorkflowUpdate = {
} | null;
returns?: unknown | null;
config?: DSLConfig_Input | null;
alias?: string | null;
};

export type WorkspaceMember = {
Expand Down
13 changes: 10 additions & 3 deletions tracecat/db/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ class Workflow(Resource, table=True):
- 1 Workflow to many WorkflowDefinitions
"""

__table_args__ = (
UniqueConstraint("alias", "owner_id", name="uq_workflow_alias_owner_id"),
)

id: str = Field(
default_factory=id_factory("wf"), nullable=False, unique=True, index=True
)
Expand All @@ -280,7 +284,7 @@ class Workflow(Resource, table=True):
status: str = "offline" # "online" or "offline"
version: int | None = None
entrypoint: str | None = Field(
None,
default=None,
description="ID of the node directly connected to the trigger.",
)
static_inputs: dict[str, Any] = Field(
Expand All @@ -294,18 +298,21 @@ class Workflow(Resource, table=True):
description="Input schema for the workflow",
)
returns: Any | None = Field(
None,
default=None,
sa_column=Column(JSONB),
description="Workflow return values",
)
object: dict[str, Any] | None = Field(
sa_column=Column(JSONB), description="React flow graph object"
default=None, sa_column=Column(JSONB), description="React flow graph object"
)
config: dict[str, Any] = Field(
default_factory=dict,
sa_column=Column(JSONB),
description="Workflow configuration",
)
alias: str | None = Field(
default=None, description="Alias for the workflow", index=True
)
icon_url: str | None = None
# Owner
owner_id: OwnerID = Field(
Expand Down
8 changes: 8 additions & 0 deletions tracecat/workflow/management/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class WorkflowRead(BaseModel):
expects: dict[str, ExpectedField] | None = None
returns: Any
config: DSLConfig | None
alias: str | None = None


class WorkflowReadMinimal(BaseModel):
Expand All @@ -46,6 +47,7 @@ class WorkflowReadMinimal(BaseModel):
updated_at: datetime
version: int | None
tags: list[TagRead] | None = None
alias: str | None = None


class WorkflowUpdate(BaseModel):
Expand All @@ -60,6 +62,7 @@ class WorkflowUpdate(BaseModel):
expects: dict[str, ExpectedField] | None = None
returns: Any | None = None
config: DSLConfig | None = None
alias: str | None = None


class WorkflowCreate(BaseModel):
Expand All @@ -74,6 +77,11 @@ class GetWorkflowDefinitionActivityInputs(BaseModel):
task: ActionStatement | None = None


class ResolveWorkflowAliasActivityInputs(BaseModel):
workflow_alias: str
role: Role


WorkflowExportFormat = Literal["json", "yaml"]


Expand Down
2 changes: 2 additions & 0 deletions tracecat/workflow/management/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ async def list_workflows(
updated_at=workflow.updated_at,
version=workflow.version,
tags=tags,
alias=workflow.alias,
)
)
return res
Expand Down Expand Up @@ -203,6 +204,7 @@ async def get_workflow(
actions=actions_responses,
webhook=WebhookResponse(**workflow.webhook.model_dump()),
schedules=workflow.schedules or [],
alias=workflow.alias,
)


Expand Down

0 comments on commit 8cad761

Please sign in to comment.