Better memory and time usage
This commit is contained in:
parent
4d61358e03
commit
eed19a225f
|
@ -8,6 +8,7 @@ node_modules
|
|||
# testing
|
||||
coverage
|
||||
*.sqlite3
|
||||
data/
|
||||
|
||||
# production
|
||||
build/
|
||||
|
|
|
@ -4,42 +4,40 @@ const db = require("../models")
|
|||
const Airport = db['Airport']
|
||||
const Flight = db['Flight']
|
||||
|
||||
db.sequelize.sync()
|
||||
db.sequelize.sync().then(() => (async () => {
|
||||
// Refresh airport data
|
||||
let airportData = await fs.promises.readFile(`${__dirname}/../data/airports.json`, 'utf-8')
|
||||
airportData = JSON.parse(airportData)
|
||||
|
||||
fs.readFile(__dirname + '/../data/airports.json', 'utf-8', (error, data) => {
|
||||
if (error) {
|
||||
console.log(error)
|
||||
return
|
||||
let icao_codes = []
|
||||
let iata_codes = []
|
||||
let results = await Airport.findAll({attributes: ['icao_code', 'iata_code']})
|
||||
for (let result of results) {
|
||||
if (result.icao_code)
|
||||
icao_codes.push(result.icao_code)
|
||||
if (result.iata_code)
|
||||
iata_codes.push(result.iata_code)
|
||||
}
|
||||
// Import all not-imported airports
|
||||
await Airport.bulkCreate(airportData['response'].filter(airport => !icao_codes.includes(airport.icao_code)
|
||||
&& !iata_codes.includes(airport.iata_code)))
|
||||
|
||||
data = JSON.parse(data)
|
||||
Airport.findAll({attributes: ['icao_code', 'iata_code']}).then(results => {
|
||||
let icao_codes = []
|
||||
let iata_codes = []
|
||||
results.forEach(result => {
|
||||
if (result.icao_code)
|
||||
icao_codes.push(result.icao_code)
|
||||
if (result.iata_code)
|
||||
iata_codes.push(result.iata_code)
|
||||
})
|
||||
// Import flight routes per departure
|
||||
let airports = await Airport.findAll({attributes: ['icao_code']})
|
||||
for (let airport of airports) {
|
||||
const airportRoutesFile = `${__dirname}/../data/routes/${airport.icao_code}.json`
|
||||
// Ignore unexisting files
|
||||
if (!(await fs.promises.stat(airportRoutesFile).catch(() => false)))
|
||||
continue
|
||||
|
||||
Airport.bulkCreate(data['response'].filter(airport => !icao_codes.includes(airport.icao_code)
|
||||
&& !iata_codes.includes(airport.iata_code))).then()
|
||||
}).then(() => {
|
||||
Airport.findAll({attributes: ['icao_code']}).then(airports => {
|
||||
airports.forEach(airport => {
|
||||
fs.readFile(__dirname + `/../data/routes/${airport.icao_code}.json`, 'utf-8', (error, data) => {
|
||||
if (error) {
|
||||
console.log(error)
|
||||
return
|
||||
}
|
||||
// Read routes
|
||||
let flightsData = await fs.promises.readFile(airportRoutesFile, 'utf-8')
|
||||
flightsData = JSON.parse(flightsData)
|
||||
|
||||
data = JSON.parse(data)
|
||||
Flight.destroy({where: {dep_icao: airport.icao_code}}).then(() => {
|
||||
Flight.bulkCreate(data['response']).then()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
// First remove old routes
|
||||
await Flight.destroy({where: {dep_icao: airport.icao_code}})
|
||||
|
||||
// Then import new routes
|
||||
await Flight.bulkCreate(flightsData['response'])
|
||||
}
|
||||
})())
|
||||
|
|
Loading…
Reference in New Issue