Skip to content

Commit 1029ceb

Browse files
author
herve.le-bars
committed
Revert "fix: repository_alert.py:24:97: E501 Line too long. Request must be tested"
This reverts commit b3c673e.
1 parent 4453b2c commit 1029ceb

File tree

1 file changed

+28
-113
lines changed

1 file changed

+28
-113
lines changed

bloom/bloom/infra/repositories/repository_alert.py

Lines changed: 28 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -19,125 +19,40 @@ def save_alerts(self, timestamp: datetime) -> None:
1919
with self.session_factory() as session:
2020
sql = text(
2121
f"""
22-
INSERT INTO
23-
alert(timestamp, vessel_id, cross_mpa, mpa_ids) (
24-
SELECT
25-
timestamp,
26-
vessel_id,
27-
(
28-
CAST(
29-
ST_Contains(mpa_fr_with_mn.geometry, current_position) AS INT
30-
) - CAST(
31-
ST_Contains(mpa_fr_with_mn.geometry, previous_position) AS INT
32-
)
33-
) as cross_mpa,
34-
ARRAY_AGG(
35-
mpa_fr_with_mn.index
36-
ORDER BY
37-
mpa_fr_with_mn.index DESC
38-
) AS mpa_ids
39-
FROM
40-
(
41-
SELECT
42-
spire_vessel_positions.vessel_id AS vessel_id,
43-
spire_vessel_positions.position AS current_position,
44-
spire_vessel_positions.timestamp AS timestamp,
45-
LAG(spire_vessel_positions.position) OVER (
46-
PARTITION BY spire_vessel_positions.vessel_id
47-
ORDER BY
48-
spire_vessel_positions.timestamp
49-
) AS previous_position
50-
FROM
51-
spire_vessel_positions
52-
WHERE
53-
spire_vessel_positions.timestamp >= TIMESTAMP '{timestamp}' - INTERVAL '15 minutes'
54-
AND spire_vessel_positions.timestamp < TIMESTAMP '{timestamp}' + INTERVAL '15 minutes'
55-
) AS foo
56-
CROSS JOIN mpa_fr_with_mn
57-
WHERE
58-
previous_position IS NOT NULL
59-
and ST_Contains(mpa_fr_with_mn.geometry, current_position)
60-
!=
61-
ST_Contains(mpa_fr_with_mn.geometry, previous_position)
62-
GROUP BY
63-
vessel_id,
64-
timestamp,
65-
cross_mpa
66-
);
67-
22+
INSERT INTO alert(timestamp,vessel_id,cross_mpa,mpa_ids)
23+
(
24+
SELECT timestamp, vessel_id, (CAST(ST_Contains(mpa_fr_with_mn.geometry,current_position) AS INT) - CAST(ST_Contains(mpa_fr_with_mn.geometry,previous_position) AS INT)) as cross_mpa, ARRAY_AGG(mpa_fr_with_mn.index ORDER BY mpa_fr_with_mn.index DESC) AS mpa_ids FROM
25+
(SELECT spire_vessel_positions.vessel_id AS vessel_id,
26+
spire_vessel_positions.position AS current_position,
27+
spire_vessel_positions.timestamp AS timestamp,
28+
LAG(spire_vessel_positions.position) OVER (PARTITION BY spire_vessel_positions.vessel_id ORDER BY spire_vessel_positions.timestamp) AS previous_position
29+
FROM spire_vessel_positions WHERE spire_vessel_positions.timestamp >= TIMESTAMP '{timestamp}' - INTERVAL '15 minutes' AND spire_vessel_positions.timestamp < TIMESTAMP '{timestamp}' + INTERVAL '15 minutes' ) AS foo
30+
CROSS JOIN mpa_fr_with_mn WHERE previous_position IS NOT NULL and ST_Contains(mpa_fr_with_mn.geometry,current_position) != ST_Contains(mpa_fr_with_mn.geometry,previous_position) GROUP BY vessel_id, timestamp,cross_mpa
31+
);
6832
""", # nosec: B608
6933
)
7034
session.execute(sql)
7135
session.commit()
7236
return
7337

