From f143f3af7fefbcabc82c5f0280c20cc22740bb71 Mon Sep 17 00:00:00 2001
From: Vermaas <vermaas@astron.nl>
Date: Fri, 26 Apr 2024 15:40:00 +0200
Subject: [PATCH] store calculated quality per sas_id in activity

---
 atdb/taskdatabase/services/activities_handler.py     | 11 +----------
 atdb/taskdatabase/services/calculated_qualities.py   | 11 +++--------
 atdb/taskdatabase/tests/test_calculated_qualities.py |  2 +-
 3 files changed, 5 insertions(+), 19 deletions(-)

diff --git a/atdb/taskdatabase/services/activities_handler.py b/atdb/taskdatabase/services/activities_handler.py
index 834a41b0..a98e6b33 100644
--- a/atdb/taskdatabase/services/activities_handler.py
+++ b/atdb/taskdatabase/services/activities_handler.py
@@ -76,10 +76,9 @@ def update_activity(task):
     and improves overall performance
 
     - to 'ARCHIVING, ARCHIVED, FINISHED'         : check for incoming/existing 'archive' json from archiver
-    - to STORED                                  : calculate quality
     - to SCRUBBED, ARCHIVING, ARCHIVED, FINISHED : calculate ingested_fraction
     - to _FAILED                                 : calculate finished_fraction
-    - to STORED, PROCESSED, DISCARDED            : check if all tasks are processed, set Activity to is_processed and AGGREGATE
+    - to (processed_statusses)                   : check if all tasks are processed, set Activity to is_processed and AGGREGATE
     - always                                     : determine if a task is in a 'verified' status
 
     """
@@ -88,14 +87,6 @@ def update_activity(task):
     activity = task.activity
 
     # depending on the status transition, perform calculations
-    if task.status == State.STORED.value:
-        logger.info(f'- calculate_qualities')
-        # quality is calculated per task and per sas_id, reget the quality per sas_id
-        try:
-            activity.calculated_quality = task.calculated_qualities['per_sasid']
-            activity.save()
-        except:
-            pass
 
     # calculate the fraction and list of statusses of ingested tasks of this SAS_ID
     if task.status in [State.SCRUBBED.value, State.PRE_ARCHIVING.value, State.PRE_ARCHIVED.value,
diff --git a/atdb/taskdatabase/services/calculated_qualities.py b/atdb/taskdatabase/services/calculated_qualities.py
index b6c72692..a9cbca5f 100644
--- a/atdb/taskdatabase/services/calculated_qualities.py
+++ b/atdb/taskdatabase/services/calculated_qualities.py
@@ -125,14 +125,9 @@ def calculate_qualities(task, tasks_for_this_sasid, quality_thresholds):
         # store the result in task.calculated_qualities (not yet saved in the database)
         qualities['per_sasid'] = calculated_quality_sasid
 
-        # save the new sas_id quality for all the other tasks (siblings) of this sas_id as well
-        for other_task in tasks_for_this_sasid:
-            # the task for which the quality is (re)calculated is saved later, but do save all its siblings
-            if other_task.id != task.id:
-                calc_q = other_task.calculated_qualities
-                calc_q['per_sasid'] = calculated_quality_sasid
-                other_task.calculated_qualities = calc_q
-                other_task.save()
+        # store the result in the activity, and save it
+        task.activity.calculated_quality = calculated_quality_sasid
+        task.activity.save()
 
     except Exception as error:
         logger.error(error)
diff --git a/atdb/taskdatabase/tests/test_calculated_qualities.py b/atdb/taskdatabase/tests/test_calculated_qualities.py
index 3563d46c..1d67535e 100644
--- a/atdb/taskdatabase/tests/test_calculated_qualities.py
+++ b/atdb/taskdatabase/tests/test_calculated_qualities.py
@@ -72,7 +72,7 @@ class TestCalculatedQualities(TestCase):
         count = 0
         tasks = Task.objects.all()
         for task in tasks:
-            if task.calculated_qualities['per_sasid']:
+            if task.calculated_qualities['per_task']:
                 count += 1
 
         self.assertEqual(count,6)
-- 
GitLab