JdbcAnalyticsTableManager extends AbstractJdbcTableManager { @Override @Transactional public List<AnalyticsTable> getAnalyticsTables( AnalyticsTableUpdateParams params ) { AnalyticsTable table = params.isLatestUpdate() ? getLatestAnalyticsTable( params, getDimensionColumns(), getValueColumns() ) : getRegularAnalyticsTable( params, getDataYears( params ), getDimensionColumns(), getValueColumns() ); return table.hasPartitionTables() ? newArrayList( table ) : newArrayList(); } JdbcAnalyticsTableManager( IdentifiableObjectManager idObjectManager,
        OrganisationUnitService organisationUnitService, CategoryService categoryService,
        SystemSettingManager systemSettingManager, DataApprovalLevelService dataApprovalLevelService,
        ResourceTableService resourceTableService, AnalyticsTableHookService tableHookService,
        StatementBuilder statementBuilder, PartitionManager partitionManager, DatabaseInfo databaseInfo,
        JdbcTemplate jdbcTemplate ); @Override AnalyticsTableType getAnalyticsTableType(); @Override @Transactional List<AnalyticsTable> getAnalyticsTables( AnalyticsTableUpdateParams params ); @Override String validState(); @Override void preCreateTables( AnalyticsTableUpdateParams params ); @Override void removeUpdatedData( AnalyticsTableUpdateParams params, List<AnalyticsTable> tables ); @Override @Async Future<?> applyAggregationLevels( ConcurrentLinkedQueue<AnalyticsTablePartition> partitions, Collection<String> dataElements, int aggregationLevel ); @Override @Async Future<?> vacuumTablesAsync( ConcurrentLinkedQueue<AnalyticsTablePartition> partitions ); @Override List<AnalyticsTableColumn> getFixedColumns();  }
@Test public void testGetRegularAnalyticsTable() { Date startTime = new DateTime( 2019, 3, 1, 10, 0 ).toDate(); List<Integer> dataYears = Lists.newArrayList( 2018, 2019 ); AnalyticsTableUpdateParams params = AnalyticsTableUpdateParams.newBuilder() .withStartTime( startTime ) .build(); when( jdbcTemplate.queryForList( Mockito.anyString(), ArgumentMatchers.<Class<Integer>>any() ) ).thenReturn( dataYears ); List<AnalyticsTable> tables = subject.getAnalyticsTables( params ); assertEquals( 1, tables.size() ); AnalyticsTable table = tables.get( 0 ); assertNotNull( table ); assertNotNull( table.getTablePartitions() ); assertEquals( 2, table.getTablePartitions().size() ); AnalyticsTablePartition partitionA = table.getTablePartitions().get( 0 ); AnalyticsTablePartition partitionB = table.getTablePartitions().get( 1 ); assertNotNull( partitionA ); assertNotNull( partitionA.getStartDate() ); assertNotNull( partitionA.getEndDate() ); assertEquals( partitionA.getYear().intValue(), new DateTime( partitionA.getStartDate() ).getYear() ); assertNotNull( partitionB ); assertNotNull( partitionB.getStartDate() ); assertNotNull( partitionB.getEndDate() ); assertEquals( partitionB.getYear().intValue(), new DateTime( partitionB.getStartDate() ).getYear() ); }
@Test public void testGetLatestAnalyticsTable() { Date lastFullTableUpdate = new DateTime( 2019, 3, 1, 2, 0 ).toDate(); Date lastLatestPartitionUpdate = new DateTime( 2019, 3, 1, 9, 0 ).toDate(); Date startTime = new DateTime( 2019, 3, 1, 10, 0 ).toDate(); AnalyticsTableUpdateParams params = AnalyticsTableUpdateParams.newBuilder() .withStartTime( startTime ) .withLatestPartition() .build(); List<Map<String, Object>> queryResp = Lists.newArrayList(); queryResp.add( ImmutableMap.of( "dataelementid", 1 ) ); when( systemSettingManager.getSystemSetting( SettingKey.LAST_SUCCESSFUL_ANALYTICS_TABLES_UPDATE ) ).thenReturn( lastFullTableUpdate ); when( systemSettingManager.getSystemSetting( SettingKey.LAST_SUCCESSFUL_LATEST_ANALYTICS_PARTITION_UPDATE ) ).thenReturn( lastLatestPartitionUpdate ); when( jdbcTemplate.queryForList( Mockito.anyString() ) ).thenReturn( queryResp ); List<AnalyticsTable> tables = subject.getAnalyticsTables( params ); assertEquals( 1, tables.size() ); AnalyticsTable table = tables.get( 0 ); assertNotNull( table ); assertNotNull( table.getTablePartitions() ); assertEquals( 1, table.getTablePartitions().size() ); AnalyticsTablePartition partition = table.getLatestPartition(); assertNotNull( partition ); assertTrue( partition.isLatestPartition() ); assertEquals( lastFullTableUpdate, partition.getStartDate() ); assertEquals( startTime, partition.getEndDate() ); }
@Test(expected=IllegalArgumentException.class) public void testGetLatestAnalyticsTableNoFullTableUpdate() { Date lastLatestPartitionUpdate = new DateTime( 2019, 3, 1, 9, 0 ).toDate(); Date startTime = new DateTime( 2019, 3, 1, 10, 0 ).toDate(); AnalyticsTableUpdateParams params = AnalyticsTableUpdateParams.newBuilder() .withStartTime( startTime ) .withLatestPartition() .build(); when( systemSettingManager.getSystemSetting( SettingKey.LAST_SUCCESSFUL_ANALYTICS_TABLES_UPDATE ) ).thenReturn( null ); when( systemSettingManager.getSystemSetting( SettingKey.LAST_SUCCESSFUL_LATEST_ANALYTICS_PARTITION_UPDATE ) ).thenReturn( lastLatestPartitionUpdate ); subject.getAnalyticsTables( params ); }