From 3588df2590fa88aea547f487a9d75fe32c4a9adf Mon Sep 17 00:00:00 2001 From: Jan-Niclas Struewer <j.n.struewer@gmail.com> Date: Thu, 14 Dec 2023 10:33:42 +0100 Subject: [PATCH] updated startup logic only returning projects with consent true --- .../iem/dataprovider/StartUpHandler.kt | 18 ++++++-- .../controller/RepositoryController.kt | 46 +++++++++++++------ .../repository/RepositoryRepository.kt | 3 ++ .../repository/service/RepositoryService.kt | 11 +++-- .../toolRun/service/ToolRunDbService.kt | 2 - 5 files changed, 55 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/de/fraunhofer/iem/dataprovider/StartUpHandler.kt b/src/main/kotlin/de/fraunhofer/iem/dataprovider/StartUpHandler.kt index 0ca02009..d15b3782 100644 --- a/src/main/kotlin/de/fraunhofer/iem/dataprovider/StartUpHandler.kt +++ b/src/main/kotlin/de/fraunhofer/iem/dataprovider/StartUpHandler.kt @@ -2,10 +2,13 @@ package de.fraunhofer.iem.dataprovider import de.fraunhofer.iem.dataprovider.configuration.InitialProjects import de.fraunhofer.iem.dataprovider.logger.getLogger +import de.fraunhofer.iem.dataprovider.repository.dto.RepositoryConsentDto +import de.fraunhofer.iem.dataprovider.repository.service.RepositoryService import de.fraunhofer.iem.dataprovider.tool.service.ToolService import de.fraunhofer.iem.dataprovider.toolRun.service.ToolRunService import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch import org.springframework.boot.context.event.ApplicationReadyEvent import org.springframework.context.event.EventListener @@ -15,7 +18,8 @@ import org.springframework.stereotype.Component class StartUpHandler( val toolRunService: ToolRunService, val initialProjects: InitialProjects, - val toolService: ToolService + val toolService: ToolService, + val repositoryService: RepositoryService ) { private val logger = getLogger(javaClass) val coroutineScope = CoroutineScope(Dispatchers.Default) @@ -27,12 +31,18 @@ class StartUpHandler( logger.info("Loading initial projects $initialProjects") - initialProjects.projectIds.forEach { projectId -> - if (projectId >= 0) { + val jobs = initialProjects.projectIds.mapNotNull { projectId -> + return@mapNotNull if (projectId >= 0) { coroutineScope.launch { - toolRunService.createToolRunForRepository(projectId) + toolRunService.createToolRunForRepository(projectId).join() + repositoryService.updateVisualizationConsent(projectId, RepositoryConsentDto(true)) } + } else { + null } } + coroutineScope.launch { + jobs.joinAll() + } } } diff --git a/src/main/kotlin/de/fraunhofer/iem/dataprovider/repository/controller/RepositoryController.kt b/src/main/kotlin/de/fraunhofer/iem/dataprovider/repository/controller/RepositoryController.kt index 8e0941fb..6b5472cb 100644 --- a/src/main/kotlin/de/fraunhofer/iem/dataprovider/repository/controller/RepositoryController.kt +++ b/src/main/kotlin/de/fraunhofer/iem/dataprovider/repository/controller/RepositoryController.kt @@ -13,6 +13,8 @@ import de.fraunhofer.iem.dataprovider.repository.service.RepositoryService import de.fraunhofer.iem.dataprovider.toolRun.service.ToolRunService import de.fraunhofer.iem.dataprovider.user.dto.ValidateUserDto import de.fraunhofer.iem.dataprovider.user.service.UserService +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import org.springframework.http.HttpHeaders import org.springframework.http.HttpStatus import org.springframework.http.ResponseCookie @@ -37,7 +39,9 @@ class RepositoryController( @GetMapping(ApiPaths.REPOSITORY) suspend fun getAllRepositories(): List<RepositoryResponseDto> { logger.info("Get all repositories") - return repositoryService.getAllRepositories().map { repositoryEntity: RepositoryEntity -> + return withContext(Dispatchers.IO) { + repositoryService.getAllRepositoriesWithConsent() + }.map { repositoryEntity: RepositoryEntity -> RepositoryResponseDto( id = repositoryEntity.id!!, name = repositoryEntity.name, @@ -50,7 +54,9 @@ class RepositoryController( @GetMapping(ApiPaths.REPOSITORY_SCORE_CARD) suspend fun getAllScoreCards(): List<ScoreCardResponseDto> { logger.info("Get all repositories as score cards") - return repositoryService.getAllRepositories().map { repositoryEntity: RepositoryEntity -> + return withContext(Dispatchers.IO) { + repositoryService.getAllRepositoriesWithConsent() + }.map { repositoryEntity: RepositoryEntity -> val rawKpis = repositoryEntity.getLastToolRun().kpiEntities.map { it.toCalculationDto() } val rootKpi = kpiService.removeKPIChildrenLowerThanSecondLevel( this.kpiService.getKpiTreeForRawKpis(rawKpis).toViewModel() @@ -71,9 +77,11 @@ class RepositoryController( @GetMapping(ApiPaths.SCORE_CARD_BY_REPOSITORY_ID) suspend fun getScoreCardByRepositoryId(@PathVariable id: Long): ScoreCardResponseDto { logger.info("Get repository score card with id $id") - val repositoryEntity = this.repositoryService.findRepoById(id) ?: throw ResponseStatusException( - HttpStatus.NOT_FOUND, "repository not found" - ) + val repositoryEntity = withContext(Dispatchers.IO) { + repositoryService.findByProjectIdAndVisualizationConsentTrue(id) ?: throw ResponseStatusException( + HttpStatus.NOT_FOUND, "repository not found" + ) + } val rawKpis = repositoryEntity.getLastToolRun().kpiEntities.map { it.toCalculationDto() } val rootKpi = kpiService.removeKPIChildrenLowerThanSecondLevel( @@ -94,9 +102,11 @@ class RepositoryController( @GetMapping(ApiPaths.REPOSITORY_ID) suspend fun getRepositoryById(@PathVariable id: Long): RepositoryResponseDto { logger.info("Get repository with id $id") - val repositoryEntity = this.repositoryService.findRepoById(id) ?: throw ResponseStatusException( - HttpStatus.NOT_FOUND, "repository not found" - ) + val repositoryEntity = withContext(Dispatchers.IO) { + repositoryService.findByProjectIdAndVisualizationConsentTrue(id) ?: throw ResponseStatusException( + HttpStatus.NOT_FOUND, "repository not found" + ) + } return RepositoryResponseDto( id = repositoryEntity.id!!, name = repositoryEntity.name, @@ -111,9 +121,11 @@ class RepositoryController( @GetMapping(ApiPaths.RAW_KPI_BY_REPOSITORY_ID) suspend fun getRawKPIByRepositoryId(@PathVariable id: Long): RawKpiDto { logger.info("Get raw KPIs for repository id $id") - val repositoryEntity = this.repositoryService.findRepoById(id) ?: throw ResponseStatusException( - HttpStatus.NOT_FOUND, "repository not found" - ) + val repositoryEntity = withContext(Dispatchers.IO) { + repositoryService.findByProjectIdAndVisualizationConsentTrue(id) ?: throw ResponseStatusException( + HttpStatus.NOT_FOUND, "repository not found" + ) + } val rawKpis = repositoryEntity.getLastToolRun().kpiEntities.map { val calcDto = it.toCalculationDto() calcDto.calculateKPI() @@ -125,9 +137,11 @@ class RepositoryController( @GetMapping(ApiPaths.KPI_BY_REPOSITORY_ID) suspend fun getKPIByRepositoryId(@PathVariable id: Long): KPITreeResponseDto { logger.info("Get KPI tree for repository id $id") - val repositoryEntity = this.repositoryService.findRepoById(id) ?: throw ResponseStatusException( - HttpStatus.NOT_FOUND, "repository not found" - ) + val repositoryEntity = withContext(Dispatchers.IO) { + repositoryService.findByProjectIdAndVisualizationConsentTrue(id) ?: throw ResponseStatusException( + HttpStatus.NOT_FOUND, "repository not found" + ) + } val rawKpis = repositoryEntity.getLastToolRun().kpiEntities.map { it.toCalculationDto() } val rootKpi = this.kpiService.getKpiTreeForRawKpis(rawKpis) return rootKpi.toViewModel() @@ -162,7 +176,9 @@ class RepositoryController( @PathVariable id: Long, @RequestBody repositoryConsentDto: RepositoryConsentDto ) { - repositoryService.updateVisualizationConsent(id, repositoryConsentDto) + withContext(Dispatchers.IO) { + repositoryService.updateVisualizationConsent(id, repositoryConsentDto) + } } @PostMapping(ApiPaths.REPOSITORY_VALIDATE_USER) diff --git a/src/main/kotlin/de/fraunhofer/iem/dataprovider/repository/repository/RepositoryRepository.kt b/src/main/kotlin/de/fraunhofer/iem/dataprovider/repository/repository/RepositoryRepository.kt index ad7750bd..ccd1be15 100644 --- a/src/main/kotlin/de/fraunhofer/iem/dataprovider/repository/repository/RepositoryRepository.kt +++ b/src/main/kotlin/de/fraunhofer/iem/dataprovider/repository/repository/RepositoryRepository.kt @@ -9,4 +9,7 @@ interface RepositoryRepository : JpaRepository<RepositoryEntity, UUID> { fun findByVisualizationConsentTrue(): List<RepositoryEntity> + + + fun findByProjectIdAndVisualizationConsentTrue(projectId: Long): RepositoryEntity? } diff --git a/src/main/kotlin/de/fraunhofer/iem/dataprovider/repository/service/RepositoryService.kt b/src/main/kotlin/de/fraunhofer/iem/dataprovider/repository/service/RepositoryService.kt index 082476f3..be5fe494 100644 --- a/src/main/kotlin/de/fraunhofer/iem/dataprovider/repository/service/RepositoryService.kt +++ b/src/main/kotlin/de/fraunhofer/iem/dataprovider/repository/service/RepositoryService.kt @@ -32,6 +32,7 @@ class RepositoryService( return repo } + @Transactional fun saveRepository(repositoryEntity: RepositoryEntity) { repositoryRepository.saveAndFlush(repositoryEntity) } @@ -44,12 +45,13 @@ class RepositoryService( } @Transactional(readOnly = true) - fun findRepoById(projectId: Long): RepositoryEntity? { - return repositoryRepository.findByProjectId(projectId) + fun findByProjectIdAndVisualizationConsentTrue(projectId: Long): RepositoryEntity? { + return repositoryRepository.findByProjectIdAndVisualizationConsentTrue(projectId) } - fun getAllRepositories(): List<RepositoryEntity> { - return repositoryRepository.findAll() + @Transactional(readOnly = true) + fun findRepoById(projectId: Long): RepositoryEntity? { + return repositoryRepository.findByProjectId(projectId) } @Transactional @@ -67,6 +69,7 @@ class RepositoryService( } } + @Transactional(readOnly = true) fun getAllRepositoriesWithConsent(): List<RepositoryEntity> { return repositoryRepository.findByVisualizationConsentTrue() } diff --git a/src/main/kotlin/de/fraunhofer/iem/dataprovider/toolRun/service/ToolRunDbService.kt b/src/main/kotlin/de/fraunhofer/iem/dataprovider/toolRun/service/ToolRunDbService.kt index 3d7de6f7..257b6b71 100644 --- a/src/main/kotlin/de/fraunhofer/iem/dataprovider/toolRun/service/ToolRunDbService.kt +++ b/src/main/kotlin/de/fraunhofer/iem/dataprovider/toolRun/service/ToolRunDbService.kt @@ -53,8 +53,6 @@ class ToolRunDbService( repositoryService.saveRepository(repo) } - - //TODO: this should return a create tool run dto @Transactional(readOnly = true) fun getToolRunByProjectId(projectId: Long): ToolRunDto { return ToolRunDto.getDtoFromEntity( -- GitLab