From f7e775188e7e2b1fdad1d5ff78568995101a3d6d Mon Sep 17 00:00:00 2001 From: Conor McNamara Date: Thu, 2 Mar 2023 12:54:50 +0000 Subject: [PATCH] Add basic statistics with emulator handling --- dist/index.html | 4 ++-- functions/index.js | 4 ++-- index.html | 2 +- src/components/Map.vue | 35 ++++++++++++++++++++--------------- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/dist/index.html b/dist/index.html index fd26aac..60985ef 100644 --- a/dist/index.html +++ b/dist/index.html @@ -5,8 +5,8 @@ Irish Rail Tracker - - + +
diff --git a/functions/index.js b/functions/index.js index ef9aa38..c40b6e0 100644 --- a/functions/index.js +++ b/functions/index.js @@ -18,7 +18,7 @@ exports.getLiveTrainData = functions.https.onRequest((request, response) => { // fetch the "liveTrainData" collection admin.firestore().collection('liveTrainData').get().then((snapshot) => { if (snapshot.empty) { - response.send({data: "Error fetching live train data from the database"}); + response.status(404).send({data: "Error fetching live train data from the database"}); return; } // iterate through each of the collection's documents @@ -40,7 +40,7 @@ exports.getStationData = functions.https.onRequest((request, response) => { // fetch the "stations" collection admin.firestore().collection('stations').get().then((snapshot) => { if (snapshot.empty) { - response.send({data: "Error fetching station data from the database"}) + response.status(404).send({data: "Error fetching station data from the database"}) return; } // iterate through each of the collection's documents diff --git a/index.html b/index.html index c1a2a1c..e5a66b2 100644 --- a/index.html +++ b/index.html @@ -2,7 +2,7 @@ - + Irish Rail Tracker diff --git a/src/components/Map.vue b/src/components/Map.vue index 64aae09..f4211bd 100644 --- a/src/components/Map.vue +++ b/src/components/Map.vue @@ -4,9 +4,8 @@

Number of actively running trains: {{ this.numRunningTrains }}

Percentage late: {{ this.percentageLate }}%

Percentage early or ontime: {{ this.percentageEarly }}%

- -

Latest train: {{ this.latestTrain["TrainCode"][0] }}, {{ this.latestTrain["Direction"][0] }}, {{ this.latestTime }} mins late

-

Earliest train: {{ this.earliestTrain["TrainCode"][0] }}, {{ this.earliestTrain["Direction"][0] }}, {{ this.earliestTime * -1 }} mins early

+

Latest train: {{ this.latestTrain["TrainCode"][0] }}, {{ this.latestTrain["Direction"][0] }}, {{ this.latestTime }} mins late

+

Earliest train: {{ this.earliestTrain["TrainCode"][0] }}, {{ this.earliestTrain["Direction"][0] }}, {{ this.earliestTime * -1 }} mins early

@@ -32,7 +31,7 @@ - + @@ -78,6 +77,7 @@ export default { strokeWidth, strokeColor, fillColor, + coordinates: [], dbLiveTrainData: [], allDataMap: {}, @@ -97,9 +97,12 @@ export default { }, created() { - // initial request of data - this.getLiveTrainData() - + if (window.location.hostname === '127.0.0.1' || window.location.hostname === 'localhost') { + this.postLiveTrainData() + } + else { + this.getLiveTrainData() + } // request new data every 60 seconds // window.setInterval(this.getLiveTrainData, 60000); }, @@ -108,8 +111,9 @@ export default { // fetch live train data from the Firestore database getLiveTrainData() { const functions = getFunctions(app); - if (window.location.hostname === '127.0.0.1') { - connectFunctionsEmulator(functions, "localhost", 5001); + let host = window.location.hostname + if (host === '127.0.0.1' || host === 'localhost') { + connectFunctionsEmulator(functions, host, 5001); } const getData = httpsCallable(functions, 'getLiveTrainData'); let loader = this.$loading.show({ @@ -121,10 +125,11 @@ export default { getData().then((response) => { try { this.dbLiveTrainData = response.data; + console.log(this.dbLiveTrainData) + this.numRunningTrains = 0; this.numTrains = 0; this.numLateRunningTrains = 0; - var latest = null var currLatestTime = 0 var earliest = null @@ -134,7 +139,7 @@ export default { for(var i=0; i { - console.log("Test") + this.getLiveTrainData() }) } // ---------------- TESTING ----------------