From 89d778bd711be89a78bc3ad00cc11c91745f3f9a Mon Sep 17 00:00:00 2001
From: Roy de Goei <goei@astron.nl>
Date: Thu, 20 Jan 2022 18:24:22 +0100
Subject: [PATCH] SDC-470: Create api-function to retrieve the minimum start
 time and maximum end time for a given sasid

---
 atdb/taskdatabase/services/algorithms.py | 28 ++++++++++++++++++++++++
 atdb/taskdatabase/urls.py                |  2 ++
 atdb/taskdatabase/views.py               | 19 ++++++++++++++++
 3 files changed, 49 insertions(+)

diff --git a/atdb/taskdatabase/services/algorithms.py b/atdb/taskdatabase/services/algorithms.py
index 511f021e..c2dc4a4b 100644
--- a/atdb/taskdatabase/services/algorithms.py
+++ b/atdb/taskdatabase/services/algorithms.py
@@ -41,6 +41,34 @@ def get_size(status_list, type):
     return sum_value
 
 
+@timeit
+def get_min_start_and_max_end_time(sas_id):
+    """
+    Retrieve the minimum start time en maximum end time of a set of taskids (sas_id) which has the
+    status 'archived' or 'finished'
+    The start time is the moment when the task start 'processing'
+    The end time is the moment when the task was 'processed'
+    """
+    min_start_time = None
+    max_end_time = None
+    logger.info("get_min_start_and_max_end_time(" + str(status_list) + ")")
+    tasks = Task.objects.filter(sas_id=sas_id).filter(status='archived') + Task.objects.filter(sas_id=sas_id).filter(status='finished')
+    for task in tasks:
+        # If more entrees are found for 'processing' task, get the latest
+        start_time = LogEntry.objects.filter(task=task.pk).filter(step_name='running').filter(status='processing').lastest('timestamp').timestamp
+        # If more entrees are found for 'processed' task, get the latest
+        end_time = LogEntry.objects.filter(task=task.pk).filter(step_name='running').filter(status='processed').lastest('timestamp').timestamp
+        if min_start_time is None:
+            min_start_time = start_time
+        elif start_time < min_start_time:
+            min_start_time = start_time
+        if max_end_time is None:
+            max_end_time = end_time
+        elif end_time > max_end_time:
+            max_end_time = end_time
+    return min_start_time, max_end_time
+
+
 def convert_logentries_to_html(log_entries):
     results = ""
 
diff --git a/atdb/taskdatabase/urls.py b/atdb/taskdatabase/urls.py
index 553d5c96..7b1eddb4 100644
--- a/atdb/taskdatabase/urls.py
+++ b/atdb/taskdatabase/urls.py
@@ -54,6 +54,8 @@ urlpatterns = [
     # --- custom requests ---
     # /atdb/get_size?status__in=defined,staged
     path('tasks/get_size/', views.GetSizeView.as_view(), name='get-size-view'),
+    # /atdb/get_min_start_and_max_end_time?sas_id=65005
+    path('get_min_start_and_max_end_time/', views.GetMinMaxTimeView.as_view(), name='get-min-start-and-max-end-time-view'),
 
     # --- controller resources ---
     path('tasks/<int:pk>/setstatus/<new_status>/<page>', views.TaskSetStatus, name='task-setstatus-view'),
diff --git a/atdb/taskdatabase/views.py b/atdb/taskdatabase/views.py
index d1aee3ff..38cb65e7 100644
--- a/atdb/taskdatabase/views.py
+++ b/atdb/taskdatabase/views.py
@@ -737,3 +737,22 @@ class GetSizeView(generics.ListAPIView):
         })
 
 
+# /atdb/get_min_start_and_max_end_time?sas_id=650065
+class GetMinMaxTimeView(generics.ListAPIView):
+    queryset = Task.objects.all()
+
+    # override list and generate a custom response
+    def list(self, request, *args, **kwargs):
+
+        # read the arguments from the query
+        try:
+            sas_id = self.request.query_params['sas_id']
+            start_time, end_time = algorithms.get_min_start_and_max_end_time(sas_id)
+            return Response({
+                'start_time': start_time,
+                'end_time': end_time,
+            })
+        except Exception as error:
+            return Response({
+                'error': str(error)
+            })
\ No newline at end of file
-- 
GitLab