diff --git a/lib/workload/stateless/stacks/workflow-manager/workflow_manager/migrations/0002_alter_state_workflow_run.py b/lib/workload/stateless/stacks/workflow-manager/workflow_manager/migrations/0002_alter_state_workflow_run.py new file mode 100644 index 000000000..442c0b3dd --- /dev/null +++ b/lib/workload/stateless/stacks/workflow-manager/workflow_manager/migrations/0002_alter_state_workflow_run.py @@ -0,0 +1,23 @@ +# Generated by Django 5.1 on 2024-09-09 05:55 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("workflow_manager", "0001_initial"), + ] + + operations = [ + migrations.AlterField( + model_name="state", + name="workflow_run", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="states", + to="workflow_manager.workflowrun", + ), + ), + ] diff --git a/lib/workload/stateless/stacks/workflow-manager/workflow_manager/migrations/0002_remove_workflowrun_libraries_and_more.py b/lib/workload/stateless/stacks/workflow-manager/workflow_manager/migrations/0002_remove_workflowrun_libraries_and_more.py deleted file mode 100644 index 096cc4be8..000000000 --- a/lib/workload/stateless/stacks/workflow-manager/workflow_manager/migrations/0002_remove_workflowrun_libraries_and_more.py +++ /dev/null @@ -1,45 +0,0 @@ -# Generated by Django 5.1 on 2024-09-09 05:16 - -import django.db.models.deletion -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ("workflow_manager", "0001_initial"), - ] - - operations = [ - migrations.RemoveField( - model_name="workflowrun", - name="libraries", - ), - migrations.AlterField( - model_name="libraryassociation", - name="library", - field=models.ForeignKey( - on_delete=django.db.models.deletion.CASCADE, - related_name="workflow_run_association", - to="workflow_manager.library", - ), - ), - migrations.AlterField( - model_name="libraryassociation", - name="workflow_run", - field=models.ForeignKey( - on_delete=django.db.models.deletion.CASCADE, - related_name="library_association", - to="workflow_manager.workflowrun", - ), - ), - migrations.AlterField( - model_name="state", - name="workflow_run", - field=models.ForeignKey( - on_delete=django.db.models.deletion.CASCADE, - related_name="states", - to="workflow_manager.workflowrun", - ), - ), - ] diff --git a/lib/workload/stateless/stacks/workflow-manager/workflow_manager/models/workflow_run.py b/lib/workload/stateless/stacks/workflow-manager/workflow_manager/models/workflow_run.py index 943b424c1..3f1b6fd03 100644 --- a/lib/workload/stateless/stacks/workflow-manager/workflow_manager/models/workflow_run.py +++ b/lib/workload/stateless/stacks/workflow-manager/workflow_manager/models/workflow_run.py @@ -29,7 +29,7 @@ class WorkflowRun(OrcaBusBaseModel): workflow = models.ForeignKey(Workflow, null=True, blank=True, on_delete=models.SET_NULL) # Link to library table - # libraries = models.ManyToManyField(Library, through="LibraryAssociation") + libraries = models.ManyToManyField(Library, through="LibraryAssociation") objects = WorkflowRunManager() @@ -54,18 +54,14 @@ def get_all_states(self): def get_latest_state(self): # retrieve all related states and get the latest one return self.states.order_by('-timestamp').first() - - def get_libraries(self): - # retrieve all related libraries objects - return Library.objects.filter(workflow_run_association__workflow_run=self).distinct() class LibraryAssociationManager(OrcaBusBaseManager): pass class LibraryAssociation(OrcaBusBaseModel): - workflow_run = models.ForeignKey(WorkflowRun, related_name="library_association", on_delete=models.CASCADE) - library = models.ForeignKey(Library, related_name="workflow_run_association", on_delete=models.CASCADE) + workflow_run = models.ForeignKey(WorkflowRun, on_delete=models.CASCADE) + library = models.ForeignKey(Library, on_delete=models.CASCADE) association_date = models.DateTimeField() status = models.CharField(max_length=255) diff --git a/lib/workload/stateless/stacks/workflow-manager/workflow_manager/serializers.py b/lib/workload/stateless/stacks/workflow-manager/workflow_manager/serializers.py index 5697424fd..8de594ee3 100644 --- a/lib/workload/stateless/stacks/workflow-manager/workflow_manager/serializers.py +++ b/lib/workload/stateless/stacks/workflow-manager/workflow_manager/serializers.py @@ -39,9 +39,14 @@ class Meta: fields = '__all__' +class LibraryModelSerializer(serializers.ModelSerializer): + class Meta: + model = Library + fields = '__all__' + class WorkflowRunModelSerializer(serializers.ModelSerializer): current_state = serializers.SerializerMethodField() - libraries = serializers.SerializerMethodField() + libraries = LibraryModelSerializer(many=True, read_only=True) workflow = WorkflowModelSerializer(read_only=True) class Meta: model = WorkflowRun @@ -50,10 +55,6 @@ class Meta: def get_current_state(self, obj)->dict: latest_state = obj.get_latest_state() return StateModelSerializer(latest_state).data if latest_state else None - - def get_libraries(self, obj)->list: - libraries = obj.get_libraries() - return [library.library_id for library in libraries] class PayloadModelSerializer(serializers.ModelSerializer): class Meta: @@ -61,11 +62,6 @@ class Meta: fields = '__all__' -class LibraryModelSerializer(serializers.ModelSerializer): - class Meta: - model = Library - fields = '__all__' - class StateModelSerializer(serializers.ModelSerializer): class Meta: diff --git a/lib/workload/stateless/stacks/workflow-manager/workflow_manager/viewsets/workflow_run.py b/lib/workload/stateless/stacks/workflow-manager/workflow_manager/viewsets/workflow_run.py index 444c791f9..83071da07 100644 --- a/lib/workload/stateless/stacks/workflow-manager/workflow_manager/viewsets/workflow_run.py +++ b/lib/workload/stateless/stacks/workflow-manager/workflow_manager/viewsets/workflow_run.py @@ -17,7 +17,7 @@ class WorkflowRunViewSet(ReadOnlyModelViewSet): search_fields = WorkflowRun.get_base_fields() def get_queryset(self): - return WorkflowRun.objects.get_by_keyword(**self.request.query_params).prefetch_related('states').prefetch_related('library_association__library').select_related('workflow') # add prefetch_related & select_related to reduce the number of queries + return WorkflowRun.objects.get_by_keyword(**self.request.query_params).prefetch_related('states').prefetch_related('libraries').select_related('workflow') # add prefetch_related & select_related to reduce the number of queries @action(detail=False, methods=['GET']) def ongoing(self, request): diff --git a/lib/workload/stateless/stacks/workflow-manager/workflow_manager_proc/tests/test_create_workflow_run_state.py b/lib/workload/stateless/stacks/workflow-manager/workflow_manager_proc/tests/test_create_workflow_run_state.py index 52b278ed2..33cc9a9ca 100644 --- a/lib/workload/stateless/stacks/workflow-manager/workflow_manager_proc/tests/test_create_workflow_run_state.py +++ b/lib/workload/stateless/stacks/workflow-manager/workflow_manager_proc/tests/test_create_workflow_run_state.py @@ -56,7 +56,7 @@ def test_create_wrsc_no_library(self): db_wfr: WorkflowRun = wfr_qs.first() self.assertEqual("ctTSO500-L000002", db_wfr.workflow_run_name) # We don't expect any library associations here! - self.assertEqual(0, db_wfr.get_libraries().count()) + self.assertEqual(0, db_wfr.libraries.count()) def test_create_wrsc_library(self): """ @@ -116,8 +116,8 @@ def test_create_wrsc_library(self): db_wfr: WorkflowRun = wfr_qs.first() self.assertEqual("ctTSO500-L000002", db_wfr.workflow_run_name) # We do expect 2 library associations here! - self.assertEqual(2, db_wfr.get_libraries().count()) - for lib in db_wfr.get_libraries().all(): + self.assertEqual(2, db_wfr.libraries.count()) + for lib in db_wfr.libraries.all(): self.assertTrue(lib.library_id in library_ids) def test_create_wrsc_library_exists(self): @@ -184,8 +184,8 @@ def test_create_wrsc_library_exists(self): db_wfr: WorkflowRun = wfr_qs.first() self.assertEqual("ctTSO500-L000002", db_wfr.workflow_run_name) # We do expect 2 library associations here! - self.assertEqual(2, db_wfr.get_libraries().count()) - for lib in db_wfr.get_libraries().all(): + self.assertEqual(2, db_wfr.libraries.count()) + for lib in db_wfr.libraries.all(): self.assertTrue(lib.library_id in library_ids) def test_get_last_state(self):