From 8deefeba00982e6b102bbe6903d0910de7970e9f Mon Sep 17 00:00:00 2001 From: Moritz Date: Sun, 23 Apr 2023 13:21:10 +0200 Subject: [PATCH] added caching of bookings. pull intervall in config --- backend/package.json | 2 +- backend/src/config.ts | 2 + backend/src/services/bookings.service.ts | 48 +++++++++++++++--------- backend/src/services/cdm.service.ts | 12 +----- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/backend/package.json b/backend/package.json index a0e0ffd..4502757 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "vacdm-backend", - "version": "1.0.2", + "version": "1.0.3", "description": "", "main": "app.js", "scripts": { diff --git a/backend/src/config.ts b/backend/src/config.ts index 7a1e9ca..ae60e1f 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -18,6 +18,7 @@ interface vacdmConfig { eventUrl: string; eventPrio: number; + eventPullInterval: number; clientId: string; clientSecret: string; @@ -57,6 +58,7 @@ export default function config(): vacdmConfig { eventUrl: process.env.EVENT_URL || 'https://slots.vatsim-germany.org/api/events/', eventPrio: Number(process.env.EVENT_PRIO) || 5, + eventPullInterval: Number(process.env.EVENT_PULL_INTERVAL || 5), vatsimAuthUrl: options.vatsimAuthUrl, clientId: options.vatsimAuthClientId, diff --git a/backend/src/services/bookings.service.ts b/backend/src/services/bookings.service.ts index 1eaf415..3355fae 100644 --- a/backend/src/services/bookings.service.ts +++ b/backend/src/services/bookings.service.ts @@ -1,25 +1,39 @@ import axios from 'axios'; import config from '../config'; import dayjs from 'dayjs'; +import Logger from '@dotfionn/logger'; -export async function getAllBookings() { - try { - const events = await axios.get( - config().eventUrl - ); +const logger = new Logger('vACDM:services:booking'); - const relevantEvents = events.data.data.filter((e) => - dayjs(new Date()) >= dayjs(e.startEvent) && - dayjs(new Date()) <= dayjs(e.endEvent) - ); +let lastPull: Date | null = null; +let relevantBookings: any[] | null = null; - const relevantBookings: any[] = []; - - for (let event of relevantEvents) { - const bookings = await axios.get(event.links.bookings); +export async function getAllBookings() { + const duration = dayjs().diff(dayjs(lastPull), 'minute'); - for (let booking of bookings.data.data) { - relevantBookings.push(booking); + try { + if ( + relevantBookings === null || + lastPull === null || + duration > config().eventPullInterval + ) { + logger.debug('Get latest Bookings'); + const events = await axios.get(config().eventUrl); + lastPull = new Date(); + const relevantEvents = events.data.data.filter( + (e) => + dayjs(new Date()) >= dayjs(e.startEvent) && + dayjs(new Date()) <= dayjs(e.endEvent) + ); + + relevantBookings = []; + + for (let event of relevantEvents) { + const bookings = await axios.get(event.links.bookings); + + for (let booking of bookings.data.data) { + relevantBookings.push(booking); + } } } @@ -34,8 +48,6 @@ export async function pilotHasBooking(cid: number): Promise { const bookings = await getAllBookings(); return bookings.findIndex((b) => b.user === cid) != -1; - - } catch (error) { throw error; } @@ -43,5 +55,5 @@ export async function pilotHasBooking(cid: number): Promise { export default { getAllBookings, - pilotHasBooking + pilotHasBooking, }; diff --git a/backend/src/services/cdm.service.ts b/backend/src/services/cdm.service.ts index 7d662a9..e1997c4 100644 --- a/backend/src/services/cdm.service.ts +++ b/backend/src/services/cdm.service.ts @@ -38,6 +38,7 @@ export function determineInitialBlock(pilot: PilotDocument): { }; } + export async function putPilotIntoBlock( pilot: PilotDocument, allPilots: PilotDocument[] @@ -179,11 +180,6 @@ export async function cleanupPilots() { }) .exec(); - logger.debug( - new Date(Date.now() - config().timeframes.timeSinceInactive), - new Date() - ); - logger.debug('pilotsToBeDeleted', pilotsToBeDeleted); for (let pilot of pilotsToBeDeleted) { @@ -231,9 +227,7 @@ export async function optimizeBlockAssignments() { const datafeedData = await datafeedService.getRawDatafeed(); - for (let pilot of allPilots) { - console.log(pilot.callsign); - + for (let pilot of allPilots) { if (pilot.hasBooking) { continue; } @@ -252,8 +246,6 @@ export async function optimizeBlockAssignments() { } - - console.log('done'); const nowPlusTen = timeUtils.addMinutes(new Date(), 10);