Sign inSign up

wudti/b2b-client-rest

By wudti

Updated over 4 years ago

NMB2B client with REST endpoint

Image
0

359

wudti/b2b-client-rest repository overview

FRQ NM B2B client with REST endpoint

This container does two things:

  • Runs as a server with REST endpoint to query flight data from NM

  • Periodically downloads flight data for a specified airport and stores it to a postgres db

Both functions can be used independently of each other. So it is also possible to use the container only for periodic querying of the flight data. If you don't want so store the flight data into the database, you can skip the database configuration. Just set AUTO_DOWNL=0 in the .env file.

Sources: https://github.com/wadti/b2b-client-rest/tree/main/b2b-client-rest/b2b-client-rest

Configuration

postgres

To set up the database and a pgadmin, run

$ docker compose up 

with a docker-compose.yml containing:

version: '3'
services:
  postgres:
    container_name: pg
    image: postgres:9.6
    hostname: postgres
    ports:
      - "5432:5342"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: flightsByAerodrome
    volumes:
       - postgres-data:/var/lib/postgresql/data
    restart: unless-stopped
    
  pgAdmin:
    container_name: pgadmin
    image: dpage/pgadmin4:latest
    ports:
      - "8080:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: TopSecret
      PGADMIN_CONFIG_ENHANCED_COOKIE_PROTECTION: 'True'
      PGADMIN_CONFIG_CONSOLE_LOG_LEVEL: 10
        
volumes:
  postgres-data:

This will pull and start a postgres database and pgadmin container, already configured for the use for this container.

To find out the IP address of the database container run:

$ docker inspect pg | grep IPAddress

You'll need this to set up the address in the .env file.

.env

The container needs to be started up with a .env file. This file must contain:

B2B_FLAVOUR=PREOPS
B2B_CERT_FORMAT=pfx
B2B_CERT=/cert/<YourCert>.p12
B2B_CERT_PASSPHRASE=<YourPassPhrase>
B2B_AERODROME=<ICAO code of airport>
B2B_REST_PORT=<restPort>
AUTO_DOWNL=0
AUTO_CRON=6,36 * * * *
AUTO_TIMESP=30
PGUSER=<pgUser>
PGHOST=172.xxx.xxx.xxx
PGPASSWORD=<pgPw>
PGDATABASE=<pgDB>
PGPORT=<pgPort>

The B2B_FLAVOUR sets if you want to use PREOPS or OPS data. All env vars wit CERT defines the authentication. B2B_AERODROME sets the aerodrome, from which you want to download the flight data periodically. B2B_REST_PORT sets the port where you can query the flight data for a given aerodrome via REST (default: 3000). PG_USER, PG_PASSWORD, PG_DATABASE and PG_PORT are set automatically set via docker-compose.yml. If you need to do any changes, make sure, to also update these env vars there!

Authentication

You'll need to have a valid NMB2B certificate and, to run this container. The passphrase needs to be provided in the .env file (B2B_CERT_PASSPHRASE).

The certificate is passed to the container via a docker volume. To set up a docker volume, run:

$ docker volume create cert
$ docker volume inspect cert

Copy your certificate to the Mountpoint you see after inspect.

Run the container

To run the container, start it via docker run:

$ docker run --mount source=cert,destination=/cert --net=host --env-file=../.env wudti/b2b-client-rest:latest

The container is now up and running and should look something like this:

yarn run v1.22.18
$ node --openssl-legacy-provider src/index.js
NMB2B client running on port 3000
Started: Thu Apr 14 2022 11:27:52 GMT+0000 (Coordinated Universal Time)
Auto downloader running for LSZH
---------------------------------

--openssl-legacy-provider as parameter is only needed when you are running the client in a node 17 container. The parameter is specified in package.json.

REST
query flights by aerodrome

To query flightsByAerodrome, you'll need to pass three arguments:

  • aerodrome: ICAO code of aerodrome (four letters)
  • from: timestamp that has to match this format: 2022-04-13T14:46:09.000Z
  • to: timestamp that has to match this format: 2022-04-13T14:46:09.000Z

from and to specify the time window you want to query.

On the host system, you can test the REST endpoint by opening you browser and enter a query that should look something like this:

http://foo.bar:3000/LSZH/2022-04-13T14:46:09.000Z/2022-04-13T16:46:09.000Z

0.1 returns a JSON, >=0.2 returns an XML.

flightsByRegulationCategory
http://foo.bar:3000/flightsByRegulationCategory

Returns a JSON -like object, summarizing how many flights are affected by regulation category.

e.g.

[["ARRIVAL","1"],["ENROUTE","72"]]

Missing categories are not returned (DEPARTURE in this case).

knownRegulations
http://foo.bar:3000/knownRegulations

Returns sum of distinct regulations by category.

e.g.

[["ARRIVAL","1"],["ENROUTE","23"]]

SQL query:

SELECT mostpenalisingregulationlocationcategory, COUNT(DISTINCT mostpenalisingregulation) 
FROM(SELECT mostpenalisingregulation, mostpenalisingregulationlocationcategory
	FROM flightsbyaerodrome.flights AS flights
	JOIN flightsbyaerodrome.meta AS meta
	ON flights.requestid = meta.requestid
	WHERE now() BETWEEN effectivetrafficwindowfrom AND effectivetrafficwindowto
	AND aerodrome = 'EHAM' 
	AND mostpenalisingregulation IS NOT NULL
	AND requestreceptiontime = (SELECT MAX(requestreceptiontime)
								FROM flightsbyaerodrome.meta 
								WHERE aerodrome = 'EHAM')) AS regulations
GROUP BY mostpenalisingregulationlocationcategory;
listSubscriptions

Returns a JSON object containing the NMs response with the users subscriptions

resumeSubscription

NOTE: Subscription ID is hardcoded

Resumes a given subscription (Paused --> Active)

pauseSubscription

NOTE: Subscription ID is hardcoded

Pauses a given subscription (Active --> Paused)

retriveRegulationSubscription

NOTE: Subscription ID is hardcoded, not finished!

This will connect to the AMQP queue (PREOPS) and retrive the subscription data

Versions

0.1

Flight data requested with REST is returned as JSON, no input validation

0.2

Flight data requested with REST is now returned as XML, no input validation

0.3

Added REGEX input validation for ICAO code and timestamps in REST request

0.4

Added flag to toggle auto download of flight data, AUTO_DOWNL=[0|1] can now be set in .env (Default = 0).

0.5

Added cron string AUTO_CRON as env var to configure download intervals.

0.6

Added AUTO_TIMESP as env var to configure the length of the requested time window [minutes]. Time span requested = Now until (Now + AUTO_TIMESP)

0.7

Added aerodrome column to meta table

0.8

Fixed bug with AUTO_TIMESP Added REST /knownRegulations, this returns all currently effective regulations stored in the database aggregated for statistical reasons as JSON.

0.9

Fixed bugs with REST endpoint

0.9.1

Added REST functionality for PubSub Services (Subscriptions hard coded)

Tag summary

Content type

Image

Digest

Size

68 MB

Last updated

over 4 years ago

docker pull wudti/b2b-client-rest:0.9.1