def _relevant_report_configs(restore_state, report_configs): """ Filter out any UCRs that are already synced. This can't exist in V1, because in V1 we send all reports as one fixture. Returns a list of full ReportConfigs to sync and a set of report ids to purge """ last_sync_log = restore_state.last_sync_log if not last_sync_log or restore_state.overwrite_cache: return report_configs, [] current_sync_log = restore_state.current_sync_log now = _utcnow() last_ucr_syncs = { log.report_uuid: log.datetime for log in last_sync_log.last_ucr_sync_times } configs_to_sync = [] for config in report_configs: if config.uuid not in last_ucr_syncs: configs_to_sync.append(config) current_sync_log.last_ucr_sync_times.append( UCRSyncLog(report_uuid=config.uuid, datetime=now) ) continue last_sync = last_ucr_syncs[config.uuid] next_sync = last_sync + timedelta(hours=float(config.sync_delay)) if now > next_sync: configs_to_sync.append(config) current_sync_log.last_ucr_sync_times.append( UCRSyncLog(report_uuid=config.uuid, datetime=now) ) else: current_sync_log.last_ucr_sync_times.append( UCRSyncLog(report_uuid=config.uuid, datetime=last_sync) ) config_uuids = {config.uuid for config in report_configs} extra_configs_on_phone = set(last_ucr_syncs.keys()).difference(config_uuids) return configs_to_sync, extra_configs_on_phone
def test_v2_report_fixtures_provider_caching(self): report_id = 'deadbeef' provider = ReportFixturesProviderV2() report_app_config = ReportAppConfig( uuid='c0ffee', report_id=report_id, filters={ 'computed_owner_name_40cc88a0_1': StaticChoiceListFilter() }, sync_delay=1.0, ) restore_user = Mock(user_id='mock-user-id') restore_state = Mock( overwrite_cache=False, restore_user=restore_user, last_sync_log=Mock(last_ucr_sync_times=()), ) with mock_report_configuration_get({report_id: MAKE_REPORT_CONFIG('test_domain', report_id)}), \ patch('corehq.apps.app_manager.fixtures.mobile_ucr.ConfigurableReportDataSource') as report_datasource, \ patch('corehq.apps.app_manager.fixtures.mobile_ucr._utcnow') as utcnow_patch: report_datasource.from_spec.return_value = self.get_data_source_mock( ) utcnow_patch.return_value = datetime(2017, 9, 11, 6, 35, 20) configs = provider._relevant_report_configs( restore_state, [report_app_config]) self.assertEqual(configs, ([report_app_config], set())) restore_state = Mock( overwrite_cache=False, restore_user=restore_user, last_sync_log=Mock(last_ucr_sync_times=( UCRSyncLog(report_uuid=report_app_config.uuid, datetime=datetime.utcnow()), )), ) configs = provider._relevant_report_configs( restore_state, [report_app_config]) self.assertEqual(configs, ([], set())) configs = provider._relevant_report_configs(restore_state, []) self.assertEqual(configs, ([], {report_app_config.uuid}))