Better memory and time usage

This commit is contained in:
Emmy D'Anello 2023-08-11 19:14:36 +02:00
parent 4d61358e03
commit eed19a225f
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 33 additions and 34 deletions

1
.gitignore vendored
View File

@ -8,6 +8,7 @@ node_modules
# testing # testing
coverage coverage
*.sqlite3 *.sqlite3
data/
# production # production
build/ build/

View File

@ -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) => { let icao_codes = []
if (error) { let iata_codes = []
console.log(error) let results = await Airport.findAll({attributes: ['icao_code', 'iata_code']})
return 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) // Import flight routes per departure
Airport.findAll({attributes: ['icao_code', 'iata_code']}).then(results => { let airports = await Airport.findAll({attributes: ['icao_code']})
let icao_codes = [] for (let airport of airports) {
let iata_codes = [] const airportRoutesFile = `${__dirname}/../data/routes/${airport.icao_code}.json`
results.forEach(result => { // Ignore unexisting files
if (result.icao_code) if (!(await fs.promises.stat(airportRoutesFile).catch(() => false)))
icao_codes.push(result.icao_code) continue
if (result.iata_code)
iata_codes.push(result.iata_code)
})
Airport.bulkCreate(data['response'].filter(airport => !icao_codes.includes(airport.icao_code) // Read routes
&& !iata_codes.includes(airport.iata_code))).then() let flightsData = await fs.promises.readFile(airportRoutesFile, 'utf-8')
}).then(() => { flightsData = JSON.parse(flightsData)
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
}
data = JSON.parse(data) // First remove old routes
Flight.destroy({where: {dep_icao: airport.icao_code}}).then(() => { await Flight.destroy({where: {dep_icao: airport.icao_code}})
Flight.bulkCreate(data['response']).then()
}) // Then import new routes
}) await Flight.bulkCreate(flightsData['response'])
}) }
}) })())
})
})