Skip to content
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

Change(WorkflowRunManager): update the workflowrun model #536

Merged
merged 8 commits into from
Sep 10, 2024
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Meta:
id = models.BigAutoField(primary_key=True)

# --- mandatory fields
workflow_run = models.ForeignKey(WorkflowRun, on_delete=models.CASCADE)
workflow_run = models.ForeignKey(WorkflowRun, related_name='states', on_delete=models.CASCADE)
status = models.CharField(max_length=255) # TODO: How and where to enforce conventions?
timestamp = models.DateTimeField()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ def to_dict(self):

def get_all_states(self):
# retrieve all states (DB records rather than a queryset)
return list(self.state_set.all()) # TODO: ensure order by timestamp ?
return list(self.states.all()) # TODO: ensure order by timestamp ?

def get_latest_state(self):
# retrieve all related states and get the latest one
return self.states.order_by('-timestamp').first()

class LibraryAssociationManager(OrcaBusBaseManager):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,29 @@ class Meta:
fields = '__all__'


class LibraryModelSerializer(serializers.ModelSerializer):
class Meta:
model = Library
fields = '__all__'

class WorkflowRunModelSerializer(serializers.ModelSerializer):
current_state = serializers.SerializerMethodField()
libraries = LibraryModelSerializer(many=True, read_only=True)
workflow = WorkflowModelSerializer(read_only=True)
class Meta:
model = WorkflowRun
fields = '__all__'

def get_current_state(self, obj)->dict:
latest_state = obj.get_latest_state()
return StateModelSerializer(latest_state).data if latest_state else None

class PayloadModelSerializer(serializers.ModelSerializer):
class Meta:
model = Payload
fields = '__all__'


class LibraryModelSerializer(serializers.ModelSerializer):
class Meta:
model = Library
fields = '__all__'


class StateModelSerializer(serializers.ModelSerializer):
class Meta:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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):
Expand Down