Skip to content

Commit

Permalink
Add capsules update script
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewmeyer committed Nov 17, 2018
1 parent 1fb50f7 commit 82f5e10
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"upcoming": "node scripts/upcoming.js",
"launchpad": "node scripts/launchpad.js",
"landpad": "node scripts/landpad.js",
"cores": "node scripts/cores.js"
"cores": "node scripts/cores.js",
"capsules": "node scripts/capsules.js"
},
"repository": {
"type": "git",
Expand Down
102 changes: 102 additions & 0 deletions scripts/capsules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env node

/**
* This script updates capsule missions and landing counts
*/

const MongoClient = require('mongodb');

// Created so we can use async await with requests, and
// to use async sleep function inside the IIFE
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index += 1) {
// Allow await for nested async functions
// eslint-disable-next-line no-await-in-loop
await callback(array[index], index, array);
}
}

(async () => {
let client;
try {
client = await MongoClient.connect(process.env.MONGO_URL, { useNewUrlParser: true });
} catch (err) {
console.log(err.stack);
process.exit(1);
}

const col = client.db('spacex-api').collection('capsule');
const launch = client.db('spacex-api').collection('launch');

const capsules = [];
const data = await col.find({}).sort({ capsule_serial: 1 }).toArray();
data.forEach(capsule => {
capsules.push(capsule.capsule_serial);
});


const start = async () => {
await asyncForEach(capsules, async capsule => {
const landings = await launch.countDocuments({
upcoming: false,
'rocket.second_stage.payloads': {
$elemMatch: {
cap_serial: capsule,
flight_time_sec: { $exists: true },
},
},
launch_success: true,
});

const missions = [];
const launch_results = await launch.find({
upcoming: false,
'rocket.second_stage.payloads': {
$elemMatch: {
cap_serial: capsule,
},
},
}).project({
_id: 0,
flight_number: 1,
mission_name: 1,
}).sort({
flight_number: 1,
}).toArray();

launch_results.forEach(i => {
const mission = {
name: i.mission_name,
flight: i.flight_number,
};
missions.push(mission);
});

let reuse_count;
if (missions.length - 1 < 0) {
reuse_count = 0;
} else {
reuse_count = missions.length - 1;
}

console.log(capsule);
console.log(missions);
console.log(`Reuse Count: ${reuse_count}`);
console.log(`Landings: ${landings}`);

await col.updateOne({ capsule_serial: capsule }, {
$set: {
reuse_count,
landings,
missions,
},
});
});
};

await start();

if (client) {
client.close();
}
})();

0 comments on commit 82f5e10

Please sign in to comment.