← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9373: Analytics, more dynamic code

 

------------------------------------------------------------
revno: 9373
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-12-19 16:51:21 +0100
message:
  Analytics, more dynamic code
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/data/DefaultQueryPlanner.java
  dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryPlannerTest.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	2012-12-19 14:35:23 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java	2012-12-19 15:51:21 +0000
@@ -48,19 +48,13 @@
     public static final String VALUE_ID = "value";
     
     private static final String LEVEL_PREFIX = "uidlevel";
-        
-    private List<String> indicators = new ArrayList<String>();
-    
-    private List<String> dataElements = new ArrayList<String>();
-    
-    private List<String> periods = new ArrayList<String>();
-    
-    private List<String> organisationUnits = new ArrayList<String>();
     
     private Map<String, List<String>> dimensions = new HashMap<String, List<String>>();
     
     private boolean categories = false;
 
+    private Map<String, List<String>> filters = new HashMap<String, List<String>>();
+    
     // -------------------------------------------------------------------------
     // Transient properties
     // -------------------------------------------------------------------------
@@ -79,25 +73,18 @@
     {
     }
     
-    public DataQueryParams( List<String> indicators, List<String> dataElements, List<String> periods,
-        List<String> organisationUnits, Map<String, List<String>> dimensions, boolean categories )
+    public DataQueryParams( Map<String, List<String>> dimensions, boolean categories, Map<String, List<String>> filters )
     {
-        this.indicators = indicators;
-        this.dataElements = dataElements;
-        this.periods = periods;
-        this.organisationUnits = organisationUnits;
         this.dimensions = dimensions;
         this.categories = categories;
+        this.filters = filters;
     }
     
     public DataQueryParams( DataQueryParams params )
     {
-        this.indicators = params.getIndicators();
-        this.dataElements = params.getDataElements();
-        this.periods = params.getPeriods();
-        this.organisationUnits = params.getOrganisationUnits();
-        this.dimensions = params.getDimensions();
+        this.dimensions = new HashMap<String, List<String>>( params.getDimensions() );
         this.categories = params.isCategories();
+        this.filters = new HashMap<String, List<String>>( params.getFilters() );
         
         this.tableName = params.getTableName();
         this.periodType = params.getPeriodType();
@@ -111,102 +98,59 @@
     public List<String> getDimensionNames()
     {
         List<String> list = new ArrayList<String>();
-        
-        if ( dataElements != null && !dataElements.isEmpty() )
-        {
-            list.add( DATAELEMENT_DIM_ID );
-        }
+
+        list.addAll( dimensions.keySet() );
         
         if ( categories )
         {
             list.add( CATEGORYOPTIONCOMBO_DIM_ID );
         }
         
-        if ( periods != null && !periods.isEmpty() )
-        {
-            list.add( periodType != null ? periodType : PERIOD_DIM_ID );
-        }
-        
-        if ( organisationUnits != null && !organisationUnits.isEmpty() )
-        {
-            list.add( organisationUnitLevel != 0 ? ( "uidlevel" + organisationUnitLevel ) : ORGUNIT_DIM_ID );
-        }
-        
-        list.addAll( dimensions.keySet() );
-        
+        if ( list.contains( PERIOD_DIM_ID ) && periodType != null )
+        {
+            list.set( list.indexOf( PERIOD_DIM_ID ), periodType );
+        }
+        
+        if ( list.contains( ORGUNIT_DIM_ID ) && organisationUnitLevel != 0 )
+        {
+            list.set( list.indexOf( ORGUNIT_DIM_ID ), LEVEL_PREFIX + organisationUnitLevel );
+        }
+                
         return list;
     }
-    
-    public List<String> getDynamicDimensionNames()
-    {
-        return new ArrayList<String>( dimensions.keySet() );
-    }
-    
+        
     public Map<String, List<String>> getDimensionMap()
     {
         Map<String, List<String>> map = new HashMap<String, List<String>>();
-        
-        map.put( DATAELEMENT_DIM_ID, dataElements );
-        map.put( periodType != null ? periodType : PERIOD_DIM_ID, periods );
-        map.put( organisationUnitLevel != 0 ? ( LEVEL_PREFIX + organisationUnitLevel ) : ORGUNIT_DIM_ID, organisationUnits );
+
         map.putAll( dimensions );
         
+        if ( periodType != null )
+        {
+            map.put( periodType, dimensions.get( PERIOD_DIM_ID ) );
+        }
+        
+        if ( organisationUnitLevel != 0 )
+        {
+            map.put( LEVEL_PREFIX + organisationUnitLevel, dimensions.get( ORGUNIT_DIM_ID ) );
+        }
+        
         return map;
     }
-        
+    
     public void setDimension( String dimension, List<String> values )
     {
-        if ( DATAELEMENT_DIM_ID.equals( dimension ) )
-        {
-            setDataElements( values );
-        }
-        else if ( PERIOD_DIM_ID.equals( dimension ) )
-        {
-            setPeriods( values );
-        }
-        else if ( ORGUNIT_DIM_ID.equals( dimension ) )
-        {
-            setOrganisationUnits( values );
-        }
-        else if ( dimensions.containsKey( dimension ) )
-        {
-            dimensions.put( dimension, values );
-        }
+        dimensions.put( dimension, values );
     }
-    
-    public List<String> getDimension( String dimension )
-    {
-        if ( DATAELEMENT_DIM_ID.equals( dimension ) )
-        {
-            return dataElements;
-        }
-        else if ( PERIOD_DIM_ID.equals( dimension ) )
-        {
-            return periods;
-        }
-        else if ( ORGUNIT_DIM_ID.equals( dimension ) )
-        {
-            return organisationUnits;
-        }
-        else if ( dimensions != null && dimensions.containsKey( dimension ) )
-        {
-            return dimensions.get( dimension );
-        }
         
-        throw new IllegalArgumentException( dimension );
-    }
-    
     @Override
     public int hashCode()
     {
         final int prime = 31;
         int result = 1;
         result = prime * result + ( categories ? 1231 : 1237);
-        result = prime * result + ( ( indicators == null ) ? 0 : indicators.hashCode() );
-        result = prime * result + ( ( dataElements == null ) ? 0 : dataElements.hashCode() );
-        result = prime * result + ( ( periods == null ) ? 0 : periods.hashCode() );
-        result = prime * result + ( ( organisationUnits == null ) ? 0 : organisationUnits.hashCode() );
         result = prime * result + ( ( dimensions == null ) ? 0 : dimensions.hashCode() );
+        result = prime * result + ( ( filters == null ) ? 0 : filters.hashCode() );
         return result;
     }
 
@@ -229,54 +173,6 @@
         }
         
         DataQueryParams other = (DataQueryParams) object;
