← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9354: Analytics, support for categories param

 

------------------------------------------------------------
revno: 9354
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2012-12-18 17:01:44 +0100
message:
  Analytics, support for categories param
modified:
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsManager.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsService.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/data/DefaultAnalyticsService.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.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/AnalyticsManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsManager.java	2012-12-14 12:37:06 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsManager.java	2012-12-18 16:01:44 +0000
@@ -34,5 +34,5 @@
 {
     static final String SEP = "-";
     
-    Future<Map<String, Double>> getAggregatedDataValueTotals(  DataQueryParams params );
+    Future<Map<String, Double>> getAggregatedDataValues(  DataQueryParams params );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsService.java	2012-12-14 12:37:06 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsService.java	2012-12-18 16:01:44 +0000
@@ -33,7 +33,7 @@
 
 public interface AnalyticsService
 {
-    Grid getAggregatedDataValueTotals( DataQueryParams params ) throws Exception;
+    Grid getAggregatedDataValues( DataQueryParams params ) throws Exception;
     
     Map<String, Double> getAggregatedDataValueMap( DataQueryParams params ) throws Exception;
 }

=== 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-18 00:49:03 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java	2012-12-18 16:01:44 +0000
@@ -111,7 +111,12 @@
         List<String> list = new ArrayList<String>();
         
         list.add( DATAELEMENT_DIM_ID );
-        list.add( CATEGORYOPTIONCOMBO_DIM_ID );
+        
+        if ( categories )
+        {
+            list.add( CATEGORYOPTIONCOMBO_DIM_ID );
+        }
+        
         list.add( PERIOD_DIM_ID );
         list.add( ORGUNIT_DIM_ID );
         list.addAll( dimensions.keySet() );

=== 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	2012-12-18 00:49:03 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2012-12-18 16:01:44 +0000
@@ -50,14 +50,12 @@
 {
     private static final String VALUE_HEADER_NAME = "Value";
     
-    //TODO period aggregation for multiple period types
-    //TODO hierarchy aggregation for org units at multiple levels
     //TODO indicator aggregation
     //TODO category sub-totals and totals
     //TODO use data mart when query can be satisfied
     //TODO create data mart for average, less-than yearly data elements
     
-    //NOTE split on high-cardinality columns like data element and org unit, not period, improves performance
+    //TODO investigate whether quarterly partitions are faster
     
     @Autowired
     private AnalyticsManager analyticsManager;
@@ -65,10 +63,8 @@
     @Autowired
     private QueryPlanner queryPlanner;
     
-    public Grid getAggregatedDataValueTotals( DataQueryParams params ) throws Exception
+    public Grid getAggregatedDataValues( DataQueryParams params ) throws Exception
     {
-        Map<String, Double> map = getAggregatedDataValueMap( params );
-        
         Grid grid = new ListGrid();
         
         for ( String col : params.getDimensionNames() )
@@ -77,6 +73,8 @@
         }
         
         grid.addHeader( new GridHeader( DataQueryParams.VALUE_ID, VALUE_HEADER_NAME, Double.class.getName(), false, false ) );
+
+        Map<String, Double> map = getAggregatedDataValueMap( params );
         
         for ( Map.Entry<String, Double> entry : map.entrySet() )
         {
@@ -104,7 +102,7 @@
         
         for ( DataQueryParams query : queries )
         {
-            futures.add( analyticsManager.getAggregatedDataValueTotals( query ) );
+            futures.add( analyticsManager.getAggregatedDataValues( query ) );
         }
         
         for ( Future<Map<String, Double>> future : futures )

=== 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	2012-12-18 00:49:03 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java	2012-12-18 16:01:44 +0000
@@ -27,8 +27,10 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.analytics.DataQueryParams.*;
 import static org.hisp.dhis.system.util.TextUtils.getCommaDelimitedString;
 import static org.hisp.dhis.system.util.TextUtils.getQuotedCommaDelimitedString;
+import static org.hisp.dhis.system.util.TextUtils.getString;
 
 import java.util.HashMap;
 import java.util.List;
@@ -67,7 +69,7 @@
     //TODO optimize when all options in dimensions are selected
     
     @Async
-    public Future<Map<String, Double>> getAggregatedDataValueTotals( DataQueryParams params )
+    public Future<Map<String, Double>> getAggregatedDataValues( DataQueryParams params )
     {
         int level = params.getOrganisationUnitLevel();
         String periodType = params.getPeriodType();
@@ -75,21 +77,21 @@
         List<String> extraDimensions = params.getDynamicDimensionNames();
         
         String sql = 
-            "select " + dimensions.get( 0 ) + ", " + 
-            dimensions.get( 1 ) + ", " +
-            periodType + " as " + dimensions.get( 2 ) + ", " + 
-            "uidlevel" + level + " as " + dimensions.get( 3 ) + ", " +
+            "select " + DATAELEMENT_DIM_ID + ", " + 
+            getString( CATEGORYOPTIONCOMBO_DIM_ID + ", ", !params.isCategories() ) +
+            periodType + " as " + PERIOD_DIM_ID + ", " + 
+            "uidlevel" + level + " as " + ORGUNIT_DIM_ID + ", " +
             getCommaDelimitedString( extraDimensions, false, true ) +
             "sum(value) as value " +
             
             "from " + params.getTableName() + " " +
-            "where " + dimensions.get( 0 ) + " in ( " + getQuotedCommaDelimitedString( params.getDataElements() ) + " ) " +
+            "where " + DATAELEMENT_DIM_ID + " in ( " + getQuotedCommaDelimitedString( params.getDataElements() ) + " ) " +
             "and " + periodType + " in ( " + getQuotedCommaDelimitedString( params.getPeriods() ) + " ) " +
             "and uidlevel" + level + " in ( " + getQuotedCommaDelimitedString( params.getOrganisationUnits() ) + " ) " +
             getExtraDimensionQuery( params ) +
         
-            "group by " + dimensions.get( 0 ) + ", " + 
-            dimensions.get( 1 ) + ", " +
+            "group by " + DATAELEMENT_DIM_ID + ", " + 
+            getString( CATEGORYOPTIONCOMBO_DIM_ID + ", ", !params.isCategories() ) +
             periodType + ", " + 
             "uidlevel" + level +
             getCommaDelimitedString( extraDimensions, true, false );
@@ -111,7 +113,7 @@
             
             key.deleteCharAt( key.length() - SEP.length() );
             
-            Double value = rowSet.getDouble( DataQueryParams.VALUE_ID );
+            Double value = rowSet.getDouble( VALUE_ID );
 
             map.put( key.toString(), value );
         }

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java	2012-12-14 15:04:48 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java	2012-12-18 16:01:44 +0000
@@ -37,6 +37,7 @@
 public class TextUtils
 {
     public static final TextUtils INSTANCE = new TextUtils();
+    public static final String EMPTY = "";
     
     private static final Pattern LINK_PATTERN = Pattern.compile( "((http://|https://|www\\.).+?)($|\\n|\\r|\\r\\n| )" );
     private static final String DELIMITER = ", ";
@@ -123,7 +124,7 @@
         
         if ( beginIndex >= string.length()  )
         {
-            return "";
+            return EMPTY;
         }
         
         if ( endIndex > string.length() )
@@ -146,11 +147,24 @@
     {
         if ( value == null || length > value.length() )
         {
-            return "";
+            return EMPTY;
         }
         
         return value.substring( 0, value.length() - length );
     }
+    
+    /**
+     * Returns an empty string if the given argument is true, the string 
+     * otherwise. This is a convenience method.
+     * 
+     * @param string the string.
+     * @param emptyString whether to return an empty string.
+     * @return a string.
+     */
+    public static String getString( String string, boolean emptyString )
+    {
+        return emptyString ? EMPTY : string;
+    }
 
     /**
      * Transforms a collection of Integers into a comma delimited String. If the
@@ -234,11 +248,11 @@
     }
     
     /**
-     * Returns null if the given string is not null and contains no charachters,
-     * the string itselft otherwise.
+     * Returns null if the given string is not null and contains no characters,
+     * the string itself otherwise.
      * 
      * @param string the string.
-     * @return null if the given string is not null and contains no charachters,
+     * @return null if the given string is not null and contains no characters,
      *         the string itself otherwise.
      */
     public static String nullIfEmpty( String string )

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java	2012-12-14 15:04:48 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java	2012-12-18 16:01:44 +0000
@@ -62,8 +62,8 @@
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.NO_CACHE ); //TODO
         
         DataQueryParams params = JacksonUtils.fromJson( in, DataQueryParams.class );
-        
-        Grid grid = analyticsService.getAggregatedDataValueTotals( params );
+
+        Grid grid = analyticsService.getAggregatedDataValues( params );
         
         model.addAttribute( "model", grid );
         model.addAttribute( "viewClass", "detailed" );        
@@ -80,7 +80,7 @@
         
         DataQueryParams params = JacksonUtils.fromJson( in, DataQueryParams.class );
         
-        Grid grid = analyticsService.getAggregatedDataValueTotals( params );
+        Grid grid = analyticsService.getAggregatedDataValues( params );
 
         GridUtils.toXml( grid, response.getOutputStream() );
     }