← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10078: Analytics, simplification

 

------------------------------------------------------------
revno: 10078
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-03-08 14:39:50 +0100
message:
  Analytics, simplification
modified:
  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/Dimension.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.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-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	2013-03-01 13:02:32 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java	2013-03-08 13:39:50 +0000
@@ -94,9 +94,7 @@
     private transient String tableName;
 
     private transient String periodType;
-    
-    private transient int organisationUnitLevel;
-    
+        
     private transient PeriodType dataPeriodType;
     
     private transient boolean skipPartitioning;
@@ -104,11 +102,11 @@
     // -------------------------------------------------------------------------
     // Constructors
     // -------------------------------------------------------------------------
-  
+    
     public DataQueryParams()
     {
     }
-        
+    
     public DataQueryParams( DataQueryParams params )
     {
         this.dimensions = new ArrayList<Dimension>( params.getDimensions() );
@@ -118,7 +116,6 @@
         
         this.tableName = params.getTableName();
         this.periodType = params.getPeriodType();
-        this.organisationUnitLevel = params.getOrganisationUnitLevel();
         this.dataPeriodType = params.getDataPeriodType();
         this.skipPartitioning = params.isSkipPartitioning();
     }
@@ -126,7 +123,7 @@
     // -------------------------------------------------------------------------
     // Logic
     // -------------------------------------------------------------------------
-
+    
     /**
      * Ensures conformity for this query. The category option combo dimension
      * can only be present if the data element dimension exists and the indicator
@@ -265,39 +262,6 @@
     {
         return getInputDimensionNamesAsList().indexOf( PERIOD_DIM_ID );
     }
-        
-    /**
-     * Populates the dimension name property on all dimensions. Will set the 
-     * name of the current period type for this query on the period dimension
-     * and the a prefixed organisation unit level on the organisation unit
-     * dimension.
-     */
-    public void populateDimensionNames()
-    {
-        for ( Dimension dimension : dimensions )
-        {
-            if ( periodType != null && PERIOD_DIM_ID.equals( dimension.getDimension() ) )
-            {
-                dimension.setDimensionName( periodType );
-            }
-            else if ( organisationUnitLevel != 0 && ORGUNIT_DIM_ID.equals( dimension.getDimension() ) )
-            {
-                dimension.setDimensionName( LEVEL_PREFIX + organisationUnitLevel );
-            }
-        }
-
-        for ( Dimension filter : filters )
-        {
-            if ( periodType != null && PERIOD_DIM_ID.equals( filter.getDimension() ) )
-            {
-                filter.setDimensionName( periodType );
-            }
-            else if ( organisationUnitLevel != 0 && ORGUNIT_DIM_ID.equals( filter.getDimension() ) )
-            {
-                filter.setDimensionName( LEVEL_PREFIX + organisationUnitLevel );
-            }
-        }
-    }
     
     /**
      * Returns the dimensions which are part of dimensions and filters. If any
@@ -501,7 +465,7 @@
     }
 
     /**
-     * Retrieves the options for the given dimension.
+     * Retrieves the options for the given dimension identifier.
      */
     public List<IdentifiableObject> getDimensionOptions( String dimension )
     {
@@ -509,21 +473,31 @@
         
         return index != -1 ? dimensions.get( index ).getItems() : null;
     }
-
+    
+    /**
+     * Retrieves the dimension with the given dimension identifier.
+     */
+    public Dimension getDimension( String dimension )
+    {
+        int index = dimensions.indexOf( new Dimension( dimension ) );
+        
+        return index != -1 ? dimensions.get( index ) : null;
+    }
+    
     /**
      * Sets the options for the given dimension.
      */
