Add basic statistics

This commit is contained in:
Conor McNamara
2023-03-01 21:42:04 +00:00
parent 2633ad564e
commit 39a18f4820
3 changed files with 90 additions and 2 deletions

1
.gitignore vendored
View File

@ -64,5 +64,4 @@ node_modules/
# dotenv environment variables file # dotenv environment variables file
.env .env
./src/api/firebase.js
firebase.js firebase.js

View File

@ -1,6 +1,20 @@
# Running Build & Deploying it to Firebase # Running Build & Deploying it to Firebase
`npm run build && firebase deploy` `npm run build && firebase deploy`
# Running Firebase Tests
`cd functions && npm run test`
# Running Locally
1. Ensure you have Java 11 or greater installed.
2. Run `npm install` in the traintracker directory.
3. Run `npm install` in the functions directory.
4. Start the emultor (needed to emulate the functions locally) `firebase emulators:start &` (the & forks it into the background).
5. Run `npm run dev `
You will need to click the "Populate Database" and "Fetch Data" buttons to get data into your local version of the webapp.
To kill the npm process do `CTRL + C` in your terminal.
To kill the firebase emulators do run fg to bring the process to the foreground, then do `CTRL + C`
# Committing # Committing
Make sure that you exclude the Firebase API key from any commits! Make sure that you exclude the Firebase API key from any commits!

View File

@ -1,4 +1,13 @@
<template> <template>
<h2>Insights:</h2>
<p>Total number of trains: {{ this.numTrains }}</p>
<p>Number of actively running trains: {{ this.numRunningTrains }}</p>
<p>Percentage late: {{ this.percentageLate }}%</p>
<p>Percentage early or ontime: {{ this.percentageEarly }}%</p>
<p>Latest train: {{ this.latestTrain["TrainCode"][0] }}, {{ this.latestTrain["Direction"][0] }}, {{ this.latestTime }} mins late</p>
<p>Earliest train: {{ this.earliestTrain["TrainCode"][0] }}, {{ this.earliestTrain["Direction"][0] }}, {{ this.earliestTime * -1 }} mins early</p>
<button @click="getLiveTrainData">Fetch Data</button> <button @click="getLiveTrainData">Fetch Data</button>
<button @click="postLiveTrainData">Populate Database</button> <button @click="postLiveTrainData">Populate Database</button>
@ -74,12 +83,21 @@ export default {
allDataMap: {}, allDataMap: {},
selectedDataMap: {}, selectedDataMap: {},
display: false, display: false,
numTrains: 0,
numRunningTrains: 0,
numLateRunningTrains: 0,
latestTrain: {},
earliestTrain: {},
percentageEarly: 0,
percentageLate: 0,
latestTime: 0,
earliestTime: 0,
} }
}, },
created() { created() {
// initial request of data // initial request of data
console.log("jere")
this.getLiveTrainData() this.getLiveTrainData()
// request new data every 60 seconds // request new data every 60 seconds
@ -103,11 +121,68 @@ export default {
getData().then((response) => { getData().then((response) => {
try { try {
this.dbLiveTrainData = response.data; this.dbLiveTrainData = response.data;
this.numRunningTrains = 0;
this.numTrains = 0;
this.numLateRunningTrains = 0;
var latest = null
var currLatestTime = 0
var earliest = null
var currEarliestTime = 0
// create an array of coordinates and hashmap with the key-values {index: JSON obj} // create an array of coordinates and hashmap with the key-values {index: JSON obj}
for(var i=0; i<this.dbLiveTrainData.length; i++) { for(var i=0; i<this.dbLiveTrainData.length; i++) {
this.coordinates[i] = ref(fromLonLat([this.dbLiveTrainData[i]["TrainLongitude"][0], this.dbLiveTrainData[i]["TrainLatitude"][0]])) this.coordinates[i] = ref(fromLonLat([this.dbLiveTrainData[i]["TrainLongitude"][0], this.dbLiveTrainData[i]["TrainLatitude"][0]]))
this.allDataMap[i] = this.dbLiveTrainData[i]; this.allDataMap[i] = this.dbLiveTrainData[i];
// check if the train is running
if (this.dbLiveTrainData[i]["TrainStatus"][0] == "R") {
this.numRunningTrains += 1;
let publicMessage = this.dbLiveTrainData[i]["PublicMessage"][0];
let startTimeStr = publicMessage.indexOf("(")
// late
if (publicMessage[startTimeStr+1] != "-" && publicMessage[startTimeStr+1] != "0") {
this.numLateRunningTrains += 1;
if (!latest) {
latest = this.dbLiveTrainData[i];
} }
let timeEnd = publicMessage.indexOf(" ", startTimeStr+1);
let num = parseInt(publicMessage.substring(startTimeStr+1, timeEnd))
// new latest train
if (num > currLatestTime) {
latest = this.dbLiveTrainData[i]
currLatestTime = num
}
}
// early or ontime
else {
if (!earliest) {
earliest = this.dbLiveTrainData[i];
}
let timeEnd = publicMessage.indexOf(" ", startTimeStr+1);
let num = parseInt(publicMessage.substring(startTimeStr+1, timeEnd))
// new earliest train, negative as early trains defined as negative x mins late
if (num < currEarliestTime) {
earliest = this.dbLiveTrainData[i]
currEarliestTime = num
}
}
}
}
this.percentageLate = ((this.numLateRunningTrains / this.numRunningTrains) * 100).toFixed(2);
this.percentageEarly = 100 - this.percentageLate;
this.numTrains = Object.keys(this.allDataMap).length;
this.latestTrain = latest;
this.earliestTrain = earliest;
this.latestTime = currLatestTime;
this.earliestTime = currEarliestTime;
loader.hide(); loader.hide();
} }
catch (error) { catch (error) {