Skip to content

Commit 2883a73

Browse files
committed
Upcoming launches update script can now rearrange flight numbers according to manifest order changes
1 parent 3b0a551 commit 2883a73

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

scripts/upcoming.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This script gathers dates and payload names from the subreddit launch manifest,
55
* fuzzy checks them against existing upcoming payload id's and updates the date if a
66
* change is made in the wiki. The proper time zone is calculated from the launch site
7-
* id of the launch.
7+
* id of the launch. It also updates the flight number order based on the launch manifest order.
88
*
99
* Hopefully the format of the wiki does not change, but there's no real reason for it to change in the
1010
* forseeable future. If it does change, this script will have to be updated as necessary.
@@ -28,6 +28,7 @@ const sites = [];
2828
const payloads = [];
2929
const promises = [];
3030
const precision = [];
31+
const flight_numbers = [];
3132

3233
// RegEx expressions for matching dates in the wiki manifest
3334
// Allows for long months or short months ex. September vs Sep
@@ -44,7 +45,8 @@ const year = /^[0-9]{4}$/;
4445
const db = client.db('spacex-api');
4546
const col = db.collection('launch');
4647

47-
const launches = await col.find({ upcoming: true }).toArray();
48+
const launches = await col.find({ upcoming: true }).sort({ flight_number: 1 }).toArray();
49+
const base_flight_number = launches[0].flight_number;
4850

4951
// Collect site names for time zone and payload name for fuzzy check
5052
launches.forEach(launch => {
@@ -132,8 +134,12 @@ const year = /^[0-9]{4}$/;
132134
localTime = time.tz('America/Chicago').format();
133135
}
134136

137+
// Add flight numbers to array to check for duplicates
138+
flight_numbers.push(base_flight_number + m_index);
139+
135140
// Build launch time objects to update
136141
calculatedTimes = {
142+
flight_number: (base_flight_number + m_index),
137143
launch_date_unix: zone.unix(),
138144
launch_date_utc: zone.toISOString(),
139145
launch_date_local: localTime,
@@ -149,13 +155,21 @@ const year = /^[0-9]{4}$/;
149155
payload = 'Iridium NEXT 8';
150156
}
151157

152-
// Add to array of promises to update all at once after the forEach functions finish
153-
promises.push(col.updateOne({ 'rocket.second_stage.payloads.payload_id': payload }, { $set: calculatedTimes }));
158+
// Add to array of promises to update all at once after the forEach iterations finish
159+
// promises.push(col.updateOne({ 'rocket.second_stage.payloads.payload_id': payload }, { $set: calculatedTimes }));
154160
}
155161
});
156162
});
163+
164+
// Check if duplicate flight numbers exist
165+
if ([...new Set(flight_numbers)].length < flight_numbers.length) {
166+
console.log('Duplicate flight numbers found');
167+
process.exit(1);
168+
}
169+
157170
// Execute all our stored update promises
158171
const output = await Promise.all(promises);
172+
159173
// Display if the document was found, and if it was modified or not
160174
output.forEach(doc => {
161175
console.log(`N: ${doc.result.n}`);

0 commit comments

Comments
 (0)