From ce9dc31f034250c408cf5a59c1f929b57d72de41 Mon Sep 17 00:00:00 2001
From: Vermaas <vermaas@astron.nl>
Date: Thu, 8 Aug 2024 11:47:00 +0200
Subject: [PATCH] merge and deploy

---
 .../services/activities_handler.py            | 20 +++++++++----------
 .../tests/test_update_activity.py             | 17 ++++++++++++++++
 2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/atdb/taskdatabase/services/activities_handler.py b/atdb/taskdatabase/services/activities_handler.py
index 45cfd5d6..21100a51 100644
--- a/atdb/taskdatabase/services/activities_handler.py
+++ b/atdb/taskdatabase/services/activities_handler.py
@@ -189,17 +189,6 @@ def update_processed_and_aggregate(task):
         if t.status != State.DISCARDED.value:
             non_discarded_found = True
 
-    # only save when changed
-    if activity.is_processed != current_is_processed:
-        # if the whole activity has become processed, then set the status of this activity to 'AGGREGATE'
-        # unless it was already aggregated
-        if (activity.is_processed & non_discarded_found):
-            if not (activity.is_aggregated):
-                activity.status = State.AGGREGATE.value
-
-        activity.save()
-
-
     if (task.workflow.aggregation_strategy == AggregationStrategy.COLLECT_H5.value):
 
         # check if there is already a storage_location, if not, add it.
@@ -227,6 +216,15 @@ def update_processed_and_aggregate(task):
             activity.save()
 
 
+    # only save when changed
+    if activity.is_processed != current_is_processed:
+        # if the whole activity has become processed, then set the status of this activity to 'AGGREGATE'
+        # unless it was already aggregated
+        if (activity.is_processed & non_discarded_found):
+            if not (activity.is_aggregated):
+                activity.status = State.AGGREGATE.value
+
+        activity.save()
 
 def update_is_verified(task):
     """
diff --git a/atdb/taskdatabase/tests/test_update_activity.py b/atdb/taskdatabase/tests/test_update_activity.py
index 364f1036..b7c08e3e 100644
--- a/atdb/taskdatabase/tests/test_update_activity.py
+++ b/atdb/taskdatabase/tests/test_update_activity.py
@@ -97,6 +97,11 @@ class TestUpdateActivity(TestCase):
                                          outputs={"inspect": {"location": "file:///project/ldv/Share/run/2023/3/26/331_30608/inspect.h5", "basename": "inspect.h5", "nameroot": "inspect"}})
         self.task11.save()
 
+        self.task12 = Task.objects.create(sas_id=114,
+                                         new_status='processed',
+                                         workflow=self.workflow_imaging_compression)
+
+        self.task12.save()
     def test_created_activity(self):
         """
         test if activity is created
@@ -217,3 +222,15 @@ class TestUpdateActivity(TestCase):
         expected = "/project/ldv/Share/aggregate/113"
         actual = self.task11.activity.storage_location
         self.assertEqual(actual, expected)
+
+    def test_update_processed_and_aggregate(self):
+
+        # arrange
+        task = Task.objects.create(sas_id=114, new_status='processed', workflow=self.workflow_imaging_compression)
+
+        # act
+        task.save()
+
+        # assert
+        aggregation_tasks = Task.objects.filter(sas_id=114, task_type='aggregation')
+        self.assertEqual(len(aggregation_tasks),1)
-- 
GitLab