Skip to content

Commit

Permalink
Add year to compute fleet segment
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Jan 21, 2025
1 parent 9932459 commit e55ff69
Show file tree
Hide file tree
Showing 22 changed files with 87 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import fr.gouv.cnsp.monitorfish.domain.repositories.FleetSegmentRepository
import fr.gouv.cnsp.monitorfish.domain.repositories.VesselRepository
import fr.gouv.cnsp.monitorfish.domain.use_cases.fleet_segment.dtos.SpeciesCatchForSegmentCalculation
import org.slf4j.LoggerFactory
import java.time.Clock
import java.time.ZonedDateTime

/**
* Return the computed fleet segments from the species catches.
Expand All @@ -18,19 +16,18 @@ import java.time.ZonedDateTime
class ComputeFleetSegments(
private val fleetSegmentRepository: FleetSegmentRepository,
private val vesselRepository: VesselRepository,
private val clock: Clock,
) {
private val logger = LoggerFactory.getLogger(ComputeFleetSegments::class.java)

fun execute(
year: Int,
vesselId: Int,
speciesCatches: List<SpeciesCatchForSegmentCalculation>,
): List<FleetSegment> {
logger.info("Got ${speciesCatches.size} catches to assign fleet segments")

val currentYear = ZonedDateTime.now(clock).year
val vesselType = vesselRepository.findVesselById(vesselId)?.vesselType
val fleetSegments = fleetSegmentRepository.findAllByYear(currentYear)
val fleetSegments = fleetSegmentRepository.findAllByYear(year)

val controlledPelagicSpeciesWeight =
speciesCatches
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ class ComputeFleetSegmentsFromControl(
faoAreas: List<String>,
gears: List<GearControl>,
species: List<SpeciesControl>,
year: Int,
): List<FleetSegment> {
val allSpecies = speciesRepository.findAll()

val speciesCatches = getSpeciesCatchesForSegmentCalculation(faoAreas, gears, species, allSpecies)

return computeFleetSegments.execute(vesselId, speciesCatches)
return computeFleetSegments.execute(year, vesselId, speciesCatches)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ComputeManualPriorNotification(
portLocode: String,
tripGearCodes: List<String>,
vesselId: Int,
year: Int,
): ManualPriorNotificationComputedValues {
val vessel = vesselRepository.findVesselById(vesselId)
requireNotNull(vessel) {
Expand All @@ -41,7 +42,7 @@ class ComputeManualPriorNotification(
val vesselFlagCountryCode = vessel.flagState

val speciesCatch = getSpeciesCatchesForSegmentCalculation(tripGearCodes, fishingCatchesWithFaoArea, species)
val tripSegments = computeFleetSegments.execute(vessel.id, speciesCatch)
val tripSegments = computeFleetSegments.execute(year, vessel.id, speciesCatch)
val types = computePnoTypes.execute(fishingCatchesWithFaoArea, tripGearCodes, vesselFlagCountryCode)
val vesselRiskFactor = computeRiskFactor.execute(portLocode, tripSegments, vesselCfr)

Expand All @@ -56,10 +57,10 @@ class ComputeManualPriorNotification(

return ManualPriorNotificationComputedValues(
isVesselUnderCharter = vessel.underCharter,
nextState,
tripSegments,
types,
vesselRiskFactor,
nextState = nextState,
tripSegments = tripSegments,
types = types,
vesselRiskFactor = vesselRiskFactor,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ class CreateOrUpdateManualPriorNotification(
// The Backend should NEVER update `risk_factors` DB table, only the pipeline is allowed to update it.
val computedValues =
computeManualPriorNotification.execute(
fishingCatches,
globalFaoArea,
portLocode,
tripGearCodes,
vesselId,
fishingCatches = fishingCatches,
globalFaoArea = globalFaoArea,
portLocode = portLocode,
tripGearCodes = tripGearCodes,
vesselId = vesselId,
year = expectedLandingDate.year,
)

val isPartOfControlUnitSubscriptions =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FleetSegmentController(
}

@PostMapping("/compute")
@Operation(summary = "compute fleet segments for the current year")
@Operation(summary = "compute fleet segments for the given year")
fun computeFleetSegments(
@RequestBody
computeFleetSegmentsDataInput: ComputeFleetSegmentsDataInput,
Expand All @@ -39,6 +39,7 @@ class FleetSegmentController(
computeFleetSegmentsDataInput.faoAreas,
computeFleetSegmentsDataInput.gears.map { it.toGearControl() },
computeFleetSegmentsDataInput.species.map { it.toSpeciesControl() },
computeFleetSegmentsDataInput.year,
)
return fleetSegments.map {
FleetSegmentDataOutput.fromFleetSegment(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class PriorNotificationController(
portLocode = manualPriorNotificationComputeDataInput.portLocode,
tripGearCodes = manualPriorNotificationComputeDataInput.tripGearCodes,
vesselId = manualPriorNotificationComputeDataInput.vesselId,
year = manualPriorNotificationComputeDataInput.year,
)

return ManualPriorNotificationComputedValuesDataOutput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ data class ComputeFleetSegmentsDataInput(
val vesselId: Int,
val gears: List<GearControlDataInput>,
val species: List<SpeciesControlDataInput>,
val year: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ data class ManualPriorNotificationComputeDataInput(
val portLocode: String,
val tripGearCodes: List<String>,
val vesselId: Int,
val year: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fr.gouv.cnsp.monitorfish.domain.use_cases.fleet_segment

import com.neovisionaries.i18n.CountryCode
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.given
import fr.gouv.cnsp.monitorfish.domain.entities.fleet_segment.ScipSpeciesType
import fr.gouv.cnsp.monitorfish.domain.entities.vessel.Vessel
Expand All @@ -14,8 +15,6 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.junit.jupiter.SpringExtension
import java.time.Clock
import java.time.ZonedDateTime

@ExtendWith(SpringExtension::class)
class ComputeFleetSegmentsUTests {
Expand All @@ -25,10 +24,6 @@ class ComputeFleetSegmentsUTests {
@MockBean
private lateinit var vesselRepository: VesselRepository

companion object {
val fixedClock: Clock = Clock.systemUTC()
}

@Test
fun `execute Should compute Lines segment`() {
// Given
Expand Down Expand Up @@ -115,7 +110,7 @@ class ComputeFleetSegmentsUTests {
scipSpeciesType = ScipSpeciesType.TUNA,
),
)
given(fleetSegmentRepository.findAllByYear(ZonedDateTime.now().year)).willReturn(fleetSegmentsForComputation)
given(fleetSegmentRepository.findAllByYear(eq(2025))).willReturn(fleetSegmentsForComputation)
given(vesselRepository.findVesselById(any())).willReturn(
Vessel(
id = 1,
Expand All @@ -134,8 +129,7 @@ class ComputeFleetSegmentsUTests {
ComputeFleetSegments(
fleetSegmentRepository,
vesselRepository,
fixedClock,
).execute(1, speciesCatches)
).execute(2025, 1, speciesCatches)

// Then
assertThat(fleetSegments).hasSize(1)
Expand Down Expand Up @@ -164,7 +158,7 @@ class ComputeFleetSegmentsUTests {
scipSpeciesType = ScipSpeciesType.PELAGIC,
),
)
given(fleetSegmentRepository.findAllByYear(ZonedDateTime.now().year)).willReturn(fleetSegmentsForComputation)
given(fleetSegmentRepository.findAllByYear(eq(2025))).willReturn(fleetSegmentsForComputation)
given(vesselRepository.findVesselById(any())).willReturn(
Vessel(
id = 1,
Expand All @@ -183,8 +177,7 @@ class ComputeFleetSegmentsUTests {
ComputeFleetSegments(
fleetSegmentRepository,
vesselRepository,
fixedClock,
).execute(1, speciesCatches)
).execute(2025, 1, speciesCatches)

// Then
assertThat(fleetSegments).hasSize(1)
Expand Down Expand Up @@ -221,7 +214,7 @@ class ComputeFleetSegmentsUTests {
scipSpeciesType = null,
),
)
given(fleetSegmentRepository.findAllByYear(ZonedDateTime.now().year)).willReturn(fleetSegmentsForComputation)
given(fleetSegmentRepository.findAllByYear(eq(2025))).willReturn(fleetSegmentsForComputation)
given(vesselRepository.findVesselById(any())).willReturn(
Vessel(
id = 1,
Expand All @@ -240,8 +233,7 @@ class ComputeFleetSegmentsUTests {
ComputeFleetSegments(
fleetSegmentRepository,
vesselRepository,
fixedClock,
).execute(1, speciesCatches)
).execute(2025, 1, speciesCatches)

// Then
assertThat(fleetSegments).hasSize(1)
Expand Down Expand Up @@ -278,7 +270,7 @@ class ComputeFleetSegmentsUTests {
scipSpeciesType = null,
),
)
given(fleetSegmentRepository.findAllByYear(ZonedDateTime.now().year)).willReturn(fleetSegmentsForComputation)
given(fleetSegmentRepository.findAllByYear(eq(2025))).willReturn(fleetSegmentsForComputation)
given(vesselRepository.findVesselById(any())).willReturn(
Vessel(
id = 1,
Expand All @@ -297,8 +289,7 @@ class ComputeFleetSegmentsUTests {
ComputeFleetSegments(
fleetSegmentRepository,
vesselRepository,
fixedClock,
).execute(1, speciesCatches)
).execute(2025, 1, speciesCatches)

// Then
assertThat(fleetSegments).hasSize(1)
Expand Down Expand Up @@ -327,7 +318,7 @@ class ComputeFleetSegmentsUTests {
scipSpeciesType = ScipSpeciesType.DEMERSAL,
),
)
given(fleetSegmentRepository.findAllByYear(ZonedDateTime.now().year)).willReturn(fleetSegmentsForComputation)
given(fleetSegmentRepository.findAllByYear(eq(2025))).willReturn(fleetSegmentsForComputation)
given(vesselRepository.findVesselById(any())).willReturn(
Vessel(
id = 1,
Expand All @@ -346,8 +337,7 @@ class ComputeFleetSegmentsUTests {
ComputeFleetSegments(
fleetSegmentRepository,
vesselRepository,
fixedClock,
).execute(1, speciesCatches)
).execute(2025, 1, speciesCatches)

// Then
assertThat(fleetSegments).hasSize(1)
Expand Down Expand Up @@ -376,7 +366,7 @@ class ComputeFleetSegmentsUTests {
scipSpeciesType = ScipSpeciesType.DEMERSAL,
),
)
given(fleetSegmentRepository.findAllByYear(ZonedDateTime.now().year)).willReturn(fleetSegmentsForComputation)
given(fleetSegmentRepository.findAllByYear(eq(2025))).willReturn(fleetSegmentsForComputation)
given(vesselRepository.findVesselById(any())).willReturn(
Vessel(
id = 1,
Expand All @@ -395,8 +385,7 @@ class ComputeFleetSegmentsUTests {
ComputeFleetSegments(
fleetSegmentRepository,
vesselRepository,
fixedClock,
).execute(1, speciesCatches)
).execute(2025, 1, speciesCatches)

// Then
assertThat(fleetSegments).hasSize(1)
Expand All @@ -406,7 +395,7 @@ class ComputeFleetSegmentsUTests {
@Test
fun `execute Should compute no segment`() {
// Given
given(fleetSegmentRepository.findAllByYear(ZonedDateTime.now().year)).willReturn(fleetSegmentsForComputation)
given(fleetSegmentRepository.findAllByYear(eq(2025))).willReturn(fleetSegmentsForComputation)
given(vesselRepository.findVesselById(any())).willReturn(
Vessel(
id = 1,
Expand All @@ -425,8 +414,7 @@ class ComputeFleetSegmentsUTests {
ComputeFleetSegments(
fleetSegmentRepository,
vesselRepository,
fixedClock,
).execute(1, listOf())
).execute(2025, 1, listOf())

// Then
assertThat(fleetSegments).hasSize(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ class CreateOrUpdateManualPriorNotificationITests : AbstractDBTests() {
// Given
val reportId = testCase.reportId

given { computeManualPriorNotification.execute(any(), any(), any(), any(), any()) }
given { computeManualPriorNotification.execute(any(), any(), any(), any(), any(), any()) }
.willReturn(
ManualPriorNotificationComputedValues(
isVesselUnderCharter = false,
Expand Down Expand Up @@ -390,7 +390,7 @@ class CreateOrUpdateManualPriorNotificationITests : AbstractDBTests() {
// Given
val reportId = testCase.reportId

given { computeManualPriorNotification.execute(any(), any(), any(), any(), any()) }
given { computeManualPriorNotification.execute(any(), any(), any(), any(), any(), any()) }
.willReturn(
ManualPriorNotificationComputedValues(
isVesselUnderCharter = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CreateOrUpdateManualPriorNotificationUTests {

// Given
given(vesselRepository.findVesselById(any())).willReturn(VesselFaker.fakeVessel())
given(computeManualPriorNotification.execute(any(), any(), any(), any(), any())).willReturn(
given(computeManualPriorNotification.execute(any(), any(), any(), any(), any(), any())).willReturn(
ManualPriorNotificationComputedValues(
isVesselUnderCharter = null,
nextState = PriorNotificationState.OUT_OF_VERIFICATION_SCOPE,
Expand Down Expand Up @@ -116,7 +116,7 @@ class CreateOrUpdateManualPriorNotificationUTests {
existingFakePriorNotification,
)
given(vesselRepository.findVesselById(any())).willReturn(VesselFaker.fakeVessel())
given(computeManualPriorNotification.execute(any(), any(), any(), any(), any())).willReturn(
given(computeManualPriorNotification.execute(any(), any(), any(), any(), any(), any())).willReturn(
ManualPriorNotificationComputedValues(
isVesselUnderCharter = null,
nextState = PriorNotificationState.OUT_OF_VERIFICATION_SCOPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class FleetSegmentControllerITests {
@Test
fun `Should compute fleet segments`() {
// Given
given(this.computeFleetSegmentsFromControl.execute(any(), any(), any(), any())).willReturn(
given(this.computeFleetSegmentsFromControl.execute(any(), any(), any(), any(), any())).willReturn(
listOf(
FleetSegment(
segment = "SWW01",
Expand Down Expand Up @@ -106,6 +106,7 @@ class FleetSegmentControllerITests {
ComputeFleetSegmentsDataInput(
faoAreas = listOf("27.1.c", "27.1.b"),
vesselId = 123,
year = 2021,
gears =
listOf(
GearControlDataInput(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class PriorNotificationControllerUTests {
@Test
fun `getManualComputation Should get a manual prior notification computed values`() {
// Given
given(this.computeManualPriorNotification.execute(any(), any(), any(), any(), any()))
given(this.computeManualPriorNotification.execute(any(), any(), any(), any(), any(), any()))
.willReturn(
ManualPriorNotificationComputedValues(
isVesselUnderCharter = null,
Expand All @@ -188,6 +188,7 @@ class PriorNotificationControllerUTests {
portLocode = "FRABC",
tripGearCodes = emptyList(),
vesselId = 42,
year = 2025,
),
)
api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ context('Side Window > Mission Form > Sea Control', () => {
cy.fill('Sous-taille', true)
cy.fill('Observations (hors infraction) sur les espèces', 'Une observation hors infraction sur les espèces.')

// This should trigger a computation of the fleet segment
cy.intercept('POST', 'bff/v1/fleet_segments/compute').as(
'computeFleetSegments'
)
cy.fill('Date et heure du contrôle', now.utcDateTupleWithTime)
cy.wait('@computeFleetSegments')

// Appréhension et déroutement
cy.fill('Appréhension d’engin(s)', true)
cy.fill('Appréhension d’espèce(s)', true)
Expand Down
1 change: 1 addition & 0 deletions frontend/src/features/FleetSegment/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type ComputeFleetSegmentsParams = {
gears: MissionAction.GearControl[]
species: MissionAction.SpeciesControl[]
vesselId: number
year: number
}

export const fleetSegmentApi = monitorfishApi.injectEndpoints({
Expand Down
Loading

0 comments on commit e55ff69

Please sign in to comment.