Skip to content

Commit

Permalink
feat: some caching improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
marudor committed Dec 14, 2024
1 parent de283d6 commit cb9e1c2
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
37 changes: 30 additions & 7 deletions src/bahnde/occupancy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { routing } from '@/bahnde/routing/routing';
import { searchStopPlace } from '@/server/StopPlace/search';
import { Cache, CacheDatabase } from '@/server/cache';
import { timezone } from '@/timezone';
import type {
RouteAuslastung,
RoutingResult,
SingleRoute,
} from '@/types/routing';
import { tz } from '@date-fns/tz';
import { formatISO } from 'date-fns';

async function getRelevantTrip(
Expand Down Expand Up @@ -63,6 +63,14 @@ const stopOccupancyCache = new Cache<RouteAuslastung | undefined>(
CacheDatabase.HafasStopOccupancy,
);

const createBaseCacheKey = (
trainNumber: string,
initialDeparture: Date,
start: string,
destination: string,
) =>
`${trainNumber}:${formatISO(initialDeparture, { in: timezone.utc })}:${start}-${destination}`;

export function setOccupanciesOfRoutes(routes: RoutingResult['routes']) {
for (const route of routes) {
for (const segment of route.segments) {
Expand All @@ -72,7 +80,12 @@ export function setOccupanciesOfRoutes(routes: RoutingResult['routes']) {
segment.train.number !== '0' &&
segment.auslastung
) {
const baseKey = `${segment.segmentStart.name}:${segment.segmentDestination.name}:${segment.train.number}:${formatISO(segment.departure.scheduledTime, { in: tz('UTC') })}`;
const baseKey = createBaseCacheKey(
segment.train.number,
segment.departure.scheduledTime,
segment.segmentStart.name,
segment.segmentDestination.name,
);
for (const stop of segment.stops) {
if (stop.auslastung) {
void stopOccupancyCache.set(
Expand All @@ -87,18 +100,28 @@ export function setOccupanciesOfRoutes(routes: RoutingResult['routes']) {
}

export async function bahnDeOccupancy(
start: string,
destination: string,
startStopPlaceName: string,
destinationStopPlaceName: string,
trainNumber: string,
time: Date,
initialDepartureDate: Date,
stopEva: string,
): Promise<RouteAuslastung | undefined> {
const keyWithouEva = `${start}:${destination}:${trainNumber}:${formatISO(time, { in: tz('UTC') })}`;
const keyWithouEva = createBaseCacheKey(
trainNumber,
initialDepartureDate,
startStopPlaceName,
destinationStopPlaceName,
);
const key = `${keyWithouEva}:${stopEva}`;
if (await stopOccupancyCache.exists(key)) {
return await stopOccupancyCache.get(key);
}
await getRelevantTrip(start, destination, trainNumber, time);
await getRelevantTrip(
startStopPlaceName,
destinationStopPlaceName,
trainNumber,
initialDepartureDate,
);

return await stopOccupancyCache.get(key);
}
2 changes: 1 addition & 1 deletion src/server/HAFAS/LocMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const locMatch = async (
return makeRequest(req, undefined, profile) as any;
}

const cacheKey = `${profile}|${type}|${searchTerm}`;
const cacheKey = `${profile}:${type}:${searchTerm}`;
const cached = await cache.get(cacheKey);

if (cached) {
Expand Down
6 changes: 3 additions & 3 deletions src/server/HAFAS/StationBoard/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import makeRequest from '@/server/HAFAS/Request';
import { timezone } from '@/timezone';
import type { AllowedHafasProfile, JourneyFilter } from '@/types/HAFAS';
import type { StationBoardRequest } from '@/types/HAFAS/StationBoard';
import type {
ArrivalStationBoardEntry,
DepartureStationBoardEntry,
StationBoardEntry,
} from '@/types/stationBoard';
import { tz } from '@date-fns/tz';
import { format } from 'date-fns';
import parse from './parse';

Expand Down Expand Up @@ -39,10 +39,10 @@ function stationBoard(
maxJny: 250,
jnyFltrL: filter,
date: format(date, 'yyyyMMdd', {
in: tz('Europe/Berlin'),
in: timezone.europeBerlin,
}),
time: format(date, 'HHmmss', {
in: tz('Europe/Berlin'),
in: timezone.europeBerlin,
}),
stbLoc: {
lid: `A=1@L=${station}`,
Expand Down
6 changes: 3 additions & 3 deletions src/server/HAFAS/TripSearch/TripSearch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import makeRequest from '@/server/HAFAS/Request';
import mapLoyalityCard from '@/server/HAFAS/TripSearch/mapLoyalityCard';
import { timezone } from '@/timezone';
import type {
AllowedHafasProfile,
JourneyFilter,
Expand All @@ -11,7 +12,6 @@ import type {
TripSearchRequest,
} from '@/types/HAFAS/TripSearch';
import type { RoutingResult } from '@/types/routing';
import { tz } from '@date-fns/tz';
import { format } from 'date-fns';
import NetzcardBetreiber from './NetzcardBetreiber.json';
import tripSearchParse from './parse';
Expand Down Expand Up @@ -113,10 +113,10 @@ export function tripSearch(
if (time) {
requestTypeSpecific = {
outDate: format(time, 'yyyyMMdd', {
in: tz('Europe/Berlin'),
in: timezone.europeBerlin,
}),
outTime: format(time, 'HHmmss', {
in: tz('Europe/Berlin'),
in: timezone.europeBerlin,
}),
};
} else if (ctxScr) {
Expand Down
4 changes: 2 additions & 2 deletions src/server/iris/helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { axiosUpstreamInterceptor } from '@/server/admin';
import { timezone } from '@/timezone';
import type { Stop } from '@/types/iris';
import { tz } from '@date-fns/tz';
import Axios from 'axios';
import type { AxiosInstance } from 'axios';
import { isValid, parse } from 'date-fns';
Expand Down Expand Up @@ -94,7 +94,7 @@ export function parseTs(ts?: string): undefined | Date {
if (ts) {
const format = ts.includes('-') ? 'yy-MM-dd HH:mm:ss.SSS' : 'yyMMddHHmm';
const parsedDate = parse(ts, format, Date.now(), {
in: tz('Europe/Berlin'),
in: timezone.europeBerlin,
});
if (isValid(parsedDate)) {
return parsedDate;
Expand Down
6 changes: 6 additions & 0 deletions src/timezone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { tz } from '@date-fns/tz';

export const timezone = {
utc: tz('UTC'),
europeBerlin: tz('Europe/Berlin'),
};

0 comments on commit cb9e1c2

Please sign in to comment.