← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12295: Event analytics, supporting fixed and relative periods step1

 

------------------------------------------------------------
revno: 12295
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2013-09-28 18:27:09 +0200
message:
  Event analytics, supporting fixed and relative periods step1
modified:
  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/event/EventQueryParams.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/EventQueryPlanner.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/PartitionUtils.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/EventAnalyticsController.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/data/DefaultQueryPlanner.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java	2013-08-31 18:07:17 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java	2013-09-28 16:27:09 +0000
@@ -66,7 +66,6 @@
 import org.hisp.dhis.dataelement.DataElementGroup;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
-import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.system.util.MathUtils;
 import org.hisp.dhis.system.util.PaginatedList;
@@ -397,7 +396,7 @@
         }
         else if ( params.getPeriods() != null && !params.getPeriods().isEmpty() )
         {
-            ListMap<String, NameableObject> periodTypePeriodMap = getPeriodTypePeriodMap( params.getPeriods() );
+            ListMap<String, NameableObject> periodTypePeriodMap = PartitionUtils.getPeriodTypePeriodMap( params.getPeriods() );
     
             for ( String periodType : periodTypePeriodMap.keySet() )
             {
@@ -411,7 +410,7 @@
         {
             DimensionalObject filter = params.getFilter( PERIOD_DIM_ID );
             
-            ListMap<String, NameableObject> periodTypePeriodMap = getPeriodTypePeriodMap( filter.getItems() );
+            ListMap<String, NameableObject> periodTypePeriodMap = PartitionUtils.getPeriodTypePeriodMap( filter.getItems() );
             
             params.removeFilter( PERIOD_DIM_ID ).setPeriodType( periodTypePeriodMap.keySet().iterator().next() ); // Using first period type
             
@@ -601,23 +600,6 @@
     // -------------------------------------------------------------------------
     
     /**
-     * Creates a mapping between period type name and period for the given periods.
-     */
-    private ListMap<String, NameableObject> getPeriodTypePeriodMap( Collection<NameableObject> periods )
-    {
-        ListMap<String, NameableObject> map = new ListMap<String, NameableObject>();
-        
-        for ( NameableObject period : periods )
-        {
-            String periodTypeName = ((Period) period).getPeriodType().getName();
-            
-            map.putValue( periodTypeName, period );
-        }
-        
-        return map;
-    }
-    
-    /**
      * Creates a mapping between level and organisation unit for the given organisation
      * units.
      */

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java	2013-09-28 15:06:20 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java	2013-09-28 16:27:09 +0000
@@ -40,6 +40,7 @@
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.common.NameableObjectUtils;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.period.Period;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramStage;
 
@@ -122,7 +123,7 @@
     public List<OrganisationUnit> getOrganisationUnits()
     {
         int index = dimensions.indexOf( new BaseDimensionalObject( DimensionalObject.ORGUNIT_DIM_ID ) );
-        return NameableObjectUtils.asTypedList( dimensions.get( index ).getItems() );
+        return index != -1 ? NameableObjectUtils.asTypedList( dimensions.get( index ).getItems(), OrganisationUnit.class ) : null;
     }
 
     public void setOrganisationUnits( List<OrganisationUnit> organisationUnits )
@@ -130,6 +131,22 @@
         setDimensionOptions( DimensionalObject.ORGUNIT_DIM_ID, DimensionType.ORGANISATIONUNIT, null, organisationUnits );
     }
     
+    public List<Period> getPeriods()
+    {
+        int index = dimensions.indexOf( new BaseDimensionalObject( DimensionalObject.PERIOD_DIM_ID ) );
+        return index != -1 ? NameableObjectUtils.asTypedList( dimensions.get( index ).getItems(), Period.class ) : null;
+    }
+    
+    public void setPeriods( List<Period> periods )
+    {
+        setDimensionOptions( DimensionalObject.PERIOD_DIM_ID, DimensionType.PERIOD, null, periods );
+    }
+    
+    public boolean hasStartEndDate()
+    {
+        return startDate != null && endDate != null;
+    }
+    
     private EventQueryParams setDimensionOptions( String dimension, DimensionType type, String dimensionName, List<? extends NameableObject> options )
     {
         int index = dimensions.indexOf( new BaseDimensionalObject( dimension ) );
@@ -183,6 +200,17 @@
         return ( getPageWithDefault() - 1 ) * getPageSizeWithDefault();
     }
     
+    public String toString()
+    {
+        return "[" +
+            "Program: " + program + ", " +
+            "Stage: " + programStage + ", " +
+            "Start date: " + startDate + ", " +
+            "End date: " + endDate + ", " +
+            "Items " + items + ", " +
+            "Dimensions " + dimensions + "]";
+    }
+    
     // -------------------------------------------------------------------------
     // Getters and setters
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java	2013-09-28 15:06:20 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java	2013-09-28 16:27:09 +0000
@@ -64,6 +64,7 @@
 import org.hisp.dhis.program.ProgramStageService;
 import org.hisp.dhis.system.grid.ListGrid;
 import org.hisp.dhis.system.util.DateUtils;
+import org.hisp.dhis.system.util.Timer;
 import org.springframework.beans.factory.annotation.Autowired;
 
 /**
@@ -116,7 +117,7 @@
     public Grid getAggregatedEventData( EventQueryParams params )
     {
         EventQueryPlanner.validate( params );
-        
+
         Grid grid = new ListGrid();
 
         // ---------------------------------------------------------------------
@@ -139,7 +140,7 @@
         //TODO relative periods
                 
         List<EventQueryParams> queries = EventQueryPlanner.planQuery( params );
-        
+
         for ( EventQueryParams query : queries )
         {
             analyticsManager.getAggregatedEventData( query, grid );
@@ -152,14 +153,14 @@
         Map<Object, Object> metaData = new HashMap<Object, Object>();        
         metaData.put( AnalyticsService.NAMES_META_KEY, getUidNameMap( params ) );
         grid.setMetaData( metaData );
-        
+
         return grid;        
     }
     
     public Grid getEvents( EventQueryParams params )
     {
         EventQueryPlanner.validate( params );
-        
+
         Grid grid = new ListGrid();
 
         // ---------------------------------------------------------------------
@@ -184,8 +185,12 @@
         // Data
         // ---------------------------------------------------------------------
 
+        Timer t = new Timer().start();
+        
         List<EventQueryParams> queries = EventQueryPlanner.planQuery( params );
         
+        t.getSplitTime( "Planned query, got: " + queries.size() );
+        
         int count = 0;
         
         for ( EventQueryParams query : queries )
@@ -198,6 +203,8 @@
             analyticsManager.getEvents( query, grid );
         }
         
+        t.getTime( "Queried events, got: " + grid.getHeight() );
+        
         // ---------------------------------------------------------------------
         // Meta-data
         // ---------------------------------------------------------------------
@@ -244,24 +251,17 @@
         Date start = null;
         Date end = null;
         
-        try
-        {
-            start = DateUtils.getMediumDate( startDate );
-            end = DateUtils.getMediumDate( endDate );
-        }
-        catch ( RuntimeException ex )
-        {
-            throw new IllegalQueryException( "Start date or end date is invalid: " + startDate + " - " + endDate );
-        }
-        
-        if ( start == null || end == null )
-        {
-            throw new IllegalQueryException( "Start date or end date is invalid: " + startDate + " - " + endDate );
-        }
-        
-        if ( start.after( end ) )
-        {
-            throw new IllegalQueryException( "Start date is after end date: " + startDate + " - " + endDate );
+        if ( startDate != null && endDate != null )
+        {
+            try
+            {
+                start = DateUtils.getMediumDate( startDate );
+                end = DateUtils.getMediumDate( endDate );
+            }
+            catch ( RuntimeException ex )
+            {
+                throw new IllegalQueryException( "Start date or end date is invalid: " + startDate + " - " + endDate );
+            }
         }
         
         for ( String it : dimension )
@@ -337,7 +337,7 @@
         {
             params.setPageSize( pageSize );
         }
-        
+
         return params;
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/EventQueryPlanner.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/EventQueryPlanner.java	2013-09-27 18:32:05 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/EventQueryPlanner.java	2013-09-28 16:27:09 +0000
@@ -65,10 +65,18 @@
             violation = "At least one organisation unit must be specified";
         }
         
-        if ( params.getStartDate() == null || params.getEndDate() == null )
+        if ( params.getPeriods() == null && ( params.getStartDate() == null || params.getEndDate() == null ) )
         {
             violation = "Start and end date or at least one period must be specified";
         }
+        
+        if ( params.getStartDate() != null && params.getEndDate() != null )
+        {
+            if ( params.getStartDate().after( params.getEndDate() ) )
+            {
+                throw new IllegalQueryException( "Start date is after end date: " + params.getStartDate() + " - " + params.getEndDate() );
+            }            
+        }
 
         if ( violation != null )
         {
@@ -98,32 +106,42 @@
         
         Program program = params.getProgram();
         
-        Date startDate = params.getStartDate();
-        Date endDate = params.getEndDate();
-        
-        Date currentStartDate = startDate;
-        Date currentEndDate = endDate;
-        
-        while ( true )
-        {
-            if ( year( currentStartDate ) < year( endDate ) ) // Spans multiple
-            {
-                // Set end date to max of current year
-                
-                currentEndDate = maxOfYear( currentStartDate ); 
-                
-                list.add( getQuery( params, currentStartDate, currentEndDate, program ) );
-                
-                // Set start date to start of next year
-                
-                currentStartDate = new Cal( ( year( currentStartDate ) + 1 ), 1, 1 ).time();                 
-            }
-            else
-            {
-                list.add( getQuery( params, currentStartDate, endDate, program ) );
-                
-                break;
-            }
+        if ( params.hasStartEndDate() )
+        {
+            Date startDate = params.getStartDate();
+            Date endDate = params.getEndDate();
+            
+            Date currentStartDate = startDate;
+            Date currentEndDate = endDate;
+            
+            while ( true )
+            {
+                if ( year( currentStartDate ) < year( endDate ) ) // Spans multiple
+                {
+                    // Set end date to max of current year
+                    
+                    currentEndDate = maxOfYear( currentStartDate ); 
+                    
+                    list.add( getQuery( params, currentStartDate, currentEndDate, program ) );
+                    
+                    // Set start date to start of next year
+                    
+                    currentStartDate = new Cal( ( year( currentStartDate ) + 1 ), 1, 1 ).time();                 
+                }
+                else
+                {
+                    list.add( getQuery( params, currentStartDate, endDate, program ) );
+                    
+                    break;
+                }
+            }
+        }
+        else
+        {
+            //TODO implement properly 
+            params.setTableName( TABLE_BASE_NAME + year( params.getPeriods().get( 0 ).getStartDate() ) + "_" + program.getUid() );
+            params.setPeriodType( params.getPeriods().get( 0 ).getPeriodType().getName() );
+            list.add( params );
         }
         
         return list;

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java	2013-09-28 15:06:20 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java	2013-09-28 16:27:09 +0000
@@ -219,11 +219,15 @@
         
         sql += "from " + params.getTableName() + " ";
         
-        if ( params.getStartDate() != null && params.getEndDate() != null )
+        if ( params.hasStartEndDate() )
         {        
             sql += "where executiondate >= '" + getMediumDateString( params.getStartDate() ) + "' ";
             sql += "and executiondate <= '" + getMediumDateString( params.getEndDate() ) + "' ";
         }
+        else // Periods
+        {
+            sql += "where " + params.getPeriodType() + " in (" + getQuotedCommaDelimitedString( getUids( params.getPeriods() ) ) + ") ";
+        }
         
         if ( params.isOrganisationUnitMode( EventQueryParams.OU_MODE_SELECTED ) )
         {
@@ -282,7 +286,7 @@
         {
             String[] split = filter.split( DataQueryParams.OPTION_SEP );
                         
-            return "(" + TextUtils.getQuotedCommaDelimitedString( Arrays.asList( split ) ) + ")";
+            return "(" + getQuotedCommaDelimitedString( Arrays.asList( split ) ) + ")";
         }
         
         return "'" + filter + "'";

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/PartitionUtils.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/PartitionUtils.java	2013-08-31 18:07:17 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/PartitionUtils.java	2013-09-28 16:27:09 +0000
@@ -29,6 +29,7 @@
  */
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
@@ -107,4 +108,21 @@
         
         return map;
     }
+
+    /**
+     * Creates a mapping between period type name and period for the given periods.
+     */
+    public static ListMap<String, NameableObject> getPeriodTypePeriodMap( Collection<NameableObject> periods )
+    {
+        ListMap<String, NameableObject> map = new ListMap<String, NameableObject>();
+        
+        for ( NameableObject period : periods )
+        {
+            String periodTypeName = ((Period) period).getPeriodType().getName();
+            
+            map.putValue( periodTypeName, period );
+        }
+        
+        return map;
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/EventAnalyticsController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/EventAnalyticsController.java	2013-09-28 15:06:20 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/EventAnalyticsController.java	2013-09-28 16:27:09 +0000
@@ -70,8 +70,8 @@
     public String getAggregateJson( // JSON, JSONP
         @PathVariable String program,
         @RequestParam(required=false) String stage,
-        @RequestParam String startDate,
-        @RequestParam String endDate,
+        @RequestParam(required=false) String startDate,
+        @RequestParam(required=false) String endDate,
         @RequestParam Set<String> dimension,
         @RequestParam(required=false) String ouMode,
         Model model,
@@ -90,8 +90,8 @@
     public void getAggregateXls(
         @PathVariable String program,
         @RequestParam(required=false) String stage,
-        @RequestParam String startDate,
-        @RequestParam String endDate,
+        @RequestParam(required=false) String startDate,
+        @RequestParam(required=false) String endDate,
         @RequestParam Set<String> dimension,
         @RequestParam(required=false) String ouMode,
         Model model,
@@ -112,8 +112,8 @@
     public String getQueryJson( // JSON, JSONP
         @PathVariable String program,
         @RequestParam(required=false) String stage,
-        @RequestParam String startDate,
-        @RequestParam String endDate,
+        @RequestParam(required=false) String startDate,
+        @RequestParam(required=false) String endDate,
         @RequestParam Set<String> dimension,
         @RequestParam(required=false) String ouMode,
         @RequestParam(required=false) Set<String> asc,
@@ -136,8 +136,8 @@
     public void getQueryXls(
         @PathVariable String program,
         @RequestParam(required=false) String stage,
-        @RequestParam String startDate,
-        @RequestParam String endDate,
+        @RequestParam(required=false) String startDate,
+        @RequestParam(required=false) String endDate,
         @RequestParam Set<String> dimension,
         @RequestParam(required=false) String ouMode,
         @RequestParam(required=false) Set<String> asc,