Skip to content

Commit

Permalink
Merge pull request #698 from catenax-ng/feat/stats/partners-per-stage
Browse files Browse the repository at this point in the history
feat(Gate): add stats endpoint for counting business partners per stage
  • Loading branch information
nicoprow authored Jan 3, 2024
2 parents 2327d2b + 579ebfd commit b68cd1e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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



}
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ 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

@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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ interface BusinessPartnerRepository : JpaRepository<BusinessPartner, Long>, Crud
@Param("bpnA") bpnAList: List<String?>?
): Set<BusinessPartner>

@Query("SELECT b.stage as stage, COUNT(b.stage) as count FROM BusinessPartner AS b GROUP BY b.stage")
fun countPerStages(): List<PartnersPerStageCount>

interface PartnersPerStageCount {
val stage: StageType
val count: Int
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
)

}


}

0 comments on commit b68cd1e

Please sign in to comment.