api.vessel_view
Description
Primary vessel resource view. Exposes identity, AIS metadata, physical
dimensions, plugin info, extended profile (make/model, polar, images),
and live GeoJSON position. Replaces api.vessel_fn(). RLS-equivalent via
vessel.id session config. One row per authenticated vessel session.
Table Definition
CREATE VIEW vessel_view AS (
WITH latest_position AS (
SELECT metrics.vessel_id,
metrics."time",
metrics.courseovergroundtrue,
metrics.speedoverground,
metrics.anglespeedapparent,
metrics.longitude,
metrics.latitude,
st_makepoint(metrics.longitude, metrics.latitude) AS geo_point
FROM api.metrics
WHERE ((metrics.latitude IS NOT NULL) AND (metrics.longitude IS NOT NULL) AND (metrics.vessel_id = current_setting('vessel.id'::text, false)))
ORDER BY metrics."time" DESC
LIMIT 1
)
SELECT m.vessel_id,
m.name,
m.mmsi,
v.created_at,
m.created_at AS first_contact,
m."time" AS last_contact,
((((now() AT TIME ZONE 'UTC'::text))::timestamp with time zone - m."time") > 'PT1H10M'::interval) AS offline,
ais.description AS ship_type,
mid.country,
iso.alpha_2,
m.length,
m.beam,
m.height,
m.plugin_version,
m.platform,
(m.configuration IS NOT NULL) AS has_config,
(m.user_data -> 'make_model'::text) AS make_model,
((m.user_data -> 'polar'::text) IS NOT NULL) AS has_polar,
(jsonb_array_length(COALESCE((m.user_data -> 'images'::text), '[]'::jsonb)) > 0) AS has_images,
(((m.user_data -> 'images'::text) -> 0) ->> 'url'::text) AS image_url,
(((m.user_data -> 'images'::text) -> 0) ->> 'updated_at'::text) AS image_updated_at,
(m.user_data -> 'images'::text) AS images,
(m.user_data -> 'specs'::text) AS specs,
jsonb_build_object('type', 'Feature', 'geometry', (st_asgeojson(st_makepoint(p.longitude, p.latitude)))::jsonb, 'properties', jsonb_build_object('name', m.name, 'time', p."time", 'longitude', p.longitude, 'latitude', p.latitude, 'make_model', (m.user_data -> 'make_model'::text), 'image_url', (((m.user_data -> 'images'::text) -> 0) ->> 'url'::text))) AS geojson
FROM (((((auth.vessels v
JOIN api.metadata m ON ((m.vessel_id = v.vessel_id)))
LEFT JOIN aistypes ais ON ((ais.id = m.ship_type)))
LEFT JOIN mid ON ((("left"(m.mmsi, 3))::numeric = mid.id)))
LEFT JOIN iso3166 iso ON ((iso.id = mid.country_id)))
LEFT JOIN latest_position p ON ((p.vessel_id = m.vessel_id)))
WHERE (m.vessel_id = current_setting('vessel.id'::text, false))
)
Columns
| Name | Type | Default | Nullable |
|---|---|---|---|
| alpha_2 | text | true | |
| beam | double precision | true | |
| country | text | true | |
| created_at | timestamp with time zone | true | |
| first_contact | timestamp with time zone | true | |
| geojson | jsonb | true | |
| has_config | boolean | true | |
| has_images | boolean | true | |
| has_polar | boolean | true | |
| height | double precision | true | |
| image_updated_at | text | true | |
| image_url | text | true | |
| images | jsonb | true | |
| last_contact | timestamp with time zone | true | |
| length | double precision | true | |
| make_model | jsonb | true | |
| mmsi | text | true | |
| name | text | true | |
| offline | boolean | true | |
| platform | text | true | |
| plugin_version | text | true | |
| ship_type | text | true | |
| specs | jsonb | true | |
| vessel_id | text | true |
Referenced Tables
| Name | Columns | Comment | Type |
|---|---|---|---|
| api.metrics | 11 | Stores time-series metrics from vessel via SignalK plugin. TimescaleDB hypertable partitioned by time. | BASE TABLE |
| auth.vessels | 7 | vessels table link to accounts email user_id column | BASE TABLE |
| api.metadata | 18 | Stores metadata received from vessel, aka signalk plugin. single-row-per-vessel store | BASE TABLE |
| public.aistypes | 2 | aistypes AIS Ship Types, https://api.vesselfinder.com/docs/ref-aistypes.html | BASE TABLE |
| public.mid | 3 | MMSI MID Codes (Maritime Mobile Service Identity) Filtered by Flag of Registration, https://www.marinevesseltraffic.com/2013/11/mmsi-mid-codes-by-flag.html | BASE TABLE |
| public.iso3166 | 4 | This is a complete list of all country ISO codes as described in the ISO 3166 international standard. Country Codes Alpha-2 & Alpha-3 https://www.iban.com/country-codes | BASE TABLE |
Relations
erDiagram
"api.vessel_view" {
text alpha_2 ""
double_precision beam ""
text country ""
timestamp_with_time_zone created_at ""
timestamp_with_time_zone first_contact ""
jsonb geojson ""
boolean has_config ""
boolean has_images ""
boolean has_polar ""
double_precision height ""
text image_updated_at ""
text image_url ""
jsonb images ""
timestamp_with_time_zone last_contact ""
double_precision length ""
jsonb make_model ""
text mmsi ""
text name ""
boolean offline ""
text platform ""
text plugin_version ""
text ship_type ""
jsonb specs ""
text vessel_id ""
}
Generated by tbls