From a0d17958c7f41a2f23bb904a4198ea180c05edb4 Mon Sep 17 00:00:00 2001 From: joe fleming Date: Fri, 2 Nov 2018 14:09:25 -0700 Subject: [PATCH] chore: change get-data exports export a function for the specific opensky data, re-org code --- src/index.mjs | 29 ++++------------------------- src/lib/fetch.mjs | 4 ---- src/lib/get-data.mjs | 25 ++++++++++++++++++++++--- 3 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index 7757fcd..0e116d7 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -1,9 +1,8 @@ import elasticsearch from 'elasticsearch'; import logger from './lib/logger.mjs'; import { createIndex, bulkInsert } from './lib/data-source.mjs'; -import getData from './lib/get-data.mjs'; +import { getOpenskyData } from './lib/get-data.mjs'; -const positionSourceMap = ['ADS-B', 'ASTERIX', 'MLAT']; export default async function(indexName, opts = {}) { const client = new elasticsearch.Client({ @@ -18,29 +17,9 @@ export default async function(indexName, opts = {}) { // create the target index const index = await createIndex(client, indexName); - const records = await getData(); - logger.debug(`ADS-B records:, ${records.length}`); + const records = await getOpenskyData(); + logger.debug(`Record count:, ${records.length}`); - // index all the data - const documents = records.map(rec => ({ - transponder: `${rec[0]}`.toLowerCase(), - callsign: `${rec[1]}`.trim(), - origin_country: rec[2], - time_position: new Date(rec[3] * 1000), - last_contact: new Date(rec[4] * 1000), - location: rec[5] && rec[6] ? `${rec[6]},${rec[5]}` : null, - lat: rec[6], - lon: rec[5], - baro_altitude: rec[7], - geo_altitude: rec[13], - on_ground: rec[8], - velocity: rec[9], - vertical_rate: rec[11], - squawk: rec[14], - spi: rec[15], - position_source: positionSourceMap[rec[16]], - })); - - await bulkInsert(client, index, documents); + await bulkInsert(client, index, records); logger.debug(`Successfully indexed ${records.length} records to ${index}`); } diff --git a/src/lib/fetch.mjs b/src/lib/fetch.mjs index 8d37646..77e488c 100644 --- a/src/lib/fetch.mjs +++ b/src/lib/fetch.mjs @@ -1,13 +1,9 @@ import axios from 'axios'; -const api = 'https://opensky-network.org/api'; - export default axios.create({ - baseURL: api, timeout: 3000, responseType: 'json', headers: { - 'X-Custom-Header': 'foobar', 'Content-Type': 'application/json; charset=utf-8', }, }); diff --git a/src/lib/get-data.mjs b/src/lib/get-data.mjs index 88888d1..cf72b8c 100644 --- a/src/lib/get-data.mjs +++ b/src/lib/get-data.mjs @@ -1,6 +1,25 @@ import fetch from './fetch.mjs'; -export default async function getData() { - const res = await fetch.get('/states/all'); - return res.data.states; +const positionSourceMap = ['ADS-B', 'ASTERIX', 'MLAT']; + +export async function getOpenskyData() { + const res = await fetch.get(`https://opensky-network.org/api/states/all`); + return res.data.states.map(rec => ({ + transponder: `${rec[0]}`.toLowerCase(), + callsign: `${rec[1]}`.trim(), + origin_country: rec[2], + time_position: new Date(rec[3] * 1000), + last_contact: new Date(rec[4] * 1000), + location: rec[5] && rec[6] ? `${rec[6]},${rec[5]}` : null, + lat: rec[6], + lon: rec[5], + baro_altitude: rec[7], + geo_altitude: rec[13], + on_ground: rec[8], + velocity: rec[9], + vertical_rate: rec[11], + squawk: rec[14], + spi: rec[15], + position_source: positionSourceMap[rec[16]], + })); }