74-
# an other query with the same result :
75-
# WITH cte_query1 AS (
76-
# SELECT
77-
# spire_vessel_positions.vessel_id AS vessel_id,
78-
# ARRAY_AGG(
79-
# mpa_fr_with_mn.index
80-
# ORDER BY
81-
# mpa_fr_with_mn.index DESC
82-
# ) AS mpa_ids
83-
# FROM
84-
# spire_vessel_positions
85-
# JOIN mpa_fr_with_mn ON ST_Contains(
86-
# mpa_fr_with_mn.geometry,
87-
# spire_vessel_positions.position
88-
# )
89-
# WHERE
90-
# spire_vessel_positions.timestamp = TO_TIMESTAMP('2023-11-17 12:00',
91-
# 'YYYY-MM-DD HH24:MI')
92-
# GROUP BY
93-
# vessel_id
94-
# ),
95-
# cte_query2 AS (
96-
# SELECT
97-
# DISTINCT spire_vessel_positions.vessel_id AS vessel_id,
98-
# ARRAY_AGG(
99-
# mpa_fr_with_mn.index
100-
# ORDER BY
101-
# mpa_fr_with_mn.index DESC
102-
# ) AS mpa_ids
103-
# FROM
104-
# spire_vessel_positions
105-
# JOIN mpa_fr_with_mn ON ST_Contains(
106-
# mpa_fr_with_mn.geometry,
107-
# spire_vessel_positions.position
108-
# )
109-
# WHERE
110-
# spire_vessel_positions.timestamp = TO_TIMESTAMP('2023-11-17 12:15', 'YYYY-MM-DD HH24:MI')
111-
# GROUP BY
112-
# vessel_id
113-
# )
114-
# SELECT
115-
# vessel_id,
116-
# mpa_ids,
117-
# -1 AS value
118-
# FROM
119-
# cte_query1
120-
# EXCEPT
121-
# SELECT
122-
# vessel_id,
123-
# mpa_ids,
124-
# -1 AS value
125-
# FROM
126-
# cte_query2
127-
# UNION ALL
128-
# SELECT
129-
# vessel_id,
130-
# mpa_ids,
131-
# 1 AS value
132-
# FROM
133-
# cte_query2
134-
# EXCEPT
135-
# SELECT
136-
# vessel_id,
137-
# mpa_ids,
138-
# 1 AS value
139-
# FROM
140-
# cte_query1
38+
# an other query with the same result :
39+
# WITH cte_query1 AS (
40+
# SELECT spire_vessel_positions.vessel_id AS vessel_id, ARRAY_AGG(mpa_fr_with_mn.index ORDER BY mpa_fr_with_mn.index DESC) AS mpa_ids
41+
# FROM spire_vessel_positions
42+
# JOIN mpa_fr_with_mn ON ST_Contains(mpa_fr_with_mn.geometry, spire_vessel_positions.position)
43+
# WHERE spire_vessel_positions.timestamp = TO_TIMESTAMP('2023-11-17 12:00', 'YYYY-MM-DD HH24:MI')
44+
# GROUP BY vessel_id
45+
# ),
46+
# cte_query2 AS (
47+
# SELECT DISTINCT spire_vessel_positions.vessel_id AS vessel_id, ARRAY_AGG(mpa_fr_with_mn.index ORDER BY mpa_fr_with_mn.index DESC) AS mpa_ids
48+
# FROM spire_vessel_positions
49+
# JOIN mpa_fr_with_mn ON ST_Contains(mpa_fr_with_mn.geometry, spire_vessel_positions.position)
50+
# WHERE spire_vessel_positions.timestamp = TO_TIMESTAMP('2023-11-17 12:15', 'YYYY-MM-DD HH24:MI')
51+
# GROUP BY vessel_id
52+
# )
53+
# SELECT vessel_id, mpa_ids, -1 AS value FROM cte_query1 EXCEPT SELECT vessel_id, mpa_ids, -1 AS value FROM cte_query2
54+
# UNION ALL
55+
# SELECT vessel_id, mpa_ids, 1 AS value FROM cte_query2 EXCEPT SELECT vessel_id, mpa_ids, 1 AS value FROM cte_query1
14156

14257
def load_alert(self, timestamp: datetime) -> list[Alert]:
14358
with self.session_factory() as session:

0 commit comments

Comments
 (0)