From 4f96614cf725bb01f07b8ed7cfb1b5dc952f87d0 Mon Sep 17 00:00:00 2001
From: Robbie Luijben <luijben@astron.nl>
Date: Tue, 21 Nov 2023 10:40:41 +0100
Subject: [PATCH] Extracted percentage to quality logic to a separate function

---
 .../services/calculated_qualities.py          | 20 ++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/atdb/taskdatabase/services/calculated_qualities.py b/atdb/taskdatabase/services/calculated_qualities.py
index a6d89ca0..d956c66f 100644
--- a/atdb/taskdatabase/services/calculated_qualities.py
+++ b/atdb/taskdatabase/services/calculated_qualities.py
@@ -22,7 +22,6 @@ def calculate_qualities(task, tasks_for_this_sasid, quality_thresholds):
         try:
 
             summary = task.quality_json["summary"]
-            quality = None
 
             summary_flavour = get_summary_flavour(task)
 
@@ -35,20 +34,23 @@ def calculate_qualities(task, tasks_for_this_sasid, quality_thresholds):
                     record = summary[key]
                     rfi_percent = float(record['rfi_percent'])
 
-
-            if rfi_percent >= 0:
-                quality = "good"
-                if rfi_percent >= quality_thresholds['moderate']:
-                    quality = "moderate"
-                if rfi_percent > quality_thresholds['poor']:
-                    quality = "poor"
-            return quality
+            return rfi_percentage_to_quality(rfi_percent, quality_thresholds['moderate'], quality_thresholds['poor'])
 
         except Exception as error:
             # when rfi_percentage is missing, then the quality cannot be calculated.
             # Just continue without it
             pass
 
+    def rfi_percentage_to_quality(rfi_percent, quality_treshold_moderate, quality_treshold_poor):
+        quality = None
+        if rfi_percent >= 0:
+            quality = "good"
+            if rfi_percent >= quality_treshold_moderate:
+                quality = "moderate"
+            if rfi_percent > quality_treshold_poor:
+                quality = "poor"
+        return quality
+
     def calculate_quality_sasid(unsaved_task, tasks_for_this_sasid):
         """
         calculate the overall quality per sas_id, based on other tasks with the same sas_id
-- 
GitLab