← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9319: Impl analytics controller

 

------------------------------------------------------------
revno: 9319
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2012-12-14 13:37:06 +0100
message:
  Impl analytics controller
added:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java
  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/data/DefaultAnalyticsService.java
  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/table/JdbcAnalyticsTableManager.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.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/Grid.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2012-10-18 19:13:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2012-12-14 12:37:06 +0000
@@ -29,6 +29,7 @@
 
 import java.sql.ResultSet;
 import java.util.List;
+import java.util.Map;
 
 import net.sf.jasperreports.engine.JRDataSource;
 
@@ -74,6 +75,11 @@
     List<GridHeader> getHeaders();
     
     /**
+     * Returns map of meta-data.
+     */
+    Map<String, String> getMetaData();
+    
+    /**
      * Returns all visible headers, ie. headers which are not hidden.
      */
     List<GridHeader> getVisibleHeaders();
@@ -118,6 +124,14 @@
     Grid addValue( Object value );
     
     /**
+     * Adds values in the given array to the end of the current row in the 
+     * specified order.
+     * 
+     * @param values the values to add.
+     */
+    Grid addValues( Object[] values );
+    
+    /**
      * Adds a number of empty values to the Grid at the current row.
      * 
      * @param number the number of values to add.

=== 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-11 20:46:36 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsManager.java	2012-12-14 12:37:06 +0000
@@ -27,12 +27,12 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.List;
+import java.util.Map;
 import java.util.concurrent.Future;
 
-import org.hisp.dhis.aggregation.AggregatedDataValue;
-
 public interface AnalyticsManager
 {
-    Future<List<AggregatedDataValue>> getAggregatedDataValueTotals(  DataQueryParams params );
+    static final String SEP = "-";
+    
+    Future<Map<String, Double>> getAggregatedDataValueTotals(  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-11 20:46:36 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsService.java	2012-12-14 12:37:06 +0000
@@ -27,11 +27,13 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.List;
+import java.util.Map;
 
-import org.hisp.dhis.aggregation.AggregatedDataValue;
+import org.hisp.dhis.common.Grid;
 
 public interface AnalyticsService
 {
-    List<AggregatedDataValue> getAggregatedDataValueTotals( DataQueryParams params ) throws Exception;
+    Grid getAggregatedDataValueTotals( 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/data/DefaultAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2012-12-13 20:45:26 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2012-12-14 12:37:06 +0000
@@ -28,13 +28,17 @@
  */
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.Future;
 
-import org.hisp.dhis.aggregation.AggregatedDataValue;
 import org.hisp.dhis.analytics.AnalyticsManager;
 import org.hisp.dhis.analytics.AnalyticsService;
 import org.hisp.dhis.analytics.DataQueryParams;
+import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.common.GridHeader;
+import org.hisp.dhis.system.grid.ListGrid;
 import org.hisp.dhis.system.util.Timer;
 import org.springframework.beans.factory.annotation.Autowired;
 
@@ -49,35 +53,57 @@
     @Autowired
     private AnalyticsManager analyticsManager;
     
