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

Préavis - Corrige la liste lorsque les messages corrigés ont des arrivées prévues éloignées #3719

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
18 changes: 18 additions & 0 deletions backend/src/main/kotlin/fr/gouv/cnsp/monitorfish/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.gouv.cnsp.monitorfish

import java.time.ZonedDateTime

class Utils {
companion object {
/**
Expand All @@ -16,5 +18,21 @@ class Utils {

return normalizedLeftString == normalizedRightString
}

/**
* Checks if the ZonedDateTime is between the start and end times.
*/
fun isZonedDateTimeBetween(
zonedDateTime: ZonedDateTime,
start: ZonedDateTime,
end: ZonedDateTime,
isInclusive: Boolean = false,
): Boolean {
return if (isInclusive) {
zonedDateTime >= start && zonedDateTime <= end
} else {
zonedDateTime > start && zonedDateTime < end
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,36 @@ class JpaLogbookReportRepository(
.toSet()

return logbookReportsWithDatCorAndDel
.filter { report ->
// Exclude reports that are referenced by other reports or have a DEL operation type
report.operationType != LogbookOperationType.DEL && report.reportId !in referencedReportIds
}
// Exclude reports that are referenced by other reports or have a DEL operation type
.filter { it.operationType != LogbookOperationType.DEL && it.reportId !in referencedReportIds }
.map { report ->
val pno = PriorNotification.fromLogbookMessage(report.toLogbookMessage(objectMapper))
// All messages returned from the SQL query are acknowledged
pno.markAsAcknowledged()

return@map pno
}
// We filter by predicted arrival date here rather than in the SQL query
// because the DAT or COR predicted arrival dates can be far away from each other
// which is quite complicated to handle in pure SQL.
//
// Example: if a DAT that has a `predictedArrivalDatetimeUtc` on DAY 2 at 4pm
// is corrected by a COR with a `predictedArrivalDatetimeUtc` on DAY 1 at 4pm,
// filtering (in the SQL) between `willArriveAfter` = DAY 2 at 3pm and `willArriveBefore` = DAY 2 at 5pm
// would only return the DAT without including the related COR.
//
// /!\ This is not foolproof:
// A difference of more than 48h between DAT and COR `predictedArrivalDatetimeUtc` will still cause issues.
.filter {
it.logbookMessageAndValue.value.predictedArrivalDatetimeUtc?.let { predictedArrivalDatetimeUtc ->
Utils.isZonedDateTimeBetween(
predictedArrivalDatetimeUtc,
ZonedDateTime.parse(filter.willArriveAfter),
ZonedDateTime.parse(filter.willArriveBefore),
isInclusive = true,
)
} == true
}
}

@Cacheable(value = ["pno_to_verify"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ interface DBLogbookReportRepository :
unaccent(lower(lr.vessel_name)) ILIKE CONCAT('%', unaccent(lower(:searchQuery)), '%') OR
lower(lr.cfr) ILIKE CONCAT('%', lower(:searchQuery), '%')
)

-- Will Arrive After
AND lr.value->>'predictedArrivalDatetimeUtc' >= :willArriveAfter

-- Will Arrive Before
AND lr.value->>'predictedArrivalDatetimeUtc' <= :willArriveBefore
),

distinct_cfrs AS (
Expand Down Expand Up @@ -203,7 +197,6 @@ interface DBLogbookReportRepository :
),

dels_targeting_searched_pno AS (

-- A DEL message has no flag_state, which we need to acknowledge messages of non french vessels.
-- So we use the flag_state of the deleted message.
SELECT del.referenced_report_id, del.operation_number, searched_pno.flag_state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ INSERT INTO vessels (id, cfr, mmsi, ircs, external_immatriculation, vessel_name,
INSERT INTO vessels (id, cfr, mmsi, ircs, external_immatriculation, vessel_name, flag_state, length, under_charter) VALUES (128, 'CFR128', 'MMSI128', 'IRCS128', 'EXTIMM128', 'THE FLOATING KANGAROO', 'AU', 31, false);

INSERT INTO vessels (id, cfr, mmsi, ircs, external_immatriculation, vessel_name, flag_state, length, under_charter) VALUES (129, 'CFR129', 'MMSI129', 'IRCS129', 'EXTIMM129', 'BON VENT', 'FR', 34.5, false);

INSERT INTO vessels (id, cfr, mmsi, ircs, external_immatriculation, vessel_name, flag_state, length, under_charter) VALUES (130, 'CFR130', 'MMSI130', 'IRCS130', 'EXTIMM130', 'L''HIPPO CAMPE', 'FR', 19.2, false);
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ INSERT INTO logbook_raw_messages (operation_number, xml_message) VALUES ('FAKE_O

INSERT INTO logbook_raw_messages (operation_number, xml_message) VALUES ('FAKE_OPERATION_121_RET', '<Flux>Message FLUX xml</Flux>');

INSERT INTO logbook_raw_messages (operation_number, xml_message) VALUES ('FAKE_OPERATION_122', '<Flux>Message FLUX xml</Flux>');

INSERT INTO logbook_raw_messages (operation_number, xml_message) VALUES ('FAKE_OPERATION_122_RET', '<Flux>Message FLUX xml</Flux>');

INSERT INTO logbook_raw_messages (operation_number, xml_message) VALUES ('FAKE_OPERATION_122_COR', '<Flux>Message FLUX xml</Flux>');

INSERT INTO logbook_raw_messages (operation_number, xml_message) VALUES ('FAKE_OPERATION_122_COR_RET', '<Flux>Message FLUX xml</Flux>');

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, vessel_name, trip_gears, trip_segments, value) VALUES (101, 'FAKE_OPERATION_101', NULL, 'FAK000999999', true, 'FRA', NOW() AT TIME ZONE 'UTC' - INTERVAL '15 minutes', 'PNO', NOW() AT TIME ZONE 'UTC' - INTERVAL '15 minutes', 'FAKE_OPERATION_101', 'DAT', NOW() AT TIME ZONE 'UTC' - INTERVAL '15 minutes', 'JT/VISIOCaptures V1.4.7', 'ERS', 'PHENOMENE', '[{"gear":"TBN","mesh":100,"dimensions":"250;180"},{"gear":"OTT","mesh":120.5,"dimensions":"250;280"}]', '[{"segment":"SWW04","segmentName":"Chaluts pélagiques"},{"segment":"SWW06","segmentName":"Sennes"}]', '{"riskFactor":2.1,"catchOnboard":[{"weight":25,"nbFish":null,"species":"COD","faoZone":"27.8.a","effortZone":"C","economicZone":"FRA","statisticalRectangle":"23E6"}],"isBeingSent":false,"isInVerificationScope":false,"isSent":false,"isVerified":false,"pnoTypes":[{"pnoTypeName":"Préavis type A","minimumNotificationPeriod":4,"hasDesignatedPorts":false},{"pnoTypeName":"Préavis type B","minimumNotificationPeriod":8,"hasDesignatedPorts":true}],"port":"FRSML","predictedArrivalDatetimeUtc":null,"predictedLandingDatetimeUtc":null,"purpose":"LAN","tripStartDate":null}');
UPDATE logbook_reports SET value = JSONB_SET(value, '{predictedArrivalDatetimeUtc}', TO_JSONB(TO_CHAR(NOW() AT TIME ZONE 'UTC' + INTERVAL '3 hours', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')), true) WHERE id = 101;
UPDATE logbook_reports SET value = JSONB_SET(value, '{predictedLandingDatetimeUtc}', TO_JSONB(TO_CHAR(NOW() AT TIME ZONE 'UTC' + INTERVAL '3.5 hours', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')), true) WHERE id = 101;
Expand Down Expand Up @@ -241,3 +249,11 @@ INSERT INTO logbook_reports (id, report_id, referenced_report_id, integration_da
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() - INTERVAL '14 minutes', NOW() - INTERVAL '14 minutes', 'FAKE_OPERATION_121_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 (122, 'FAKE_OPERATION_122', NULL, 'CFR130', true, 'FRA', '2024-09-01 13:00:00', 'PNO', '2024-09-01 13:00:00', 'FAKE_OPERATION_122', 'DAT', '2024-09-01 13:00:00', 'TurboCatch (3.7-1)', 'ERS', NULL, NULL, 'L''HIPPO CAMPE', '{"riskFactor":2.9,"catchOnboard":[{"weight":150,"nbFish":null,"species":"ANF","faoZone":"27.8.a","effortZone":"C","economicZone":"FRA","statisticalRectangle":"23E6"}],"pnoTypes":[{"pnoTypeName":"Préavis type Z","minimumNotificationPeriod":4,"hasDesignatedPorts":false}],"port":"BROIA","predictedArrivalDatetimeUtc":"2024-09-02T21:00:00Z","predictedLandingDatetimeUtc":"2024-09-02T21:30:00Z","purpose":"LAN","tripStartDate":"2024-09-01T06:00:00Z"}');

INSERT INTO logbook_reports (id, report_id, referenced_report_id, integration_datetime_utc, operation_datetime_utc, operation_number, operation_type, transmission_format, value) VALUES (1122, NULL, 'FAKE_OPERATION_122', '2024-09-01 13:05:00', '2024-09-01 13:05:00', 'FAKE_OPERATION_122_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 (2122, 'FAKE_OPERATION_122_COR', 'FAKE_OPERATION_122', 'CFR130', true, 'FRA', '2024-09-01 13:20:00', 'PNO', '2024-09-01 13:20:00', 'FAKE_OPERATION_122', 'COR', '2024-09-01 13:20:00', 'TurboCatch (3.7-1)', 'ERS', NULL, NULL, 'L''HIPPO CAMPE', '{"riskFactor":2.9,"catchOnboard":[{"weight":150,"nbFish":null,"species":"ANF","faoZone":"27.8.a","effortZone":"C","economicZone":"FRA","statisticalRectangle":"23E6"}],"pnoTypes":[{"pnoTypeName":"Préavis type Z","minimumNotificationPeriod":4,"hasDesignatedPorts":false}],"port":"BROIA","predictedArrivalDatetimeUtc":"2024-09-01T15:00:00Z","predictedLandingDatetimeUtc":"2024-09-01T15:30:00Z","purpose":"LAN","tripStartDate":"2024-09-01T06:00:00Z"}');

INSERT INTO logbook_reports (id, report_id, referenced_report_id, integration_datetime_utc, operation_datetime_utc, operation_number, operation_type, transmission_format, value) VALUES (3122, NULL, 'FAKE_OPERATION_122_COR', '2024-09-01 13:25:00', '2024-09-01 13:25:00', 'FAKE_OPERATION_122_COR_RET', 'RET', 'ERS', '{"returnStatus":"000"}');
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,19 @@
"flag_state": "FR",
"length": 34.5,
"under_charter": false
},

// - Vessel: L'HIPPO CAMPE
{
"id": 130,
"cfr": "CFR130",
"mmsi": "MMSI130",
"ircs": "IRCS130",
"external_immatriculation": "EXTIMM130",
"vessel_name": "L'HIPPO CAMPE",
"flag_state": "FR",
"length": 19.2,
"under_charter": false
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@
{ "operation_number": "FAKE_OPERATION_120", "xml_message": "<Flux>Message FLUX xml</Flux>" },
{ "operation_number": "FAKE_OPERATION_121", "xml_message": "<Flux>Message FLUX xml</Flux>" },
{ "operation_number": "FAKE_OPERATION_120_RET", "xml_message": "<Flux>Message FLUX xml</Flux>" },
{ "operation_number": "FAKE_OPERATION_121_RET", "xml_message": "<Flux>Message FLUX xml</Flux>" }
{ "operation_number": "FAKE_OPERATION_121_RET", "xml_message": "<Flux>Message FLUX xml</Flux>" },
{ "operation_number": "FAKE_OPERATION_122", "xml_message": "<Flux>Message FLUX xml</Flux>" },
{ "operation_number": "FAKE_OPERATION_122_RET", "xml_message": "<Flux>Message FLUX xml</Flux>" },
{ "operation_number": "FAKE_OPERATION_122_COR", "xml_message": "<Flux>Message FLUX xml</Flux>" },
{ "operation_number": "FAKE_OPERATION_122_COR_RET", "xml_message": "<Flux>Message FLUX xml</Flux>" }
]
},
{
Expand Down Expand Up @@ -1528,7 +1532,7 @@
"trip_segments": null,
"vessel_name": "MARIAGE ÎLE HASARD",
"value:jsonb": {
"gearOnboard": [{"gear": "GTR", "mesh": 100.0}],
"gearOnboard": [{ "gear": "GTR", "mesh": 100.0 }],
"departurePort": "AEJAZ",
"anticipatedActivity": "FSH",
"tripStartDate": "NOW() - INTERVAL '15 minutes'",
Expand All @@ -1547,6 +1551,126 @@
"value:jsonb": {
"returnStatus": "000"
}
},

// - Vessel: L'HIPPO CAMPE
// - Flag state: FR
// - DAT with a predicted arrival date 30h after the COR predicted arrival
{
"id": 122,
"report_id": "FAKE_OPERATION_122",
"referenced_report_id": null,
"cfr": "CFR130",
"enriched": true,
"flag_state": "FRA",
"integration_datetime_utc": "2024-09-01 13:00:00",
"log_type": "PNO",
"operation_datetime_utc": "2024-09-01 13:00:00",
"operation_number": "FAKE_OPERATION_122",
"operation_type": "DAT",
"report_datetime_utc": "2024-09-01 13:00:00",
"software": "TurboCatch (3.7-1)",
"transmission_format": "ERS",
"trip_gears": null,
"trip_segments": null,
"vessel_name": "L'HIPPO CAMPE",
"value:jsonb": {
"riskFactor": 2.9,
"catchOnboard": [
{
"weight": 150.0,
"nbFish": null,
"species": "ANF",
"faoZone": "27.8.a",
"effortZone": "C",
"economicZone": "FRA",
"statisticalRectangle": "23E6"
}
],
"pnoTypes": [
{
"pnoTypeName": "Préavis type Z",
"minimumNotificationPeriod": 4.0,
"hasDesignatedPorts": false
}
],
"port": "BROIA",
"predictedArrivalDatetimeUtc": "2024-09-02T21:00:00Z",
"predictedLandingDatetimeUtc": "2024-09-02T21:30:00Z",
"purpose": "LAN",
"tripStartDate": "2024-09-01T06:00:00Z"
}
},
{
"id": 1122,
"report_id": null,
"referenced_report_id": "FAKE_OPERATION_122",
"integration_datetime_utc": "2024-09-01 13:05:00",
"operation_datetime_utc": "2024-09-01 13:05:00",
"operation_number": "FAKE_OPERATION_122_RET",
"operation_type": "RET",
"transmission_format": "ERS",
"value:jsonb": {
"returnStatus": "000"
}
},
{
"id": 2122,
"report_id": "FAKE_OPERATION_122_COR",
"referenced_report_id": "FAKE_OPERATION_122",
"cfr": "CFR130",
"enriched": true,
"flag_state": "FRA",
"integration_datetime_utc": "2024-09-01 13:20:00",
"log_type": "PNO",
"operation_datetime_utc": "2024-09-01 13:20:00",
"operation_number": "FAKE_OPERATION_122",
"operation_type": "COR",
"report_datetime_utc": "2024-09-01 13:20:00",
"software": "TurboCatch (3.7-1)",
"transmission_format": "ERS",
"trip_gears": null,
"trip_segments": null,
"vessel_name": "L'HIPPO CAMPE",
"value:jsonb": {
"riskFactor": 2.9,
"catchOnboard": [
{
"weight": 150.0,
"nbFish": null,
"species": "ANF",
"faoZone": "27.8.a",
"effortZone": "C",
"economicZone": "FRA",
"statisticalRectangle": "23E6"
}
],
"pnoTypes": [
{
"pnoTypeName": "Préavis type Z",
"minimumNotificationPeriod": 4.0,
"hasDesignatedPorts": false
}
],
"port": "BROIA",
"predictedArrivalDatetimeUtc": "2024-09-01T15:00:00Z",
"predictedLandingDatetimeUtc": "2024-09-01T15:30:00Z",
"purpose": "LAN",
"tripStartDate": "2024-09-01T06:00:00Z"
}
},
{
"id": 3122,
"report_id": null,
"referenced_report_id": "FAKE_OPERATION_122_COR",
"integration_datetime_utc": "2024-09-01 13:25:00",
"operation_datetime_utc": "2024-09-01 13:25:00",
"operation_number": "FAKE_OPERATION_122_COR_RET",
"operation_type": "RET",
"transmission_format": "ERS",
"value:jsonb": {
"returnStatus": "000"
}
}
]
}
Expand Down
Loading
Loading