Extend operations handled by functions

This commit is contained in:
Conor McNamara
2023-03-27 20:21:10 +01:00
parent 789479cc99
commit dc25571c4d
4 changed files with 142 additions and 47 deletions

View File

@ -209,6 +209,49 @@ exports.postLiveTrainData = functions.https.onRequest((request, response) => {
return jsonData; return jsonData;
} }
function getRunningOriginDestination(publicMessage) {
let startOrigin = publicMessage.indexOf("-") + 1
let endOrigin = publicMessage.indexOf("to ") - 1;
let origin = publicMessage.substring(startOrigin, endOrigin);
let startDestination = endOrigin + 4;
let endDestination = publicMessage.indexOf("(") - 1;
let destination = publicMessage.substring(startDestination, endDestination);
return {origin: origin, destination: destination}
}
function getTerminatedOriginDestination(publicMessage) {
let startOrigin = publicMessage.indexOf("-") + 1
let endOrigin = publicMessage.indexOf("to ") - 1;
let origin = publicMessage.substring(startOrigin, endOrigin);
let startDestination = endOrigin + 4;
let endDestination = publicMessage.indexOf("(");
let destination = publicMessage.substring(startDestination, endDestination);
return {origin: origin, destination: destination}
}
function getNotYetRunningOriginDestination(publicMessage) {
let startOrigin = publicMessage.indexOf(".") + 1
let endOrigin = publicMessage.indexOf("to ") - 1;
let origin = publicMessage.substring(startOrigin, endOrigin);
let startDestination = endOrigin + 3;
let endDestination = publicMessage.indexOf(". E");
let destination = publicMessage.substring(startDestination, endDestination);
return {origin: origin, destination: destination}
}
function getOriginDestination(publicMessage, trainType) {
if (trainType == "R") return getRunningOriginDestination(publicMessage)
else if (trainType == "T") return getTerminatedOriginDestination(publicMessage)
else if (trainType == "N") return getNotYetRunningOriginDestination(publicMessage)
}
function getPunctuality(publicMessage, trainType) {
if (trainType == "N") return
let start = publicMessage.indexOf("(") + 1
let end = publicMessage.indexOf(")")
return publicMessage.substring(start, end)
}
// helper function to write to the database // helper function to write to the database
function batchWriteDB(request, response, db, jsonData, trainTypeCode) { function batchWriteDB(request, response, db, jsonData, trainTypeCode) {
if (!jsonData) return if (!jsonData) return
@ -217,10 +260,22 @@ exports.postLiveTrainData = functions.https.onRequest((request, response) => {
cors(request, response, () => { cors(request, response, () => {
var batchWrite = db.batch(); var batchWrite = db.batch();
jsonData.forEach((doc) => { jsonData.forEach((doc) => {
// ignore trains with longitudes or latitudes equal zero // ignore trains with longitudes or latitudes equal zero
if (!(doc["TrainLongitude"] == 0 || doc["TrainLatitude"] == 0)) { if (!(doc["TrainLongitude"] == 0 || doc["TrainLatitude"] == 0)) {
doc["TrainType"] = [trainTypeCode] doc["TrainType"] = [trainTypeCode]
// get the origin, destination and filter out \n from the public message
doc["PublicMessage"][0] = doc["PublicMessage"][0].replace(/\\n/g, ". ");
var originDestination = getOriginDestination(doc["PublicMessage"][0], doc["TrainStatus"][0])
doc["Origin"] = [originDestination.origin]
doc["Destination"] = [originDestination.destination]
// get the lateness if the train is running or terminated
if (doc["TrainStatus"][0] == "R" || doc["TrainStatus"][0] == "T") {
doc["Punctuality"] = [getPunctuality(doc["PublicMessage"][0], doc["TrainStatus"][0])]
}
var docID = db.collection('liveTrainData').doc(doc["TrainCode"][0]); var docID = db.collection('liveTrainData').doc(doc["TrainCode"][0]);
batchWrite.set(docID, doc); batchWrite.set(docID, doc);
} }
@ -275,7 +330,7 @@ exports.postLiveTrainData = functions.https.onRequest((request, response) => {
}) })
// scheduled version // scheduled version
exports.scheduledPostLiveTrainData = functions.pubsub.schedule('every 10 minutes').onRun(async (context) => { exports.scheduledPostLiveTrainData = functions.pubsub.schedule('every 1 minutes').onRun(async (context) => {
// helper function to parse train JSON objects // helper function to parse train JSON objects
function parseJSON(result) { function parseJSON(result) {
let jsonStr = JSON.stringify(result); let jsonStr = JSON.stringify(result);
@ -284,6 +339,49 @@ exports.scheduledPostLiveTrainData = functions.pubsub.schedule('every 10 minutes
return jsonData; return jsonData;
} }
function getRunningOriginDestination(publicMessage) {
let startOrigin = publicMessage.indexOf("-") + 1
let endOrigin = publicMessage.indexOf("to ") - 1;
let origin = publicMessage.substring(startOrigin, endOrigin);
let startDestination = endOrigin + 4;
let endDestination = publicMessage.indexOf("(") - 1;
let destination = publicMessage.substring(startDestination, endDestination);
return {origin: origin, destination: destination}
}
function getTerminatedOriginDestination(publicMessage) {
let startOrigin = publicMessage.indexOf("-") + 1
let endOrigin = publicMessage.indexOf("to ") - 1;
let origin = publicMessage.substring(startOrigin, endOrigin);
let startDestination = endOrigin + 4;
let endDestination = publicMessage.indexOf("(");
let destination = publicMessage.substring(startDestination, endDestination);
return {origin: origin, destination: destination}
}
function getNotYetRunningOriginDestination(publicMessage) {
let startOrigin = publicMessage.indexOf(".") + 1
let endOrigin = publicMessage.indexOf("to ") - 1;
let origin = publicMessage.substring(startOrigin, endOrigin);
let startDestination = endOrigin + 3;
let endDestination = publicMessage.indexOf(". E");
let destination = publicMessage.substring(startDestination, endDestination);
return {origin: origin, destination: destination}
}
function getOriginDestination(publicMessage, trainType) {
if (trainType == "R") return getRunningOriginDestination(publicMessage)
else if (trainType == "T") return getTerminatedOriginDestination(publicMessage)
else if (trainType == "N") return getNotYetRunningOriginDestination(publicMessage)
}
function getPunctuality(publicMessage, trainType) {
if (trainType == "N") return
let start = publicMessage.indexOf("(") + 1
let end = publicMessage.indexOf(")")
return publicMessage.substring(start, end)
}
// helper function to write to the database // helper function to write to the database
function batchWriteDB(db, jsonData, trainTypeCode) { function batchWriteDB(db, jsonData, trainTypeCode) {
if (!jsonData) return if (!jsonData) return
@ -292,6 +390,18 @@ exports.scheduledPostLiveTrainData = functions.pubsub.schedule('every 10 minutes
// ignore trains with longitudes or latitudes equal zero // ignore trains with longitudes or latitudes equal zero
if (!(doc["TrainLongitude"] == 0 || doc["TrainLatitude"] == 0)) { if (!(doc["TrainLongitude"] == 0 || doc["TrainLatitude"] == 0)) {
doc["TrainType"] = [trainTypeCode] doc["TrainType"] = [trainTypeCode]
// get the origin, destination and filter out \n from the public message
doc["PublicMessage"][0] = doc["PublicMessage"][0].replace(/\\n/g, ". ");
var originDestination = getOriginDestination(doc["PublicMessage"][0], doc["TrainStatus"][0])
doc["Origin"] = [originDestination.origin]
doc["Destination"] = [originDestination.destination]
// get the lateness if the train is running or terminated
if (doc["TrainStatus"][0] == "R" || doc["TrainStatus"][0] == "T") {
doc["Punctuality"] = [getPunctuality(doc["PublicMessage"][0], doc["TrainStatus"][0])]
}
var docID = db.collection('liveTrainData').doc(doc["TrainCode"][0]); var docID = db.collection('liveTrainData').doc(doc["TrainCode"][0]);
batchWrite.set(docID, doc); batchWrite.set(docID, doc);
} }

View File

@ -3,7 +3,7 @@
<div v-on:click="store.setDisplaySelectedTrain(false)" id="xButton">X</div> <div v-on:click="store.setDisplaySelectedTrain(false)" id="xButton">X</div>
<h2 style="padding: 10px;">Train Code: {{ store.selectedTrain["TrainCode"][0] }}</h2> <h2 style="padding: 10px;">Train Code: {{ store.selectedTrain["TrainCode"][0] }}</h2>
<div id="sidebarHeader"> <div id="sidebarHeader">
<p id="originDiv">Origin:<br> {{ getOrigin(store.selectedTrain["PublicMessage"][0]) }}</p> <p id="originDiv">Origin:<br> {{ store.selectedTrain["Origin"][0] }}</p>
<div id = "imageDiv" v-if="getTrainType() === 'DART'"> <div id = "imageDiv" v-if="getTrainType() === 'DART'">
<img v-if="isTrainRunning() && isTrainLate()" src="../assets/red-train-tram-solid.png" class="headerImage" alt="Late DART Icon"> <img v-if="isTrainRunning() && isTrainLate()" src="../assets/red-train-tram-solid.png" class="headerImage" alt="Late DART Icon">
<img v-else-if="isTrainRunning() && !isTrainLate()" src="../assets/green-train-tram-solid.png" class="headerImage" alt="On-Time DART Icon"> <img v-else-if="isTrainRunning() && !isTrainLate()" src="../assets/green-train-tram-solid.png" class="headerImage" alt="On-Time DART Icon">
@ -14,7 +14,7 @@
<img v-else-if="isTrainRunning() && !isTrainLate()" src="../assets/green-train-solid.png" class="headerImage" alt="On-Time Train Icon"> <img v-else-if="isTrainRunning() && !isTrainLate()" src="../assets/green-train-solid.png" class="headerImage" alt="On-Time Train Icon">
<img v-else src="../assets/train-solid.svg" class="headerImage" alt="Not Running Train Icon"> <img v-else src="../assets/train-solid.svg" class="headerImage" alt="Not Running Train Icon">
</div> </div>
<p id="destinationDiv">Destination:<br> {{ getDestination(store.selectedTrain["PublicMessage"][0]) }}</p> <p id="destinationDiv">Destination:<br> {{ store.selectedTrain["Destination"][0] }}</p>
</div> </div>
<div id="sidebarDiv"> <div id="sidebarDiv">
@ -49,6 +49,13 @@
<p id="directionP"><span style="color:grey; font-size: 14px;">Direction:</span><br>{{ store.selectedTrain["Direction"][0] }}</p> <p id="directionP"><span style="color:grey; font-size: 14px;">Direction:</span><br>{{ store.selectedTrain["Direction"][0] }}</p>
</div> </div>
<div v-if="store.selectedTrain['TrainStatus'][0] != 'N'" id="punctualityDiv">
<div id="punctualityIcon">
<img id="punctualityImage" src="../assets/publicMessageIcon.png">
</div>
<p id="punctualityP"><span style="color:grey; font-size: 14px;">Punctuality:</span><br>{{ store.selectedTrain["Punctuality"][0] }}</p>
</div>
<div id="publicMessageDiv"> <div id="publicMessageDiv">
<div id="publicMessageIcon"> <div id="publicMessageIcon">
<img id="publicMessageImage" src="../assets/publicMessageIcon.png"> <img id="publicMessageImage" src="../assets/publicMessageIcon.png">
@ -86,19 +93,6 @@ export default {
return false; return false;
}, },
getOrigin(publicMessage) {
let startOrigin = publicMessage.indexOf("-") + 1
let endOrigin = publicMessage.indexOf("to ") - 1;
return publicMessage.substring(startOrigin, endOrigin);
},
getDestination(publicMessage) {
let endOrigin = publicMessage.indexOf("to ");
let startDestination = endOrigin + 3;
let endDestination = publicMessage.indexOf("(") - 1;
return publicMessage.substring(startDestination, endDestination);
},
// method to determine whether or not a selected train is running // method to determine whether or not a selected train is running
isTrainRunning() { isTrainRunning() {
if (store.selectedTrain["TrainStatus"][0] == "R") { if (store.selectedTrain["TrainStatus"][0] == "R") {
@ -112,7 +106,7 @@ export default {
// method that returns the type of train (either "Train" or "DART") // method that returns the type of train (either "Train" or "DART")
getTrainType() { getTrainType() {
return store.selectedTrain["TrainType"][0]; return store.selectedTrain["TrainType"][0];
}, }
} }
} }
</script> </script>
@ -130,6 +124,7 @@ export default {
background-color: rgb(214, 214, 214); background-color: rgb(214, 214, 214);
box-shadow: 0 0 5px 2px rgb(190, 190, 190); box-shadow: 0 0 5px 2px rgb(190, 190, 190);
} }
#sidebarDiv{ #sidebarDiv{
position: absolute; position: absolute;
height: 80%; height: 80%;
@ -137,11 +132,13 @@ export default {
color: rgb(0, 0, 0); color: rgb(0, 0, 0);
padding-top: 10px; padding-top: 10px;
} }
.headerImage{ .headerImage{
height: 100%; height: 100%;
width: 100%; width: 100%;
padding: 10px; padding: 10px;
} }
#imageDiv{ #imageDiv{
background-color: rgb(230, 230, 230); background-color: rgb(230, 230, 230);
border-radius: 50%; border-radius: 50%;
@ -157,6 +154,7 @@ export default {
right:10px; right:10px;
z-index: 5; z-index: 5;
} }
#xButton:hover{ #xButton:hover{
color:red; color:red;
} }
@ -168,6 +166,7 @@ export default {
align-self: center; align-self: center;
padding-top: 3%; padding-top: 3%;
} }
#originDiv{ #originDiv{
order:0; order:0;
} }
@ -178,8 +177,7 @@ export default {
/* Sidebar Section Divs */ /* Sidebar Section Divs */
#typeDiv, #dateDiv, #positionDiv, #directionDiv, #publicMessageDiv, #punctualityDiv{
#typeDiv, #dateDiv, #positionDiv, #directionDiv, #publicMessageDiv{
background-color: rgb(230, 230, 230); background-color: rgb(230, 230, 230);
height: 12%; height: 12%;
position: absolute; position: absolute;
@ -187,7 +185,7 @@ export default {
width:100%; width:100%;
} }
#typeIcon, #dateIcon, #positionIcon, #directionIcon, #publicMessageIcon{ #typeIcon, #dateIcon, #positionIcon, #directionIcon, #publicMessageIcon, #punctualityIcon{
background-color: rgb(214, 214, 214); background-color: rgb(214, 214, 214);
width:20%; width:20%;
height: 100%; height: 100%;
@ -201,7 +199,7 @@ export default {
align-items: center; align-items: center;
} }
#typeP, #dateP, #longP, #latP, #directionP, #publicMessageP{ #typeP, #dateP, #longP, #latP, #directionP, #publicMessageP, #punctualityP{
font-size: 16px; font-size: 16px;
position: relative; position: relative;
bottom: 2px; bottom: 2px;
@ -226,6 +224,7 @@ export default {
#typeImage{ #typeImage{
width: 70%; width: 70%;
} }
#dateImage, #positionImage, #directionImage{ #dateImage, #positionImage, #directionImage{
padding-top: 2px; padding-top: 2px;
padding-bottom: 2px; padding-bottom: 2px;
@ -241,13 +240,12 @@ export default {
#dateDiv{top: 60px;} #dateDiv{top: 60px;}
#positionDiv{top: 110px;} #positionDiv{top: 110px;}
#directionDiv{top: 160px;} #directionDiv{top: 160px;}
#punctualityDiv{top: 210px;}
#publicMessageDiv{ #publicMessageDiv{
top:210px; top:260px;
height: 40%; height: 40%;
} }
@media screen and (max-width: 850px) { @media screen and (max-width: 850px) {
.headerImage{ .headerImage{
height: 100%; height: 100%;

View File

@ -45,7 +45,8 @@
<table> <table>
<div style="left:3px; top:3px;" class="form-check form-switch"> <div style="left:3px; top:3px;" class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" v-model="showTopEarliestLatest"/> <input class="form-check-input" type="checkbox" role="switch" v-model="showTopEarliestLatest"/>
<label class="form-check-label" for="showTopEarliestLatest">Show All Train Entries</label> <label v-if="showTopEarliestLatest" class="form-check-label" for="showTopEarliestLatest">Showing All Trains</label>
<label v-else class="form-check-label" for="showTopEarliestLatest">Showing Top 3 Earliest/Latest Trains</label>
</div> </div>
<thead> <thead>
<tr> <tr>
@ -62,8 +63,8 @@
<td v-if="train.time > 0"><span style="color: #fc1919;">{{ train.time }} mins late</span></td> <td v-if="train.time > 0"><span style="color: #fc1919;">{{ train.time }} mins late</span></td>
<td v-else><span style="color: rgb(129, 213, 3);">{{ train.time * -1}} mins early</span></td> <td v-else><span style="color: rgb(129, 213, 3);">{{ train.time * -1}} mins early</span></td>
<td>{{ this.rawData[train.jsonIndex]["TrainType"][0] }}</td> <td>{{ this.rawData[train.jsonIndex]["TrainType"][0] }}</td>
<td>{{ getOrigin(this.rawData[train.jsonIndex]["PublicMessage"][0]) }}</td> <td>{{ this.rawData[train.jsonIndex]["Origin"][0] }}</td>
<td>{{ getDestination(this.rawData[train.jsonIndex]["PublicMessage"][0]) }}</td> <td>{{ this.rawData[train.jsonIndex]["Destination"][0] }}</td>
</tr> </tr>
</tbody> </tbody>
@ -73,8 +74,8 @@
<td v-if="train.time > 0"><span style="color: #fc1919;">{{ train.time }} mins late</span></td> <td v-if="train.time > 0"><span style="color: #fc1919;">{{ train.time }} mins late</span></td>
<td v-else><span style="color: rgb(129, 213, 3);">{{ train.time * -1}} mins early</span></td> <td v-else><span style="color: rgb(129, 213, 3);">{{ train.time * -1}} mins early</span></td>
<td>{{ this.rawData[train.jsonIndex]["TrainType"][0] }}</td> <td>{{ this.rawData[train.jsonIndex]["TrainType"][0] }}</td>
<td>{{ getOrigin(this.rawData[train.jsonIndex]["PublicMessage"][0]) }}</td> <td>{{ this.rawData[train.jsonIndex]["Origin"][0] }}</td>
<td>{{ getDestination(this.rawData[train.jsonIndex]["PublicMessage"][0]) }}</td> <td>{{ this.rawData[train.jsonIndex]["Destination"][0] }}</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
@ -158,19 +159,6 @@ export default {
this.toast() this.toast()
}, },
getOrigin(publicMessage) {
let startOrigin = publicMessage.indexOf("-") + 1
let endOrigin = publicMessage.indexOf("to ") - 1;
return publicMessage.substring(startOrigin, endOrigin);
},
getDestination(publicMessage) {
let endOrigin = publicMessage.indexOf("to ");
let startDestination = endOrigin + 3;
let endDestination = publicMessage.indexOf("(") - 1;
return publicMessage.substring(startDestination, endDestination);
},
postTrainAndStationData() { postTrainAndStationData() {
const functions = getFunctions(app); const functions = getFunctions(app);
let host = window.location.hostname let host = window.location.hostname

View File

@ -434,8 +434,7 @@ export default {
if (train["TrainType"][0] == "Train") insights["numTrains"] += 1; if (train["TrainType"][0] == "Train") insights["numTrains"] += 1;
else if (train["TrainType"][0] == "DART") insights["numDarts"] += 1; else if (train["TrainType"][0] == "DART") insights["numDarts"] += 1;
// filter out \n in public messages
train["PublicMessage"][0] = train["PublicMessage"][0].replace(/\\n/g, ". ");
let publicMessage = train["PublicMessage"][0]; let publicMessage = train["PublicMessage"][0];
currentMessages.push(publicMessage); currentMessages.push(publicMessage);
@ -662,8 +661,8 @@ export default {
color: black; color: black;
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif; font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
text-align: bottom; text-align: bottom;
font-size: 18px; font-size: 16.5px;
height: 4.2vh; height: 4vh;
} }
/* Phone Screen CSS */ /* Phone Screen CSS */