← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2047: Fixed a bug with average int aggregation in AggregationService + added unit tests

 

------------------------------------------------------------
revno: 2047
committer: Lars <larshelg@larshelg-laptop>
branch nick: trunk
timestamp: Sun 2010-06-27 17:34:05 +0200
message:
  Fixed a bug with average int aggregation in AggregationService + added unit tests
added:
  dhis-2/dhis-services/dhis-service-aggregationengine-default/src/test/resources/AggregationServiceTest-Description.txt
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/aggregation/AggregationService.java
  dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/DefaultAggregationService.java
  dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/cache/AggregationCache.java
  dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/cache/MemoryAggregationCache.java
  dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/AbstractDataElementAggregation.java
  dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/AverageIntDataElementAggregation.java
  dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/SumBoolDataElementAggregation.java
  dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-services/dhis-service-aggregationengine-default/src/test/java/org/hisp/dhis/aggregation/AggregationServiceTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/aggregation/AggregationService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/aggregation/AggregationService.java	2010-04-12 21:23:33 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/aggregation/AggregationService.java	2010-06-27 15:34:05 +0000
@@ -97,4 +97,6 @@
      */
     double getAggregatedDenominatorValue( Indicator indicator, Date startDate, Date endDate,
         OrganisationUnit organisationUnit );
+    
+    void clearCache();
 }

=== modified file 'dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/DefaultAggregationService.java'
--- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/DefaultAggregationService.java	2010-04-12 21:23:33 +0000
+++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/DefaultAggregationService.java	2010-06-27 15:34:05 +0000
@@ -30,6 +30,7 @@
 import java.util.Date;
 
 import org.hisp.dhis.aggregation.AggregationService;
+import org.hisp.dhis.aggregation.impl.cache.AggregationCache;
 import org.hisp.dhis.aggregation.impl.dataelement.AbstractDataElementAggregation;
 import org.hisp.dhis.aggregation.impl.indicator.IndicatorAggregation;
 import org.hisp.dhis.dataelement.DataElement;
@@ -83,6 +84,13 @@
         this.indicatorAggregation = indicatorAggregation;
     }
     
+    private AggregationCache aggregationCache;
+
+    public void setAggregationCache( AggregationCache aggregationCache )
+    {
+        this.aggregationCache = aggregationCache;
+    }
+
     // -------------------------------------------------------------------------
     // DataElement
     // -------------------------------------------------------------------------
@@ -118,6 +126,11 @@
         return indicatorAggregation.getAggregatedDenominatorValue( indicator, startDate, endDate, organisationUnit );
     }
     
+    public void clearCache()
+    {
+        aggregationCache.clearCache();
+    }
+    
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/cache/AggregationCache.java'
--- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/cache/AggregationCache.java	2010-06-08 19:47:40 +0000
+++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/cache/AggregationCache.java	2010-06-27 15:34:05 +0000
@@ -48,5 +48,7 @@
     
     Collection<Integer> getPeriodIds( Date startDate, Date endDate );
     
-    double getAggregatedDataValue( DataElement dataElement, DataElementCategoryOptionCombo optionCombo, Date startDate, Date endDate, OrganisationUnit organisationUnit );    
+    double getAggregatedDataValue( DataElement dataElement, DataElementCategoryOptionCombo optionCombo, Date startDate, Date endDate, OrganisationUnit organisationUnit );
+    
+    void clearCache();
 }

