Zmiany w wyglądzie tabelki, połączenie kolumn ze szlakami

This commit is contained in:
2020-08-24 19:45:52 +02:00
parent b5b5cb6f85
commit 520a1083dc
4 changed files with 246 additions and 159 deletions
+228 -149
View File
@@ -1,175 +1,254 @@
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
// import * as functions from "firebase-functions";
// import * as admin from "firebase-admin";
admin.initializeApp();
// admin.initializeApp();
// const db = admin.firestore();
import axios from "axios";
// import axios from "axios";
import stationJSONList from "./stations.json";
// import stationJSONList from "./stations.json";
let stationAPIData: {
stationName: string;
dispatcherName: string;
isOnline: boolean;
region: string;
}[] = [];
// let stationAPIData: {
// stationName: string;
// dispatcherName: string;
// isOnline: boolean;
// region: string;
// }[] = [];
let previousOnlineStations: {
stationName: string;
dispatcherName: string;
occupiedFrom: number;
}[] = [];
// let previousOnlineStations: {
// stationName: string;
// dispatcherName: string;
// occupiedFrom: number;
// }[] = [];
const API_URL = "https://api.td2.info.pl:9640/?method=getStationsOnline";
// const API_URL = "https://api.td2.info.pl:9640/?method=getStationsOnline";
exports.updateHistory = functions.pubsub
.schedule("*/5 * * * *")
.onRun(async () => {
try {
stationAPIData = await (await axios.get(API_URL)).data.message;
} catch (error) {
return;
}
// exports.updateHistory = functions.pubsub
// .schedule("*/5 * * * *")
// .onRun(async (context) => {
// try {
// stationAPIData = await (await axios.get(API_URL)).data.message;
// } catch (error) {
// return;
// }
// On server start
if (previousOnlineStations.length == 0) {
stationAPIData
.filter(
(station) =>
station.isOnline &&
station.region === "eu" &&
stationJSONList.some(
(data) => data.stationName === station.stationName
)
)
.forEach((station) => {
const occupiedFrom = Date.now();
// // On server start
// if (previousOnlineStations.length == 0) {
// stationAPIData
// .filter(
// (station) =>
// station.isOnline &&
// station.region === "eu" &&
// stationJSONList.some(
// (data) => data.stationName === station.stationName
// )
// )
// .forEach((station) => {
// const occupiedFrom = Date.now();
previousOnlineStations.push({
stationName: station.stationName,
dispatcherName: station.dispatcherName,
occupiedFrom,
});
});
// previousOnlineStations.push({
// stationName: station.stationName,
// dispatcherName: station.dispatcherName,
// occupiedFrom,
// });
// });
return;
}
// return;
// }
// When array with previous stations isn't empty
previousOnlineStations.forEach((prevStation) => {
const currStationData = stationAPIData.find(
(currStation) => currStation.stationName === prevStation.stationName
);
// // When array with previous stations isn't empty
// previousOnlineStations.forEach((prevStation) => {
// const currStationData = stationAPIData.find(
// (currStation) => currStation.stationName === prevStation.stationName
// );
// Dispatcher left
if (!currStationData) {
previousOnlineStations = previousOnlineStations.filter(
(s) => s.stationName !== prevStation.stationName
);
}
// Dispatchers switched
else if (prevStation.dispatcherName !== currStationData.dispatcherName) {
previousOnlineStations = previousOnlineStations.filter(
(s) => s.stationName !== prevStation.stationName
);
// // Dispatcher left
// if (!currStationData) {
// previousOnlineStations = previousOnlineStations.filter(
// (s) => s.stationName !== prevStation.stationName
// );
// }
// // Dispatchers switched
// else if (prevStation.dispatcherName !== currStationData.dispatcherName) {
// previousOnlineStations = previousOnlineStations.filter(
// (s) => s.stationName !== prevStation.stationName
// );
previousOnlineStations.push({
stationName: currStationData.stationName,
dispatcherName: currStationData.dispatcherName,
occupiedFrom: Date.now(),
});
}
});
// previousOnlineStations.push({
// stationName: currStationData.stationName,
// dispatcherName: currStationData.dispatcherName,
// occupiedFrom: Date.now(),
// });
// }
// });
stationAPIData
.filter(
(stationData) =>
!previousOnlineStations.find(
(prevStation) => prevStation.stationName === stationData.stationName
)
)
.forEach((stationData) => {
previousOnlineStations.push({
stationName: stationData.stationName,
dispatcherName: stationData.dispatcherName,
occupiedFrom: Date.now(),
});
});
});
// stationAPIData
// .filter(
// (stationData) =>
// !previousOnlineStations.find(
// (prevStation) => prevStation.stationName === stationData.stationName
// )
// )
// .forEach((stationData) => {
// previousOnlineStations.push({
// stationName: stationData.stationName,
// dispatcherName: stationData.dispatcherName,
// occupiedFrom: Date.now(),
// });
// });
exports.getHistoryData = functions.https.onCall((data, context) => {
return { previousOnlineStations };
});
// const stationsDoc = db.collection("stations").doc("previous");
// // const scheduledUpdate = functions.pubsub
// // .schedule("0 * * * *")
// // .onRun(async (context) => {
// // let stationData: {
// // stationName: string;
// // dispatcherName: string;
// // isOnline: boolean;
// // region: string;
// // }[];
// stationsDoc.set({ previousOnlineStations });
// });
// // try {
// // stationData = await (
// // await axios.get(
// // "https://api.td2.info.pl:9640/?method=getStationsOnline"
// // )
// // ).data.message;
// // } catch (error) {
// // return;
// // }
// exports.test = functions.https.onRequest(async (req, res) => {
// try {
// stationAPIData = await (await axios.get(API_URL)).data.message;
// } catch (error) {
// return;
// }
// // const historyRef = db.collection("history");
// // On server start
// if (previousOnlineStations.length == 0) {
// stationAPIData
// .filter(
// (station) =>
// station.isOnline &&
// station.region === "eu" &&
// stationJSONList.some(
// (data) => data.stationName === station.stationName
// )
// )
// .forEach((station) => {
// const occupiedFrom = Date.now();
// // stationData.forEach(async (station) => {
// // const docRef = historyRef.doc(station.stationName);
// // const docSnapshot = await docRef.get();
// previousOnlineStations.push({
// stationName: station.stationName,
// dispatcherName: station.dispatcherName,
// occupiedFrom,
// });
// });
// // if (!docSnapshot.exists) {
// // docRef.set({
// // occupiedFrom: Date.now(),
// // currentDispatcherName: station.dispatcherName,
// // });
// // return;
// // }
// // });
// return;
// }
// // const snapshot = await historyRef.get();
// // When array with previous stations isn't empty
// previousOnlineStations.forEach((prevStation) => {
// const currStationData = stationAPIData.find(
// (currStation) => currStation.stationName === prevStation.stationName
// );
// // snapshot.forEach(async (doc) => {
// // const docData = doc.data();
// // const docRef = historyRef.doc(doc.id);
// // Dispatcher left
// if (!currStationData) {
// previousOnlineStations = previousOnlineStations.filter(
// (s) => s.stationName !== prevStation.stationName
// );
// }
// // Dispatchers switched
// else if (prevStation.dispatcherName !== currStationData.dispatcherName) {
// previousOnlineStations = previousOnlineStations.filter(
// (s) => s.stationName !== prevStation.stationName
// );
// // const APIStationData = stationData
// // .filter((station) => station.isOnline && station.region === "eu")
// // .find((station) => station.stationName == doc.id);
// previousOnlineStations.push({
// stationName: currStationData.stationName,
// dispatcherName: currStationData.dispatcherName,
// occupiedFrom: Date.now(),
// });
// }
// });
// // if (docData.currentDispatcherName != "") {
// // if (
// // !APIStationData ||
// // APIStationData.dispatcherName != docData.currentDispatcherName
// // ) {
// // docRef.update({
// // currentDispatcherName: !APIStationData
// // ? ""
// // : APIStationData.dispatcherName,
// // occupiedFrom: !APIStationData ? 0 : Date.now(),
// // });
// stationAPIData
// .filter(
// (stationData) =>
// !previousOnlineStations.find(
// (prevStation) => prevStation.stationName === stationData.stationName
// )
// )
// .forEach((stationData) => {
// previousOnlineStations.push({
// stationName: stationData.stationName,
// dispatcherName: stationData.dispatcherName,
// occupiedFrom: Date.now(),
// });
// });
// // docRef.collection("dispatcherHistory").add({
// // currentDispatcherName: docData.currentDispatcherName,
// // occupiedFrom: docData.occupiedFrom,
// // occupiedTo: Date.now(),
// // });
// // }
// // } else if (APIStationData) {
// // docRef.update({
// // currentDispatcherName: APIStationData.dispatcherName,
// // occupiedFrom: Date.now(),
// // });
// // }
// // });
// // });
// const stationsDoc = db.collection("stations").doc("previous");
// stationsDoc.set({ previousOnlineStations });
// res.status(200);
// });
// // // const scheduledUpdate = functions.pubsub
// // // .schedule("0 * * * *")
// // // .onRun(async (context) => {
// // // let stationData: {
// // // stationName: string;
// // // dispatcherName: string;
// // // isOnline: boolean;
// // // region: string;
// // // }[];
// // // try {
// // // stationData = await (
// // // await axios.get(
// // // "https://api.td2.info.pl:9640/?method=getStationsOnline"
// // // )
// // // ).data.message;
// // // } catch (error) {
// // // return;
// // // }
// // // const historyRef = db.collection("history");
// // // stationData.forEach(async (station) => {
// // // const docRef = historyRef.doc(station.stationName);
// // // const docSnapshot = await docRef.get();
// // // if (!docSnapshot.exists) {
// // // docRef.set({
// // // occupiedFrom: Date.now(),
// // // currentDispatcherName: station.dispatcherName,
// // // });
// // // return;
// // // }
// // // });
// // // const snapshot = await historyRef.get();
// // // snapshot.forEach(async (doc) => {
// // // const docData = doc.data();
// // // const docRef = historyRef.doc(doc.id);
// // // const APIStationData = stationData
// // // .filter((station) => station.isOnline && station.region === "eu")
// // // .find((station) => station.stationName == doc.id);
// // // if (docData.currentDispatcherName != "") {
// // // if (
// // // !APIStationData ||
// // // APIStationData.dispatcherName != docData.currentDispatcherName
// // // ) {
// // // docRef.update({
// // // currentDispatcherName: !APIStationData
// // // ? ""
// // // : APIStationData.dispatcherName,
// // // occupiedFrom: !APIStationData ? 0 : Date.now(),
// // // });
// // // docRef.collection("dispatcherHistory").add({
// // // currentDispatcherName: docData.currentDispatcherName,
// // // occupiedFrom: docData.occupiedFrom,
// // // occupiedTo: Date.now(),
// // // });
// // // }
// // // } else if (APIStationData) {
// // // docRef.update({
// // // currentDispatcherName: APIStationData.dispatcherName,
// // // occupiedFrom: Date.now(),
// // // });
// // // }
// // // });
// // // });