Skip to content

Commit

Permalink
Show referenced objects when trying to archive a pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
SmittieC committed Dec 17, 2024
1 parent ed54697 commit 62df778
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
2 changes: 1 addition & 1 deletion apps/assistants/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class OpenAiAssistantTable(tables.Table):
title="Archive",
icon_class="fa-solid fa-box-archive",
required_permissions=["assistants.delete_openaiassistant"],
confirm_message="This will only not delete the assistant from OpenAI.",
confirm_message="This will not delete the assistant from OpenAI.",
hx_method="delete",
),
actions.AjaxAction(
Expand Down
1 change: 1 addition & 0 deletions apps/assistants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def delete(self, request, team_slug: str, pk: int):
response = render_to_string(
"assistants/partials/referenced_objects.html",
context={
"object_name": "assistant",
"experiments": experiments,
"pipeline_nodes": pipeline_nodes,
},
Expand Down
14 changes: 10 additions & 4 deletions apps/pipelines/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def archive(self) -> bool:
if self.get_related_experiments_queryset().exists():
return False

if self.get_related_actions_queryset().exists():
if self.get_static_trigger_experiments():
return False

super().archive()
Expand All @@ -289,11 +289,17 @@ def get_node_param_values(self, node_cls, param_name: str) -> list:
def get_related_experiments_queryset(self) -> models.QuerySet:
return self.experiment_set.filter(is_archived=False)

def get_related_actions_queryset(self) -> models.QuerySet:
def get_static_trigger_experiments(self) -> models.QuerySet:
from apps.events.models import EventAction, EventActionType

return EventAction.objects.filter(
action_type=EventActionType.PIPELINE_START, params__pipeline_id=self.id, static_trigger__is_archived=False
return (
EventAction.objects.filter(
action_type=EventActionType.PIPELINE_START,
params__pipeline_id=self.id,
static_trigger__is_archived=False,
)
.annotate(experiment=models.F("static_trigger__experiment"))
.values("experiment")
)


Expand Down
8 changes: 6 additions & 2 deletions apps/pipelines/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ class PipelineTable(tables.Table):
actions = actions.ActionsColumn(
actions=[
actions.edit_action(url_name="pipelines:edit"),
actions.delete_action(
url_name="pipelines:delete",
actions.AjaxAction(
"pipelines:delete",
title="Archive",
icon_class="fa-solid fa-box-archive",
required_permissions=["pipelines.delete_pipeline"],
confirm_message="This will delete the pipeline and any associated logs. Are you sure?",
hx_method="delete",
),
]
)
Expand Down
35 changes: 30 additions & 5 deletions apps/pipelines/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
from django.contrib import messages
from django.contrib.auth.decorators import permission_required
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.db.models import Count, QuerySet
from django.db.models import Count, QuerySet, Subquery
from django.http import Http404, HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string
from django.template.response import TemplateResponse
from django.urls import reverse
from django.views import View
Expand All @@ -18,7 +19,7 @@
from django_tables2 import SingleTableView

from apps.assistants.models import OpenAiAssistant
from apps.experiments.models import AgentTools, SourceMaterial
from apps.experiments.models import AgentTools, Experiment, SourceMaterial
from apps.pipelines.flow import FlowPipelineData
from apps.pipelines.models import Pipeline, PipelineRun
from apps.pipelines.nodes.base import OptionsSource
Expand All @@ -28,6 +29,8 @@
from apps.teams.decorators import login_and_team_required
from apps.teams.mixins import LoginAndTeamRequiredMixin

from ..generics.chips import Chip


class PipelineHome(LoginAndTeamRequiredMixin, TemplateView, PermissionRequiredMixin):
permission_required = "pipelines.view_pipeline"
Expand Down Expand Up @@ -86,9 +89,31 @@ class DeletePipeline(LoginAndTeamRequiredMixin, View, PermissionRequiredMixin):

def delete(self, request, team_slug: str, pk: int):
pipeline = get_object_or_404(Pipeline.objects.prefetch_related("node_set"), id=pk, team=request.team)
pipeline.archive()
messages.success(request, f"{pipeline.name} deleted")
return HttpResponse()
if pipeline.archive():
messages.success(request, "Pipeline Archived")
return HttpResponse()
else:
experiments = [
Chip(label=experiment.name, url=experiment.get_absolute_url())
for experiment in pipeline.get_related_experiments_queryset()
]

query = pipeline.get_static_trigger_experiments()
static_trigger_experiments = [
Chip(label=experiment.name, url=experiment.get_absolute_url())
for experiment in Experiment.objects.filter(id__in=Subquery(query)).all()
]

response = render_to_string(
"assistants/partials/referenced_objects.html",
context={
"object_name": "pipeline",
"experiments": experiments,
"static_trigger_experiments": static_trigger_experiments,
},
)

return HttpResponse(response, headers={"HX-Reswap": "none"}, status=400)


def _pipeline_node_parameter_values(team, llm_providers, llm_provider_models):
Expand Down
11 changes: 10 additions & 1 deletion templates/assistants/partials/referenced_objects.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
<p>This assistant is still referenced by other objects. You must archive those first.</p>
<p>This {{ object_name }} is still referenced by other objects. You must archive those first.</p>
{% if experiments %}
<h2 class="font-semibold my-2">Experiments</h2>
<ul class="list-disc list-inside">
Expand All @@ -17,4 +17,13 @@ <h2 class="font-semibold my-2">Pipelines</h2>
{% endfor %}
</ul>
{% endif %}

{% if static_trigger_experiments %}
<h2 class="font-semibold my-2">Static Trigger Experiments</h2>
<ul class="list-disc list-inside">
{% for experiment in static_trigger_experiments %}
<li>{% include "generic/chip.html" with chip=experiment %}</li>
{% endfor %}
</ul>
{% endif %}
</div>

0 comments on commit 62df778

Please sign in to comment.