From 579ebfdb349d0569bd17bfe35be54c3d99b62443 Mon Sep 17 00:00:00 2001 From: Nico Koprowski Date: Wed, 3 Jan 2024 14:43:11 +0800 Subject: [PATCH] feat(Gate): add stats endpoint for counting business partners per stage --- .../tractusx/bpdm/gate/api/StatsApi.kt | 15 ++++++++++- .../api/model/response/StatsStagesResponse.kt | 25 +++++++++++++++++++ .../bpdm/gate/controller/StatsController.kt | 7 +++++- .../generic/BusinessPartnerRepository.kt | 8 ++++++ .../bpdm/gate/service/StatsService.kt | 18 ++++++++++++- 5 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/StatsStagesResponse.kt diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/StatsApi.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/StatsApi.kt index e9b5e6505..146e3d9a5 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/StatsApi.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/StatsApi.kt @@ -23,6 +23,7 @@ import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.responses.ApiResponse import io.swagger.v3.oas.annotations.responses.ApiResponses import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsSharingStatesResponse +import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsStagesResponse import org.springframework.http.MediaType import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping @@ -41,6 +42,18 @@ interface StatsApi { ) @GetMapping("/sharing-states") @GetExchange("/sharing-states") - fun getSharingStates(): StatsSharingStatesResponse + fun countPartnersBySharingState(): StatsSharingStatesResponse + + @Operation + @ApiResponses( + value = [ + ApiResponse(responseCode = "200") + ] + ) + @GetMapping("/stages") + @GetExchange("/stages") + fun countPartnersPerStage(): StatsStagesResponse + + } \ No newline at end of file diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/StatsStagesResponse.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/StatsStagesResponse.kt new file mode 100644 index 000000000..fb75f0700 --- /dev/null +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/StatsStagesResponse.kt @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ******************************************************************************/ + +package org.eclipse.tractusx.bpdm.gate.api.model.response + +data class StatsStagesResponse( + val inputTotal: Int, + val outputTotal: Int +) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/StatsController.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/StatsController.kt index c18d9c9dd..39ce15447 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/StatsController.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/StatsController.kt @@ -21,6 +21,7 @@ package org.eclipse.tractusx.bpdm.gate.controller import org.eclipse.tractusx.bpdm.gate.api.StatsApi import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsSharingStatesResponse +import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsStagesResponse import org.eclipse.tractusx.bpdm.gate.service.StatsService import org.springframework.web.bind.annotation.RestController @@ -28,7 +29,11 @@ import org.springframework.web.bind.annotation.RestController class StatsController( private val statsService: StatsService ) : StatsApi { - override fun getSharingStates(): StatsSharingStatesResponse { + override fun countPartnersBySharingState(): StatsSharingStatesResponse { return statsService.countSharingStates() } + + override fun countPartnersPerStage(): StatsStagesResponse { + return statsService.countBusinessPartnersPerStage() + } } \ No newline at end of file diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/generic/BusinessPartnerRepository.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/generic/BusinessPartnerRepository.kt index f2ef3911c..f5c495fb9 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/generic/BusinessPartnerRepository.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/generic/BusinessPartnerRepository.kt @@ -47,4 +47,12 @@ interface BusinessPartnerRepository : JpaRepository, Crud @Param("bpnA") bpnAList: List? ): Set + @Query("SELECT b.stage as stage, COUNT(b.stage) as count FROM BusinessPartner AS b GROUP BY b.stage") + fun countPerStages(): List + + interface PartnersPerStageCount { + val stage: StageType + val count: Int + } + } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/StatsService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/StatsService.kt index 815ec2076..7f1a6db27 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/StatsService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/StatsService.kt @@ -19,14 +19,18 @@ package org.eclipse.tractusx.bpdm.gate.service +import org.eclipse.tractusx.bpdm.common.model.StageType import org.eclipse.tractusx.bpdm.gate.api.model.SharingStateType import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsSharingStatesResponse +import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsStagesResponse import org.eclipse.tractusx.bpdm.gate.repository.SharingStateRepository +import org.eclipse.tractusx.bpdm.gate.repository.generic.BusinessPartnerRepository import org.springframework.stereotype.Service @Service class StatsService( - private val sharingStateRepository: SharingStateRepository + private val sharingStateRepository: SharingStateRepository, + private val businessPartnerRepository: BusinessPartnerRepository ) { fun countSharingStates(): StatsSharingStatesResponse { @@ -42,4 +46,16 @@ class StatsService( ) } + fun countBusinessPartnersPerStage(): StatsStagesResponse { + val counts = businessPartnerRepository.countPerStages() + val countsByType = counts.associate { Pair(it.stage, it.count) } + + return StatsStagesResponse( + inputTotal = countsByType[StageType.Input] ?: 0, + outputTotal = countsByType[StageType.Output] ?: 0 + ) + + } + + } \ No newline at end of file