← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13921: Analytics, supportffor org unit group counts in indicators, WIP

 

------------------------------------------------------------
revno: 13921
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-02-04 10:24:06 +0200
message:
  Analytics, supportffor org unit group counts in indicators, WIP
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DimensionItem.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java
  dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.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/common/DimensionalObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObject.java	2013-10-03 12:17:41 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObject.java	2014-02-04 08:24:06 +0000
@@ -46,6 +46,7 @@
     final String CATEGORYOPTIONCOMBO_DIM_ID = "co";
     final String PERIOD_DIM_ID = "pe";
     final String ORGUNIT_DIM_ID = "ou";
+    final String ORGUNIT_GROUP_DIM_ID = "oug"; // Used for org unit target
     final String ITEM_DIM_ID = "item";
     
     final String DIMENSION_SEP = "-";

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java	2014-01-31 18:08:43 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java	2014-02-04 08:24:06 +0000
@@ -177,12 +177,21 @@
     Set<DataElement> getDataElementsInExpression( String expression );
     
     /**
+     * Returns all OrganisationUnitGroups in the numerator and denominator
+     * expressions in the given Indicators.
+     * 
+     * @param indicators the set of indicators.
+     * @return a Set of OrganisationUnitGroups.
+     */
+    Set<OrganisationUnitGroup> getOrganisationUnitGroupsInIndicators( Collection<Indicator> indicators );
+    
+    /**
      * Returns all OrganisationUnitGroups in the given expression string.
      * 
      * @param expression the expression string.
      * @return a Set of OrganisationUnitGroups included in the expression string.
      */
-    Set<OrganisationUnitGroup> getOrganisationUnitGroupsInExpresion( String expression );
+    Set<OrganisationUnitGroup> getOrganisationUnitGroupsInExpression( String expression );
     
     /**
      * Returns all CategoryOptionCombos in the given expression string.

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java	2014-01-07 15:52:34 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java	2014-02-04 08:24:06 +0000
@@ -343,6 +343,35 @@
         
         return indexes;
     }
+    
+    /**
+     * Removes all dimensions which are not of the given type from dimensions
+     * and filters.
+     */
+    public DataQueryParams pruneToDimensionType( DimensionType type )
+    {
+        Iterator<DimensionalObject> dimensionIter = dimensions.iterator();
+        
+        while ( dimensionIter.hasNext() )
+        {
+            if ( !dimensionIter.next().getType().equals( type ) )
+            {
+                dimensionIter.remove();
+            }
+        }
+        
+        Iterator<DimensionalObject> filterIter = filters.iterator();
+        
+        while ( filterIter.hasNext() )
+        {
+            if ( !filterIter.next().getType().equals( type ) )
+            {
+                filterIter.remove();
+            }
+        }
+        
+        return this;
+    }
 
     /**
      * Removes the dimension with the given identifier.

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DimensionItem.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DimensionItem.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DimensionItem.java	2014-02-04 08:24:06 +0000
@@ -127,8 +127,8 @@
 
     /**
      * Returns the period dimension item object from the given list of
-     * dimension items. If no items are given, items are null or there are 
-     * no period dimension, null is returned.
+     * dimension items. If no items are given, items are null or there are no 
+     * period dimension, null is returned.
      */
     public static NameableObject getPeriodItem( List<DimensionItem> items )
     {

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2014-01-31 18:08:43 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2014-02-04 08:24:06 +0000
@@ -226,7 +226,11 @@
             List<Indicator> indicators = asTypedList( params.getIndicators() );
             
             expressionService.explodeExpressions( indicators );
-            
+
+            // -----------------------------------------------------------------
+            // Get indicator values
+            // -----------------------------------------------------------------
+
             DataQueryParams dataSourceParams = params.instance();
             dataSourceParams.removeDimension( DATAELEMENT_DIM_ID );
             dataSourceParams.removeDimension( DATASET_DIM_ID );
@@ -257,7 +261,7 @@
                     }
                     
                     Period period = filterPeriod != null ? filterPeriod : (Period) DimensionItem.getPeriodItem( options );
-
+                    
                     int days = daysBetween( period.getStartDate(), period.getEndDate() );
                     
                     Double value = expressionService.getIndicatorValue( indicator, period, valueMap, constantMap, null, days ); //TODO oug