-    public List<AggregatedDataValue> getAggregatedDataValueTotals( DataQueryParams params ) throws Exception
+    public Grid getAggregatedDataValueTotals( DataQueryParams params ) throws Exception
+    {
+        Map<String, Double> map = getAggregatedDataValueMap( params );
+        
+        Grid grid = new ListGrid();
+        
+        grid.addHeader( new GridHeader( "Data element", "de", String.class.getName(), false, true ) );
+        grid.addHeader( new GridHeader( "Category option combo", "coc", String.class.getName(), false, true ) );
+        grid.addHeader( new GridHeader( "Period", "de", String.class.getName(), false, true ) );
+        grid.addHeader( new GridHeader( "Organisation unit", "ou", String.class.getName(), false, true ) );
+        grid.addHeader( new GridHeader( "Value", "value", Double.class.getName(), false, false ) );
+        
+        for ( Map.Entry<String, Double> entry : map.entrySet() )
+        {
+            grid.addRow();
+            grid.addValues( entry.getKey().split( AnalyticsManager.SEP ) );
+            grid.addValue( entry.getValue() );
+        }
+        
+        return grid;
+    }
+    
+    public Map<String, Double> getAggregatedDataValueMap( DataQueryParams params ) throws Exception
     {
         Timer t = new Timer().start();
-        
+
         List<DataQueryParams> queries = QueryPlanner.planQuery( params, 6 );
         
         t.getTime( "Planned query" );
         
-        List<Future<List<AggregatedDataValue>>> futures = new ArrayList<Future<List<AggregatedDataValue>>>();
+        List<Future<Map<String, Double>>> futures = new ArrayList<Future<Map<String, Double>>>();
         
-        List<AggregatedDataValue> values = new ArrayList<AggregatedDataValue>();
+        Map<String, Double> map = new HashMap<String, Double>();
         
         for ( DataQueryParams query : queries )
         {
             futures.add( analyticsManager.getAggregatedDataValueTotals( query ) );
         }
         
-        for ( Future<List<AggregatedDataValue>> future : futures )
+        for ( Future<Map<String, Double>> future : futures )
         {
-            List<AggregatedDataValue> taskValues = future.get();
+            Map<String, Double> taskValues = future.get();
             
             if ( taskValues != null )
             {
-                values.addAll( taskValues );
+                map.putAll( taskValues );
             }
         }
         
         t.getTime( "Got aggregated values" );
         
-        return values;
+        return map;
     }
 }

=== 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-13 20:45:26 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java	2012-12-14 12:37:06 +0000
@@ -29,21 +29,21 @@
 
 import static org.hisp.dhis.system.util.TextUtils.getQuotedCommaDelimitedString;
 
-import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.Future;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.hisp.dhis.aggregation.AggregatedDataValue;
 import org.hisp.dhis.analytics.AnalyticsManager;
 import org.hisp.dhis.analytics.DataQueryParams;
 import org.hisp.dhis.expression.ExpressionService;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.period.PeriodType;
-import org.hisp.dhis.system.objectmapper.AggregatedDataValueRowMapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.support.rowset.SqlRowSet;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.scheduling.annotation.AsyncResult;
 
@@ -76,24 +76,40 @@
     // -------------------------------------------------------------------------
 
     @Async
