-
Notifications
You must be signed in to change notification settings - Fork 14
Duplicate Pipeline Management Command #1711
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
Merged
+93
−0
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
64112e0
feat: add migration to duplicate shared pipelines for one-to-one expe…
claude[bot] 5e8db8f
Merge branch 'main' into claude/issue-1656-20250603_204517
stephherbers b7114b0
convert to management command
stephherbers 3da7125
remove django migration
stephherbers 74ed3ad
filter by working_version = None and specify that the Pipeline and No…
stephherbers 6fe45d2
Merge branch 'main' into claude/issue-1656-20250603_204517
stephherbers 81b1496
add in archived pipelines
stephherbers adf0af4
Merge branch 'main' into claude/issue-1656-20250603_204517
stephherbers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
apps/experiments/management/commands/duplicate_shared_pipelines.py
This file contains hidden or 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,91 @@ | ||
from django.core.management.base import BaseCommand | ||
from django.db import transaction | ||
from django.db.models import Count | ||
|
||
from apps.experiments.models import Experiment | ||
from apps.pipelines.models import Node, Pipeline | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Find pipelines that are shared by multiple experiments and create individual copies\ | ||
for each experiment (except one)" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--dry-run", | ||
action="store_true", | ||
help="Show what would be done without making changes", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
dry_run = options["dry_run"] | ||
|
||
shared_pipelines = Pipeline.objects.annotate(experiment_count=Count("experiment")).filter( | ||
snopoke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
experiment_count__gt=1, working_version=None | ||
) | ||
|
||
if not shared_pipelines.exists(): | ||
self.stdout.write(self.style.SUCCESS("No shared pipelines found.")) | ||
return | ||
|
||
self.stdout.write(f"Found {shared_pipelines.count()} shared pipelines to duplicate") | ||
|
||
if dry_run: | ||
self.stdout.write(self.style.WARNING("DRY RUN - No changes will be made")) | ||
else: | ||
confirm = input("Do you want to proceed? (y/N): ") | ||
if confirm.lower() != "y": | ||
self.stdout.write("Cancelled.") | ||
return | ||
|
||
total_created = 0 | ||
|
||
for shared_pipeline in shared_pipelines: | ||
self.stdout.write(f"Processing pipeline: {shared_pipeline.name} (ID: {shared_pipeline.id})") | ||
|
||
experiments = list(Experiment.objects.filter(pipeline=shared_pipeline)) | ||
self.stdout.write(f" Shared by {len(experiments)} experiments") | ||
|
||
if dry_run: | ||
self.stdout.write(f" Would create {len(experiments) - 1} pipeline copies") | ||
total_created += len(experiments) - 1 | ||
continue | ||
|
||
for _, experiment in enumerate(experiments[1:], 1): | ||
try: | ||
with transaction.atomic(): | ||
new_pipeline = Pipeline.objects.create( | ||
team=shared_pipeline.team, | ||
name=f"{shared_pipeline.name} (Copy for {experiment.name})", | ||
data=shared_pipeline.data, | ||
working_version=None, | ||
version_number=1, | ||
is_archived=shared_pipeline.is_archived, | ||
) | ||
original_nodes = Node.objects.filter(pipeline=shared_pipeline) | ||
for node in original_nodes: | ||
Node.objects.create( | ||
flow_id=node.flow_id, | ||
type=node.type, | ||
label=node.label, | ||
params=node.params, | ||
working_version=None, | ||
is_archived=node.is_archived, | ||
pipeline=new_pipeline, | ||
) | ||
experiment.pipeline = new_pipeline | ||
experiment.save(update_fields=["pipeline"]) | ||
|
||
self.stdout.write(f" Created pipeline copy {new_pipeline.id} for experiment {experiment.name}") | ||
total_created += 1 | ||
|
||
except Exception as e: | ||
self.stdout.write( | ||
self.style.ERROR(f" Error creating pipeline copy for experiment {experiment.name}: {str(e)}") | ||
) | ||
raise | ||
|
||
if dry_run: | ||
self.stdout.write(self.style.SUCCESS(f"DRY RUN: Would create {total_created} pipeline copies")) | ||
else: | ||
self.stdout.write(self.style.SUCCESS(f"Successfully created {total_created} pipeline copies")) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.