Skip to content

Commit 1fb50f7

Browse files
committed
Add cores update script
1 parent 566e9ad commit 1fb50f7

File tree

2 files changed

+136
-1
lines changed

2 files changed

+136
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"ships": "node scripts/ships.js",
1414
"upcoming": "node scripts/upcoming.js",
1515
"launchpad": "node scripts/launchpad.js",
16-
"landpad": "node scripts/landpad.js"
16+
"landpad": "node scripts/landpad.js",
17+
"cores": "node scripts/cores.js"
1718
},
1819
"repository": {
1920
"type": "git",

scripts/cores.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* This script updates core missions, reuse, and landing counts
5+
*/
6+
7+
const MongoClient = require('mongodb');
8+
9+
// Created so we can use async await with requests, and
10+
// to use async sleep function inside the IIFE
11+
async function asyncForEach(array, callback) {
12+
for (let index = 0; index < array.length; index += 1) {
13+
// Allow await for nested async functions
14+
// eslint-disable-next-line no-await-in-loop
15+
await callback(array[index], index, array);
16+
}
17+
}
18+
19+
(async () => {
20+
let client;
21+
try {
22+
client = await MongoClient.connect(process.env.MONGO_URL, { useNewUrlParser: true });
23+
} catch (err) {
24+
console.log(err.stack);
25+
process.exit(1);
26+
}
27+
28+
const col = client.db('spacex-api').collection('core');
29+
const launch = client.db('spacex-api').collection('launch');
30+
31+
const cores = [];
32+
const data = await col.find({}).sort({ core_serial: 1 }).toArray();
33+
data.forEach(core => {
34+
cores.push(core.core_serial);
35+
});
36+
37+
38+
const start = async () => {
39+
await asyncForEach(cores, async core => {
40+
const rtls_attempts = await launch.countDocuments({
41+
upcoming: false,
42+
'rocket.first_stage.cores': {
43+
$elemMatch: {
44+
core_serial: core, landing_type: 'RTLS', landing_intent: true,
45+
},
46+
},
47+
launch_success: true,
48+
});
49+
const rtls_successes = await launch.countDocuments({
50+
upcoming: false,
51+
'rocket.first_stage.cores': {
52+
$elemMatch: {
53+
core_serial: core, landing_type: 'RTLS', landing_intent: true, land_success: true,
54+
},
55+
},
56+
launch_success: true,
57+
});
58+
const asds_attempts = await launch.countDocuments({
59+
upcoming: false,
60+
'rocket.first_stage.cores': {
61+
$elemMatch: {
62+
core_serial: core, landing_type: 'ASDS', landing_intent: true,
63+
},
64+
},
65+
launch_success: true,
66+
});
67+
const asds_sucesses = await launch.countDocuments({
68+
upcoming: false,
69+
'rocket.first_stage.cores': {
70+
$elemMatch: {
71+
core_serial: core, landing_type: 'ASDS', landing_intent: true, land_success: true,
72+
},
73+
},
74+
launch_success: true,
75+
});
76+
77+
const missions = [];
78+
const launch_results = await launch.find({
79+
upcoming: false,
80+
'rocket.first_stage.cores': {
81+
$elemMatch: {
82+
core_serial: core,
83+
},
84+
},
85+
}).project({
86+
_id: 0,
87+
flight_number: 1,
88+
mission_name: 1,
89+
}).sort({
90+
flight_number: 1,
91+
}).toArray();
92+
93+
launch_results.forEach(i => {
94+
const mission = {
95+
name: i.mission_name,
96+
flight: i.flight_number,
97+
};
98+
missions.push(mission);
99+
});
100+
101+
let reuse_count;
102+
if (missions.length - 1 < 0) {
103+
reuse_count = 0;
104+
} else {
105+
reuse_count = missions.length - 1;
106+
}
107+
108+
console.log(core);
109+
console.log(missions);
110+
console.log(`Reuse Count: ${reuse_count}`);
111+
console.log(`RTLS Attempts: ${rtls_attempts}`);
112+
console.log(`RTLS Sucesses: ${rtls_successes}`);
113+
console.log(`ASDS Attempts: ${asds_attempts}`);
114+
console.log(`ASDS Successes: ${asds_sucesses}\n`);
115+
116+
await col.updateOne({ core_serial: core }, {
117+
$set: {
118+
reuse_count,
119+
rtls_attempts,
120+
rtls_landings: rtls_successes,
121+
asds_attempts,
122+
asds_landings: asds_sucesses,
123+
missions,
124+
},
125+
});
126+
});
127+
};
128+
129+
await start();
130+
131+
if (client) {
132+
client.close();
133+
}
134+
})();

0 commit comments

Comments
 (0)