Continued work on filtering train service data

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-06-07 11:23:30 +01:00
parent dc583a4632
commit c4b3be9086

View File

@ -16,6 +16,7 @@ async function findByHeadcodeToday(headcode) {
daysRun: {$in: [shortDay]} daysRun: {$in: [shortDay]}
}; };
const queryData = await db.query('timetable', query); const queryData = await db.query('timetable', query);
console.log(JSON.stringify(queryData));
let trainData = await parseTrains(queryData); let trainData = await parseTrains(queryData);
let preparedData = []; let preparedData = [];
for (const trainService in trainData) { for (const trainService in trainData) {
@ -50,10 +51,11 @@ async function getPublicStops(data) {
async function parseTrains(data) { // Takes a single days data from a headcode query and returns only relevant services async function parseTrains(data) { // Takes a single days data from a headcode query and returns only relevant services
let trainUids = []; let trainUids = [];
for (const i in data) { for (const i of data) {
const trainUid = data[i]['trainUid']; console.log(i);
const trainUid = i['trainUid'];
if (!trainUids.includes(trainUid)) { if (!trainUids.includes(trainUid)) {
data.push(trainUid); trainUids.push(trainUid);
} }
} }
let parsedData = []; let parsedData = [];
@ -71,108 +73,51 @@ async function findByTrainUid(uid, date = new Date()) { // Date defaults to toda
scheduleEndDate: {$gte: date} scheduleEndDate: {$gte: date}
}; };
const queryData = await db.query('timetable', query); const queryData = await db.query('timetable', query);
let c = [];
let n = [];
let o = [];
let p = [];
for (const serviceDetail of queryData) {
const trainUid = serviceDetail['trainUid'];
switch (serviceDetail['stpIndicator']) { // This won't work without further post procesing
case 'C':
c.push(trainUid);
break;
case 'N':
if (!c.includes(trainUid)) {
n.push(trainUid);
}
break;
case 'O':
if (!c.includes(trainUid) && !n.includes(trainUid)) {
o.push(trainUid);
}
break;
case 'P':
if (!c.includes(trainUid) && !n.includes(trainUid) && !o.includes(trainUid)) {
p.push(trainUid);
}
break;
}
}
}
/* Thoughts:
async function findByTrainUid(uid, date = new Date()) {
const query = {
trainUid: uid,
scheduleStartDate: { $lte: date },
scheduleEndDate: { $gte: date }
};
const queryData = await db.query('timetable', query);
if (queryData.length === 0) { if (queryData.length === 0) {
// Handle empty queryData return [];
return {
c: [],
n: [],
o: [],
p: []
};
} }
let stpIndicators = {};
const trainUidSet = new Set();
const trainUidStatus = {}; // Track the status of each trainUid
for (const serviceDetail of queryData) { for (const serviceDetail of queryData) {
const trainUid = serviceDetail['trainUid']; const trainUid = serviceDetail['trainUid'];
const stpIndicator = serviceDetail['stpIndicator']; const stpIndicator = serviceDetail['stpIndicator'];
if (!stpIndicators[trainUid]) {
if (!trainUidStatus[trainUid]) { stpIndicators[trainUid] = {
trainUidStatus[trainUid] = { hasC: false,
hasO: false,
hasN: false, hasN: false,
hasC: false hasO: false,
hasP: false
}; };
} }
if (stpIndicator === 'C') {
if (stpIndicator === 'O') { stpIndicators[trainUid].hasC = true;
trainUidStatus[trainUid].hasO = true; }
} else if (stpIndicator === 'N') { if (stpIndicator === 'N') {
trainUidStatus[trainUid].hasN = true; stpIndicators[trainUid].hasN = true;
} else if (stpIndicator === 'C') { }
trainUidStatus[trainUid].hasC = true; if (stpIndicator === 'O') {
stpIndicators[trainUid].hasO = true;
}
if (stpIndicator === 'P') {
stpIndicators[trainUid].hasP = true;
} }
trainUidSet.add(trainUid);
} }
console.log(JSON.stringify(stpIndicators));
const c = []; let preparedData = [];
const n = [];
const o = [];
const p = [];
for (const serviceDetail of queryData) { for (const serviceDetail of queryData) {
const trainUid = serviceDetail['trainUid']; const trainUid = serviceDetail['trainUid'];
const thisStpIndicators = stpIndicators[trainUid];
const stpIndicator = serviceDetail['stpIndicator']; const stpIndicator = serviceDetail['stpIndicator'];
if (stpIndicator === 'C') { if (stpIndicator === 'C') {
continue; // Skip 'C' entries continue;
} }
if (stpIndicator === 'N' && !thisStpIndicators.hasC) {
if (stpIndicator === 'N' && !trainUidStatus[trainUid].hasC) { preparedData.push(serviceDetail);
n.push(trainUid); } else if (stpIndicator === 'O' && !thisStpIndicators.hasC && !thisStpIndicators.hasN) {
} else if (stpIndicator === 'O' && !trainUidStatus[trainUid].hasN && !trainUidStatus[trainUid].hasC) { preparedData.push(serviceDetail);
o.push(trainUid); } else if (stpIndicator === 'P' && !thisStpIndicators.hasC && !thisStpIndicators.hasN && !thisStpIndicators.hasO) {
} else if (stpIndicator === 'P' && !trainUidStatus[trainUid].hasN && !trainUidStatus[trainUid].hasO && !trainUidStatus[trainUid].hasC) { preparedData.push(serviceDetail);
p.push(trainUid);
} }
} }
return preparedData;
return {
c,
n,
o,
p
};
} }
*/