dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #20634
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9542: Analytics, indicator support
------------------------------------------------------------
revno: 9542
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-01-17 15:26:35 +0100
message:
Analytics, indicator support
modified:
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/DimensionOption.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/test/java/org/hisp/dhis/analytics/DimensionOptionTest.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/DataQueryParams.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java 2013-01-17 10:13:42 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java 2013-01-17 14:26:35 +0000
@@ -127,13 +127,18 @@
* is set, the organisation unit dimension name will be replaced by the name
* of the organisation unit level column.
*/
- public List<String> getDimensionNames()
+ public List<String> getSelectDimensionNames()
{
- List<String> list = getDimensionNamesIgnoreCategories();
+ List<String> list = getSelectDimensionNamesAsList();
- if ( categories )
- {
- list.add( CATEGORYOPTIONCOMBO_DIM_ID );
+ if ( list.contains( PERIOD_DIM_ID ) && periodType != null )
+ {
+ list.set( list.indexOf( PERIOD_DIM_ID ), periodType );
+ }
+
+ if ( list.contains( ORGUNIT_DIM_ID ) && organisationUnitLevel != 0 )
+ {
+ list.set( list.indexOf( ORGUNIT_DIM_ID ), LEVEL_PREFIX + organisationUnitLevel );
}
return list;
@@ -147,9 +152,9 @@
* of the organisation unit level column. Does not include the categories
* dimension, even if the categories property of this object is true.
*/
- public List<String> getDimensionNamesIgnoreCategories()
+ public List<String> getQueryDimensionNames()
{
- List<String> list = getDimensionNamesAsListIgnoreCategories();
+ List<String> list = getQueryDimensionNamesAsList();
if ( list.contains( PERIOD_DIM_ID ) && periodType != null )
{
@@ -165,11 +170,19 @@
}
/**
+ * Removes the dimension with the given identifier.
+ */
+ public void removeDimension( String dimension )
+ {
+ this.dimensions.remove( dimension );
+ }
+
+ /**
* Returns the index of the indicator dimension in the dimension map.
*/
public int getIndicatorDimensionIndex()
{
- return getDimensionNamesAsList().indexOf( INDICATOR_DIM_ID );
+ return getAllDimensionNamesAsList().indexOf( INDICATOR_DIM_ID );
}
/**
@@ -177,7 +190,7 @@
*/
public int getDataElementDimensionIndex()
{
- return getDimensionNamesAsList().indexOf( DATAELEMENT_DIM_ID );
+ return getAllDimensionNamesAsList().indexOf( DATAELEMENT_DIM_ID );
}
/**
@@ -185,7 +198,7 @@
*/
public int getCategoryOptionComboDimensionIndex()
{
- return getDimensionNamesAsList().indexOf( CATEGORYOPTIONCOMBO_DIM_ID );
+ return getAllDimensionNamesAsList().indexOf( CATEGORYOPTIONCOMBO_DIM_ID );
}
/**
@@ -193,7 +206,7 @@
*/
public int getPeriodDimensionIndex()
{
- return getDimensionNamesAsList().indexOf( PERIOD_DIM_ID );
+ return getAllDimensionNamesAsList().indexOf( PERIOD_DIM_ID );
}
/**
@@ -320,34 +333,34 @@
}
/**
- * Generates all permutations of the dimension options for this query.
+ * Generates all permutations of the dimension and filter options for this query.
*/
public List<List<DimensionOption>> getDimensionOptionPermutations()
{
List<DimensionOption[]> dimensionOptions = new ArrayList<DimensionOption[]>();
- List<String> dimensionNames = getDimensionNamesAsList();
-
List<String> ignoreDims = Arrays.asList( DATAELEMENT_DIM_ID, CATEGORYOPTIONCOMBO_DIM_ID, INDICATOR_DIM_ID );
- for ( String dim : dimensionNames )
+ for ( String dimension : getInputDimensionNamesAsList() )
{
- if ( !ignoreDims.contains( dim ) )
+ if ( !ignoreDims.contains( dimension ) )
{
List<DimensionOption> options = new ArrayList<DimensionOption>();
- for ( IdentifiableObject option : dimensions.get( dim ) )
+ for ( IdentifiableObject option : dimensions.get( dimension ) )
{
- options.add( new DimensionOption( dim, option ) );
+ options.add( new DimensionOption( dimension, option ) );
}
dimensionOptions.add( options.toArray( DIM_OPT_ARR ) );
}
}
-
+
CombinationGenerator<DimensionOption> generator = new CombinationGenerator<DimensionOption>( dimensionOptions.toArray( DIM_OPT_2D_ARR ) );
- return generator.getCombinations();
+ List<List<DimensionOption>> permutations = generator.getCombinations();
+
+ return permutations;
}
/**
@@ -404,38 +417,46 @@
// Supportive methods
// -------------------------------------------------------------------------
- /**
- * Returns the dimension names as a list. The indicator key is included as
- * indicator is not a true dimension, rather a formula based on the data
- * element dimension.
- */
- private List<String> getDimensionNamesAsListIgnoreCategories()
- {
- List<String> list = new ArrayList<String>( dimensions.keySet() );
-
- list.remove( INDICATOR_DIM_ID );
-
- return list;
- }
-
- /**
- * Returns the dimension names as a list. The indicator key is included as
- * indicator is not a true dimension, rather a formula based on the data
- * element dimension. Adds the category option combo dimension if the
- * categories parameter of this query is true.
- */
- public List<String> getDimensionNamesAsList()
- {
- List<String> list = getDimensionNamesAsListIgnoreCategories();
-
- if ( categories )
- {
- list.add( CATEGORYOPTIONCOMBO_DIM_ID );
- }
-
- return list;
- }
-
+ private List<String> getInputDimensionNamesAsList()
+ {
+ return new ArrayList<String>( dimensions.keySet() );
+ }
+
+ private List<String> getSelectDimensionNamesAsList()
+ {
+ List<String> list = getInputDimensionNamesAsList();
+
+ list.remove( INDICATOR_DIM_ID );
+
+ if ( categories )
+ {
+ list.add( CATEGORYOPTIONCOMBO_DIM_ID );
+ }
+
+ return list;
+ }
+
+ private List<String> getQueryDimensionNamesAsList()
+ {
+ List<String> list = getInputDimensionNamesAsList();
+
+ list.remove( INDICATOR_DIM_ID );
+
+ return list;
+ }
+
+ private List<String> getAllDimensionNamesAsList()
+ {
+ List<String> list = getInputDimensionNamesAsList();
+
+ if ( categories )
+ {
+ list.add( CATEGORYOPTIONCOMBO_DIM_ID );
+ }
+
+ return list;
+ }
+
// -------------------------------------------------------------------------
// hashCode, equals and toString
// -------------------------------------------------------------------------
=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DimensionOption.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DimensionOption.java 2013-01-17 11:02:22 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DimensionOption.java 2013-01-17 14:26:35 +0000
@@ -103,7 +103,7 @@
* Returns an array of identifiers of the dimension options in the given list.
* If no options are given or options is null, an empty array is returned.
*/
- public static String[] getOptions( List<DimensionOption> options )
+ public static String[] getOptionIdentifiers( List<DimensionOption> options )
{
List<String> optionUids = new ArrayList<String>();
=== 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 2013-01-17 11:02:22 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java 2013-01-17 14:26:35 +0000
@@ -118,7 +118,7 @@
grid.setMetaData( params.getUidNameMap() );
- for ( String col : params.getDimensionNames() )
+ for ( String col : params.getSelectDimensionNames() )
{
grid.addHeader( new GridHeader( col, col, String.class.getName(), false, true ) );
}
@@ -129,7 +129,7 @@
// Indicators
// ---------------------------------------------------------------------
- if ( !params.getIndicators().isEmpty() )
+ if ( params.getIndicators() != null && !params.getIndicators().isEmpty() )
{
Map<String, Double> constantMap = constantService.getConstantMap();
@@ -140,33 +140,35 @@
List<Indicator> indicators = asTypedList( params.getIndicators() );
Map<String, Double> aggregatedDataMap = getAggregatedDataValueMap( dataSourceParams );
-
+
Map<String, Map<DataElementOperand, Double>> permutationOperandValueMap = dataSourceParams.getPermutationOperandValueMap( aggregatedDataMap );
List<List<DimensionOption>> dimensionOptionPermutations = dataSourceParams.getDimensionOptionPermutations();
-
+
for ( Indicator indicator : indicators )
{
for ( List<DimensionOption> options : dimensionOptionPermutations )
{
String permKey = DimensionOption.asOptionKey( options );
-
+
Map<DataElementOperand, Double> valueMap = permutationOperandValueMap.get( permKey );
-
- Period period = (Period) DimensionOption.getPeriodOption( options );
-
- Assert.notNull( period );
-
+
if ( valueMap != null )
{
+ Period period = (Period) DimensionOption.getPeriodOption( options );
+
+ Assert.notNull( period );
+
Double value = expressionService.getIndicatorValue( indicator, period, valueMap, constantMap, null );
if ( value != null )
{
- options.set( indicatorIndex, new DimensionOption( INDICATOR_DIM_ID, indicator ) );
+ List<DimensionOption> row = new ArrayList<DimensionOption>( options );
+
+ row.add( indicatorIndex, new DimensionOption( INDICATOR_DIM_ID, indicator ) );
grid.addRow();
- grid.addValues( DimensionOption.getOptions( options ) );
+ grid.addValues( DimensionOption.getOptionIdentifiers( row ) );
grid.addValue( value );
}
}
@@ -178,7 +180,7 @@
// Data elements
// ---------------------------------------------------------------------
- if ( !params.getDataElements().isEmpty() )
+ if ( params.getDataElements() != null && !params.getDataElements().isEmpty() )
{
Map<String, Double> aggregatedDataMap = getAggregatedDataValueMap( params );
@@ -325,7 +327,7 @@
List<IdentifiableObject> dataElements = asList( expressionService.getDataElementsInIndicators( indicators ) );
immutableParams.setDataElements( dataElements );
- immutableParams.setIndicators( new ArrayList<IdentifiableObject>() );
+ immutableParams.removeDimension( INDICATOR_DIM_ID );
immutableParams.setCategories( true );
return immutableParams;
=== 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 2013-01-16 18:23:37 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java 2013-01-17 14:26:35 +0000
@@ -85,13 +85,13 @@
ListMap<IdentifiableObject, IdentifiableObject> dataPeriodAggregationPeriodMap = params.getDataPeriodAggregationPeriodMap();
params.replaceAggregationPeriodsWithDataPeriods( dataPeriodAggregationPeriodMap );
- List<String> dimensions = params.getDimensionNames();
- List<String> queryDimensions = params.getDimensionNamesIgnoreCategories();
+ List<String> selectDimensions = params.getSelectDimensionNames();
+ List<String> queryDimensions = params.getQueryDimensionNames();
Map<String, List<IdentifiableObject>> dimensionMap = params.getDimensionMap();
SqlHelper sqlHelper = new SqlHelper();
- String sql = "select " + getCommaDelimitedString( dimensions ) + ", ";
+ String sql = "select " + getCommaDelimitedString( selectDimensions ) + ", ";
int days = PeriodType.getPeriodTypeByName( params.getPeriodType() ).getFrequencyOrder();
@@ -109,7 +109,7 @@
sql += sqlHelper.whereAnd() + " " + filter + " in (" + getQuotedCommaDelimitedString( getUids( params.getFilters().get( filter ) ) ) + " ) ";
}
- sql += "group by " + getCommaDelimitedString( dimensions );
+ sql += "group by " + getCommaDelimitedString( selectDimensions );
log.info( sql );
@@ -121,7 +121,7 @@
{
StringBuilder key = new StringBuilder();
- for ( String dim : dimensions )
+ for ( String dim : selectDimensions )
{
key.append( rowSet.getString( dim ) + DIMENSION_SEP );
}
=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DimensionOptionTest.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DimensionOptionTest.java 2013-01-17 11:02:22 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DimensionOptionTest.java 2013-01-17 14:26:35 +0000
@@ -77,8 +77,8 @@
{
String[] expected = { deA.getUid(), peA.getUid(), ouA.getUid() };
- assertArrayEquals( expected, DimensionOption.getOptions( options ) );
- assertArrayEquals( new String[0], DimensionOption.getOptions( null ) );
+ assertArrayEquals( expected, DimensionOption.getOptionIdentifiers( options ) );
+ assertArrayEquals( new String[0], DimensionOption.getOptionIdentifiers( null ) );
}
@Test
=== 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 2013-01-07 15:58:50 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java 2013-01-17 14:26:35 +0000
@@ -171,6 +171,12 @@
return false;
}
+ if ( params.getFilterNames() != null && params.getFilterNames().contains( DataQueryParams.INDICATOR_DIM_ID ) )
+ {
+ ContextUtils.conflictResponse( response, "Indicators cannot be specified as filter" );
+ return false;
+ }
+
return true;
}
}