mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
experiment with using RepositoryDataLoader for ETA data
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
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 systemIdsToSupport = ["263"];
|
||||
@@ -201,27 +201,39 @@ export class RepositoryDataLoader {
|
||||
private async fetchAndUpdateEtaDataForExistingOrderedStops() {
|
||||
// TODO implement once I figure out how to associate ETA data with shuttles
|
||||
|
||||
// const systems = await this.repository.getSystems();
|
||||
// await Promise.all(systems.map(async (system: ISystem) => {
|
||||
// const stops = await this.repository.getStopsBySystemId(system.id);
|
||||
//
|
||||
// await Promise.all(stops.map(async (stop: IStop) => {
|
||||
// const orderedStops = await this.repository.getOrderedStopsByStopId(stop.id);
|
||||
//
|
||||
// await Promise.all(orderedStops.map(async (orderedStop) => {
|
||||
// const params = {
|
||||
// eta: "3",
|
||||
// stopIds: stop.id,
|
||||
// routeId: orderedStop.routeId,
|
||||
// position: orderedStop.position,
|
||||
// };
|
||||
//
|
||||
// // How to get shuttle ID?????????
|
||||
// // API doesn't provide it
|
||||
// // I might be cooked
|
||||
// }));
|
||||
// }));
|
||||
// }));
|
||||
const systems = await this.repository.getSystems()
|
||||
await Promise.all(systems.map(async (system: ISystem) => {
|
||||
const stops = await this.repository.getStopsBySystemId(system.id);
|
||||
await Promise.all(stops.map(async (stop) => {
|
||||
const params = {
|
||||
eta: "3",
|
||||
stopIds: stop.id,
|
||||
};
|
||||
|
||||
const query = new URLSearchParams(params).toString();
|
||||
const response = await fetch(`${baseUrl}?${query}`, {
|
||||
method: "GET",
|
||||
});
|
||||
const json = await response.json();
|
||||
|
||||
if (json.ETAs && json.ETAs[stop.id]) {
|
||||
// Continue with the parsing
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user