Commit 5771f02d authored by Nacho's avatar Nacho
Browse files

Inicio del proyecto

parent b90c7bfe
Loading
Loading
Loading
Loading

Dockerfile

0 → 100644
+3 −0
Original line number Diff line number Diff line
FROM mdillon/postgis:10-alpine

COPY /scripts/ /docker-entrypoint-initdb.d/

docker-compose.dev.yml

0 → 100644
+6 −0
Original line number Diff line number Diff line
version: '3.5'

services:
  postgres:
    container_name: postgres
    restart: on-failure:3
+14 −0
Original line number Diff line number Diff line
version: '3.5'

services:
  postgres:
    deploy:
      mode: replicated
      replicas: 1
      placement:
        constraints:
          - node.role == worker
      restart_policy:
        condition: on-failure
        delay: 1m
        window: 3m
+20 −0
Original line number Diff line number Diff line
version: '3.5'

services:
  postgres:
    image: ${IMAGE_NAME}:${IMAGE_TAG:-latest}
    environment:
      - POSTGRES_PASSWORD
      - POSTGRES_USER
      - POSTGRES_DB
      - PGDATA=/var/lib/postgresql/data/db-files/
    networks:
      postgres-net:
        aliases:
          - ais-db
    volumes:
      - ais-postgres-vol:/var/lib/postgresql/data

networks:
  postgres-net:
    external: true

scripts/vessel.sh

0 → 100644
+134 −0
Original line number Diff line number Diff line
#!/bin/sh

set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
	CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

	-- Importante callSign y navStat en camelcase para coincidir con el esquema

	CREATE TABLE last_position
	(
	  mmsi integer PRIMARY KEY,
	  shape geometry(Point,4326),
	  longitude double precision NOT NULL,
	  latitude double precision NOT NULL,
	  updated timestamp with time zone NOT NULL DEFAULT now(),
	  tstamp timestamp with time zone NOT NULL,
	  uuid uuid NOT NULL DEFAULT uuid_generate_v4(),
	  inserted timestamp with time zone NOT NULL,
	  cog double precision,
	  sog double precision,
	  draught double precision,
	  type integer,
	  a double precision,
	  b double precision,
	  c double precision,
	  d double precision,
	  imo integer,
	  heading integer,
	  navStat integer,
	  name text,
	  dest text,
	  callSign text,
	  eta text,
	  CONSTRAINT "mmsi_date_last_position" UNIQUE ("mmsi", "tstamp")
	)
	WITH (
	  OIDS=FALSE
	);

	CREATE INDEX sidx_last_position_shape
	  ON last_position
	  USING gist (shape);

	CREATE FUNCTION before_insert_or_update_save_change_date()
	RETURNS trigger
    LANGUAGE plpgsql
    AS \$\$
		BEGIN
			IF TG_OP = 'INSERT' THEN
				NEW.inserted := now();
			END IF;
			IF TG_OP = 'UPDATE' THEN
				NEW.inserted := OLD.inserted;
			END IF;
			NEW.updated := now();
			RETURN NEW;
		END;
	\$\$;

	CREATE TRIGGER tracking_before_insert_or_update_save_change_date
	  BEFORE INSERT OR UPDATE
	  ON last_position
	  FOR EACH ROW
	  EXECUTE PROCEDURE before_insert_or_update_save_change_date();

	CREATE OR REPLACE FUNCTION create_shape()
	RETURNS TRIGGER
	LANGUAGE plpgsql
	AS \$\$
		BEGIN
			IF NEW.longitude IS NOT NULL AND NEW.latitude IS NOT NULL THEN
				SELECT ST_SetSRID(ST_MakePoint(NEW.longitude, NEW.latitude), 4326) INTO NEW.shape;
			END IF;
			RETURN NEW;
		END;
	\$\$;

	CREATE TRIGGER create_shape_last_position
		BEFORE INSERT OR UPDATE
		ON last_position
		FOR EACH ROW EXECUTE PROCEDURE create_shape();

	-- Last Week

	CREATE TABLE last_week
	(
	  uuid uuid NOT NULL DEFAULT uuid_generate_v4(),
	  mmsi integer NOT NULL,
	  shape geometry(Point,4326),
	  longitude double precision NOT NULL,
	  latitude double precision NOT NULL,
	  updated timestamp with time zone NOT NULL DEFAULT now(),
	  tstamp timestamp with time zone NOT NULL,
	  inserted timestamp with time zone NOT NULL,
	  cog double precision,
	  sog double precision,
	  draught double precision,
	  type integer,
	  a double precision,
	  b double precision,
	  c double precision,
	  d double precision,
	  imo integer,
	  heading integer,
	  navStat integer,
	  name text,
	  dest text,
	  callSign text,
	  eta text,
	  PRIMARY KEY (uuid),
	  CONSTRAINT "mmsi_date_last_week" UNIQUE ("mmsi", "tstamp")
	)
	WITH (
	  OIDS=FALSE
	);

	CREATE INDEX sidx_last_week_shape
	  ON last_week
	  USING gist (shape);


	CREATE TRIGGER tracking_before_insert_or_update_save_change_date
	  BEFORE INSERT OR UPDATE
	  ON last_week
	  FOR EACH ROW
	  EXECUTE PROCEDURE before_insert_or_update_save_change_date();


	CREATE TRIGGER create_shape_last_week
		BEFORE INSERT OR UPDATE
		ON last_week
		FOR EACH ROW EXECUTE PROCEDURE create_shape();
EOSQL
 No newline at end of file