From e4d7e9bd746beac3b0b55a675b3cf675c7ae63c1 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Sat, 21 Dec 2024 18:23:08 -0800 Subject: [PATCH] add stop and system data updates from api --- src/index.ts | 5 +- src/sharedMemoryUpdater.ts | 96 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/sharedMemoryUpdater.ts diff --git a/src/index.ts b/src/index.ts index 8bbc890..225afcd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,11 +5,12 @@ import { resolvers } from "./resolvers"; import { sharedMemory } from "./sharedMemory"; import { loadTestData } from "./testData"; import { ServerContext } from "./serverContext"; +import { startDataUpdater } from "./sharedMemoryUpdater"; const typeDefs = readFileSync("./schema.graphql", "utf8"); async function main() { - loadTestData(sharedMemory); + // loadTestData(sharedMemory); const server = new ApolloServer({ typeDefs, @@ -27,6 +28,8 @@ async function main() { }, }); + startDataUpdater(); + console.log(`Server ready at: ${url}`); } diff --git a/src/sharedMemoryUpdater.ts b/src/sharedMemoryUpdater.ts new file mode 100644 index 0000000..f24040b --- /dev/null +++ b/src/sharedMemoryUpdater.ts @@ -0,0 +1,96 @@ +import { sharedMemory } from "./sharedMemory"; +import { Stop, System } from "./generated/graphql"; + +const baseUrl = "https://passiogo.com/mapGetData.php"; + +async function updateStopAndOrderedStopDataForSystem(systemToConstruct: System) { + const params = { + getStops: "2", + }; + + const formDataJsonObject = { + "s0": systemToConstruct.id, + "sA": "1", + } + const formData = new FormData(); + formData.set("json", JSON.stringify(formDataJsonObject)); + + const query = new URLSearchParams(params).toString(); + const response = await fetch(`${baseUrl}?${query}`, { + method: "POST", + body: formData, + }); + const json = await response.json(); + + if (typeof json.stops === "object") { + const jsonStops = Object.values(json.stops); + + systemToConstruct.stops = jsonStops.map((jsonStop: any) => { + return { + id: jsonStop.id, + name: jsonStop.name, + coordinates: { + latitude: jsonStop.latitude, + longitude: jsonStop.longitude, + }, + system: systemToConstruct, + etas: [], + orderedStops: [], + } + }); + } +} + +async function updateSystemData() { + // Construct the graph from the bottom up, so that + // system data is populated with all necessary fields + + const params = { + getSystems: "2", + }; + const query = new URLSearchParams(params).toString(); + const response = await fetch(`${baseUrl}?${query}`); + const json = await response.json() + + if (typeof json.all === "object") { + sharedMemory.systems = await Promise.all(json.all.map(async (jsonSystem: any) => { + const constructedSystem: System = { + id: jsonSystem.id, + name: jsonSystem.fullname, + routes: [], + stops: [], + shuttles: [], + }; + + try { + await updateStopAndOrderedStopDataForSystem(constructedSystem); + } catch (e) { + console.error(e); + } + + return constructedSystem; + })); + } +} + +async function fetchDataAndUpdateStore() { + try { + await updateSystemData(); + } catch (e) { + console.error(e); + } +} + +async function runDataUpdateScheduler() { + // while (true) { + const timeoutInMilliseconds = 10000; + await fetchDataAndUpdateStore(); + await new Promise((resolve) => setTimeout(resolve, timeoutInMilliseconds)); + // } +} + +function startDataUpdater() { + runDataUpdateScheduler(); +} + +export { startDataUpdater };