← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2952: Improved performance of completeness report

 

------------------------------------------------------------
revno: 2952
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2011-03-01 18:14:27 +0100
message:
  Improved performance of completeness report
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessResult.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultCompleteDataSetRegistrationService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableCreator.java
  dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/impl/DefaultDataMartService.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/AbstractDataSetCompletenessService.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/RegistrationDataSetCompletenessService.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/jdbc/JDBCDataSetCompletenessStore.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/DerbyStatementBuilder.java
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/H2StatementBuilder.java
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/MySQLStatementBuilder.java
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/PostgreSQLStatementBuilder.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/completeness/DataSetCompletenessResult.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessResult.java	2011-02-03 17:03:02 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessResult.java	2011-03-01 17:14:27 +0000
@@ -197,7 +197,7 @@
     @Override
     public String toString()
     {
-        String toString = "[" + name + " - " + registrations + " / " + sources + "]";
+        String toString = "[Name: " + name + ", Reg: " + registrations + ", On time: " + registrationsOnTime + ", Src: " + sources + "]";
         
         return toString;
     }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessStore.java	2011-02-03 20:42:05 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessStore.java	2011-03-01 17:14:27 +0000
@@ -42,6 +42,10 @@
 {
     String ID = DataSetCompletenessStore.class.getName();
     
+    int getCompleteDataSetRegistrations( DataSet dataSet, Period period, Collection<? extends Source> sources );
+    
+    int getCompleteDataSetRegistrations( DataSet dataSet, Period period, Collection<? extends Source> sources, Date deadline );
+    
     /**
      * Gets the percentage value for the datasetcompleteness with the given parameters.
      * 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultCompleteDataSetRegistrationService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultCompleteDataSetRegistrationService.java	2010-04-12 21:23:33 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultCompleteDataSetRegistrationService.java	2011-03-01 17:14:27 +0000
@@ -80,6 +80,7 @@
     }    
 
     @SuppressWarnings( "unchecked" )
+    @Deprecated
     public int getCompleteDataSetRegistrationsForDataSet( DataSet dataSet, Collection<? extends Source> sources, Period period )
     {
         final Collection<? extends Source> intersectingSources = CollectionUtils.intersection( sources, dataSet.getSources() );
@@ -93,6 +94,7 @@
     }
     
     @SuppressWarnings( "unchecked" )
+    @Deprecated
     public int getCompleteDataSetRegistrationsForDataSet( DataSet dataSet, Collection<? extends Source> sources, Period period, Date deadline )
     {
         final Collection<? extends Source> intersectingSources = CollectionUtils.intersection( sources, dataSet.getSources() );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableCreator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableCreator.java	2010-10-24 15:41:13 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableCreator.java	2011-03-01 17:14:27 +0000
@@ -101,7 +101,9 @@
 
         try
         {
-            jdbcTemplate.execute( statementBuilder.getCreateDataValueIndex() );
+            final String sql = "CREATE INDEX crosstab ON datavalue ( periodid, sourceid )";
+            
+            jdbcTemplate.execute( sql );
             
             log.info( "Created index crosstab on table datavalue" );
         }

=== modified file 'dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/impl/DefaultDataMartService.java'
--- dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/impl/DefaultDataMartService.java	2011-01-21 12:44:09 +0000
+++ dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/impl/DefaultDataMartService.java	2011-03-01 17:14:27 +0000
@@ -31,6 +31,7 @@
 
 import java.util.Collection;
 import java.util.HashSet;
+import java.util.List;
 
 import org.hisp.dhis.common.GenericIdentifiableObjectStore;
 import org.hisp.dhis.dataelement.DataElement;
@@ -42,7 +43,9 @@
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.period.RelativePeriods;
+import org.hisp.dhis.system.filter.PastAndCurrentPeriodFilter;
 import org.hisp.dhis.system.process.OutputHolderState;
+import org.hisp.dhis.system.util.FilterUtils;
 import org.springframework.transaction.annotation.Transactional;
 
 /**
@@ -111,12 +114,7 @@
     public int export( Collection<Integer> dataElementIds, Collection<Integer> indicatorIds,
         Collection<Integer> periodIds, Collection<Integer> organisationUnitIds, RelativePeriods relatives )
     {
-        if ( relatives != null )
-        {
-            periodIds.addAll( getIdentifiers( Period.class, periodService.reloadPeriods( relatives.getRelativePeriods( 1, null, false ) ) ) );
-        }
-        
-        return dataMartEngine.export( dataElementIds, indicatorIds, periodIds, organisationUnitIds, false, new OutputHolderState() );
+        return export( dataElementIds, indicatorIds, periodIds, organisationUnitIds, relatives, false );
     }
 
     @Transactional
@@ -125,7 +123,11 @@
     {
         if ( relatives != null )
         {
-            periodIds.addAll( getIdentifiers( Period.class, periodService.reloadPeriods( relatives.getRelativePeriods( 1, null, false ) ) ) );
+            List<Period> periods = relatives.getRelativePeriods( 1, null, false );
+            
+            FilterUtils.filter( periods, new PastAndCurrentPeriodFilter() );
+            
+            periodIds.addAll( getIdentifiers( Period.class, periodService.reloadPeriods( periods ) ) );
         }
         
         return dataMartEngine.export( dataElementIds, indicatorIds, periodIds, organisationUnitIds, useIndexes, new OutputHolderState() );

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/AbstractDataSetCompletenessService.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/AbstractDataSetCompletenessService.java	2011-02-18 20:20:07 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/AbstractDataSetCompletenessService.java	2011-03-01 17:14:27 +0000
@@ -54,7 +54,9 @@
 import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.period.RelativePeriods;
 import org.hisp.dhis.source.Source;
+import org.hisp.dhis.system.filter.PastAndCurrentPeriodFilter;
 import org.hisp.dhis.system.util.ConversionUtils;
+import org.hisp.dhis.system.util.FilterUtils;
 import org.springframework.transaction.annotation.Transactional;
 
 /**
@@ -144,6 +146,8 @@
         {
             List<Period> periods = relatives.getRelativePeriods( 1, null, false );
             
+            FilterUtils.filter( periods, new PastAndCurrentPeriodFilter() );
+            
             Collection<Integer> periodIds = ConversionUtils.getIdentifiers( Period.class, periodService.reloadPeriods( periods ) );
             
             exportDataSetCompleteness( dataSetIds, periodIds, organisationUnitIds );
@@ -273,7 +277,7 @@
             children = organisationUnitService.getOrganisationUnitWithChildren( unit.getId() );
             
             final DataSetCompletenessResult result = new DataSetCompletenessResult();
-            
+
             result.setSources( getSources( dataSet, children ) );
             
             if ( result.getSources() > 0 )
@@ -281,7 +285,7 @@
                 result.setName( unit.getName() );
                 result.setRegistrations( getRegistrations( dataSet, children, period ) );
                 result.setRegistrationsOnTime( deadline != null ? getRegistrationsOnTime( dataSet, children, period, deadline ) : 0 );
-                            
+                
                 result.setDataSetId( dataSetId );
                 result.setPeriodId( periodId );
                 result.setOrganisationUnitId( unit.getId() );

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/RegistrationDataSetCompletenessService.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/RegistrationDataSetCompletenessService.java	2010-08-27 07:07:57 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/RegistrationDataSetCompletenessService.java	2011-03-01 17:14:27 +0000
@@ -30,7 +30,6 @@
 import java.util.Collection;
 import java.util.Date;
 
-import org.hisp.dhis.dataset.CompleteDataSetRegistrationService;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.source.Source;
@@ -41,21 +40,14 @@
 public class RegistrationDataSetCompletenessService
     extends AbstractDataSetCompletenessService
 {
-    private CompleteDataSetRegistrationService registrationService;
-
-    public void setRegistrationService( CompleteDataSetRegistrationService registrationService )
-    {
-        this.registrationService = registrationService;
-    }
-
     public int getRegistrations( DataSet dataSet, Collection<? extends Source> children, Period period )
     {
-        return registrationService.getCompleteDataSetRegistrationsForDataSet( dataSet, children, period );
+        return completenessStore.getCompleteDataSetRegistrations( dataSet, period, children );
     }
 
     public int getRegistrationsOnTime( DataSet dataSet, Collection<? extends Source> children, Period period, Date deadline )
     {
-        return registrationService.getCompleteDataSetRegistrationsForDataSet( dataSet, children, period, deadline );
+        return completenessStore.getCompleteDataSetRegistrations( dataSet, period, children, deadline );
     }
     
     public int getSources( DataSet dataSet, Collection<? extends Source> children )

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/jdbc/JDBCDataSetCompletenessStore.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/jdbc/JDBCDataSetCompletenessStore.java	2011-02-03 20:42:05 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/jdbc/JDBCDataSetCompletenessStore.java	2011-03-01 17:14:27 +0000
@@ -28,15 +28,19 @@
  */
 
 import static org.hisp.dhis.system.util.TextUtils.getCommaDelimitedString;
+import static org.hisp.dhis.system.util.DateUtils.getMediumDateString;
+import static org.hisp.dhis.system.util.ConversionUtils.getIdentifiers;
 
 import java.util.Collection;
 import java.util.Date;
 
 import org.amplecode.quick.StatementManager;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.completeness.DataSetCompletenessStore;
 import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.source.Source;
 import org.hisp.dhis.system.util.ConversionUtils;
@@ -67,6 +71,47 @@
     // DataSetCompletenessStore
     // -------------------------------------------------------------------------
 
+    @SuppressWarnings("unchecked")
+    public int getCompleteDataSetRegistrations( DataSet dataSet, Period period, Collection<? extends Source> sources )
+    {
+        final Collection<? extends Source> intersectingSources = CollectionUtils.intersection( sources, dataSet.getSources() );
+        
+        if ( intersectingSources == null || intersectingSources.size() == 0 )
+        {
+            return 0;
+        }        
+        
+        final String sql =
+            "SELECT COUNT(*) " +
+            "FROM completedatasetregistration " +
+            "WHERE datasetid = " + dataSet.getId() + " " +
+            "AND periodid = " + period.getId() + " " +
+            "AND sourceid IN ( " + getCommaDelimitedString( getIdentifiers( OrganisationUnit.class, intersectingSources ) ) + " )";
+        
+        return statementManager.getHolder().queryForInteger( sql );
+    }
+
+    @SuppressWarnings("unchecked")
+    public int getCompleteDataSetRegistrations( DataSet dataSet, Period period, Collection<? extends Source> sources, Date deadline )
+    {
+        final Collection<? extends Source> intersectingSources = CollectionUtils.intersection( sources, dataSet.getSources() );
+        
+        if ( intersectingSources == null || intersectingSources.size() == 0 )
+        {
+            return 0;
+        }        
+        
+        final String sql =
+            "SELECT COUNT(*) " +
+            "FROM completedatasetregistration " +
+            "WHERE datasetid = " + dataSet.getId() + " " +
+            "AND periodid = " + period.getId() + " " +
+            "AND sourceid IN ( " + getCommaDelimitedString( getIdentifiers( OrganisationUnit.class, intersectingSources ) ) + " ) " +
+            "AND date <= '" + getMediumDateString( deadline ) + "'";
+        
+        return statementManager.getHolder().queryForInteger( sql );
+    }
+    
     public double getPercentage( int dataSetId, int periodId, int organisationUnitId )
     {
         final String sql =

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml	2011-02-23 21:08:31 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml	2011-03-01 17:14:27 +0000
@@ -160,9 +160,7 @@
   
   <bean id="registrationDataCompletenessService"
 	class="org.hisp.dhis.completeness.impl.RegistrationDataSetCompletenessService" 
-	parent="compulsoryDataCompletenessService">	
-    <property name="registrationService"
-      ref="org.hisp.dhis.dataset.CompleteDataSetRegistrationService"/>
+	parent="compulsoryDataCompletenessService">
   </bean>
   
   <bean id="org.hisp.dhis.completeness.DataSetCompletenessStore"
@@ -288,6 +286,8 @@
     </aop:aspect>
       
     <aop:aspect ref="statementInterceptor">
+      <aop:around pointcut="execution( * org.hisp.dhis.completeness.DataSetCompletenessService.exportDataSetCompleteness(..) )" method="intercept"/>
+      <aop:around pointcut="execution( * org.hisp.dhis.completeness.DataSetCompletenessService.getDataSetCompleteness(..) )" method="intercept"/>
       <aop:around pointcut="execution( * org.hisp.dhis.chart.ChartService.getJFreeChart(..) )" method="intercept"/>
       <aop:around pointcut="execution( * org.hisp.dhis.datasetreport.DataSetReportService.getAggregatedValueMap(..) )" method="intercept"/>
     </aop:aspect>

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java	2011-01-16 07:12:01 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java	2011-03-01 17:14:27 +0000
@@ -79,12 +79,6 @@
     String getCreateDataSetCompletenessTable();
     
     /**
-     * Creates a create index statement for the datavalue table.
-     * @return a create index statement for the datavalue table.
-     */
-    String getCreateDataValueIndex();
-    
-    /**
      * Creates a delete datavalue statement.
      * @return a delete datavalue statement.
      */

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/DerbyStatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/DerbyStatementBuilder.java	2011-02-14 08:56:35 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/DerbyStatementBuilder.java	2011-03-01 17:14:27 +0000
@@ -108,13 +108,6 @@
             "valueOnTime DOUBLE );";
     }
 
-    public String getCreateDataValueIndex()
-    {
-        return
-            "CREATE INDEX crosstab " +
-            "ON datavalue ( periodid, sourceid );";
-    }
-
     public String getDeleteZeroDataValues()
     {
         return 

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/H2StatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/H2StatementBuilder.java	2011-02-14 08:56:35 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/H2StatementBuilder.java	2011-03-01 17:14:27 +0000
@@ -109,13 +109,6 @@
             "valueOnTime DOUBLE );";
     }
     
-    public String getCreateDataValueIndex()
-    {
-        return
-            "CREATE INDEX crosstab " +
-            "ON datavalue ( periodid, sourceid );";
-    }
-
     public String getDeleteZeroDataValues()
     {
         return

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/MySQLStatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/MySQLStatementBuilder.java	2011-02-14 08:56:35 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/MySQLStatementBuilder.java	2011-03-01 17:14:27 +0000
@@ -110,13 +110,6 @@
             "valueOnTime DOUBLE );";
     }
 
-    public String getCreateDataValueIndex()
-    {
-        return
-            "CREATE INDEX crosstab " +
-            "ON datavalue ( periodid, sourceid );";
-    }
-
     public String getDeleteZeroDataValues()
     {
         return

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/PostgreSQLStatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/PostgreSQLStatementBuilder.java	2011-02-14 08:56:35 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/PostgreSQLStatementBuilder.java	2011-03-01 17:14:27 +0000
@@ -108,13 +108,6 @@
             "valueOnTime DOUBLE PRECISION );";
     }
 
-    public String getCreateDataValueIndex()
-    {
-        return
-            "CREATE INDEX crosstab " +
-            "ON datavalue ( periodid, sourceid );";
-    }
-    
     public String getDeleteZeroDataValues()
     {
         return