-
-        if ( indicators == null )
-        {
-            if ( other.indicators != null )
-            {
-                return false;
-            }
-        }
-        else if ( !indicators.equals( other.indicators ) )
-        {
-            return false;
-        }
-        
-        if ( dataElements == null )
-        {
-            if ( other.dataElements != null )
-            {
-                return false;
-            }
-        }
-        else if ( !dataElements.equals( other.dataElements ) )
-        {
-            return false;
-        }
-
-        if ( periods == null )
-        {
-            if ( other.periods != null )
-            {
-                return false;
-            }
-        }
-        else if ( !periods.equals( other.periods ) )
-        {
-            return false;
-        }
-
-        if ( organisationUnits == null )
-        {
-            if ( other.organisationUnits != null )
-            {
-                return false;
-            }
-        }
-        else if ( !organisationUnits.equals( other.organisationUnits ) )
-        {
-            return false;
-        }
         
         if ( dimensions == null )
         {
@@ -294,6 +190,18 @@
         {
             return false;
         }
+
+        if ( filters == null )
+        {
+            if ( other.filters != null )
+            {
+                return false;
+            }
+        }
+        else if ( !filters.equals( other.filters ) )
+        {
+            return false;
+        }
         
         return true;
     }
@@ -301,58 +209,13 @@
     @Override
     public String toString()
     {
-        return "[in: " + indicators + ", de: " + dataElements + ", pe: " + periods
-            + ", ou: " + organisationUnits + ", categories: " + categories + "]";
+        return dimensions != null ? dimensions.toString() : "";
     }
         
     // -------------------------------------------------------------------------
-    // Get and set methods
+    // Get and set methods for serialize properties
     // -------------------------------------------------------------------------
   