-    public void setDimensionOptions( String dimension, DimensionType type, List<IdentifiableObject> options )
+    public void setDimensionOptions( String dimension, DimensionType type, String dimensionName, List<IdentifiableObject> options )
     {
         int index = dimensions.indexOf( new Dimension( dimension ) );
         
         if ( index != -1 )
         {
-            dimensions.set( index, new Dimension( dimension, type, options ) );
+            dimensions.set( index, new Dimension( dimension, type, dimensionName, options ) );
         }
         else
         {
-            dimensions.add( new Dimension( dimension, type, options ) );
+            dimensions.add( new Dimension( dimension, type, dimensionName, options ) );
         }
     }
     
@@ -540,17 +514,17 @@
     /**
      * Sets the options for the given filter.
      */
-    public void setFilterOptions( String filter, DimensionType type, List<IdentifiableObject> options )
+    public void setFilterOptions( String filter, DimensionType type, String dimensionName, List<IdentifiableObject> options )
     {
         int index = filters.indexOf( new Dimension( filter ) );
         
         if ( index != -1 )
         {
-            filters.set( index, new Dimension( filter, type, options ) );
+            filters.set( index, new Dimension( filter, type, dimensionName, options ) );
         }
         else
         {
-            filters.add( new Dimension( filter, type, options ) );
+            filters.add( new Dimension( filter, type, dimensionName, options ) );
         }
     }
     
@@ -777,16 +751,6 @@
         this.periodType = periodType;
     }
 
-    public int getOrganisationUnitLevel()
-    {
-        return organisationUnitLevel;
-    }
-
-    public void setOrganisationUnitLevel( int organisationUnitLevel )
-    {
-        this.organisationUnitLevel = organisationUnitLevel;
-    }
-
     public PeriodType getDataPeriodType()
     {
         return dataPeriodType;
@@ -816,15 +780,15 @@
         return getDimensionOptions( key ) != null ? getDimensionOptions( key ) : getFilterOptions( key );
     }
     
-    public void resetDimensionOrFilter( String key, List<IdentifiableObject> options )
+    public void resetDimensionOrFilter( Dimension dimension, List<IdentifiableObject> options )
     {
-        if ( getDimensionOptions( key ) != null )
+        if ( dimensions.contains( dimension ) )
         {
-            setDimensionOptions( key, null, options );
+            setDimensionOptions( dimension.getDimension(), dimension.getType(), dimension.getDimensionName(), options );
         }
-        else if ( getFilterOptions( key ) != null )
+        else if ( filters.contains( dimension ) )
         {
-            setFilterOptions( key, null, options );
+            setFilterOptions( dimension.getDimension(), dimension.getType(), dimension.getDimensionName(), options );
         }
     }
     
@@ -844,7 +808,7 @@
     
     public void setIndicators( List<IdentifiableObject> indicators )
     {
-        setDimensionOptions( INDICATOR_DIM_ID, DimensionType.INDICATOR, indicators );
+        setDimensionOptions( INDICATOR_DIM_ID, DimensionType.INDICATOR, null, indicators );
     }
     
     public List<IdentifiableObject> getDataElements()
@@ -854,7 +818,7 @@
     
     public void setDataElements( List<IdentifiableObject> dataElements )
     {
-        setDimensionOptions( DATAELEMENT_DIM_ID, DimensionType.DATAELEMENT, dataElements );
+        setDimensionOptions( DATAELEMENT_DIM_ID, DimensionType.DATAELEMENT, null, dataElements );
     }
     
     public List<IdentifiableObject> getDataSets()
@@ -864,7 +828,7 @@
     
     public void setDataSets( List<IdentifiableObject> dataSets )
     {
-        setDimensionOptions( DATASET_DIM_ID, DimensionType.DATASET, dataSets );
+        setDimensionOptions( DATASET_DIM_ID, DimensionType.DATASET, null, dataSets );
     }
     
     public List<IdentifiableObject> getPeriods()
@@ -874,7 +838,7 @@
     
     public void setPeriods( List<IdentifiableObject> periods )
     {
-        setDimensionOptions( PERIOD_DIM_ID, DimensionType.PERIOD, periods );
+        setDimensionOptions( PERIOD_DIM_ID, DimensionType.PERIOD, null, periods );
     }
 
     public List<IdentifiableObject> getOrganisationUnits()
