Skip to content
Draft
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
9 changes: 9 additions & 0 deletions pipeline/src/entities/pnos.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class PnoToRender:
is_verified: bool
is_being_sent: bool
source: PnoSource
is_correction: bool
previous_notification_date_utc: datetime | None

def __post_init__(self):
datetime_attrs = [
Expand All @@ -70,6 +72,7 @@ def __post_init__(self):
"predicted_arrival_datetime_utc",
"predicted_landing_datetime_utc",
"last_control_datetime_utc",
"previous_notification_date_utc",
]

for att in datetime_attrs:
Expand All @@ -85,6 +88,7 @@ def __post_init__(self):
"vessel_length",
"risk_factor",
"last_control_datetime_utc",
"previous_notification_date_utc",
]

for att in nullables_to_correct:
Expand Down Expand Up @@ -131,6 +135,8 @@ class PreRenderedPno:
source: PnoSource
purpose_suffix: str
is_zero: bool
is_correction: bool
previous_notification_date_utc: datetime | None

@staticmethod
def assert_equal(left: object, right: object):
Expand Down Expand Up @@ -225,6 +231,9 @@ class RenderedPno:
control_units: List[ControlUnit] | None = None
additional_addressees: List[PnoAddressee] = None
is_zero: bool = False
is_correction: bool = False
previous_notification_date_utc: datetime | None = None


def get_addressees(
self, communication_means: CommunicationMeans
Expand Down
11 changes: 9 additions & 2 deletions pipeline/src/flows/distribute_pnos.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ def format_sr_list(sr_list: list) -> str:
source=pno.source,
is_landing=pno.purpose == "LAN",
purpose_suffix=purpose_suffix,
is_zero=is_prior_notification_zero(pno)
is_zero=is_prior_notification_zero(pno),
is_correction=pno.is_correction,
previous_notification_date_utc=pno.previous_notification_date_utc,
)


Expand Down Expand Up @@ -489,6 +491,8 @@ def format_nullable_datetime(d: datetime, format: str = date_format):
is_landing=pno.is_landing,
purpose_suffix=pno.purpose_suffix,
is_zero=pno.is_zero,
is_correction=pno.is_correction,
previous_notification_date_utc=pno.previous_notification_date_utc,
)

html_email_body = email_body_template.render(
Expand All @@ -514,7 +518,9 @@ def format_nullable_datetime(d: datetime, format: str = date_format):
is_landing=pno.is_landing,
purpose_suffix=pno.purpose_suffix,
purpose=pno.purpose,
is_zero=pno.is_zero
is_zero=pno.is_zero,
is_correction=pno.is_correction,
previous_notification_date_utc=pno.previous_notification_date_utc,
)

sms_date_format = "%d/%m/%Y, %Hh%M UTC"
Expand All @@ -533,6 +539,7 @@ def format_nullable_datetime(d: datetime, format: str = date_format):
port_name=pno.port_name,
note=pno.note,
is_zero=pno.is_zero,
is_correction=pno.is_correction,
)

