Mark summits that don't appear in SOTA DB CSV anymore as retired and don't return them in searches

master
Manuel Kasper 2023-08-22 09:43:55 +02:00
rodzic bdc644cb27
commit 5e764a7ade
2 zmienionych plików z 26 dodań i 4 usunięć

Wyświetl plik

@ -23,6 +23,7 @@ async function processSummitList(db) {
let body = response.data.substring(response.data.indexOf("\n")+1, response.data.length);
let summits = parse(body, {columns: true, relax_column_count: true});
let csvSummitCodes = new Set();
if (summits.length < 100000) {
console.error("Bad number of summits, expecting more than 100000");
@ -41,6 +42,7 @@ async function processSummitList(db) {
summit.ActivationDate = null;
summit.ActivationCall = null;
}
csvSummitCodes.add(summit.SummitCode);
bulkWrites.push({updateOne: {
filter: {code: summit.SummitCode},
@ -60,6 +62,9 @@ async function processSummitList(db) {
activationCount: parseInt(summit.ActivationCount),
activationCall: summit.ActivationCall,
activationDate: summit.ActivationDate
},
$unset: {
retired: ""
}},
upsert: true
}});
@ -115,6 +120,19 @@ async function processSummitList(db) {
await db.collection('summits').bulkWrite(bulkWrites);
}
// Fetch all non-retired summit codes in DB and find those that don't exist in the CSV anymore
let dbSummitCodes = new Set(await db.collection('summits').distinct('code', {'retired': {$in: [null, false]}}));
let retiredSummitCodes = new Set([...dbSummitCodes].filter(x => !csvSummitCodes.has(x)));
// Mark those summits as retired in DB, and also warn if one of them has photos
for (const code of retiredSummitCodes) {
let summit = await db.collection('summits').findOne({code});
if (summit.photos) {
console.error(`[ALERT] Summit ${code} has been retired, but still has photos!`);
}
await db.collection('summits').updateOne({code}, {$set: {retired: true}});
}
// Update associations
for (let association of associations.values()) {
association.regions = [...association.regions.values()];

Wyświetl plik

@ -71,7 +71,10 @@ app.get('/summits/search', (req, res) => {
limit = limitOverride;
}
}
db.getDb().collection('summits').find({$or: [{code: {'$regex': req.query.q, '$options': 'i'}}, {name: {'$regex': req.query.q, '$options': 'i'}}, {nameNd: {'$regex': req.query.q, '$options': 'i'}}]}, {projection: {'_id': false, 'photos': false, 'routes': false, 'links': false, 'resources': false}}).limit(limit).toArray((err, summits) => {
db.getDb().collection('summits').find({
$or: [{code: {'$regex': req.query.q, '$options': 'i'}}, {name: {'$regex': req.query.q, '$options': 'i'}}, {nameNd: {'$regex': req.query.q, '$options': 'i'}}],
retired: {$in: [null, false]}
}, {projection: {'_id': false, 'photos': false, 'routes': false, 'links': false, 'resources': false}}).limit(limit).toArray((err, summits) => {
if (err) {
console.error(err);
res.status(500).end();
@ -91,7 +94,8 @@ app.get('/summits/near', (req, res) => {
}
}
let query = {
coordinates: {$near: {$geometry: {type: "Point", coordinates: [parseFloat(req.query.lon), parseFloat(req.query.lat)]}}}
coordinates: {$near: {$geometry: {type: "Point", coordinates: [parseFloat(req.query.lon), parseFloat(req.query.lat)]}}},
retired: {$in: [null, false]}
};
if (req.query.maxDistance) {
query.coordinates.$near.$maxDistance = parseFloat(req.query.maxDistance);
@ -142,7 +146,7 @@ app.get('/summits/:association/:code', (req, res) => {
noCache: true
};
db.getDb().collection('summits').findOne({code: req.params.association + '/' + req.params.code}, {projection: {'_id': false}}, (err, summit) => {
db.getDb().collection('summits').findOne({code: req.params.association + '/' + req.params.code, retired: {$in: [null, false]}}, {projection: {'_id': false}}, (err, summit) => {
if (err) {
console.error(err);
res.status(500).end();
@ -209,7 +213,7 @@ app.get('/regions/:association/:region', (req, res) => {
res.status(400).end();
return;
}
db.getDb().collection('summits').find({code: {'$regex': '^' + region}}, {projection: {'_id': false, 'routes': false, 'links': false, 'resources': false}}).toArray((err, summits) => {
db.getDb().collection('summits').find({code: {'$regex': '^' + region}, retired: {$in: [null, false]}}, {projection: {'_id': false, 'routes': false, 'links': false, 'resources': false}}).toArray((err, summits) => {
if (err) {
console.error(err);
res.status(500).end();