experiment with using RepositoryDataLoader for ETA data

This commit is contained in:
2025-01-17 14:39:05 -08:00
parent e4b08be1e4
commit 33c1a512d2
2 changed files with 38 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
import { GetterSetterRepository } from "../repositories/GetterSetterRepository"; import { GetterSetterRepository } from "../repositories/GetterSetterRepository";
import { IRoute, IShuttle, IStop, ISystem } from "../entities/entities"; import { IEta, IRoute, IShuttle, IStop, ISystem } from "../entities/entities";
const timeout = 10000; const timeout = 10000;
const systemIdsToSupport = ["263"]; const systemIdsToSupport = ["263"];
@@ -201,27 +201,39 @@ export class RepositoryDataLoader {
private async fetchAndUpdateEtaDataForExistingOrderedStops() { private async fetchAndUpdateEtaDataForExistingOrderedStops() {
// TODO implement once I figure out how to associate ETA data with shuttles // TODO implement once I figure out how to associate ETA data with shuttles
// const systems = await this.repository.getSystems(); const systems = await this.repository.getSystems()
// await Promise.all(systems.map(async (system: ISystem) => { await Promise.all(systems.map(async (system: ISystem) => {
// const stops = await this.repository.getStopsBySystemId(system.id); const stops = await this.repository.getStopsBySystemId(system.id);
// await Promise.all(stops.map(async (stop) => {
// await Promise.all(stops.map(async (stop: IStop) => { const params = {
// const orderedStops = await this.repository.getOrderedStopsByStopId(stop.id); eta: "3",
// stopIds: stop.id,
// await Promise.all(orderedStops.map(async (orderedStop) => { };
// const params = {
// eta: "3", const query = new URLSearchParams(params).toString();
// stopIds: stop.id, const response = await fetch(`${baseUrl}?${query}`, {
// routeId: orderedStop.routeId, method: "GET",
// position: orderedStop.position, });
// }; const json = await response.json();
//
// // How to get shuttle ID????????? if (json.ETAs && json.ETAs[stop.id]) {
// // API doesn't provide it // Continue with the parsing
// // I might be cooked json.ETAs[stop.id].forEach((jsonEta: any) => {
// })); // Update cache
// })); const shuttleId: string = jsonEta.busId;
// }));
const eta: IEta = {
secondsRemaining: jsonEta.secondsSpent,
shuttleId: `${shuttleId}`,
stopId: stop.id,
millisecondsSinceEpoch: Date.now(),
};
this.repository.addOrUpdateEta(eta);
});
}
}));
}))
} }
private async updateStopDataForSystemAndApiResponse(system: ISystem, json: any) { private async updateStopDataForSystemAndApiResponse(system: ISystem, json: any) {

View File

@@ -132,10 +132,10 @@ export const resolvers: Resolvers<ServerContext> = {
Shuttle: { Shuttle: {
eta: async (parent, args, contextValue, info) => { eta: async (parent, args, contextValue, info) => {
if (!args.forStopId) return null; if (!args.forStopId) return null;
const etaForStopId = await contextValue.apiBasedRepository.getEtaForShuttleAndStopId(parent.id, args.forStopId); const etaForStopId = await contextValue.repository.getEtaForShuttleAndStopId(parent.id, args.forStopId);
if (etaForStopId === null) return null; if (etaForStopId === null) return null;
const stop = await contextValue.apiBasedRepository.getStopById(etaForStopId.stopId); const stop = await contextValue.repository.getStopById(etaForStopId.stopId);
if (stop === null) return null; if (stop === null) return null;
return { return {
@@ -149,14 +149,14 @@ export const resolvers: Resolvers<ServerContext> = {
}; };
}, },
etas: async (parent, args, contextValue, info) => { etas: async (parent, args, contextValue, info) => {
const etasForShuttle = await contextValue.apiBasedRepository.getEtasForShuttleId(parent.id); const etasForShuttle = await contextValue.repository.getEtasForShuttleId(parent.id);
if (!etasForShuttle) return null; if (!etasForShuttle) return null;
const computedEtas = await Promise.all(etasForShuttle.map(async ({ const computedEtas = await Promise.all(etasForShuttle.map(async ({
secondsRemaining, secondsRemaining,
stopId, stopId,
}): Promise<Eta | null> => { }): Promise<Eta | null> => {
const stop = await contextValue.apiBasedRepository.getStopById(stopId); const stop = await contextValue.repository.getStopById(stopId);
if (stop === null) return null; if (stop === null) return null;
return { return {