← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14992: Adhoc, added sample random data value command

 

------------------------------------------------------------
revno: 14992
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2014-04-25 16:00:28 +0200
message:
  Adhoc, added sample random data value command
added:
  tools/dhis-adhoc/src/main/java/org/hisp/dhis/adhoc/command/RandomDataPopulator.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java
  tools/dhis-adhoc/pom.xml
  tools/dhis-adhoc/src/main/resources/META-INF/dhis/beans.xml


--
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/dataelement/DataElement.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java	2014-04-25 11:22:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java	2014-04-25 14:00:28 +0000
@@ -83,18 +83,18 @@
      */
     private static final long serialVersionUID = -7131541880444446669L;
 
+    public static final String VALUE_TYPE_INT = "int";
     public static final String VALUE_TYPE_STRING = "string";
-    public static final String VALUE_TYPE_INT = "int";
-    public static final String VALUE_TYPE_NUMBER = "number";
     public static final String VALUE_TYPE_USER_NAME = "username";
     public static final String VALUE_TYPE_BOOL = "bool";
     public static final String VALUE_TYPE_TRUE_ONLY = "trueOnly";
     public static final String VALUE_TYPE_DATE = "date";
     public static final String VALUE_TYPE_UNIT_INTERVAL = "unitInterval";
 
-    public static final String VALUE_TYPE_ZERO_OR_POSITIVE_INT = "zeroPositiveInt";
+    public static final String VALUE_TYPE_NUMBER = "number";
     public static final String VALUE_TYPE_POSITIVE_INT = "posInt";
     public static final String VALUE_TYPE_NEGATIVE_INT = "negInt";
+    public static final String VALUE_TYPE_ZERO_OR_POSITIVE_INT = "zeroPositiveInt";
     public static final String VALUE_TYPE_TEXT = "text";
     public static final String VALUE_TYPE_LONG_TEXT = "longText";
 

=== modified file 'tools/dhis-adhoc/pom.xml'
--- tools/dhis-adhoc/pom.xml	2014-02-10 10:37:03 +0000
+++ tools/dhis-adhoc/pom.xml	2014-04-25 14:00:28 +0000
@@ -6,7 +6,7 @@
   <parent>
     <groupId>org.hisp.dhis</groupId>
     <artifactId>dhis-services</artifactId>
-    <version>2.14-SNAPSHOT</version>
+    <version>2.15-SNAPSHOT</version>
   </parent>
   
   <artifactId>dhis-adhoc</artifactId>

