Better memory and time usage
This commit is contained in:
parent
4d61358e03
commit
eed19a225f
|
@ -8,6 +8,7 @@ node_modules
|
||||||
# testing
|
# testing
|
||||||
coverage
|
coverage
|
||||||
*.sqlite3
|
*.sqlite3
|
||||||
|
data/
|
||||||
|
|
||||||
# production
|
# production
|
||||||
build/
|
build/
|
||||||
|
|
|
@ -4,42 +4,40 @@ const db = require("../models")
|
||||||
const Airport = db['Airport']
|
const Airport = db['Airport']
|
||||||
const Flight = db['Flight']
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
data = JSON.parse(data)
|
|
||||||
Airport.findAll({attributes: ['icao_code', 'iata_code']}).then(results => {
|
|
||||||
let icao_codes = []
|
let icao_codes = []
|
||||||
let iata_codes = []
|
let iata_codes = []
|
||||||
results.forEach(result => {
|
let results = await Airport.findAll({attributes: ['icao_code', 'iata_code']})
|
||||||
|
for (let result of results) {
|
||||||
if (result.icao_code)
|
if (result.icao_code)
|
||||||
icao_codes.push(result.icao_code)
|
icao_codes.push(result.icao_code)
|
||||||
if (result.iata_code)
|
if (result.iata_code)
|
||||||
iata_codes.push(result.iata_code)
|
iata_codes.push(result.iata_code)
|
||||||
})
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
// 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)
|
// Import flight routes per departure
|
||||||
Flight.destroy({where: {dep_icao: airport.icao_code}}).then(() => {
|
let airports = await Airport.findAll({attributes: ['icao_code']})
|
||||||
Flight.bulkCreate(data['response']).then()
|
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
|
||||||
})
|
|
||||||
})
|
// Read routes
|
||||||
|
let flightsData = await fs.promises.readFile(airportRoutesFile, 'utf-8')
|
||||||
|
flightsData = JSON.parse(flightsData)
|
||||||
|
|
||||||
|
// 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