Skip to content

Commit

Permalink
Merge pull request #540 from umccr/feat/add-query-search-terms-of-wor…
Browse files Browse the repository at this point in the history
…kflowrun

Change(WorkflowRunManager): add more query and search terms
  • Loading branch information
raylrui authored Sep 16, 2024
2 parents d31478b + 87fdf3e commit a0facb4
Showing 1 changed file with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.db.models import Q, Max
from django.db.models import Q, Max, F
from rest_framework import filters
from rest_framework.decorators import action
from rest_framework.viewsets import ReadOnlyModelViewSet
Expand All @@ -19,9 +19,15 @@ class WorkflowRunViewSet(ReadOnlyModelViewSet):
def get_queryset(self):

"""
custom queryset to filter by:
custom queryset:
add filter by:
start_time, end_time : range of latest state timestamp
is_ongoing : filter by ongoing workflow runs
status : filter by latest state status
add search terms:
library_id: filter by library_id
orcabus_id: filter by orcabus_id
"""
# default time is 0
start_time = self.request.query_params.get('start_time', 0)
Expand All @@ -30,6 +36,12 @@ def get_queryset(self):
# get is ongoing flag
is_ongoing = self.request.query_params.get('is_ongoing', 'false')

# get status
status = self.request.query_params.get('status', '')

# get search query params
search_params = self.request.query_params.get('search', '')

# exclude the custom query params from the rest of the query params
def exclude_params(params):
for param in params:
Expand All @@ -38,9 +50,11 @@ def exclude_params(params):
exclude_params([
'start_time',
'end_time',
'is_ongoing'
'is_ongoing',
'status',
'search'
])

# get all workflow runs with rest of the query params
result_set = 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

Expand All @@ -56,6 +70,23 @@ def exclude_params(params):
~Q(states__status="SUCCEEDED")
)

if status:
result_set = result_set.annotate(latest_state_time=Max('states__timestamp')).filter(
states__timestamp=F('latest_state_time'),
states__status=status
)

# # Combine search across multiple fields (worfkflow run name, comment, library_id, orcabus_id, workflow name)
if search_params:
result_set = result_set.filter(
Q(workflow_run_name__icontains=search_params) |
Q(comment__icontains=search_params) |
Q(libraries__library_id__icontains=search_params) |
Q(libraries__orcabus_id__icontains=search_params) |
Q(workflow__workflow_name__icontains=search_params)
)


return result_set

@action(detail=False, methods=['GET'])
Expand Down

0 comments on commit a0facb4

Please sign in to comment.