AbstractJdbcEventAnalyticsManager { protected String getAggregateClause( EventQueryParams params ) { EventOutputType outputType = params.getOutputType(); if ( params.hasValueDimension() ) { Assert.isTrue( params.getAggregationTypeFallback().getAggregationType().isAggregateable(), "Event query aggregation type must be aggregatable" ); String function = params.getAggregationTypeFallback().getAggregationType().getValue(); String expression = quoteAlias( params.getValue().getUid() ); return function + "(" + expression + ")"; } else if ( params.hasProgramIndicatorDimension() ) { String function = params.getProgramIndicator().getAggregationTypeFallback().getValue(); function = TextUtils.emptyIfEqual( function, AggregationType.CUSTOM.getValue() ); String expression = programIndicatorService.getAnalyticsSql( params.getProgramIndicator().getExpression(), params.getProgramIndicator(), params.getEarliestStartDate(), params.getLatestEndDate() ); return function + "(" + expression + ")"; } else { if ( params.hasEnrollmentProgramIndicatorDimension() ) { if ( EventOutputType.TRACKED_ENTITY_INSTANCE.equals( outputType ) && params.isProgramRegistration() ) { return "count(distinct tei)"; } else { return "count(pi)"; } } else { if ( EventOutputType.TRACKED_ENTITY_INSTANCE.equals( outputType ) && params.isProgramRegistration() ) { return "count(distinct " + quoteAlias( "tei") + ")"; } else if ( EventOutputType.ENROLLMENT.equals( outputType ) ) { return "count(distinct " + quoteAlias( "pi") + ")"; } else { return "count(" + quoteAlias( "psi") + ")"; } } } } AbstractJdbcEventAnalyticsManager( @Qualifier( "readOnlyJdbcTemplate" ) JdbcTemplate jdbcTemplate,
        StatementBuilder statementBuilder, ProgramIndicatorService programIndicatorService,
        ProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder ); Grid getAggregatedEventData( EventQueryParams params, Grid grid, int maxLimit );  }
@Test public void verifyGetAggregateClauseWithValue() { DimensionalItemObject dio = new BaseDimensionalItemObject( dataElementA.getUid() ); EventQueryParams params = new EventQueryParams.Builder( createRequestParams() ) .withValue( dio ) .withAggregationType( AnalyticsAggregationType.SUM ) .build(); String clause = subject.getAggregateClause( params ); assertThat( clause, is( "sum(ax.\"fWIAEtYVEGk\")" ) ); }
@Test( expected = IllegalArgumentException.class ) public void verifyGetAggregateClauseWithValueFails() { DimensionalItemObject dio = new BaseDimensionalItemObject( dataElementA.getUid() ); EventQueryParams params = new EventQueryParams.Builder( createRequestParams() ) .withValue( dio ) .withAggregationType( fromAggregationType( AggregationType.CUSTOM ) ) .build(); subject.getAggregateClause( params ); }
@Test public void verifyGetAggregateClauseWithProgramIndicator() { ProgramIndicator programIndicator = createProgramIndicator( 'A', programA, "9.0", null ); EventQueryParams params = new EventQueryParams.Builder( createRequestParams() ) .withProgramIndicator( programIndicator ) .build(); when( programIndicatorService.getAnalyticsSql( programIndicator.getExpression(), programIndicator, params.getEarliestStartDate(), params.getLatestEndDate() ) ) .thenReturn( "select * from table" ); String clause = subject.getAggregateClause( params ); assertThat( clause, is( "avg(select * from table)" ) ); }
@Test public void verifyGetAggregateClauseWithProgramIndicatorAndCustomAggregationType() { ProgramIndicator programIndicator = createProgramIndicator( 'A', programA, "9.0", null ); programIndicator.setAggregationType( AggregationType.CUSTOM ); EventQueryParams params = new EventQueryParams.Builder( createRequestParams() ) .withProgramIndicator( programIndicator ).build(); when( programIndicatorService.getAnalyticsSql( programIndicator.getExpression(), programIndicator, params.getEarliestStartDate(), params.getLatestEndDate() ) ) .thenReturn( "select * from table" ); String clause = subject.getAggregateClause( params ); assertThat( clause, is( "(select * from table)" ) ); }
@Test public void verifyGetAggregateClauseWithEnrollmentDimension() { ProgramIndicator programIndicator = createProgramIndicator( 'A', programA, "9.0", null ); programIndicator.setAnalyticsType( AnalyticsType.ENROLLMENT ); EventQueryParams params = new EventQueryParams.Builder( createRequestParams() ) .withProgramIndicator( programIndicator ) .build(); when( programIndicatorService.getAnalyticsSql( programIndicator.getExpression(), programIndicator, params.getEarliestStartDate(), params.getLatestEndDate() ) ) .thenReturn( "select * from table" ); String clause = subject.getAggregateClause( params ); assertThat( clause, is( "avg(select * from table)" ) ); }