=== added file 'tools/dhis-adhoc/src/main/java/org/hisp/dhis/adhoc/command/RandomDataPopulator.java'
--- tools/dhis-adhoc/src/main/java/org/hisp/dhis/adhoc/command/RandomDataPopulator.java	1970-01-01 00:00:00 +0000
+++ tools/dhis-adhoc/src/main/java/org/hisp/dhis/adhoc/command/RandomDataPopulator.java	2014-04-25 14:00:28 +0000
@@ -0,0 +1,173 @@
+package org.hisp.dhis.adhoc.command;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+import org.amplecode.quick.BatchHandler;
+import org.amplecode.quick.BatchHandlerFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.adhoc.Command;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
+import org.hisp.dhis.dataelement.DataElementCategoryService;
+import org.hisp.dhis.dataelement.DataElementService;
+import org.hisp.dhis.dataset.DataSetService;
+import org.hisp.dhis.datavalue.DataValue;
+import org.hisp.dhis.datavalue.DataValueService;
+import org.hisp.dhis.jdbc.batchhandler.DataValueBatchHandler;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.period.Period;
+import org.hisp.dhis.period.PeriodService;
+import org.hisp.dhis.system.util.ListUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
+
+public class RandomDataPopulator
+    implements Command
+{
+    private static final Log log = LogFactory.getLog( RandomDataPopulator.class );
+    
+    private static final String DE_GROUP = "Svac1cNQhRS";
+    private static final String DE_WEIGHT = "h0xKKjijTdI";
+    private static final int OU_LEVEL = 4;
+    private static final String PE_WEIGHT = "2014";
+    private static final List<String> PERIODS = Arrays.asList( 
+        "201301", "201302", "201303", "201304", "201305", "201306", "201307", "201308", "201309", "201310","201311", "201312", 
+        "201401", "201402", "201403", "201404", "201405", "201406", "201407", "201408", "201409", "201410","201411", "201412"
+    );
+    
+    @Autowired
+    private DataElementService dataElementService;
+    
+    @Autowired
+    private DataSetService dataSetService;
+    
+    @Autowired
+    private PeriodService periodService;
+    
+    @Autowired
+    private DataElementCategoryService categoryService;
+    
+    @Autowired
+    private BatchHandlerFactory batchHandlerFactory;
+    
+    @Autowired
+    private OrganisationUnitService organisationUnitService;
+
+    @Autowired
+    private DataValueService dataValueService;
+    
+    @Override
+    @Transactional
+    public void execute()
+        throws Exception
+    {
+        // ---------------------------------------------------------------------
+        // Data elements
+        // ---------------------------------------------------------------------
+
+        List<DataElement> des = new ArrayList<DataElement>( dataElementService.getDataElementGroup( DE_GROUP ).getMembers() );
+
+        log.info( "Data elements: " + des.size() );
+        
+        // ---------------------------------------------------------------------
+        // Organisation units
+        // ---------------------------------------------------------------------
+
+        List<OrganisationUnit> ous = new ArrayList<OrganisationUnit>( organisationUnitService.getOrganisationUnitsAtLevel( OU_LEVEL ) );
+        Collections.shuffle( ous );
+        ous = ListUtils.subList( ous, 0, 4000 );
+        
+        log.info( "Organisation units: " + ous.size() );
+
+        // ---------------------------------------------------------------------
+        // Periods (might fail if not present in database due to single tx)
+        // ---------------------------------------------------------------------
+
+        List<Period> pes = periodService.reloadIsoPeriods( PERIODS );
+        
+        log.info( "Periods: " + pes );
+
+        // ---------------------------------------------------------------------
+        // Category option combos
+        // ---------------------------------------------------------------------
+        
+        DataElementCategoryOptionCombo coc = categoryService.getDataElementCategoryOptionCombo( 1153734 );
+        DataElementCategoryOptionCombo aoc = categoryService.getDefaultDataElementCategoryOptionCombo();
+
+        // ---------------------------------------------------------------------
+        // Weight data
+        // ---------------------------------------------------------------------
+        
+        DataElement deWeight = dataElementService.getDataElement( DE_WEIGHT );
+        Period peWeight = periodService.reloadIsoPeriod( PE_WEIGHT );
+        
+        Collection<DataValue> values = dataValueService.getDataValues( deWeight, peWeight, ous );
+        
+        Map<String, String> valueMap = new HashMap<String, String>();
+        
+        for ( DataValue value : values )
+        {
+            valueMap.put( value.getSource().getUid(), value.getValue() );
+        }
+        
+        log.info( "Weight data values: " + valueMap.keySet().size() );
+        
+        // ---------------------------------------------------------------------
+        // Setup and generation
+        // ---------------------------------------------------------------------
+        
+        BatchHandler<DataValue> handler = batchHandlerFactory.createBatchHandler( DataValueBatchHandler.class ).init();
+
+        Date d = new Date();
+        
+        int total = pes.size();
+        int count = 0;
+
+        Random r = new Random();
+        
+        for ( Period pe : pes )
+        {
+            double peFactor = ( ( r.nextInt( 50 ) + 75 ) / 100d );
+            
+            for ( OrganisationUnit ou : ous )
+            {
+                String val = valueMap.get( ou.getUid() );
+            
+                for ( DataElement de : des )
+                {   
+                    if ( val != null )
+                    {
+                        DataValue dv = new DataValue( de, pe, ou, coc, aoc, String.valueOf( getVal( val, peFactor ) ), "", d, "" );
+                        handler.addObject( dv );
+                    }
+                }
+            }
+            
+            log.info( "Done for " + ( ++count ) + " out of " + total );
+        }
+        
+        handler.flush();
+        
+        log.info( "Data population completed" );
+    }
+    
+    private Integer getVal( String value, double peFactor )
+    {
+        Random r = new Random();
+        Double val = Double.parseDouble( value );
+        Double delta = 30 * ( r.nextDouble() - 0.5 );
+        Double weightedVal = ( val * 0.6 ) + delta;
+        weightedVal = weightedVal * peFactor;
+        return weightedVal.intValue();
+    }    
+}

=== modified file 'tools/dhis-adhoc/src/main/resources/META-INF/dhis/beans.xml'
--- tools/dhis-adhoc/src/main/resources/META-INF/dhis/beans.xml	2013-01-24 08:39:47 +0000
+++ tools/dhis-adhoc/src/main/resources/META-INF/dhis/beans.xml	2014-04-25 14:00:28 +0000
@@ -3,5 +3,7 @@
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd";>
 
     <bean id="exampleCommand" class="org.hisp.dhis.adhoc.command.ExampleCommand" />
+    
+    <bean id="randomDataPopulator" class="org.hisp.dhis.adhoc.command.RandomDataPopulator" />
 
 </beans>