@@ -884,7 +848,7 @@
     
     public void setOrganisationUnits( List<IdentifiableObject> organisationUnits )
     {
-        setDimensionOptions( ORGUNIT_DIM_ID, DimensionType.ORGANISATIONUNIT, organisationUnits );
+        setDimensionOptions( ORGUNIT_DIM_ID, DimensionType.ORGANISATIONUNIT, null, organisationUnits );
     }
     
     public List<Dimension> getDataElementGroupSets()
@@ -906,12 +870,12 @@
     
     public void setDataElementGroupSet( Dimension dimension, List<IdentifiableObject> dataElementGroups )
     {
-        setDimensionOptions( dimension.getDimension(), DimensionType.DATAELEMENT_GROUPSET, dataElementGroups );
+        setDimensionOptions( dimension.getDimension(), DimensionType.DATAELEMENT_GROUPSET, null, dataElementGroups );
     }
     
     public void enableCategoryOptionCombos()
     {
-        setDimensionOptions( CATEGORYOPTIONCOMBO_DIM_ID, DimensionType.CATEGORY_OPTION_COMBO, new ArrayList<IdentifiableObject>() );
+        setDimensionOptions( CATEGORYOPTIONCOMBO_DIM_ID, DimensionType.CATEGORY_OPTION_COMBO, null, new ArrayList<IdentifiableObject>() );
     }
     
     // -------------------------------------------------------------------------
@@ -925,7 +889,7 @@
     
     public void setFilterDataElements( List<IdentifiableObject> dataElements )
     {
-        setFilterOptions( DATAELEMENT_DIM_ID, DimensionType.DATAELEMENT, dataElements );
+        setFilterOptions( DATAELEMENT_DIM_ID, DimensionType.DATAELEMENT, null, dataElements );
     }
     
     public List<IdentifiableObject> getFilterPeriods()
@@ -935,7 +899,7 @@
     
     public void setFilterPeriods( List<IdentifiableObject> periods )
     {
-        setFilterOptions( PERIOD_DIM_ID, DimensionType.PERIOD, periods );
+        setFilterOptions( PERIOD_DIM_ID, DimensionType.PERIOD, null, periods );
     }
     
     public List<IdentifiableObject> getFilterOrganisationUnits()
@@ -945,6 +909,6 @@
     
     public void setFilterOrganisationUnits( List<IdentifiableObject> organisationUnits )
     {
-        setFilterOptions( ORGUNIT_DIM_ID, DimensionType.ORGANISATIONUNIT, organisationUnits );
+        setFilterOptions( ORGUNIT_DIM_ID, DimensionType.ORGANISATIONUNIT, null, organisationUnits );
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/Dimension.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/Dimension.java	2013-02-25 15:15:07 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/Dimension.java	2013-03-08 13:39:50 +0000
@@ -64,6 +64,14 @@
         this.items = items;
     }
 
+    public Dimension( String dimension, DimensionType type, String dimensionName, List<IdentifiableObject> items )
+    {
+        this.dimension = dimension;
+        this.type = type;
+        this.dimensionName = dimensionName;
+        this.items = items;
+    }
+
     // -------------------------------------------------------------------------
     // Logic
     // -------------------------------------------------------------------------
@@ -101,6 +109,9 @@
         this.type = type;
     }
 
