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]}
};
const queryData = await db.query('timetable', query);
console.log(JSON.stringify(queryData));
let trainData = await parseTrains(queryData);
let preparedData = [];
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
let trainUids = [];
for (const i in data) {
const trainUid = data[i]['trainUid'];
for (const i of data) {
console.log(i);
const trainUid = i['trainUid'];
if (!trainUids.includes(trainUid)) {
data.push(trainUid);
trainUids.push(trainUid);
}
}
let parsedData = [];
@ -71,108 +73,51 @@ async function findByTrainUid(uid, date = new Date()) { // Date defaults to toda
scheduleEndDate: {$gte: date}
};
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) {
// Handle empty queryData
return {
c: [],
n: [],
o: [],
p: []
};
return [];
}
const trainUidSet = new Set();
const trainUidStatus = {}; // Track the status of each trainUid
let stpIndicators = {};
for (const serviceDetail of queryData) {
const trainUid = serviceDetail['trainUid'];
const stpIndicator = serviceDetail['stpIndicator'];
if (!trainUidStatus[trainUid]) {
trainUidStatus[trainUid] = {
hasO: false,
if (!stpIndicators[trainUid]) {
stpIndicators[trainUid] = {
hasC: false,
hasN: false,
hasC: false
hasO: false,
hasP: false
};
}
if (stpIndicator === 'O') {
trainUidStatus[trainUid].hasO = true;
} else if (stpIndicator === 'N') {
trainUidStatus[trainUid].hasN = true;
} else if (stpIndicator === 'C') {
trainUidStatus[trainUid].hasC = true;
if (stpIndicator === 'C') {
stpIndicators[trainUid].hasC = true;
}
if (stpIndicator === 'N') {
stpIndicators[trainUid].hasN = true;
}
if (stpIndicator === 'O') {
stpIndicators[trainUid].hasO = true;
}
if (stpIndicator === 'P') {
stpIndicators[trainUid].hasP = true;
}
trainUidSet.add(trainUid);
}
const c = [];
const n = [];
const o = [];
const p = [];
console.log(JSON.stringify(stpIndicators));
let preparedData = [];
for (const serviceDetail of queryData) {
const trainUid = serviceDetail['trainUid'];
const thisStpIndicators = stpIndicators[trainUid];
const stpIndicator = serviceDetail['stpIndicator'];
if (stpIndicator === 'C') {
continue; // Skip 'C' entries
continue;
}
if (stpIndicator === 'N' && !trainUidStatus[trainUid].hasC) {
n.push(trainUid);
} else if (stpIndicator === 'O' && !trainUidStatus[trainUid].hasN && !trainUidStatus[trainUid].hasC) {
o.push(trainUid);
} else if (stpIndicator === 'P' && !trainUidStatus[trainUid].hasN && !trainUidStatus[trainUid].hasO && !trainUidStatus[trainUid].hasC) {
p.push(trainUid);
if (stpIndicator === 'N' && !thisStpIndicators.hasC) {
preparedData.push(serviceDetail);
} else if (stpIndicator === 'O' && !thisStpIndicators.hasC && !thisStpIndicators.hasN) {
preparedData.push(serviceDetail);
} else if (stpIndicator === 'P' && !thisStpIndicators.hasC && !thisStpIndicators.hasN && !thisStpIndicators.hasO) {
preparedData.push(serviceDetail);
}
}
return {
c,
n,
o,
p
};
}
*/
return preparedData;
}