Skip to content

Commit

Permalink
Finalize scheduled archive reportings
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Sep 17, 2024
1 parent e09d07a commit 5cdb5ff
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import fr.gouv.cnsp.monitorfish.domain.entities.alerts.type.AlertType
import fr.gouv.cnsp.monitorfish.domain.entities.reporting.InfractionSuspicion
import fr.gouv.cnsp.monitorfish.domain.entities.reporting.Observation
import fr.gouv.cnsp.monitorfish.domain.entities.reporting.Reporting
import fr.gouv.cnsp.monitorfish.domain.entities.reporting.ReportingValue
import fr.gouv.cnsp.monitorfish.domain.entities.reporting.filters.ReportingFilter
import fr.gouv.cnsp.monitorfish.domain.entities.vessel.VesselIdentifier
import java.time.ZonedDateTime
Expand Down Expand Up @@ -50,7 +49,7 @@ interface ReportingRepository {
fromDate: ZonedDateTime,
): List<Reporting>

fun findUnarchivedReportingsAfterNewVesselTrip(): List<Pair<Int, AlertType>>
fun findUnarchivedReportings(): List<Pair<Int, AlertType>>

fun archive(id: Int)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.transaction.annotation.Transactional

// TODO TEST

@UseCase
class ArchiveOutdatedReportings(private val reportingRepository: ReportingRepository) {
private val logger = LoggerFactory.getLogger(ArchiveOutdatedReportings::class.java)
Expand All @@ -17,16 +15,17 @@ class ArchiveOutdatedReportings(private val reportingRepository: ReportingReposi
@Scheduled(fixedDelay = 300000, initialDelay = 6000)
@Transactional
fun execute() {
val reportingCandidatesToArchive = reportingRepository.findUnarchivedReportingsAfterNewVesselTrip()
val reportingCandidatesToArchive = reportingRepository.findUnarchivedReportings()

val filteredReportingIdsToArchive = reportingCandidatesToArchive.filter {
it.second.type == AlertTypeMapping.MISSING_FAR_ALERT ||
it.second.type == AlertTypeMapping.THREE_MILES_TRAWLING_ALERT
}.map { it.first }
val filteredReportingIdsToArchive =
reportingCandidatesToArchive.filter {
it.second.type == AlertTypeMapping.MISSING_FAR_ALERT ||
it.second.type == AlertTypeMapping.THREE_MILES_TRAWLING_ALERT
}.map { it.first }

logger.info("Found ${filteredReportingIdsToArchive.size} reportings to archive.")
//val numberOfArchivedReportings = reportingRepository.archiveReportings(filteredReportingIdsToArchive)
val numberOfArchivedReportings = reportingRepository.archiveReportings(filteredReportingIdsToArchive)

//logger.info("Archived $numberOfArchivedReportings reportings")
logger.info("Archived $numberOfArchivedReportings reportings")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@ class GetVesselReportings(
vesselIdentifier: VesselIdentifier?,
fromDate: ZonedDateTime,
): VesselReportings {

val (controlUnits, controlUnitsTimeTaken) = measureTimedValue { getAllControlUnits.execute() }
logger.info("TIME_RECORD - 'getAllControlUnits' took $controlUnitsTimeTaken")

val (reportings, reportingsTimeTaken) =
measureTimedValue { findReportings(
vesselId,
vesselIdentifier,
internalReferenceNumber,
fromDate,
ircs,
externalReferenceNumber,
) }
measureTimedValue {
findReportings(
vesselId,
vesselIdentifier,
internalReferenceNumber,
fromDate,
ircs,
externalReferenceNumber,
)
}
logger.info("TIME_RECORD - 'findReportings' took $reportingsTimeTaken")

val (current, currentTimeTaken) =
Expand Down Expand Up @@ -73,7 +74,10 @@ class GetVesselReportings(
}
logger.info("TIME_RECORD - 'archivedYearsToReportings' took $archivedYearsToReportingsTimeTaken")

val (infractionSuspicionsSummary, infractionSuspicionsSummaryTimeTaken) = measureTimedValue { getInfractionSuspicionsSummary(reportings.filter { it.isArchived }) }
val (infractionSuspicionsSummary, infractionSuspicionsSummaryTimeTaken) =
measureTimedValue {
getInfractionSuspicionsSummary(reportings.filter { it.isArchived })
}
logger.info("TIME_RECORD - 'infractionSuspicionsSummary' took $infractionSuspicionsSummaryTimeTaken")
val numberOfInfractionSuspicions = infractionSuspicionsSummary.sumOf { it.numberOfOccurrences }
val numberOfObservation =
Expand Down Expand Up @@ -124,7 +128,7 @@ class GetVesselReportings(
}

return@map ReportingTitleAndNumberOfOccurrences(
title = infraction?.infraction?.let {"$it (NATINF $natinfCode)"} ?: "NATINF $natinfCode",
title = infraction?.infraction?.let { "$it (NATINF $natinfCode)" } ?: "NATINF $natinfCode",
numberOfOccurrences = reportings.size,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,16 @@ class JpaReportingRepository(
dbReportingRepository.archiveReporting(id)
}

override fun findUnarchivedReportingsAfterNewVesselTrip(): List<Pair<Int, AlertType>> {
override fun findUnarchivedReportings(): List<Pair<Int, AlertType>> {
return dbReportingRepository.findAllUnarchivedAfterDEPLogbookMessage().map { result ->
Pair(
result[0] as Int,
ReportingMapper.getReportingValueFromJSON(mapper, result[1] as String?, ReportingType.ALERT) as AlertType)
ReportingMapper.getReportingValueFromJSON(
mapper,
result[1] as String?,
ReportingType.ALERT,
) as AlertType,
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ interface DBReportingRepository : CrudRepository<ReportingEntity, Int> {
SELECT lr.cfr, lr.ircs, lr.external_identification, lr.operation_number, MAX(lr.operation_datetime_utc) as last_dep_date_time
FROM logbook_reports lr
WHERE
lr.operation_datetime_utc > NOW() - INTERVAL '1 day' AND
lr.operation_datetime_utc > NOW() - INTERVAL '1 hour' AND
lr.log_type = 'DEP'
GROUP BY lr.cfr, lr.ircs, lr.external_identification, lr.operation_number
),
Expand All @@ -94,7 +94,7 @@ interface DBReportingRepository : CrudRepository<ReportingEntity, Int> {
SELECT DISTINCT referenced_report_id
FROM logbook_reports lr
WHERE
lr.operation_datetime_utc > NOW() - INTERVAL '1 day' AND
lr.operation_datetime_utc > NOW() - INTERVAL '1 hour' AND
lr.operation_type = 'RET' AND
lr.value->>'returnStatus' = '000'
)
Expand All @@ -115,16 +115,12 @@ interface DBReportingRepository : CrudRepository<ReportingEntity, Int> {
WHERE
r.archived is false AND
r.deleted is false AND
rdp.last_dep_date_time >= r.validation_date
rdp.last_dep_date_time >= r.validation_date AND
rdp.operation_number IN (SELECT referenced_report_id FROM acknowledged_report_ids)
""",
nativeQuery = true,
)
fun findAllUnarchivedAfterDEPLogbookMessage(): List<Array<Any>>
/*
rdp.operation_number IN (SELECT referenced_report_id FROM acknowledged_report_ids)
*/

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,6 @@ UPDATE logbook_reports SET value = JSONB_SET(value, '{tripStartDate}', TO_JSONB(

INSERT INTO logbook_reports (id, report_id, referenced_report_id, integration_datetime_utc, operation_datetime_utc, operation_number, operation_type, transmission_format, value) VALUES (1120, NULL, 'FAKE_OPERATION_120', NOW() AT TIME ZONE 'UTC' - INTERVAL '14 minutes', NOW() AT TIME ZONE 'UTC' - INTERVAL '14 minutes', 'FAKE_OPERATION_120_RET', 'RET', 'ERS', '{"returnStatus":"000"}');

INSERT INTO logbook_reports (id, report_id, referenced_report_id, cfr, enriched, flag_state, integration_datetime_utc, log_type, operation_datetime_utc, operation_number, operation_type, report_datetime_utc, software, transmission_format, trip_gears, trip_segments, vessel_name, value) VALUES (121, 'FAKE_OPERATION_121', NULL, 'ABC000180832', true, 'FRA', NOW() AT TIME ZONE 'UTC' - INTERVAL '15 minutes', 'DEP', NOW() AT TIME ZONE 'UTC' - INTERVAL '15 minutes', 'FAKE_OPERATION_121', 'DAT', NOW() AT TIME ZONE 'UTC' - INTERVAL '15 minutes', 'TurboCatch (3.7-1)', 'ERS', NULL, NULL, 'MARIAGE ÎLE HASARD', '{"gearOnboard":[{"gear":"GTR","mesh":100}],"departurePort":"AEJAZ","anticipatedActivity":"FSH","tripStartDate":"NOW() AT TIME ZONE ''UTC'' - INTERVAL ''15 minutes''","departureDatetimeUtc":"NOW() AT TIME ZONE ''UTC'' - INTERVAL ''15 minutes''"}');
INSERT INTO logbook_reports (id, report_id, referenced_report_id, cfr, enriched, flag_state, integration_datetime_utc, log_type, operation_datetime_utc, operation_number, operation_type, report_datetime_utc, software, transmission_format, trip_gears, trip_segments, vessel_name, value) VALUES (121, 'FAKE_OPERATION_121', NULL, 'ABC000180832', true, 'FRA', NOW() - INTERVAL '15 minutes', 'DEP', NOW() - INTERVAL '15 minutes', 'FAKE_OPERATION_121', 'DAT', NOW() - INTERVAL '15 minutes', 'TurboCatch (3.7-1)', 'ERS', NULL, NULL, 'MARIAGE ÎLE HASARD', '{"gearOnboard":[{"gear":"GTR","mesh":100}],"departurePort":"AEJAZ","anticipatedActivity":"FSH","tripStartDate":"NOW() - INTERVAL ''15 minutes''","departureDatetimeUtc":"NOW() - INTERVAL ''15 minutes''"}');

INSERT INTO logbook_reports (id, report_id, referenced_report_id, integration_datetime_utc, operation_datetime_utc, operation_number, operation_type, transmission_format, value) VALUES (1120, NULL, 'FAKE_OPERATION_121', NOW() AT TIME ZONE 'UTC' - INTERVAL '14 minutes', NOW() AT TIME ZONE 'UTC' - INTERVAL '14 minutes', 'FAKE_OPERATION_121_RET', 'RET', 'ERS', '{"returnStatus":"000"}');
INSERT INTO logbook_reports (id, report_id, referenced_report_id, integration_datetime_utc, operation_datetime_utc, operation_number, operation_type, transmission_format, value) VALUES (1120, NULL, 'FAKE_OPERATION_121', NOW() - INTERVAL '14 minutes', NOW() - INTERVAL '14 minutes', 'FAKE_OPERATION_121_RET', 'RET', 'ERS', '{"returnStatus":"000"}');
Original file line number Diff line number Diff line change
Expand Up @@ -1516,12 +1516,12 @@
"cfr": "ABC000180832",
"enriched": true,
"flag_state": "FRA",
"integration_datetime_utc:sql": "NOW() AT TIME ZONE 'UTC' - INTERVAL '15 minutes'",
"integration_datetime_utc:sql": "NOW() - INTERVAL '15 minutes'",
"log_type": "DEP",
"operation_datetime_utc:sql": "NOW() AT TIME ZONE 'UTC' - INTERVAL '15 minutes'",
"operation_datetime_utc:sql": "NOW() - INTERVAL '15 minutes'",
"operation_number": "FAKE_OPERATION_121",
"operation_type": "DAT",
"report_datetime_utc:sql": "NOW() AT TIME ZONE 'UTC' - INTERVAL '15 minutes'",
"report_datetime_utc:sql": "NOW() - INTERVAL '15 minutes'",
"software": "TurboCatch (3.7-1)",
"transmission_format": "ERS",
"trip_gears": null,
Expand All @@ -1531,16 +1531,16 @@
"gearOnboard": [{"gear": "GTR", "mesh": 100.0}],
"departurePort": "AEJAZ",
"anticipatedActivity": "FSH",
"tripStartDate": "NOW() AT TIME ZONE 'UTC' - INTERVAL '15 minutes'",
"departureDatetimeUtc": "NOW() AT TIME ZONE 'UTC' - INTERVAL '15 minutes'"
"tripStartDate": "NOW() - INTERVAL '15 minutes'",
"departureDatetimeUtc": "NOW() - INTERVAL '15 minutes'"
}
},
{
"id": 1120,
"report_id": null,
"referenced_report_id": "FAKE_OPERATION_121",
"integration_datetime_utc:sql": "NOW() AT TIME ZONE 'UTC' - INTERVAL '14 minutes'",
"operation_datetime_utc:sql": "NOW() AT TIME ZONE 'UTC' - INTERVAL '14 minutes'",
"integration_datetime_utc:sql": "NOW() - INTERVAL '14 minutes'",
"operation_datetime_utc:sql": "NOW() - INTERVAL '14 minutes'",
"operation_number": "FAKE_OPERATION_121_RET",
"operation_type": "RET",
"transmission_format": "ERS",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package fr.gouv.cnsp.monitorfish.domain.use_cases.reporting

import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.verify
import fr.gouv.cnsp.monitorfish.domain.entities.alerts.type.MissingFARAlert
import fr.gouv.cnsp.monitorfish.domain.entities.alerts.type.ThreeMilesTrawlingAlert
import fr.gouv.cnsp.monitorfish.domain.entities.alerts.type.TwelveMilesFishingAlert
import fr.gouv.cnsp.monitorfish.domain.repositories.ReportingRepository
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.BDDMockito.given
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.junit.jupiter.SpringExtension

@ExtendWith(SpringExtension::class)
class ArchiveOutdatedReportingsUTests {
@MockBean
private lateinit var reportingRepository: ReportingRepository

@Test
fun `execute Should archive outdated reportings`() {
// Given
given(reportingRepository.findUnarchivedReportings()).willReturn(
listOf(
Pair(1, TwelveMilesFishingAlert("NAMO")),
Pair(2, ThreeMilesTrawlingAlert("NAMO")),
Pair(3, MissingFARAlert("NAMO")),
),
)

// When
ArchiveOutdatedReportings(reportingRepository).execute()

// Then
verify(reportingRepository).archiveReportings(eq(listOf(2, 3)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,13 @@ class GetVesselReportingsUTests {
val infractionSuspicionsSummary = result.summary.infractionSuspicionsSummary
assertThat(result.summary.infractionSuspicionsSummary).hasSize(4)
assertThat(infractionSuspicionsSummary[0].numberOfOccurrences).isEqualTo(2)
assertThat(infractionSuspicionsSummary[0].title).isEqualTo("12 milles - Pêche sans droits historiques (NATINF 2610)")
assertThat(
infractionSuspicionsSummary[0].title,
).isEqualTo("12 milles - Pêche sans droits historiques (NATINF 2610)")
assertThat(infractionSuspicionsSummary[1].numberOfOccurrences).isEqualTo(1)
assertThat(infractionSuspicionsSummary[1].title).isEqualTo("Non-emission de message \"FAR\" en 48h (NATINF 27689)")
assertThat(
infractionSuspicionsSummary[1].title,
).isEqualTo("Non-emission de message \"FAR\" en 48h (NATINF 27689)")
assertThat(infractionSuspicionsSummary[2].numberOfOccurrences).isEqualTo(1)
assertThat(
infractionSuspicionsSummary[2].title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fr.gouv.cnsp.monitorfish.infrastructure.database.repositories
import com.neovisionaries.i18n.CountryCode
import fr.gouv.cnsp.monitorfish.config.MapperConfiguration
import fr.gouv.cnsp.monitorfish.domain.entities.alerts.PendingAlert
import fr.gouv.cnsp.monitorfish.domain.entities.alerts.type.AlertTypeMapping
import fr.gouv.cnsp.monitorfish.domain.entities.alerts.type.ThreeMilesTrawlingAlert
import fr.gouv.cnsp.monitorfish.domain.entities.reporting.*
import fr.gouv.cnsp.monitorfish.domain.entities.reporting.filters.ReportingFilter
Expand Down Expand Up @@ -423,11 +424,23 @@ class JpaReportingRepositoryITests : AbstractDBTests() {

@Test
@Transactional
fun `findReportingUnarchivedAfterDEPLogbookMessage Should return archive candidates`() {
fun `findUnarchivedReportings Should return archive candidates`() {
// When
val reportings = jpaReportingRepository.findUnarchivedReportingsAfterNewVesselTrip()
val reportings = jpaReportingRepository.findUnarchivedReportings()

// Then
assertThat(reportings).hasSize(1)
assertThat(reportings.first().first).isEqualTo(1)
assertThat(reportings.first().second.type).isEqualTo(AlertTypeMapping.THREE_MILES_TRAWLING_ALERT)
}

@Test
@Transactional
fun `archiveReportings Should archive reportings`() {
// When
val archivedReportings = jpaReportingRepository.archiveReportings(listOf(1))

// Then
assertThat(archivedReportings).isEqualTo(1)
}
}

0 comments on commit 5cdb5ff

Please sign in to comment.