api.moorages_view
Description
Moorages listing web view
Table Definition
CREATE VIEW moorages_view AS (
WITH logbook_counts AS (
SELECT logbook._from_moorage_id AS moorage_id,
count(*) AS entries
FROM api.logbook
WHERE ((logbook.vessel_id = current_setting('vessel.id'::text, true)) AND (logbook._from_moorage_id IS NOT NULL) AND (logbook.active = false))
GROUP BY logbook._from_moorage_id
UNION ALL
SELECT logbook._to_moorage_id AS moorage_id,
count(*) AS entries
FROM api.logbook
WHERE ((logbook.vessel_id = current_setting('vessel.id'::text, true)) AND (logbook._to_moorage_id IS NOT NULL) AND (logbook.active = false))
GROUP BY logbook._to_moorage_id
), logbook_total AS (
SELECT logbook_counts.moorage_id,
sum(logbook_counts.entries) AS total_arrivals_departures
FROM logbook_counts
GROUP BY logbook_counts.moorage_id
), stays_agg AS (
SELECT stays.moorage_id,
sum(stays.duration) AS total_duration
FROM api.stays
WHERE ((stays.vessel_id = current_setting('vessel.id'::text, true)) AND (stays.active = false))
GROUP BY stays.moorage_id
)
SELECT m.id,
m.name AS moorage,
sa.description AS default_stay,
sa.stay_code AS default_stay_id,
COALESCE(lt.total_arrivals_departures, (0)::numeric) AS arrivals_departures,
COALESCE(st.total_duration, 'PT0S'::interval) AS total_duration
FROM (((api.moorages m
JOIN api.stays_at sa ON ((m.stay_code = sa.stay_code)))
LEFT JOIN logbook_total lt ON ((lt.moorage_id = m.id)))
LEFT JOIN stays_agg st ON ((st.moorage_id = m.id)))
WHERE ((m.vessel_id = current_setting('vessel.id'::text, true)) AND (m.geog IS NOT NULL))
ORDER BY COALESCE(st.total_duration, 'PT0S'::interval) DESC
)
Columns
| Name | Type | Default | Nullable |
|---|---|---|---|
| arrivals_departures | numeric | true | |
| default_stay | text | true | |
| default_stay_id | integer | true | |
| id | integer | true | |
| moorage | text | true | |
| total_duration | interval | true |
Referenced Tables
| Name | Columns | Comment | Type |
|---|---|---|---|
| api.logbook | 43 | The logbook table stores vessel navigation entries with timestamps, locations, and trip metrics. RLS policies filter by vessel_id automatically. These indexes optimize different query patterns while minimizing storage. | BASE TABLE |
| api.stays | 15 | The stays table records time spent at moorages. Each stay links a moorage with arrival/departure timestamps. RLS policies filter by vessel_id. | BASE TABLE |
| api.moorages | 14 | The moorages table stores locations where vessels can stay (marinas, anchorages, etc.). Each moorage has geographic coordinates and metadata. | BASE TABLE |
| api.stays_at | 2 | Stay Type | BASE TABLE |
| logbook_total | 0 | ||
| stays_agg | 0 |
Relations
erDiagram
"api.moorages_view" {
numeric arrivals_departures ""
text default_stay ""
integer default_stay_id ""
integer id ""
text moorage ""
interval total_duration ""
}
Generated by tbls