=== modified file 'dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/cache/MemoryAggregationCache.java'
--- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/cache/MemoryAggregationCache.java	2010-06-08 19:47:40 +0000
+++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/cache/MemoryAggregationCache.java	2010-06-27 15:34:05 +0000
@@ -49,10 +49,8 @@
 public class MemoryAggregationCache
     implements AggregationCache
 {
-    private OrganisationUnitHierarchy hierarchyCache = null;
-    
-    private Map<String, Period> periodCache = new HashMap<String, Period>();
-    
+    private OrganisationUnitHierarchy hierarchyCache = null;    
+    private Map<String, Period> periodCache = new HashMap<String, Period>();    
     private Map<String, Collection<Integer>> periodIdCache = new HashMap<String, Collection<Integer>>();
     
     private static final String SEPARATOR = "-";
@@ -143,22 +141,13 @@
     
     public double getAggregatedDataValue( DataElement dataElement, DataElementCategoryOptionCombo optionCombo, Date startDate, Date endDate, OrganisationUnit organisationUnit )
     {
-        //String key = dataElement.getId() + SEPARATOR + optionCombo.getId() + "-" + startDate.toString() + "-" + endDate.toString() + "-" + organisationUnit.getId();
-        
-        /*Double value = aggregatedValueCache.get( key );
-        
-        if ( value != null )
-        {
-            return value.doubleValue();
-        }*/
-        
-        Double value = aggregationService.getAggregatedDataValue( dataElement, optionCombo, startDate, endDate, organisationUnit );
-        
-        /*if ( value != AggregationService.NO_VALUES_REGISTERED )
-        {
-            aggregatedValueCache.put( key, value );
-        }*/
-        
-        return value.doubleValue();
+        return aggregationService.getAggregatedDataValue( dataElement, optionCombo, startDate, endDate, organisationUnit );
+    }
+
+    public void clearCache()
+    {
+        hierarchyCache = null;
+        periodCache.clear();
+        periodIdCache.clear();
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/AbstractDataElementAggregation.java'
--- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/AbstractDataElementAggregation.java	2010-06-08 19:47:40 +0000
+++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/AbstractDataElementAggregation.java	2010-06-27 15:34:05 +0000
@@ -49,8 +49,6 @@
 
     protected final String TRUE = "true";
 
-    protected final String FALSE = "false";
-
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
@@ -122,11 +120,7 @@
         Collection<DataValue> dataValues = getDataValues( dataElementId, optionComboId, organisationUnitId,
             aggregationStartDate, aggregationEndDate );
 
-        double[] fraction = getAggregateOfValues( dataValues, aggregationStartDate, aggregationEndDate,
+        return getAggregateOfValues( dataValues, aggregationStartDate, aggregationEndDate,
             aggregationStartDate, aggregationEndDate );
-
-        double sums[] = { fraction[0], fraction[1] };
-
-        return sums;
     }    
 }

=== modified file 'dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/AverageIntDataElementAggregation.java'
--- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/AverageIntDataElementAggregation.java	2010-06-08 19:47:40 +0000
+++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/AverageIntDataElementAggregation.java	2010-06-27 15:34:05 +0000
@@ -54,16 +54,22 @@
 
         boolean valuesRegistered = false;
 
-        double[] sums = getSumAndRelevantDays( dataElement.getId(), optionCombo.getId(), aggregationStartDate, aggregationEndDate, 
-            organisationUnit.getId() );
-
-        if ( sums[1] > 0 )
+        OrganisationUnitHierarchy hierarchy = aggregationCache.getOrganisationUnitHierarchy();
+        
+        Collection<Integer> organisationUnitIds = hierarchy.getChildren( organisationUnit.getId() );
+        
+        for ( Integer id : organisationUnitIds )
         {
-            double average = sums[0] / sums[1];
-
-            totalSum += average;
-
-            valuesRegistered = true;
+            double[] sums = getSumAndRelevantDays( dataElement.getId(), optionCombo.getId(), aggregationStartDate, aggregationEndDate, id );
+    
+            if ( sums[1] > 0 )
+            {
+                double average = sums[0] / sums[1];
+    
+                totalSum += average;
+    
+                valuesRegistered = true;
+            }
         }
 
         if ( valuesRegistered )
@@ -89,13 +95,11 @@
      */
     protected Collection<DataValue> getDataValues( int dataElementId, int optionComboId, int organisationUnitId,
         Date startDate, Date endDate )
-    {
-        OrganisationUnitHierarchy hierarchy = aggregationCache.getOrganisationUnitHierarchy();
-        
+    {   
         Collection<Integer> periods = aggregationCache.getPeriodIds( startDate, endDate );
         
         Collection<DataValue> values = aggregationStore.getDataValues( organisationUnitId, dataElementId, optionComboId, periods );
-
+        
         return values;
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/SumBoolDataElementAggregation.java'
--- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/SumBoolDataElementAggregation.java	2010-06-08 19:47:40 +0000
+++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/SumBoolDataElementAggregation.java	2010-06-27 15:34:05 +0000
@@ -73,7 +73,7 @@
         Collection<Integer> periods = aggregationCache.getPeriodIds( startDate, endDate );
 
         Collection<DataValue> values = aggregationStore.getDataValues( hierarchy.getChildren( organisationUnitId ), dataElementId, optionComboId, periods );
-
+        
         return values;
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/resources/META-INF/dhis/beans.xml	2010-06-18 14:38:59 +0000
+++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/resources/META-INF/dhis/beans.xml	2010-06-27 15:34:05 +0000
@@ -25,6 +25,8 @@
       ref="org.hisp.dhis.aggregation.impl.dataelement.AverageBoolDataElementAggregation"/>
 	<property name="indicatorAggregation"
 	  ref="org.hisp.dhis.aggregation.impl.indicator.IndicatorAggregation"/>
+    <property name="aggregationCache"
+      ref="org.hisp.dhis.aggregation.impl.cache.AggregationCache"/>
   </bean>
   
   <bean id="org.hisp.dhis.aggregation.impl.cache.AggregationCache"

=== modified file 'dhis-2/dhis-services/dhis-service-aggregationengine-default/src/test/java/org/hisp/dhis/aggregation/AggregationServiceTest.java'
--- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/test/java/org/hisp/dhis/aggregation/AggregationServiceTest.java	2010-06-18 15:21:28 +0000
+++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/test/java/org/hisp/dhis/aggregation/AggregationServiceTest.java	2010-06-27 15:34:05 +0000
@@ -50,7 +50,6 @@
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.period.PeriodType;
-import org.hisp.dhis.period.QuarterlyPeriodType;
 import org.junit.Test;
 
 /**
@@ -64,7 +63,7 @@
     private final String F = "false";
     
     private AggregationService aggregationService;
-
+    
     private DataElementCategoryCombo categoryCombo;
     
     private DataElementCategoryOptionCombo categoryOptionCombo;
@@ -81,7 +80,6 @@
     private Period periodA;
     private Period periodB;
     private Period periodC;
-    private Period periodD;
     
     private OrganisationUnit unitA;
     private OrganisationUnit unitB;
@@ -161,17 +159,14 @@
         // ---------------------------------------------------------------------
         
         PeriodType monthly = new MonthlyPeriodType();
-        PeriodType quarterly = new QuarterlyPeriodType();
         
         periodA = createPeriod( monthly, mar01, mar31 );
         periodB = createPeriod( monthly, apr01, apr30 );
         periodC = createPeriod( monthly, may01, may31 );
-        periodD = createPeriod( quarterly, mar01, may31 );
         
         periodIds.add( periodService.addPeriod( periodA ) );
         periodIds.add( periodService.addPeriod( periodB ) );
         periodIds.add( periodService.addPeriod( periodC ) );
-        periodIds.add( periodService.addPeriod( periodD ) );
         
         // ---------------------------------------------------------------------
         // Setup OrganisationUnits
@@ -242,6 +237,8 @@
         dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitF, T, categoryOptionCombo ) );
         dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitG, T, categoryOptionCombo ) );
         dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitH, T, categoryOptionCombo ) );
+        
+        aggregationService.clearCache();
     }
 
     @Override
@@ -251,13 +248,56 @@
     }
 
     @Test
-    public void testSumIntDataElementDataMart()
-    {
-        dataElementA.setAggregationOperator( DataElement.AGGREGATION_OPERATOR_SUM );
-        
+    public void sumIntDataElement()
+    {
+        assertEquals( 90.0, aggregationService.getAggregatedDataValue( dataElementA, categoryOptionCombo, mar01, mar31, unitC ) );
+        assertEquals( 105.0, aggregationService.getAggregatedDataValue( dataElementA, categoryOptionCombo, mar01, mar31, unitF ) );
+        assertEquals( 150.0, aggregationService.getAggregatedDataValue( dataElementA, categoryOptionCombo, mar01, mar31, unitB ) );
+
+        assertEquals( 255.0, aggregationService.getAggregatedDataValue( dataElementA, categoryOptionCombo, mar01, may31, unitC ) );
+        assertEquals( 345.0, aggregationService.getAggregatedDataValue( dataElementA, categoryOptionCombo, mar01, may31, unitF ) );
+        assertEquals( 580.0, aggregationService.getAggregatedDataValue( dataElementA, categoryOptionCombo, mar01, may31, unitB ) );
+    }
+    
+    @Test
+    public void sumBoolDataElement()
+    {
+        assertEquals( 1.0, aggregationService.getAggregatedDataValue( dataElementB, categoryOptionCombo, mar01, mar31, unitC ) );
+        assertEquals( 2.0, aggregationService.getAggregatedDataValue( dataElementB, categoryOptionCombo, mar01, mar31, unitF ) );
+        assertEquals( 3.0, aggregationService.getAggregatedDataValue( dataElementB, categoryOptionCombo, mar01, mar31, unitB ) );
+
+        assertEquals( 2.0, aggregationService.getAggregatedDataValue( dataElementB, categoryOptionCombo, mar01, may31, unitC ) );
+        assertEquals( 7.0, aggregationService.getAggregatedDataValue( dataElementB, categoryOptionCombo, mar01, may31, unitF ) );
+        assertEquals( 10.0, aggregationService.getAggregatedDataValue( dataElementB, categoryOptionCombo, mar01, may31, unitB ) );
+    }
+    
+    @Test
+    public void averageIntDataElement()
+    {
+        dataElementA.setAggregationOperator( DataElement.AGGREGATION_OPERATOR_AVERAGE );
         dataElementService.updateDataElement( dataElementA );
         
         assertEquals( 90.0, aggregationService.getAggregatedDataValue( dataElementA, categoryOptionCombo, mar01, mar31, unitC ) );
         assertEquals( 105.0, aggregationService.getAggregatedDataValue( dataElementA, categoryOptionCombo, mar01, mar31, unitF ) );
+        assertEquals( 150.0, aggregationService.getAggregatedDataValue( dataElementA, categoryOptionCombo, mar01, mar31, unitB ) );
+
+        assertEquals( 85.2, aggregationService.getAggregatedDataValue( dataElementA, categoryOptionCombo, mar01, may31, unitC ), 0.1 );
+        assertEquals( 115.3, aggregationService.getAggregatedDataValue( dataElementA, categoryOptionCombo, mar01, may31, unitF ), 0.1 );
+        assertEquals( 193.3, aggregationService.getAggregatedDataValue( dataElementA, categoryOptionCombo, mar01, may31, unitB ), 0.1 );
+    }
+    
+    @Test
+    public void averageBoolDataElement()
+    {
+        dataElementB.setAggregationOperator( DataElement.AGGREGATION_OPERATOR_AVERAGE );
+        dataElementService.updateDataElement( dataElementB );
+        
+        assertEquals( 1.0, aggregationService.getAggregatedDataValue( dataElementB, categoryOptionCombo, mar01, mar31, unitC ) );
+        assertEquals( 0.67, aggregationService.getAggregatedDataValue( dataElementB, categoryOptionCombo, mar01, mar31, unitF ), 0.01 );
+        assertEquals( 0.6, aggregationService.getAggregatedDataValue( dataElementB, categoryOptionCombo, mar01, mar31, unitB ) );
+        
+        assertEquals( 0.66, aggregationService.getAggregatedDataValue( dataElementB, categoryOptionCombo, mar01, may31, unitC ), 0.01 );
+        assertEquals( 0.78, aggregationService.getAggregatedDataValue( dataElementB, categoryOptionCombo, mar01, may31, unitF ), 0.01 );
+        assertEquals( 0.67, aggregationService.getAggregatedDataValue( dataElementB, categoryOptionCombo, mar01, may31, unitB ), 0.01 );
     }
 }

=== added file 'dhis-2/dhis-services/dhis-service-aggregationengine-default/src/test/resources/AggregationServiceTest-Description.txt'
--- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/test/resources/AggregationServiceTest-Description.txt	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/test/resources/AggregationServiceTest-Description.txt	2010-06-27 15:34:05 +0000
@@ -0,0 +1,26 @@
+TEST DESCRIPTION AGGREGATIONSERVICETEST
+
+CAPITAL LETTERS ARE ORGANISATION UNITS
+DATA ELEMENT A IS INT, DATA ELEMENT B IS BOOLEAN
+VALUES ARE FOR PERIOD A, B, C
+
+Period A = March
+Period B = April
+Period C = May
+
+
+					A				I
+
+
+
+			B				C
+							90,70,95
+							T,T,F
+
+D			E			F	
+10,40,40	35,65,45	25,55,30
+T,F,T		F,T,F		T,T,T
+
+				G				H
+				20,20,50		60,15,70
+				F,F,T			T,T,T