+    /**
+     * Returns dimension name with fall back to dimension.
+     */
     public String getDimensionName()
     {
         return dimensionName != null ? dimensionName : dimension;

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java	2013-03-08 11:10:35 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java	2013-03-08 13:39:50 +0000
@@ -31,7 +31,7 @@
 import static org.hisp.dhis.analytics.AggregationType.AVERAGE_INT;
 import static org.hisp.dhis.analytics.AggregationType.AVERAGE_INT_DISAGGREGATION;
 import static org.hisp.dhis.analytics.AggregationType.SUM;
-import static org.hisp.dhis.analytics.DataQueryParams.CATEGORYOPTIONCOMBO_DIM_ID;
+import static org.hisp.dhis.analytics.DataQueryParams.*;
 import static org.hisp.dhis.analytics.DataQueryParams.INDICATOR_DIM_ID;
 import static org.hisp.dhis.analytics.DataQueryParams.MAX_DIM_OPT_PERM;
 import static org.hisp.dhis.dataelement.DataElement.AGGREGATION_OPERATOR_AVERAGE;
@@ -47,6 +47,7 @@
 import org.hisp.dhis.analytics.AggregationType;
 import org.hisp.dhis.analytics.DataQueryParams;
 import org.hisp.dhis.analytics.Dimension;
+import org.hisp.dhis.analytics.DimensionType;
 import org.hisp.dhis.analytics.IllegalQueryException;
 import org.hisp.dhis.analytics.QueryPlanner;
 import org.hisp.dhis.analytics.table.PartitionUtils;
@@ -146,15 +147,15 @@
         for ( DataQueryParams byPartition : groupedByPartition )
         {
             List<DataQueryParams> groupedByOrgUnitLevel = groupByOrgUnitLevel( byPartition );
-            
+
             for ( DataQueryParams byOrgUnitLevel : groupedByOrgUnitLevel )
             {
                 List<DataQueryParams> groupedByPeriodType = groupByPeriodType( byOrgUnitLevel );
-                
+
                 for ( DataQueryParams byPeriodType : groupedByPeriodType )
                 {
                     List<DataQueryParams> groupedByAggregationType = groupByAggregationType( byPeriodType );
-                    
+
                     for ( DataQueryParams byAggregationType : groupedByAggregationType )
                     {
                         if ( AVERAGE_INT_DISAGGREGATION.equals( byAggregationType.getAggregationType() ) )
@@ -164,7 +165,6 @@
                             for ( DataQueryParams byDataPeriodType : groupedByDataPeriodType )
                             {
                                 byDataPeriodType.setTableName( byPartition.getTableName() );
-                                byDataPeriodType.setOrganisationUnitLevel( byOrgUnitLevel.getOrganisationUnitLevel() );
                                 byDataPeriodType.setPeriodType( byPeriodType.getPeriodType() );
                                 byDataPeriodType.setAggregationType( byAggregationType.getAggregationType() );
                                 
@@ -174,7 +174,6 @@
                         else
                         {
                             byAggregationType.setTableName( byPartition.getTableName() );
-                            byAggregationType.setOrganisationUnitLevel( byOrgUnitLevel.getOrganisationUnitLevel() );
                             byAggregationType.setPeriodType( byPeriodType.getPeriodType() );
                             
                             queries.add( byAggregationType );
@@ -205,7 +204,7 @@
         // ---------------------------------------------------------------------
         
         queries = splitByDimensionOrFilter( queries, DataQueryParams.ORGUNIT_DIM_ID, optimalQueries );
-        
+
         return queries;
     }
         
@@ -236,13 +235,15 @@
                 subQueries.add( new DataQueryParams( query ) );
                 continue;
             }
+
+            Dimension dim = query.getDimension( dimension );
             
             List<List<IdentifiableObject>> valuePages = new PaginatedList<IdentifiableObject>( values ).setNumberOfPages( optimalForSubQuery ).getPages();
             
             for ( List<IdentifiableObject> valuePage : valuePages )
             {
                 DataQueryParams subQuery = new DataQueryParams( query );
-                subQuery.resetDimensionOrFilter( dimension, valuePage );
+                subQuery.resetDimensionOrFilter( dim, valuePage );
                 subQueries.add( subQuery );
             }
         }
@@ -276,7 +277,7 @@
                 queries.add( query );            
             }
         }
-        else if ( params.getFilterPeriods() != null && !params.getFilterPeriods().isEmpty() )
+        else if ( params.getFilterPeriods() != null && !params.getFilterPeriods().isEmpty() ) //TODO fix
         {
             ListMap<String, IdentifiableObject> tablePeriodMap = PartitionUtils.getTablePeriodMap( params.getFilterPeriods(), tableName );
             
@@ -315,7 +316,7 @@
             for ( String periodType : periodTypePeriodMap.keySet() )
             {
                 DataQueryParams query = new DataQueryParams( params );
-                query.setPeriods( periodTypePeriodMap.get( periodType ) );
+                query.setDimensionOptions( PERIOD_DIM_ID, DimensionType.PERIOD, periodType, periodTypePeriodMap.get( periodType ) );
                 query.setPeriodType( periodType );
                 queries.add( query );
             }
@@ -327,7 +328,7 @@
             for ( String periodType : periodTypePeriodMap.keySet() )
             {
                 DataQueryParams query = new DataQueryParams( params );
-                query.setFilterPeriods( periodTypePeriodMap.get( periodType ) );
+                query.setFilterOptions( PERIOD_DIM_ID, DimensionType.PERIOD, periodType, periodTypePeriodMap.get( periodType ) );
                 query.setPeriodType( periodType );
                 queries.add( query );
             }
@@ -355,8 +356,7 @@
             for ( Integer level : levelOrgUnitMap.keySet() )
             {
                 DataQueryParams query = new DataQueryParams( params );
-                query.setOrganisationUnits( levelOrgUnitMap.get( level ) );
-                query.setOrganisationUnitLevel( level );
+                query.setDimensionOptions( ORGUNIT_DIM_ID, DimensionType.ORGANISATIONUNIT, LEVEL_PREFIX + level, levelOrgUnitMap.get( level ) );
                 queries.add( query );
             }
         }
@@ -367,8 +367,7 @@
             for ( Integer level : levelOrgUnitMap.keySet() )
             {
                 DataQueryParams query = new DataQueryParams( params );
-                query.setFilterOrganisationUnits( levelOrgUnitMap.get( level ) );
-                query.setOrganisationUnitLevel( level );
+                query.setFilterOptions( ORGUNIT_DIM_ID, DimensionType.ORGANISATIONUNIT, LEVEL_PREFIX + level, levelOrgUnitMap.get( level ) );
                 queries.add( query );
             }
         }
@@ -393,13 +392,6 @@
      * aggregation period type. Average bool means that the data elements have the
      * average aggregation operator and the bool value type.
      * 
-     * The query is grouped on data elements if any exists, then on data element
-     * group sets if any exists. In the case where multiple data element group
-     * sets exists, the query will be grouped on the data element groups of the
-     * first group set found. A constraint for data element groups is that they
-     * must contain data elements with equal aggregation type. Hence it is not
-     * meaningful to split on multiple data element group sets.
-     * 
      * If the aggregation type is already set/overridden in the request, the
      * query will be returned unchanged. If there are no dimension items specified
      * the aggregation type will fall back to sum.

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java	2013-02-26 18:33:01 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java	2013-03-08 13:39:50 +0000
@@ -92,9 +92,7 @@
     {
         ListMap<IdentifiableObject, IdentifiableObject> dataPeriodAggregationPeriodMap = params.getDataPeriodAggregationPeriodMap();
         params.replaceAggregationPeriodsWithDataPeriods( dataPeriodAggregationPeriodMap );
-                
-        params.populateDimensionNames();
-
+        
         List<Dimension> dimensions = params.getQueryDimensions();
 
         SqlHelper sqlHelper = new SqlHelper();
@@ -119,7 +117,7 @@
         {
             sql += "sum(value)";
         }
-                
+        
         sql += " as value from " + params.getTableName() + " ";
         
         for ( Dimension dim : dimensions )