-    @JsonProperty( value = INDICATOR_DIM_ID )
-    public List<String> getIndicators()
-    {
-        return indicators;
-    }
-
-    public void setIndicators( List<String> indicators )
-    {
-        this.indicators = indicators;
-    }
-
-    @JsonProperty( value = DATAELEMENT_DIM_ID )
-    public List<String> getDataElements()
-    {
-        return dataElements;
-    }
-
-    public void setDataElements( List<String> dataElements )
-    {
-        this.dataElements = dataElements;
-    }
-
-    @JsonProperty( value = PERIOD_DIM_ID )
-    public List<String> getPeriods()
-    {
-        return periods;
-    }
-
-    public void setPeriods( List<String> periods )
-    {
-        this.periods = periods;
-    }
-
-    @JsonProperty( value = ORGUNIT_DIM_ID )
-    public List<String> getOrganisationUnits()
-    {
-        return organisationUnits;
-    }
-
-    public void setOrganisationUnits( List<String> organisationUnits )
-    {
-        this.organisationUnits = organisationUnits;
-    }
-
     @JsonProperty( value = "dimensions" )
     public Map<String, List<String>> getDimensions()
     {
@@ -375,6 +238,21 @@
         this.categories = categories;
     }
 
+    @JsonProperty( value = "filters" )
+    public Map<String, List<String>> getFilters()
+    {
+        return filters;
+    }
+
+    public void setFilters( Map<String, List<String>> filters )
+    {
+        this.filters = filters;
+    }
+
+    // -------------------------------------------------------------------------
+    // Get and set methods for transient properties
+    // -------------------------------------------------------------------------
+  
     public String getTableName()
     {
         return tableName;
@@ -404,4 +282,38 @@
     {
         this.organisationUnitLevel = organisationUnitLevel;
     }
+    
+    // -------------------------------------------------------------------------
+    // Get and set helpers
+    // -------------------------------------------------------------------------
+  
+    public List<String> getDatElements()
+    {
+        return dimensions.get( DATAELEMENT_DIM_ID );
+    }
+    
+    public void setDataElements( List<String> dataElements )
+    {
+        dimensions.put( DATAELEMENT_DIM_ID, dataElements );
+    }
+    
+    public List<String> getPeriods()
+    {
+        return dimensions.get( PERIOD_DIM_ID );
+    }
+    
+    public void setPeriods( List<String> periods )
+    {
+        dimensions.put( PERIOD_DIM_ID, periods );
+    }
+
+    public List<String> getOrganisationUnits()
+    {
+        return dimensions.get( ORGUNIT_DIM_ID );
+    }
+    
+    public void setOrganisationUnits( List<String> organisationUnits )
+    {
+        this.dimensions.put( ORGUNIT_DIM_ID, organisationUnits );
+    }
 }

=== 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	2012-12-19 14:35:23 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java	2012-12-19 15:51:21 +0000
@@ -124,7 +124,7 @@
         
         for ( DataQueryParams query : queries )
         {
-            List<String> values = query.getDimension( dimension );
+            List<String> values = query.getDimensions().get( dimension );
 
             List<List<String>> valuePages = new PaginatedList<String>( values ).setNumberOfPages( pageNo ).getPages();
             
@@ -138,8 +138,7 @@
 
         return subQueries;
     }
-    
-    
+        
     /**
      * Groups the given query into sub queries based on its periods and which 
      * partition it should be executed against. Sets the partition table name on

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryPlannerTest.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryPlannerTest.java	2012-12-18 00:49:03 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryPlannerTest.java	2012-12-19 15:51:21 +0000
@@ -171,9 +171,9 @@
     }
     
     /**
-     * Splits on 3 data elements, then splits in 2 queries on periods for a total
-     * of 6 queries.
+     * Splits on 3 data elements.
      */
+    @Test
     public void planQueryD()
     {
         DataQueryParams params = new DataQueryParams();
@@ -181,9 +181,9 @@
         params.setOrganisationUnits( Arrays.asList( ouA.getUid() ) );
         params.setPeriods( Arrays.asList( "200001", "200002", "200003", "200004", "200005", "200006", "200007", "200008", "200009" ) );
         
-        List<DataQueryParams> queries = queryPlanner.planQuery( params, 6 );
+        List<DataQueryParams> queries = queryPlanner.planQuery( params, 4 );
         
-        assertEquals( 6, queries.size() );
+        assertEquals( 3, queries.size() );
         
         for ( DataQueryParams query : queries )
         {