Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Gate): add stats endpoint for counting address types per stage #699

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ package org.eclipse.tractusx.bpdm.gate.api
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.common.model.StageType
import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsAddressTypesResponse
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.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.service.annotation.GetExchange
import org.springframework.web.service.annotation.HttpExchange
Expand Down Expand Up @@ -54,6 +57,14 @@ interface StatsApi {
@GetExchange("/stages")
fun countPartnersPerStage(): StatsStagesResponse


@Operation
@ApiResponses(
value = [
ApiResponse(responseCode = "200")
]
)
@GetMapping("/{stage}/address-types")
@GetExchange("/{stage}/address-types")
fun countAddressTypes(@PathVariable("stage") stage: StageType): StatsAddressTypesResponse

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*******************************************************************************
* 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 StatsAddressTypesResponse(
val legalAndSiteTotal: Int,
val legalTotal: Int,
val siteTotal: Int,
val additionalTotal: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

package org.eclipse.tractusx.bpdm.gate.controller

import org.eclipse.tractusx.bpdm.common.model.StageType
import org.eclipse.tractusx.bpdm.gate.api.StatsApi
import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsAddressTypesResponse
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
Expand All @@ -36,4 +38,8 @@ class StatsController(
override fun countPartnersPerStage(): StatsStagesResponse {
return statsService.countBusinessPartnersPerStage()
}

override fun countAddressTypes(stage: StageType): StatsAddressTypesResponse {
return statsService.countAddressTypes(stage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@

package org.eclipse.tractusx.bpdm.gate.repository.generic

import org.eclipse.tractusx.bpdm.common.dto.AddressType
import org.eclipse.tractusx.bpdm.common.model.StageType
import org.eclipse.tractusx.bpdm.gate.entity.generic.PostalAddress
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository

@Repository
interface PostalAddressRepository : JpaRepository<PostalAddress, Long>
interface PostalAddressRepository : JpaRepository<PostalAddress, Long> {

@Query("SELECT a.addressType as type, COUNT(a.addressType) as count FROM BusinessPartner b JOIN b.postalAddress AS a WHERE b.stage = :stage GROUP BY a.addressType")
fun countAddressTypes(stage: StageType): List<AddressTypeCount>


interface AddressTypeCount {
val type: AddressType?
val count: Int
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@

package org.eclipse.tractusx.bpdm.gate.service

import org.eclipse.tractusx.bpdm.common.dto.AddressType
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.StatsAddressTypesResponse
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.eclipse.tractusx.bpdm.gate.repository.generic.PostalAddressRepository
import org.springframework.stereotype.Service

@Service
class StatsService(
private val sharingStateRepository: SharingStateRepository,
private val businessPartnerRepository: BusinessPartnerRepository
private val businessPartnerRepository: BusinessPartnerRepository,
private val postalAddressRepository: PostalAddressRepository
) {

fun countSharingStates(): StatsSharingStatesResponse {
Expand All @@ -54,7 +58,18 @@ class StatsService(
inputTotal = countsByType[StageType.Input] ?: 0,
outputTotal = countsByType[StageType.Output] ?: 0
)
}

fun countAddressTypes(stage: StageType): StatsAddressTypesResponse {
val counts = postalAddressRepository.countAddressTypes(stage)
val countsByType = counts.associate { Pair(it.type, it.count) }

return StatsAddressTypesResponse(
legalAndSiteTotal = countsByType[AddressType.LegalAndSiteMainAddress] ?: 0,
legalTotal = countsByType[AddressType.LegalAddress] ?: 0,
siteTotal = countsByType[AddressType.SiteMainAddress] ?: 0,
additionalTotal = countsByType[AddressType.AdditionalAddress] ?: 0
)
}


Expand Down
Loading