@@ -478,6 +482,26 @@
     }
     
     /**
+     * Get a mapping between xx and count of organisation units for the given
+     * indicators.
+     */
+    private Map<String, Double> getOrgUnitTargetMap( DataQueryParams params, Collection<Indicator> indicators )
+    {
+        Set<OrganisationUnitGroup> orgUnitGroups = expressionService.getOrganisationUnitGroupsInIndicators( indicators );
+        
+        if ( orgUnitGroups == null || orgUnitGroups.isEmpty() )
+        {
+            return null;
+        }
+        
+        DataQueryParams orgUnitTargetParams = params.instance().pruneToDimensionType( DimensionType.ORGANISATIONUNIT );
+        orgUnitTargetParams.getDimensions().add( new BaseDimensionalObject( DimensionalObject.ORGUNIT_GROUP_DIM_ID, null, new ArrayList<NameableObject>( orgUnitGroups ) ) );
+        orgUnitTargetParams.setSkipPartitioning( true );
+        
+        return getAggregatedOrganisationUnitTargetMap( orgUnitTargetParams );
+    }
+    
+    /**
      * Generates a mapping where the key represents the dimensional item identifiers
      * concatenated by "-" and the value is the corresponding aggregated data value
      * based on the given grid.

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java	2014-02-04 08:24:06 +0000
@@ -40,16 +40,20 @@
 import java.util.List;
 import java.util.Map;
 
+import org.hisp.dhis.DhisConvenienceTest;
 import org.hisp.dhis.common.BaseDimensionalObject;
 import org.hisp.dhis.common.DimensionType;
+import org.hisp.dhis.common.DimensionalObject;
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.period.Period;
+import org.hisp.dhis.system.util.ListUtils;
 import org.junit.Test;
 
 /**
  * @author Lars Helge Overland
  */
 public class DataQueryParamsTest
+    extends DhisConvenienceTest
 {
     @Test
     public void testGetDimensionFromParam()
@@ -105,4 +109,25 @@
         
         assertTrue( params.hasPeriods() );
     }
+
+    @Test
+    public void testPruneToDimensionType()
+    {
+        DataQueryParams params = new DataQueryParams();
+        params.getDimensions().add( new BaseDimensionalObject( DimensionalObject.INDICATOR_DIM_ID, DimensionType.INDICATOR, null, null, 
+            ListUtils.getList( createIndicator( 'A', null ), createIndicator( 'B', null ) ) ) );
+        params.getDimensions().add( new BaseDimensionalObject( DimensionalObject.ORGUNIT_DIM_ID, DimensionType.ORGANISATIONUNIT, null, null,
+            ListUtils.getList( createOrganisationUnit( 'A' ), createOrganisationUnit( 'B' ) ) ) );
+        params.getFilters().add( new BaseDimensionalObject( DimensionalObject.PERIOD_DIM_ID, DimensionType.PERIOD, null, null,
+            ListUtils.getList( createPeriod( "201201" ), createPeriod( "201202" ) ) ) );
+
+        assertEquals( 2, params.getDimensions().size() );
+        assertEquals( 1, params.getFilters().size() );
+        
+        params.pruneToDimensionType( DimensionType.ORGANISATIONUNIT );
+        
+        assertEquals( 1, params.getDimensions().size() );
+        assertEquals( DimensionType.ORGANISATIONUNIT, params.getDimensions().get( 0 ).getType() );
+        assertEquals( 0, params.getFilters().size() );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java	2014-01-31 18:08:43 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java	2014-02-04 08:24:06 +0000
@@ -236,7 +236,25 @@
         return dataElementsInExpression;
     }
     
-    public Set<OrganisationUnitGroup> getOrganisationUnitGroupsInExpresion( String expression )
+    public Set<OrganisationUnitGroup> getOrganisationUnitGroupsInIndicators( Collection<Indicator> indicators )
+    {
+        Set<OrganisationUnitGroup> groups = null;
+        
+        if ( indicators != null )
+        {
+            groups = new HashSet<OrganisationUnitGroup>();
+            
+            for ( Indicator indicator : indicators )
+            {
+                groups.addAll( getOrganisationUnitGroupsInExpression( indicator.getNumerator() ) );
+                groups.addAll( getOrganisationUnitGroupsInExpression( indicator.getDenominator() ) );
+            }
+        }
+        
+        return groups;
+    }
+    
+    public Set<OrganisationUnitGroup> getOrganisationUnitGroupsInExpression( String expression )
     {
         Set<OrganisationUnitGroup> groupsInExpression = null;