Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

NameTypeDefaultNullable
alpha_2texttrue
beamdouble precisiontrue
countrytexttrue
created_attimestamp with time zonetrue
first_contacttimestamp with time zonetrue
geojsonjsonbtrue
has_configbooleantrue
has_imagesbooleantrue
has_polarbooleantrue
heightdouble precisiontrue
image_updated_attexttrue
image_urltexttrue
imagesjsonbtrue
last_contacttimestamp with time zonetrue
lengthdouble precisiontrue
make_modeljsonbtrue
mmsitexttrue
nametexttrue
offlinebooleantrue
platformtexttrue
plugin_versiontexttrue
ship_typetexttrue
specsjsonbtrue
vessel_idtexttrue

Referenced Tables

NameColumnsCommentType
api.metrics11Stores time-series metrics from vessel via SignalK plugin.
TimescaleDB hypertable partitioned by time.
BASE TABLE
auth.vessels7vessels table link to accounts email user_id columnBASE TABLE
api.metadata18Stores metadata received from vessel, aka signalk plugin. single-row-per-vessel storeBASE TABLE
public.aistypes2aistypes AIS Ship Types, https://api.vesselfinder.com/docs/ref-aistypes.htmlBASE TABLE
public.mid3MMSI MID Codes (Maritime Mobile Service Identity) Filtered by Flag of Registration, https://www.marinevesseltraffic.com/2013/11/mmsi-mid-codes-by-flag.htmlBASE TABLE
public.iso31664This 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-codesBASE 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