Skip to content

Commit feb8b07

Browse files
Remove beacon_status from beacons table
1 parent aed05d4 commit feb8b07

4 files changed

Lines changed: 98 additions & 11 deletions

File tree

backend/src/main/resources/db/migration/internal/V0.393__Add_beacon_malfunction_initial_location.sql renamed to backend/src/main/resources/db/migration/internal/V0.393__Update_beacon_malfunctions_table.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ WHERE initial_vessel_status != 'AT_SEA'::beacon_malfunctions_vessel_status;
5454
ALTER TABLE beacon_malfunctions
5555
ALTER COLUMN is_followed SET NOT NULL;
5656

57-
-- Drop unused column
57+
-- Drop unused columns
5858
ALTER TABLE beacon_malfunctions
5959
DROP COLUMN beacon_status_at_malfunction_creation;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
DROP VIEW satellite_operators_statuses;
2+
3+
CREATE VIEW satellite_operators_statuses AS
4+
5+
WITH recent_positions AS (
6+
SELECT
7+
DATE_TRUNC('hour', date_time) AS date_hour,
8+
s.id AS satellite_operator_id,
9+
v.id AS vessel_id
10+
FROM positions p
11+
JOIN vessels v
12+
ON v.cfr = p.internal_reference_number
13+
JOIN beacons b
14+
ON b.vessel_id = v.id
15+
JOIN satellite_operators s
16+
ON s.id = b.satellite_operator_id
17+
WHERE
18+
date_time < DATE_TRUNC('hour', CURRENT_TIMESTAMP AT TIME ZONE 'UTC') AND
19+
date_time >= DATE_TRUNC('hour', CURRENT_TIMESTAMP AT TIME ZONE 'UTC') - INTERVAL '24 hours'
20+
21+
UNION ALL
22+
23+
SELECT
24+
DATE_TRUNC('hour', date_time) AS date_hour,
25+
s.id AS satellite_operator_id,
26+
v.id AS vessel_id
27+
FROM positions p
28+
JOIN vessels v
29+
ON v.ircs = p.ircs
30+
JOIN beacons b
31+
ON b.vessel_id = v.id
32+
JOIN satellite_operators s
33+
ON s.id = b.satellite_operator_id
34+
WHERE
35+
date_time < DATE_TRUNC('hour', CURRENT_TIMESTAMP AT TIME ZONE 'UTC') AND
36+
date_time >= DATE_TRUNC('hour', CURRENT_TIMESTAMP AT TIME ZONE 'UTC') - INTERVAL '24 hours'
37+
),
38+
39+
positions_histogram AS (
40+
SELECT
41+
date_hour,
42+
satellite_operator_id,
43+
COUNT(DISTINCT vessel_id) AS nb_vessels_emitting
44+
FROM recent_positions p
45+
GROUP BY date_hour, satellite_operator_id
46+
),
47+
48+
nb_attributed_beacons_per_operator AS (
49+
SELECT
50+
s.id AS satellite_operator_id,
51+
COUNT(*) AS nb_attributed_beacons
52+
FROM beacons b
53+
JOIN satellite_operators s
54+
ON s.id = b.satellite_operator_id
55+
WHERE vessel_id IS NOT NULL
56+
GROUP BY s.id
57+
),
58+
59+
shares_histogram AS (
60+
SELECT
61+
date_hour,
62+
s.id AS satellite_operator_id,
63+
nb_vessels_emitting::DOUBLE PRECISION / nb_attributed_beacons::DOUBLE PRECISION AS share_vessels_emitting
64+
FROM nb_attributed_beacons_per_operator n
65+
JOIN positions_histogram h
66+
ON h.satellite_operator_id = n.satellite_operator_id
67+
JOIN satellite_operators s
68+
ON n.satellite_operator_id = s.id
69+
),
70+
71+
nb_hours_share_ok_per_operator AS (
72+
SELECT
73+
s.id AS satellite_operator_id,
74+
SUM(CASE WHEN share_vessels_emitting > 0.7 THEN 1 ELSE 0 END) AS nb_hours_with_share_ok
75+
FROM satellite_operators s
76+
LEFT JOIN shares_histogram sh
77+
ON s.id = sh.satellite_operator_id
78+
GROUP BY s.id
79+
)
80+
81+
SELECT
82+
satellite_operator_id,
83+
CASE WHEN nb_hours_with_share_ok = 24 THEN true ELSE false END AS operator_is_up
84+
FROM nb_hours_share_ok_per_operator
85+
ORDER BY 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE beacons
2+
DROP COLUMN beacon_status;
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
INSERT INTO public.beacons (
2-
beacon_number, vessel_id, beacon_status, is_coastal, satellite_operator_id, logging_datetime_utc)
2+
beacon_number, vessel_id, is_coastal, satellite_operator_id, logging_datetime_utc)
33
VALUES
4-
( 'FGEDX85', 1, 'ACTIVATED', TRUE, 1, '2021-5-12 12:23'),
5-
( '123456', 2, 'ACTIVATED', FALSE, 1, '2021-4-22 22:25'),
6-
( 'ETETE4', 3, 'ACTIVATED', FALSE, 1, '2021-3-20 12:38'),
7-
( 'A56CZ2', 4, 'ACTIVATED', FALSE, 2, '2021-2-20 12:25'),
8-
( 'FEZFS65', 5, 'UNSUPERVISED', FALSE, 2, '2021-1-12 10:47'),
9-
( 'LHGY122', 7, 'IN_TEST', FALSE, 2, '2021-12-2 12:21'),
10-
( 'NB56FR8', 8, 'ACTIVATED', FALSE, 2, '2021-6-2 9:23'),
11-
( 'PO8U9U4', 9, 'ACTIVATED', FALSE, 2, '2021-5-2 12:27'),
12-
( 'ABC1234', null, 'ACTIVATED', FALSE, 2, '2021-5-1 12:20');
4+
( 'FGEDX85', 1, TRUE, 1, '2021-5-12 12:23'),
5+
( '123456', 2, FALSE, 1, '2021-4-22 22:25'),
6+
( 'ETETE4', 3, FALSE, 1, '2021-3-20 12:38'),
7+
( 'A56CZ2', 4, FALSE, 2, '2021-2-20 12:25'),
8+
( 'FEZFS65', 5, FALSE, 2, '2021-1-12 10:47'),
9+
( 'LHGY122', 7, FALSE, 2, '2021-12-2 12:21'),
10+
( 'NB56FR8', 8, FALSE, 2, '2021-6-2 9:23'),
11+
( 'PO8U9U4', 9, FALSE, 2, '2021-5-2 12:27'),
12+
( 'ABC1234', null, FALSE, 2, '2021-5-1 12:20');

0 commit comments

Comments
 (0)