Skip to content

Commit

Permalink
Update job interface migration
Browse files Browse the repository at this point in the history
  • Loading branch information
amickan committed Dec 19, 2024
1 parent 97f2f62 commit f57c1c7
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
from django.db import migrations

from grandchallenge.algorithms.models import (
get_existing_interface_for_inputs_and_outputs,
)


def add_algorithm_interfaces_to_jobs(apps, _schema_editor):
Algorithm = apps.get_model("algorithms", "Algorithm") # noqa: N806
AlgorithmInterface = apps.get_model( # noqa: N806
"algorithms", "AlgorithmInterface"
)
Job = apps.get_model("algorithms", "Job") # noqa: N806

for algorithm in Algorithm.objects.prefetch_related("interfaces").all():
default_interface = algorithm.interfaces.get(
algorithmalgorithminterface__is_default=True
jobs = Job.objects.select_related("algorithm_image__algorithm").all()
for job in jobs:
interface = get_existing_interface_for_inputs_and_outputs(
inputs=job.inputs.all(), outputs=job.outputs.all()
)
jobs = Job.objects.filter(algorithm_image__algorithm=algorithm)
for job in jobs:
job.algorithm_interface = default_interface
jobs.bulk_update(jobs, ["algorithm_interface"])
if not interface:
interface = AlgorithmInterface.objects.create()
interface.inputs.set(job.inputs.all())
interface.outputs.set(job.outputs.all())

job.algorithm_interface = interface
job.algorithm_image.algorithm.interfaces.add(interface)
jobs.bulk_update(jobs, ["algorithm_interface"])


class Migration(migrations.Migration):
Expand Down

0 comments on commit f57c1c7

Please sign in to comment.