pdf = weasyprint.HTML(string=html_for_pdf).write_pdf(
Expand Down
25 changes: 23 additions & 2 deletions pipeline/src/queries/monitorfish/pnos_to_generate.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ last_controls AS (
LEFT JOIN LATERAL jsonb_array_elements(last_control_infractions) AS inf
ON true
GROUP BY 1, 2, 3
),

last_successful_sends AS (
SELECT
prior_notification_report_id,
prior_notification_source,
MAX(date_time_utc) AS last_successful_send_date_utc
FROM prior_notification_sent_messages
WHERE success = true
GROUP BY prior_notification_report_id, prior_notification_source
)

(SELECT DISTINCT ON (r.report_id) -- In rare cases the same PNO with the same data and the same report_id is sent multiple times in messages with different operation numbers
Expand Down Expand Up @@ -67,14 +77,20 @@ last_controls AS (
COALESCE(lc.last_control_infractions, '[]'::jsonb) AS last_control_infractions,
(value->>'isVerified')::BOOLEAN AS is_verified,
(value->>'isBeingSent')::BOOLEAN AS is_being_sent,
'LOGBOOK' AS source
'LOGBOOK' AS source,
r.operation_type = 'COR' AS is_correction,
lss.last_successful_send_date_utc AS previous_notification_date_utc
FROM logbook_reports r
LEFT JOIN vessels v
ON v.cfr = r.cfr
LEFT JOIN last_controls lc
ON lc.cfr = r.cfr
LEFT JOIN ports p
ON p.locode = r.value->>'port'
LEFT JOIN last_successful_sends lss
ON r.operation_type = 'COR'
AND lss.prior_notification_report_id = r.referenced_report_id
AND lss.prior_notification_source = 'LOGBOOK'
WHERE
operation_datetime_utc >= :start_datetime_utc
AND operation_datetime_utc < :end_datetime_utc
Expand Down Expand Up @@ -133,14 +149,19 @@ UNION ALL
COALESCE(lc.last_control_infractions, '[]'::jsonb) AS last_control_infractions,
(value->>'isVerified')::BOOLEAN AS is_verified,
(value->>'isBeingSent')::BOOLEAN AS is_being_sent,
'MANUAL' AS source
'MANUAL' AS source,
lss.last_successful_send_date_utc IS NOT NULL AS is_correction,
lss.last_successful_send_date_utc AS previous_notification_date_utc
FROM manual_prior_notifications r
LEFT JOIN vessels v
ON v.id = r.vessel_id
LEFT JOIN last_controls lc
ON lc.vessel_id = r.vessel_id
LEFT JOIN ports p
ON p.locode = r.value->>'port'
LEFT JOIN last_successful_sends lss
ON lss.prior_notification_report_id = r.report_id
AND lss.prior_notification_source = 'MANUAL'
WHERE
(
(value->>'isInvalidated') IS NULL
Expand Down
2 changes: 1 addition & 1 deletion pipeline/src/sms/prior_notification.jinja
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PNO{{ " zéro" if is_zero else "" }} à {{ port_name }}
PNO{{ " zéro" if is_zero else "" }}{{ " COR" if is_correction else "" }} à {{ port_name }}
- {{ vessel_name }}{{ " ({})".format(cfr) if cfr }}
{{ "- Segment(s) {}".format(trip_segments) if trip_segments }}
- Note de risque {{ "{:.2f}".format(risk_factor) }}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PNO à Somewhere over the top
PNO COR à Somewhere over the top
- CAPITAINE HADDOCK (ABC000000000)

- Note de risque 1.74
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
DELETE FROM public.prior_notification_sent_messages;

-- Add successful send for original report #15 so the COR correction will have is_correction = true
INSERT INTO public.prior_notification_sent_messages (
prior_notification_report_id,
prior_notification_source,
success,
date_time_utc,
communication_means,
recipient_address_or_number,
recipient_name,
recipient_organization,
error_message
) VALUES (
'15',
'LOGBOOK',
true,
((now() AT TIME ZONE 'UTC') - INTERVAL '1 month 35 minutes')::TIMESTAMP,
'EMAIL',
'test@example.org',
'Test Recipient',
'Test Organization',
NULL
),
-- Add successful send for manual PNO report 00000000-0000-4000-0000-000000000007 so it will have is_correction = true
(
'00000000-0000-4000-0000-000000000007',
'MANUAL',
true,
((now() AT TIME ZONE 'UTC') - INTERVAL '20 minutes')::TIMESTAMP,
'EMAIL',
'test@example.org',
'Test Recipient',
'Test Organization',
NULL
);
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ INSERT INTO public.manual_prior_notifications (
('00000000-0000-4000-0000-000000000003', NULL, 6, NOW() AT TIME ZONE 'UTC' - INTERVAL '3 months', false, 'FRA', NOW() AT TIME ZONE 'UTC' - INTERVAL '3 months', 'null', 'null', 'I DO 4H REPORT', '{"riskFactor": 2.8, "isBeingSent": true, "isInVerificationScope": false, "isSent": true, "isVerified": true, "catchOnboard":[{"faoZone": "37.1","weight": 172,"nbFish": 3,"species": "BFT"}],"catchToLand":[{"faoZone": "21.1.A","weight": 72,"nbFish": null,"species": "SOS"}],"note": null,"pnoTypes":[{"pnoTypeName": "Préavis type A","minimumNotificationPeriod": 4,"hasDesignatedPorts": false}],"port": "FRDPE","predictedArrivalDatetimeUtc": "2021-05-06T07:41:03.340Z","predictedLandingDatetimeUtc": "2021-05-06T11:41:03.340Z","purpose": "LAN","tripStartDate": "2021-05-04T11:41:03.340Z"}'),
('00000000-0000-4000-0000-000000000004', NULL, 6, NOW() AT TIME ZONE 'UTC' - INTERVAL '3 weeks', false, 'FRA', NOW() AT TIME ZONE 'UTC' - INTERVAL '3 weeks', NULL, NULL, 'I DO 4H REPORT', '{"riskFactor": 3.1, "isBeingSent": false, "isInVerificationScope": false, "isSent": true, "isVerified": false, "note": null,"pnoTypes":[{"pnoTypeName": "Préavis type A","minimumNotificationPeriod": 4,"hasDesignatedPorts": false}],"port": "FRDKK","predictedArrivalDatetimeUtc": "2021-05-06T07:41:03.340Z","predictedLandingDatetimeUtc": "2021-05-06T11:41:03.340Z","purpose": "LAN","tripStartDate": "2021-05-04T11:41:03.340Z"}'),
('00000000-0000-4000-0000-000000000005', NULL, 6, NOW() AT TIME ZONE 'UTC' - INTERVAL '3 days', false, 'FRA', NOW() AT TIME ZONE 'UTC' - INTERVAL '3 days', '[{"gear": "LNP"}, {"gear": "OTM", "mesh": 80}]', '[{"segment": "NWW09","segmentName": "Lignes"}, {"segment": "SWW01","segmentName": "Chaluts de fond"}]', 'I DO 4H REPORT', '{"riskFactor": 3.8, "isBeingSent": true, "isInVerificationScope": true, "isSent": true, "isVerified": true, "catchOnboard":[{"faoZone": "21.1.a","weight": 72,"nbFish": null,"species": "GHL"}, {"faoZone": "21.1.a","weight": 172,"nbFish": null,"species": "GHL"}, {"faoZone": "21.1.a","weight": 72,"nbFish": 3,"species": "BFT"}, {"faoZone": "27.2.a","weight": 32,"nbFish": 2,"species": "BFT"}, {"faoZone": "27.2.a","weight" : 502,"nbFish": 2,"species": "SWO"}, {"faoZone": "27.2.a","weight": 202,"nbFish": 1,"species": "SWO"}],"catchToLand":[{"faoZone": "21.1.a","weight": 72,"nbFish": null,"species": "GHL"}, {"faoZone": "21.1.a","weight": 172,"nbFish": null,"species": "GHL"}, {"faoZone": "21.1.a","weight": 72,"nbFish": 3,"species": "BFT"}, {"faoZone": "27.2.a","weight": 32,"nbFish": 2,"species": "BFT"}, {"faoZone": "27.2.a","weight": 502,"nbFish": 2,"species": "SWO"}, {"faoZone": "27.2.a","weight": 202,"nbFish": 1,"species": "SWO"}],"note": "Ceci est une note de préavis manuel","pnoTypes":[{"pnoTypeName": "Préavis type A","minimumNotificationPeriod": 4,"hasDesignatedPorts": false}, {"pnoTypeName": "Préavis type B","minimumNotificationPeriod": 4,"hasDesignatedPorts": true}],"port": "FRLEH","predictedArrivalDatetimeUtc": "2021-05-06T07:41:03.340Z","predictedLandingDatetimeUtc": "2021-05-06T11:41:03.340Z","purpose": "LAN","tripStartDate": "2021-05-04T11:41:03.340Z", "isInvalidated": false}'),
('00000000-0000-4000-0000-000000000006', NULL, 6, NOW() AT TIME ZONE 'UTC' - INTERVAL '3 days', false, 'FRA', NOW() AT TIME ZONE 'UTC' - INTERVAL '3 days', '[{"gear": "LNP"}, {"gear": "OTM", "mesh": 80}]', '[{"segment": "NWW09","segmentName": "Lignes"}, {"segment": "SWW01","segmentName": "Chaluts de fond"}]', 'I DO 4H REPORT', '{"riskFactor": 3.8, "isBeingSent": true, "isInVerificationScope": true, "isSent": true, "isVerified": true, "catchOnboard":[{"faoZone": "21.1.a","weight": 72,"nbFish": null,"species": "GHL"}, {"faoZone": "21.1.a","weight": 172,"nbFish": null,"species": "GHL"}, {"faoZone": "21.1.a","weight": 72,"nbFish": 3,"species": "BFT"}, {"faoZone": "27.2.a","weight": 32,"nbFish": 2,"species": "BFT"}, {"faoZone": "27.2.a","weight" : 502,"nbFish": 2,"species": "SWO"}, {"faoZone": "27.2.a","weight": 202,"nbFish": 1,"species": "SWO"}],"catchToLand":[{"faoZone": "21.1.a","weight": 72,"nbFish": null,"species": "GHL"}, {"faoZone": "21.1.a","weight": 172,"nbFish": null,"species": "GHL"}, {"faoZone": "21.1.a","weight": 72,"nbFish": 3,"species": "BFT"}, {"faoZone": "27.2.a","weight": 32,"nbFish": 2,"species": "BFT"}, {"faoZone": "27.2.a","weight": 502,"nbFish": 2,"species": "SWO"}, {"faoZone": "27.2.a","weight": 202,"nbFish": 1,"species": "SWO"}],"note": "Ceci est une note de préavis manuel","pnoTypes":[{"pnoTypeName": "Préavis type A","minimumNotificationPeriod": 4,"hasDesignatedPorts": false}, {"pnoTypeName": "Préavis type B","minimumNotificationPeriod": 4,"hasDesignatedPorts": true}],"port": "FRLEH","predictedArrivalDatetimeUtc": "2021-05-06T07:41:03.340Z","predictedLandingDatetimeUtc": "2021-05-06T11:41:03.340Z","purpose": "LAN","tripStartDate": "2021-05-04T11:41:03.340Z", "isInvalidated": true}');
('00000000-0000-4000-0000-000000000006', NULL, 6, NOW() AT TIME ZONE 'UTC' - INTERVAL '3 days', false, 'FRA', NOW() AT TIME ZONE 'UTC' - INTERVAL '3 days', '[{"gear": "LNP"}, {"gear": "OTM", "mesh": 80}]', '[{"segment": "NWW09","segmentName": "Lignes"}, {"segment": "SWW01","segmentName": "Chaluts de fond"}]', 'I DO 4H REPORT', '{"riskFactor": 3.8, "isBeingSent": true, "isInVerificationScope": true, "isSent": true, "isVerified": true, "catchOnboard":[{"faoZone": "21.1.a","weight": 72,"nbFish": null,"species": "GHL"}, {"faoZone": "21.1.a","weight": 172,"nbFish": null,"species": "GHL"}, {"faoZone": "21.1.a","weight": 72,"nbFish": 3,"species": "BFT"}, {"faoZone": "27.2.a","weight": 32,"nbFish": 2,"species": "BFT"}, {"faoZone": "27.2.a","weight" : 502,"nbFish": 2,"species": "SWO"}, {"faoZone": "27.2.a","weight": 202,"nbFish": 1,"species": "SWO"}],"catchToLand":[{"faoZone": "21.1.a","weight": 72,"nbFish": null,"species": "GHL"}, {"faoZone": "21.1.a","weight": 172,"nbFish": null,"species": "GHL"}, {"faoZone": "21.1.a","weight": 72,"nbFish": 3,"species": "BFT"}, {"faoZone": "27.2.a","weight": 32,"nbFish": 2,"species": "BFT"}, {"faoZone": "27.2.a","weight": 502,"nbFish": 2,"species": "SWO"}, {"faoZone": "27.2.a","weight": 202,"nbFish": 1,"species": "SWO"}],"note": "Ceci est une note de préavis manuel","pnoTypes":[{"pnoTypeName": "Préavis type A","minimumNotificationPeriod": 4,"hasDesignatedPorts": false}, {"pnoTypeName": "Préavis type B","minimumNotificationPeriod": 4,"hasDesignatedPorts": true}],"port": "FRLEH","predictedArrivalDatetimeUtc": "2021-05-06T07:41:03.340Z","predictedLandingDatetimeUtc": "2021-05-06T11:41:03.340Z","purpose": "LAN","tripStartDate": "2021-05-04T11:41:03.340Z", "isInvalidated": true}'),
-- Manual PNO that will have is_correction = true (has been successfully sent before)
('00000000-0000-4000-0000-000000000007', 'ABC000306959', 2, NOW() AT TIME ZONE 'UTC' - INTERVAL '10 minutes', false, 'FRA', NOW() AT TIME ZONE 'UTC' - INTERVAL '10 minutes', '[{"gear": "OTB", "mesh": 140, "dimensions": "250.0"}]', '[]', 'ÉTABLIR IMPRESSION LORSQUE', '{"riskFactor": 2.14443662414848, "isBeingSent": true, "isInVerificationScope": false, "isSent": false, "isVerified": false, "catchOnboard": [{"faoZone": "27.8.a", "weight": 150.0, "nbFish": null, "species": "GHL"}, {"faoZone": "27.8.a", "weight": 1450.0, "nbFish": null, "species": "HKE"}], "catchToLand": [{"faoZone": "27.8.a", "weight": 150.0, "nbFish": null, "species": "GHL"}, {"faoZone": "27.8.a", "weight": 1450.0, "nbFish": null, "species": "HKE"}], "note": null, "pnoTypes": [{"pnoTypeName": "Préavis type 2", "minimumNotificationPeriod": 4.0, "hasDesignatedPorts": true}], "port": "FRDKK", "purpose": "LAN", "predictedArrivalDatetimeUtc": "2020-05-06T11:41:03.340Z", "predictedLandingDatetimeUtc": "2020-05-06T15:41:03.340Z", "tripStartDate": "2020-05-04T19:41:03.340Z", "isInvalidated": false}');
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
('13', '<ERS>Message ERS xml</ERS>'),
('14', '<ERS>Message ERS xml</ERS>'),
('15', '<ERS>Message ERS xml</ERS>'),
('15b', '<ERS>Message ERS xml</ERS>'),
('16', '<ERS>Message ERS xml</ERS>'),
('17', '<ERS>Message ERS xml</ERS>'),
('18', '<ERS>Message ERS xml</ERS>'),
Expand Down Expand Up @@ -211,9 +212,18 @@
'15', null, ((now() AT TIME ZONE 'UTC') - INTERVAL '1 month 34 minutes')::TIMESTAMP,
'___TARGET___', 'TRGT', 'TARGET', 'NAVIRE CIBLE', 'FRA', null, 'PNO',
'{"port": "FRDPE", "purpose": "LAN", "catchOnboard": [{"nbFish": null, "faoZone": "27.8.a", "weight": 150.0, "species": "GHL"}, {"nbFish": null, "faoZone": "27.8.a", "weight": 1450.0, "species": "HKE"}, {"nbFish": 2, "faoZone": "27.8.a", "weight": 150.0, "species": "BFT"}, {"nbFish": 2, "faoZone": "27.8.a", "weight": 70.0, "species": "SWO"}, {"nbFish": 2, "faoZone": "27.8.b", "weight": 150.0, "species": "BFT"}, {"nbFish": null, "faoZone": "27.8.b", "weight": 250.0, "species": "GHL"}], "tripStartDate": "2020-05-04T19:41:03.340Z", "predictedArrivalDatetimeUtc": "2020-05-06T11:41:03.340Z", "pnoTypes": [], "isInVerificationScope": false, "isVerified": false, "isSent": false, "isBeingSent": true}',
((now() AT TIME ZONE 'UTC') - INTERVAL '1 month 30 minutes')::TIMESTAMP, '20510003', 'ERS',

Check failure on line 215 in pipeline/tests/test_data/remote_database/V666.5__Reset_test_logbook.sql

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal 3 times.

See more on https://sonarcloud.io/project/issues?id=MTES-MCT_monitorfish&issues=AZ3UnNTOJlDI95Viqylw&open=AZ3UnNTOJlDI95Viqylw&pullRequest=4982
true, '[{"gear": "OTB", "mesh": 140, "dimensions": "250.0"}]', '[]'
),
-- CORRECTION PNO - This will set is_correction = true
(
'15b', 'OOF', ((now() AT TIME ZONE 'UTC') - INTERVAL '1 month 28 minutes')::TIMESTAMP, 'COR',
'15', '15', ((now() AT TIME ZONE 'UTC') - INTERVAL '1 month 30 minutes')::TIMESTAMP,
'___TARGET___', 'TRGT', 'TARGET', 'NAVIRE CIBLE', 'FRA', null, 'PNO',
'{"port": "FRDPE", "purpose": "LAN", "catchOnboard": [{"nbFish": null, "faoZone": "27.8.a", "weight": 150.0, "species": "GHL"}, {"nbFish": null, "faoZone": "27.8.a", "weight": 1450.0, "species": "HKE"}, {"nbFish": 2, "faoZone": "27.8.a", "weight": 150.0, "species": "BFT"}, {"nbFish": 2, "faoZone": "27.8.a", "weight": 70.0, "species": "SWO"}, {"nbFish": 2, "faoZone": "27.8.b", "weight": 150.0, "species": "BFT"}, {"nbFish": null, "faoZone": "27.8.b", "weight": 250.0, "species": "GHL"}], "tripStartDate": "2020-05-04T19:41:03.340Z", "predictedArrivalDatetimeUtc": "2020-05-06T11:41:03.340Z", "pnoTypes": [], "isInVerificationScope": false, "isVerified": false, "isSent": false, "isBeingSent": true}',
((now() AT TIME ZONE 'UTC') - INTERVAL '1 month 26 minutes')::TIMESTAMP, '20510003', 'ERS',
true, '[{"gear": "OTB", "mesh": 140, "dimensions": "250.0"}]', '[]'
),
(
'16', 'OOE', ((now() AT TIME ZONE 'UTC') - INTERVAL '1 month 1 hour')::TIMESTAMP, 'RET',
NULL, '11', NULL,
Expand Down
Loading
Loading