-    public Future<List<AggregatedDataValue>> getAggregatedDataValueTotals( DataQueryParams params )
+    public Future<Map<String, Double>> getAggregatedDataValueTotals( DataQueryParams params )
     {
         int level = organisationUnitService.getLevelOfOrganisationUnit( params.getOrganisationUnits().iterator().next() );
         
         String periodType = PeriodType.getPeriodTypeFromIsoString( params.getPeriods().iterator().next() ).getName().toLowerCase();
         
         final String sql = 
-            "SELECT dataelementid, 0 as categoryoptioncomboid, " + periodType + " as periodid, idlevel" + level + " as organisationunitid, SUM(value) as value " +
+            "SELECT dataelement, 0 as categoryoptioncombo, " + 
+            periodType + " as period, uidlevel" + level + " as organisationunit, SUM(value) as value " +
             "FROM " + params.getTableName() + " " +
-            "WHERE dataelementid IN ( " + getQuotedCommaDelimitedString( params.getDataElements() ) + " ) " +
+            "WHERE dataelement IN ( " + getQuotedCommaDelimitedString( params.getDataElements() ) + " ) " +
             "AND " + periodType + " IN ( " + getQuotedCommaDelimitedString( params.getPeriods() ) + " ) " +
-            "AND idlevel" + level + " IN ( " + getQuotedCommaDelimitedString( params.getOrganisationUnits() ) + " ) " +
-            "GROUP BY dataelementid, periodid, idlevel" + level;
-                
+            "AND uidlevel" + level + " IN ( " + getQuotedCommaDelimitedString( params.getOrganisationUnits() ) + " ) " +
+            "GROUP BY dataelement, " + periodType + ", uidlevel" + level;
+
         log.info( sql );
         
-        List<AggregatedDataValue> values = jdbcTemplate.query( sql, new AggregatedDataValueRowMapper() );
-        
-        return new AsyncResult<List<AggregatedDataValue>>( values );
+        SqlRowSet rowSet = jdbcTemplate.queryForRowSet( sql );
+        
+        Map<String, Double> map = new HashMap<String, Double>();
+        
+        while ( rowSet.next() )
+        {
+            String key = 
+                rowSet.getString( "dataelement" ) + SEP +
+                rowSet.getString( "categoryoptioncombo" ) + SEP +
+                rowSet.getString( "period" ) + SEP +
+                rowSet.getString( "organisationunit" );
+            
+            Double value = rowSet.getDouble( "value" );
+            
+            map.put( key, value );
+        }
+        
+        return new AsyncResult<Map<String, Double>>( map );
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java	2012-12-13 20:45:26 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java	2012-12-14 12:37:06 +0000
@@ -198,7 +198,7 @@
      * 
      * 0 = column name
      * 1 = data type
-     * 2 = column alias and value
+     * 2 = column alias and name
      */
     public List<String[]> getDimensionColumns()
     {

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2012-10-18 19:13:26 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2012-12-14 12:37:06 +0000
@@ -75,6 +75,11 @@
      * A List which represents the column headers of the grid.
      */
     private List<GridHeader> headers;
+    
+    /**
+     * A Map which can hold arbitrary meta-data.
+     */
+    private Map<String, String> metaData;
 
     /**
      * A two dimensional List which simulates a grid where the first list
@@ -103,6 +108,7 @@
     public ListGrid()
     {
         headers = new ArrayList<GridHeader>();
+        metaData = new HashMap<String, String>();
         grid = new ArrayList<List<Object>>();
     }
 
@@ -211,6 +217,13 @@
         return (grid != null && grid.size() > 0) ? grid.get( 0 ).size() : 0;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class } )
+    public Map<String, String> getMetaData()
+    {
+        return metaData;
+    }
+
     public int getVisibleWidth()
     {
         verifyGridState();
@@ -234,6 +247,18 @@
         return this;
     }
     
+    public Grid addValues( Object[] values )
+    {
+        List<Object> row = grid.get( currentRowWriteIndex );
+        
+        for ( Object value : values )
+        {
+            row.add( value );
+        }
+        
+        return this;
+    }
+    
     public Grid addEmptyValues( int number )
     {
         for ( int i = 0; i < number; i++ )

=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java	2012-12-14 12:37:06 +0000
@@ -0,0 +1,71 @@
+package org.hisp.dhis.api.controller;
+
+/*
+ * Copyright (c) 2004-2012, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.io.InputStream;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.hisp.dhis.analytics.AnalyticsService;
+import org.hisp.dhis.analytics.DataQueryParams;
+import org.hisp.dhis.api.utils.ContextUtils;
+import org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
+import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.dxf2.utils.JacksonUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+@Controller
+@RequestMapping( "/analytics" )
+public class AnalyticsController
+{
+    @Autowired
+    private AnalyticsService analyticsService;
+    
+    @Autowired
+    private ContextUtils contextUtils;
+    
+    @RequestMapping( method = RequestMethod.GET, consumes = { "application/json" }, produces = { "application/json" } )
+    public String get( InputStream in,
+        Model model,
+        HttpServletResponse response ) throws Exception
+    {
+        DataQueryParams params = JacksonUtils.fromJson( in, DataQueryParams.class );
+        
+        Grid grid = analyticsService.getAggregatedDataValueTotals( params );
+        
+        model.addAttribute( "model", grid );
+        model.addAttribute( "viewClass", "detailed" );        
+        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.NO_CACHE ); //TODO
+        
+        return "grid";
+    }
+}