Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
30 changes: 25 additions & 5 deletions backend/workflow_manager/workflow_v2/workflow_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,18 +994,38 @@ def make_async_result(obj: AsyncResult) -> dict[str, Any]:
"info": obj.info,
}

USAGE_DISPLAY_LIMIT = 5

@staticmethod
def can_update_workflow(workflow_id: str) -> dict[str, Any]:
try:
workflow: Workflow = Workflow.objects.get(pk=workflow_id)
if not workflow or workflow is None:
raise WorkflowDoesNotExistError()
used_count = Pipeline.objects.filter(workflow=workflow).count()
if used_count == 0:
used_count = APIDeployment.objects.filter(workflow=workflow).count()
return {"can_update": used_count == 0}

limit = WorkflowHelper.USAGE_DISPLAY_LIMIT
pipelines = list(
Pipeline.objects.filter(workflow=workflow).values(
"pipeline_name", "pipeline_type"
)[:limit]
)
api_deployments = list(
APIDeployment.objects.filter(workflow=workflow).values_list(
"display_name", flat=True
)[:limit]
)
pipeline_count = Pipeline.objects.filter(workflow=workflow).count()
api_count = APIDeployment.objects.filter(workflow=workflow).count()

return {
"can_update": (pipeline_count + api_count) == 0,
"pipelines": pipelines,
"api_names": list(api_deployments),
"pipeline_count": pipeline_count,
"api_count": api_count,
}
except Workflow.DoesNotExist:
logger.error(f"Error getting workflow: {id}")
logger.error(f"Error getting workflow: {workflow_id}")
raise WorkflowDoesNotExistError()


Expand Down
100 changes: 74 additions & 26 deletions frontend/src/components/workflows/workflow/Workflows.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,37 +146,85 @@ function Workflows() {
});
}

const canDeleteProject = async (id) => {
let status = false;
await projectApiService.canUpdate(id).then((res) => {
status = res?.data?.can_update || false;
});
return status;
const checkWorkflowUsage = async (id) => {
const res = await projectApiService.canUpdate(id);
const data = res?.data || {};
return {
canUpdate: data.can_update || false,
pipelines: data.pipelines || [],
apiNames: data.api_names || [],
pipelineCount: data.pipeline_count || 0,
apiCount: data.api_count || 0,
};
};

const getUsageMessage = (workflowName, usage) => {
const { pipelines, apiNames, pipelineCount, apiCount } = usage;
const totalCount = pipelineCount + apiCount;
if (totalCount === 0) {
return `Cannot delete \`${workflowName}\` as it is currently in use.`;
}

const displayLimit = 3;
const lines = [];

if (apiNames.length > 0) {
const shown = apiNames.slice(0, displayLimit);
shown.forEach((name) => {
lines.push(`- \`${name}\` (API Deployment)`);
});
if (apiCount > shown.length) {
lines.push(
`- ...and ${apiCount - shown.length} more API deployment(s)`
);
}
}

if (pipelines.length > 0) {
const shown = pipelines.slice(0, displayLimit);
shown.forEach((p) => {
const name = p.pipeline_name;
const type = p.pipeline_type;
lines.push(`- \`${name}\` (${type} Pipeline)`);
});
const remaining = pipelineCount - shown.length;
if (remaining > 0) {
lines.push(`- ...and ${remaining} more pipeline(s)`);
}
}

const details = lines.join("\n");
return `Cannot delete \`${workflowName}\` as it is used in:\n${details}`;
};

const deleteProject = async (_evt, project) => {
const canDelete = await canDeleteProject(project.id);
if (canDelete) {
projectApiService
.deleteProject(project.id)
.then(() => {
getProjectList();
setAlertDetails({
type: "success",
content: "Workflow deleted successfully",
try {
const usage = await checkWorkflowUsage(project.id);
if (usage.canUpdate) {
projectApiService
.deleteProject(project.id)
.then(() => {
getProjectList();
setAlertDetails({
type: "success",
content: "Workflow deleted successfully",
});
})
.catch((err) => {
setAlertDetails(
handleException(err, `Unable to delete workflow ${project.id}`)
);
});
})
.catch((err) => {
setAlertDetails(
handleException(err, `Unable to delete workflow ${project.id}`)
);
} else {
setAlertDetails({
type: "error",
content: getUsageMessage(project.workflow_name, usage),
});
} else {
setAlertDetails({
type: "error",
content:
"Cannot delete this Workflow, since it is used in one or many of the API/ETL/Task pipelines",
});
}
} catch (err) {
setAlertDetails(
handleException(err, `Unable to delete workflow ${project.id}`)
);
}
};

Expand Down