From b2a42128316504ab64ba807180cbab8a3a45b56a Mon Sep 17 00:00:00 2001
From: Mario Raciti <mario.raciti@inaf.it>
Date: Mon, 12 Apr 2021 10:59:19 +0200
Subject: [PATCH] TMSS-692: Refactoring

---
 .../src/tmss/tmssapp/adapters/reports.py      | 55 ++++++++++++-------
 SAS/TMSS/backend/test/t_adapter.py            |  4 --
 2 files changed, 35 insertions(+), 24 deletions(-)

diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py b/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py
index a981588aad2..3b4c4ea6234 100644
--- a/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py
+++ b/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py
@@ -9,6 +9,29 @@ import logging
 logger = logging.getLogger(__name__)
 
 
+def create_project_report(request: Request, project: models.Project) -> {}:
+    """
+    Create a project report as a JSON object.
+    """
+    result = {'project': project.pk}
+    result['quota'] = _get_quotas_from_project(request, project.pk)
+    result['durations'] = _get_subs_and_durations_from_project(project.pk)
+    result['LTA dataproducts'] = _get_lta_dataproducts(project.name)
+    result['SAPs'] = _get_saps(project.pk)
+
+    return result
+
+
+def _get_quotas_from_project(request: Request, project_pk: int) -> []:
+    """
+    Help function to retrieve quotas.
+    """
+    project_quotas = models.ProjectQuota.objects.filter(project=project_pk)
+    project_quotas_data = [serializers.ProjectQuotaSerializer(pq, context={'request': request}).data for pq in project_quotas]
+    quotas = [{k: pqd[k] for k in ('id', 'resource_type_id', 'value')} for pqd in project_quotas_data]
+    return quotas
+
+
 def _get_subs_and_durations_from_project(project_pk: int) -> {}:
     """
     Help function to retrieve durations and scheduling_units distinguished by success/fail.
@@ -37,28 +60,20 @@ def _get_subs_and_durations_from_project(project_pk: int) -> {}:
     return durations
 
 
-def create_project_report(request: Request, project: models.Project) -> {}:
+def _get_lta_dataproducts(project_name: str) -> int:
     """
-    Create a project report as a JSON object.
+    Help function to retrieve the sum of the LTA dataproducts sizes.
     """
-    project_pk = project.pk
-    result = {'project': project_pk}  # Object to be returned
-
-    # Add project_quota(s) to result
-    project_quotas = models.ProjectQuota.objects.filter(project=project_pk)
-    project_quotas_data = [serializers.ProjectQuotaSerializer(pq, context={'request': request}).data for pq in project_quotas]
-    result['quota'] = [{k: pqd[k] for k in ('id', 'resource_type_id', 'value')} for pqd in project_quotas_data]
-
-    # Add durations to result
-    result['durations'] = _get_subs_and_durations_from_project(project_pk)
-
-    # Add sum of dataproducts sizes, querying from Subtasks of type 'ingest' within 'finished' status
-    result['LTA dataproducts'] = models.Dataproduct.objects.filter(producer__subtask__specifications_template__type='ingest')\
-        .filter(producer__subtask__state__value='finished')\
-        .filter(producer__subtask__task_blueprint__draft__scheduling_unit_draft__scheduling_set__project__name=project.name)\
+    # Query dataproducts from Subtasks of type 'ingest' within 'finished' status
+    return models.Dataproduct.objects.filter(producer__subtask__specifications_template__type='ingest') \
+        .filter(producer__subtask__state__value='finished') \
+        .filter(producer__subtask__task_blueprint__draft__scheduling_unit_draft__scheduling_set__project__name=project_name) \
         .aggregate(Sum('size'))
 
-    # TODO: For each unique target (SAP name) get the sum of target observation durations from the tasks.
-    result['SAPs'] = [{'sap_name': 'placeholder', 'total_exposure': 0},]
 
-    return result
+def _get_saps(project_pk: int) -> []:
+    """
+    Help function to retrieve SAPs.
+    """
+    # TODO: For each unique target (SAP name) get the sum of target observation durations from the tasks.
+    return [{'sap_name': 'placeholder', 'total_exposure': 0}, ]
diff --git a/SAS/TMSS/backend/test/t_adapter.py b/SAS/TMSS/backend/test/t_adapter.py
index 8e26113a2f6..83adbc8a6ca 100755
--- a/SAS/TMSS/backend/test/t_adapter.py
+++ b/SAS/TMSS/backend/test/t_adapter.py
@@ -519,10 +519,6 @@ class ProjectReportTest(unittest.TestCase):
             **SchedulingUnitDraft_test_data(scheduling_set=self.scheduling_set))
         self.task_draft = models.TaskDraft.objects.create(
             **TaskDraft_test_data(scheduling_unit_draft=self.scheduling_unit_draft))
-        self.scheduling_unit_blueprint = models.SchedulingUnitBlueprint.objects.create(
-            **SchedulingUnitBlueprint_test_data(draft=self.scheduling_unit_draft))
-        self.task_blueprint = models.TaskBlueprint.objects.create(
-            **TaskBlueprint_test_data(task_draft=self.task_draft, scheduling_unit_blueprint=self.scheduling_unit_blueprint))
 
         # Create test_data_creator as superuser
         self.test_data_creator = TMSSRESTTestDataCreator(BASE_URL, AUTH)
-- 
GitLab