From 28a408ea3150f950adc0e4aac188d791433ca3fc Mon Sep 17 00:00:00 2001 From: Giuseppe Nespolino Date: Tue, 10 Aug 2021 14:22:36 +0200 Subject: [PATCH] feat: stored by as dimension [DHIS2-4902] (#8361) --- .../main/java/org/hisp/dhis/analytics/ColumnDataType.java | 1 + .../hisp/dhis/analytics/event/EventAnalyticsService.java | 2 ++ .../analytics/event/data/DefaultEventAnalyticsService.java | 5 ++++- .../analytics/event/data/JdbcEventAnalyticsManager.java | 2 +- .../analytics/table/JdbcEventAnalyticsTableManager.java | 4 ++++ .../analytics/event/data/EventsAnalyticsManagerTest.java | 6 +++--- .../analytics/table/JdbcEventAnalyticsTableManagerTest.java | 6 +++--- 7 files changed, 18 insertions(+), 8 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/ColumnDataType.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/ColumnDataType.java index 5a30a8737b9f..e452ca4a2d60 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/ColumnDataType.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/ColumnDataType.java @@ -34,6 +34,7 @@ public enum ColumnDataType { CHARACTER_11( "character(11)" ), CHARACTER_50( "character(50)" ), + VARCHAR_255( "varchar(255)" ), TEXT( "text" ), DATE( "date" ), TIMESTAMP( "timestamp" ), diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java index 843a8ca1b62e..2a45ff194adc 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java @@ -52,6 +52,8 @@ public interface EventAnalyticsService String ITEM_EVENT_DATE = "eventdate"; + String ITEM_STORED_BY = "storedby"; + String ITEM_ENROLLMENT_DATE = "enrollmentdate"; String ITEM_INCIDENT_DATE = "incidentdate"; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java index a573ee67ba76..0c018cb39f5f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java @@ -117,6 +117,8 @@ public class DefaultEventAnalyticsService private static final String NAME_EVENT_DATE = "Event date"; + private static final String NAME_STORED_BY = "Stored by"; + private static final String NAME_ENROLLMENT_DATE = "Enrollment date"; private static final String NAME_INCIDENT_DATE = "Incident date"; @@ -693,7 +695,8 @@ protected Grid createGridWithHeaders( EventQueryParams params ) .addHeader( new GridHeader( ITEM_EVENT, NAME_EVENT, TEXT, false, true ) ) .addHeader( new GridHeader( ITEM_PROGRAM_STAGE, NAME_PROGRAM_STAGE, TEXT, false, true ) ) .addHeader( new GridHeader( ITEM_EVENT_DATE, - LabelMapper.getEventDateLabel( params.getProgramStage(), NAME_EVENT_DATE ), DATE, false, true ) ); + LabelMapper.getEventDateLabel( params.getProgramStage(), NAME_EVENT_DATE ), DATE, false, true ) ) + .addHeader( new GridHeader( ITEM_STORED_BY, NAME_STORED_BY, TEXT, false, true ) ); if ( params.getProgram().isRegistration() ) { diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java index 995a5bc9b3a9..3730e7174b82 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java @@ -321,7 +321,7 @@ private SqlRowSet queryForRows( final String sql ) protected String getSelectClause( EventQueryParams params ) { ImmutableList.Builder cols = new ImmutableList.Builder() - .add( "psi", "ps", "executiondate" ); + .add( "psi", "ps", "executiondate", "storedby" ); if ( params.getProgram().isRegistration() ) { diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 48df1d539bee..9f121ceb8680 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -33,6 +33,7 @@ import static org.hisp.dhis.analytics.ColumnDataType.GEOMETRY; import static org.hisp.dhis.analytics.ColumnDataType.TEXT; import static org.hisp.dhis.analytics.ColumnDataType.TIMESTAMP; +import static org.hisp.dhis.analytics.ColumnDataType.VARCHAR_255; import static org.hisp.dhis.analytics.ColumnNotNullConstraint.NOT_NULL; import static org.hisp.dhis.analytics.util.AnalyticsSqlUtils.getClosingParentheses; import static org.hisp.dhis.analytics.util.AnalyticsSqlUtils.quote; @@ -103,6 +104,8 @@ public JdbcEventAnalyticsTableManager( IdentifiableObjectManager idObjectManager databaseInfo, jdbcTemplate ); } + public static final String STORED_BY_COL_NAME = "storedby"; + private static final List FIXED_COLS = ImmutableList.of( new AnalyticsTableColumn( quote( "psi" ), CHARACTER_11, NOT_NULL, "psi.uid" ), new AnalyticsTableColumn( quote( "pi" ), CHARACTER_11, NOT_NULL, "pi.uid" ), @@ -115,6 +118,7 @@ public JdbcEventAnalyticsTableManager( IdentifiableObjectManager idObjectManager new AnalyticsTableColumn( quote( "completeddate" ), TIMESTAMP, "psi.completeddate" ), new AnalyticsTableColumn( quote( "created" ), TIMESTAMP, "psi.created" ), new AnalyticsTableColumn( quote( "lastupdated" ), TIMESTAMP, "psi.lastupdated" ), + new AnalyticsTableColumn( quote( STORED_BY_COL_NAME ), VARCHAR_255, "psi.storedby" ), new AnalyticsTableColumn( quote( "pistatus" ), CHARACTER_50, "pi.status" ), new AnalyticsTableColumn( quote( "psistatus" ), CHARACTER_50, "psi.status" ), new AnalyticsTableColumn( quote( "psigeometry" ), GEOMETRY, "psi.geometry" ) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventsAnalyticsManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventsAnalyticsManagerTest.java index a6de07f32ac1..b2301bf7558b 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventsAnalyticsManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventsAnalyticsManagerTest.java @@ -110,7 +110,7 @@ public class EventsAnalyticsManagerTest extends EventAnalyticsTest private final static String TABLE_NAME = "analytics_event"; - private final static String DEFAULT_COLUMNS_WITH_REGISTRATION = "psi,ps,executiondate,enrollmentdate,incidentdate,tei,pi,ST_AsGeoJSON(psigeometry, 6) as geometry,longitude,latitude,ouname,oucode"; + private final static String DEFAULT_COLUMNS_WITH_REGISTRATION = "psi,ps,executiondate,storedby,enrollmentdate,incidentdate,tei,pi,ST_AsGeoJSON(psigeometry, 6) as geometry,longitude,latitude,ouname,oucode"; @Before public void setUp() @@ -137,7 +137,7 @@ public void verifyGetEventSqlWithProgramWithNoRegistration() verify( jdbcTemplate ).queryForRowSet( sql.capture() ); - String expected = "select psi,ps,executiondate,ST_AsGeoJSON(psigeometry, 6) as geometry,longitude,latitude,ouname,oucode,ax.\"monthly\",ax.\"ou\" from " + String expected = "select psi,ps,executiondate,storedby,ST_AsGeoJSON(psigeometry, 6) as geometry,longitude,latitude,ouname,oucode,ax.\"monthly\",ax.\"ou\" from " + getTable( programA.getUid() ) + " as ax where ax.\"monthly\" in ('2000Q1') and (ax.\"uidlevel0\" in ('ouabcdefghA') ) limit 101"; @@ -157,7 +157,7 @@ public void verifyGetEventSqlWithOrgUnitTypeDataElement() verify( jdbcTemplate ).queryForRowSet( sql.capture() ); - String expected = "select psi,ps,executiondate,enrollmentdate,incidentdate,tei,pi,ST_AsGeoJSON(psigeometry, 6) " + String expected = "select psi,ps,executiondate,storedby,enrollmentdate,incidentdate,tei,pi,ST_AsGeoJSON(psigeometry, 6) " + "as geometry,longitude,latitude,ouname,oucode,ax.\"monthly\",ax.\"ou\",\"" + dataElement.getUid() + "_name" + "\" " + diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java index 26d1888da836..fa01688eeb9c 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java @@ -224,7 +224,7 @@ public void verifyGetTableWithCategoryCombo() assertThat( tables, hasSize( 1 ) ); new AnalyticsTableAsserter.Builder( tables.get( 0 ) ).withTableType( AnalyticsTableType.EVENT ) - .withTableName( TABLE_PREFIX + program.getUid().toLowerCase() ).withColumnSize( 42 ) + .withTableName( TABLE_PREFIX + program.getUid().toLowerCase() ).withColumnSize( 43 ) .withDefaultColumns( subject.getFixedColumns() ).addColumns( periodColumns ) .addColumn( categoryA.getUid(), CHARACTER_11, "acs.", categoryA.getCreated() ) .addColumn( categoryB.getUid(), CHARACTER_11, "acs.", categoryB.getCreated() ).build().verify(); @@ -283,7 +283,7 @@ public void verifyGetTableWithDataElements() new AnalyticsTableAsserter.Builder( tables.get( 0 ) ) .withTableName( TABLE_PREFIX + program.getUid().toLowerCase() ).withTableType( AnalyticsTableType.EVENT ) - .withColumnSize( 49 ).addColumns( periodColumns ) + .withColumnSize( 50 ).addColumns( periodColumns ) .addColumn( d1.getUid(), TEXT, toAlias( aliasD1, d1.getUid() ) ) // ValueType.TEXT .addColumn( d2.getUid(), DOUBLE, toAlias( aliasD2, d2.getUid() ) ) // ValueType.PERCENTAGE .addColumn( d3.getUid(), INTEGER, toAlias( aliasD3, d3.getUid() ) ) // ValueType.BOOLEAN @@ -337,7 +337,7 @@ public void verifyGetTableWithTrackedEntityAttribute() new AnalyticsTableAsserter.Builder( tables.get( 0 ) ) .withTableName( TABLE_PREFIX + program.getUid().toLowerCase() ).withTableType( AnalyticsTableType.EVENT ) - .withColumnSize( 44 ).addColumns( periodColumns ) + .withColumnSize( 45 ).addColumns( periodColumns ) .addColumn( d1.getUid(), TEXT, toAlias( aliasD1, d1.getUid() ) ) // ValueType.TEXT .addColumn( tea1.getUid(), TEXT, String.format( aliasTea1, "ou.uid", tea1.getId(), tea1.getUid() ) ) // ValueType.ORGANISATION_UNIT // Second Geometry column created from the OU column above