Repository: akvo/akvo-rsr

Issue:
Error in disaggregation view
The PGView for disaggregation is incorrect. It includes data from all updates rather than just approved updates.


Discussion:

Resolving patch:
diff --git a/akvo/rsr/models/result/indicator_period_aggregation.py b/akvo/rsr/models/result/indicator_period_aggregation.py
--- a/akvo/rsr/models/result/indicator_period_aggregation.py
+++ b/akvo/rsr/models/result/indicator_period_aggregation.py
@@ -52,12 +52,16 @@ class Meta:
 DISAGG_SQL = """
     WITH aggregated_disaggs AS (
         SELECT
-            dimension_id,
-            sum(("value") :: DECIMAL(20,2)) AS value,
-            sum((numerator) :: DECIMAL(20,2)) AS numerator,
-            sum((denominator) :: DECIMAL(20,2)) AS denominator
+            disagg.dimension_id AS dimension_id,
+            sum((disagg.value) :: DECIMAL(20,2)) AS value,
+            sum((disagg.numerator) :: DECIMAL(20,2)) AS numerator,
+            sum((disagg.denominator) :: DECIMAL(20,2)) AS denominator
         FROM
-            rsr_disaggregation
+            rsr_disaggregation disagg,
+            rsr_indicatorperioddata "update"
+        WHERE
+            update.status = 'A' AND
+            disagg.update_id = update.id
         GROUP BY
             dimension_id
     ),


Test patch:
diff --git a/akvo/rsr/tests/models/test_results_aggregation.py b/akvo/rsr/tests/models/test_results_aggregation.py
--- a/akvo/rsr/tests/models/test_results_aggregation.py
+++ b/akvo/rsr/tests/models/test_results_aggregation.py
@@ -131,10 +131,13 @@ def setUp(self):
             indicator=self.indicator, name="Color", value="Red"
         )
         self.update1 = IndicatorPeriodData.objects.create(
-            period=self.period, user=self.user, value=17.11
+            period=self.period, user=self.user, value=17.11, status='A'
         )
         self.update2 = IndicatorPeriodData.objects.create(
-            period=self.period, user=self.user, value=47.89
+            period=self.period, user=self.user, value=47.89, status='A'
+        )
+        self.update3 = IndicatorPeriodData.objects.create(
+            period=self.period, user=self.user, value=88.88, status='D'
         )
         #  set up PG views
         vs = ViewSyncer()
@@ -150,6 +153,7 @@ def test_disaggregated_values_aggregated_over_period(self):
         period = self.period
         update1 = self.update1
         update2 = self.update2
+        update3 = self.update3
         dimension1 = self.dimension1
         dimension2 = self.dimension2
         dimension3 = self.dimension3
@@ -161,14 +165,21 @@ def test_disaggregated_values_aggregated_over_period(self):
         Disaggregation.objects.create(
             dimension=dimension2, update=update1, value=6.01
         )
+        Disaggregation.objects.create(
+            dimension=dimension3, update=update1, value=23.45
+        )
         Disaggregation.objects.create(
             dimension=dimension1, update=update2, value=31.57
         )
         Disaggregation.objects.create(
             dimension=dimension2, update=update2, value=16.32
         )
+        # Note the following disaggregation are for draft updates and should not be included in the SUM
         Disaggregation.objects.create(
-            dimension=dimension3, update=update1, value=23.45
+            dimension=dimension1, update=update3, value=12.34
+        )
+        Disaggregation.objects.create(
+            dimension=dimension2, update=update3, value=47.11
         )
         period_disaggregations = PeriodDisaggregation.objects.filter(
             indicator=indicator, period=period