chore: change get-data exports
export a function for the specific opensky data, re-org code
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
import elasticsearch from 'elasticsearch';
|
import elasticsearch from 'elasticsearch';
|
||||||
import logger from './lib/logger.mjs';
|
import logger from './lib/logger.mjs';
|
||||||
import { createIndex, bulkInsert } from './lib/data-source.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 = {}) {
|
export default async function(indexName, opts = {}) {
|
||||||
const client = new elasticsearch.Client({
|
const client = new elasticsearch.Client({
|
||||||
@@ -18,29 +17,9 @@ export default async function(indexName, opts = {}) {
|
|||||||
// create the target index
|
// create the target index
|
||||||
const index = await createIndex(client, indexName);
|
const index = await createIndex(client, indexName);
|
||||||
|
|
||||||
const records = await getData();
|
const records = await getOpenskyData();
|
||||||
logger.debug(`ADS-B records:, ${records.length}`);
|
logger.debug(`Record count:, ${records.length}`);
|
||||||
|
|
||||||
// index all the data
|
await bulkInsert(client, index, records);
|
||||||
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);
|
|
||||||
logger.debug(`Successfully indexed ${records.length} records to ${index}`);
|
logger.debug(`Successfully indexed ${records.length} records to ${index}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
const api = 'https://opensky-network.org/api';
|
|
||||||
|
|
||||||
export default axios.create({
|
export default axios.create({
|
||||||
baseURL: api,
|
|
||||||
timeout: 3000,
|
timeout: 3000,
|
||||||
responseType: 'json',
|
responseType: 'json',
|
||||||
headers: {
|
headers: {
|
||||||
'X-Custom-Header': 'foobar',
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
'Content-Type': 'application/json; charset=utf-8',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,25 @@
|
|||||||
import fetch from './fetch.mjs';
|
import fetch from './fetch.mjs';
|
||||||
|
|
||||||
export default async function getData() {
|
const positionSourceMap = ['ADS-B', 'ASTERIX', 'MLAT'];
|
||||||
const res = await fetch.get('/states/all');
|
|
||||||
return res.data.states;
|
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]],
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user