Skip to content

Commit

Permalink
fix: more performant find cacheKey
Browse files Browse the repository at this point in the history
  • Loading branch information
marudor committed Nov 9, 2024
1 parent bb568c2 commit 94fcaf6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 73 deletions.
60 changes: 40 additions & 20 deletions src/external/risJourneys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,39 @@ function mapToParsedJourneyMatchResponse(
lastStop: mapStationShortToRouteStops(journeyMatch.destinationSchedule),
};
}

function findCategoryFilter(matches: JourneyMatch[], category?: string) {
if (category) {
const categoryFiltered = matches.filter(
(j) => j.transport.category.toLowerCase() === category.toLowerCase(),
);
if (categoryFiltered.length) {
return categoryFiltered;
}
}
return matches;
}

function findAdministrationFilter(
matches: JourneyMatch[],
administration?: string,
) {
if (administration) {
const filtered = matches.filter(
(j) => j.administrationID === administration,
);
if (filtered.length) {
return filtered;
}
}
return matches;
}

export async function findJourney(
trainNumber: number,
date: Date,
category?: string,
date?: Date,
withOEV?: boolean,
originEvaNumber?: string,
administration?: string,
): Promise<JourneyMatch[]> {
try {
Expand All @@ -101,13 +128,11 @@ export async function findJourney(
return [];
}

const cacheKey = `${trainNumber}|${category?.toUpperCase()}|${
date && format(date, 'yyyy-MM-dd')
}|${withOEV ?? false}|${originEvaNumber}`;
const cacheKey = `${trainNumber}|${format(date, 'yyyy-MM-dd')}|${withOEV ?? false}`;

const cacheHit = await journeyFindCache.get(cacheKey);
if (cacheHit) {
return cacheHit;
return findCategoryFilter(cacheHit, category);
}

const result = await client.find({
Expand All @@ -116,19 +141,8 @@ export async function findJourney(
// category,
date: date && format(date, 'yyyy-MM-dd'),
transports: withOEV ? undefined : trainTypes,
originEvaNumber,
administrationID: administration,
});

if (category) {
const categoryFiltered = result.data.journeys.filter(
(j) => j.transport.category.toLowerCase() === category.toLowerCase(),
);
if (categoryFiltered.length) {
result.data.journeys = categoryFiltered;
}
}

result.data.journeys.sort(sortJourneys);

if (isWithin20Hours) {
Expand All @@ -140,6 +154,12 @@ export async function findJourney(
);
}

result.data.journeys = findCategoryFilter(result.data.journeys, category);
result.data.journeys = findAdministrationFilter(
result.data.journeys,
administration,
);

for (const j of result.data.journeys) {
void additionalJourneyInformation(
`${j.transport.category} ${j.transport.number}`,
Expand All @@ -157,11 +177,11 @@ export async function findJourney(

export async function findJourneyHafasCompatible(
trainNumber: number,
date: Date,
category?: string,
date?: Date,
onlyFv?: boolean,
withOEV?: boolean,
): Promise<ParsedJourneyMatchResponse[]> {
const risResult = await findJourney(trainNumber, category, date, onlyFv);
const risResult = await findJourney(trainNumber, date, category, withOEV);

return risResult.map(mapToParsedJourneyMatchResponse);
}
Expand Down
59 changes: 33 additions & 26 deletions src/external/risJourneysV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { ParsedProduct } from '@/types/HAFAS';
import type { ParsedJourneyMatchResponse } from '@/types/HAFAS/JourneyMatch';
import type { RouteStop } from '@/types/routing';
import axios from 'axios';
import { format, isAfter, isBefore, isSameDay, subDays } from 'date-fns';
import { format, isBefore, isSameDay, subDays } from 'date-fns';

const risJourneysV2Configuration = new RisJourneysConfiguration({
basePath: process.env.RIS_JOURNEYS_V2_URL,
Expand Down Expand Up @@ -169,35 +169,40 @@ async function fixJourneysFoundIfNeeded(
);
}

const newTimetableStarts = new Date('2024-12-15T02:00:00Z');
const searchableFrom = new Date('2024-10-16T02:00:00Z');
function findAdministrationFilter(
matches: JourneyFindResult[],
administration?: string,
) {
if (administration) {
const filtered = matches.filter(
(m) =>
m.journeyRelation.headerAdministrationID === administration ||
m.journeyRelation.startAdministrationID === administration,
);
if (filtered.length) {
return filtered;
}
}
return matches;
}

export async function findJourney(
trainNumber: number,
category?: string,
date?: Date,
date: Date,
_category?: string,
withOEV?: boolean,
originEvaNumber?: string,
administration?: string,
): Promise<JourneyFindResult[]> {
try {
if (date) {
if (
isAfter(date, newTimetableStarts) &&
isBefore(new Date(), searchableFrom)
) {
return [];
}
const sevenDaysAgo = subDays(new Date(), 7);
const olderThan7Days = isBefore(date, sevenDaysAgo);
if (olderThan7Days) {
return [];
}
}

const cacheKey = `${trainNumber}|${category?.toUpperCase()}|${
date && format(date, 'yyyy-MM-dd')
}|${withOEV ?? false}|${originEvaNumber}`;
const cacheKey = `${trainNumber}|${format(date, 'yyyy-MM-dd')}|${withOEV ?? false}`;

const cacheHit = await journeyFindCache.get(cacheKey);
if (cacheHit) {
Expand All @@ -206,25 +211,27 @@ export async function findJourney(

const result = await client.find({
journeyNumber: trainNumber,
category,
date: date && format(date, 'yyyy-MM-dd'),
// category,
date: format(date, 'yyyy-MM-dd'),
transportTypes: withOEV ? undefined : trainTypes,
administrationID: administration,
limit: 50,
limit: 500,
});

if (date) {
result.data.journeys = result.data.journeys.filter((j) =>
isSameDay(new Date(j.journeyRelation.startTime), date),
);
}
result.data.journeys = result.data.journeys.filter((j) =>
isSameDay(new Date(j.journeyRelation.startTime), date),
);

await fixJourneysFoundIfNeeded(result.data.journeys);

result.data.journeys.sort(sortJourneys);

void journeyFindCache.set(cacheKey, result.data.journeys);

result.data.journeys = findAdministrationFilter(
result.data.journeys,
administration,
);

for (const j of result.data.journeys) {
void additionalJourneyInformation(
`${j.journeyRelation.startCategory} ${j.journeyRelation.startJourneyNumber}`,
Expand All @@ -245,11 +252,11 @@ export async function findJourney(

export async function findJourneyHafasCompatible(
trainNumber: number,
date: Date,
category?: string,
date?: Date,
withOEV?: boolean,
): Promise<ParsedJourneyMatchResponse[]> {
const risResult = await findJourney(trainNumber, category, date, withOEV);
const risResult = await findJourney(trainNumber, date, category, withOEV);

return Promise.all(risResult.map(mapToParsedJourneyMatchResponse));
}
Expand Down
38 changes: 11 additions & 27 deletions src/server/rpc/journeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import { z } from 'zod';

function findV1OrV2HafasCompatible(
trainNumber: number,
date: Date,
category?: string,
date?: Date,
withOEV?: boolean,
) {
let useV2 = true;
Expand All @@ -36,18 +36,17 @@ function findV1OrV2HafasCompatible(

if (useV2) {
logger.debug('Using JourneysV2 (HAFAS compatible) find');
return findJourneyHafasCompatibleV2(trainNumber, category, date, withOEV);
return findJourneyHafasCompatibleV2(trainNumber, date, category, withOEV);
}
logger.debug('Using JourneysV1 (HAFAS compatible) find');
return findJourneyHafasCompatible(trainNumber, category, date, withOEV);
return findJourneyHafasCompatible(trainNumber, date, category, withOEV);
}

function findJourneyV1OrV2(
trainNumber: number,
date: Date,
category?: string,
date?: Date,
onlyFv?: boolean,
originEvaNumber?: string,
withOEV?: boolean,
administration?: string,
) {
let useV2 = true;
Expand All @@ -60,24 +59,10 @@ function findJourneyV1OrV2(
}
if (useV2) {
logger.debug('Using JourneysV2 find');
return findJourneyV2(
trainNumber,
category,
date,
onlyFv,
originEvaNumber,
administration,
);
return findJourneyV2(trainNumber, date, category, withOEV, administration);
}
logger.debug('Using JourneysV1 find');
return findJourney(
trainNumber,
category,
date,
onlyFv,
originEvaNumber,
administration,
);
return findJourney(trainNumber, date, category, withOEV, administration);
}

export type JourneyRPCQuery = QueryProcedure<{
Expand Down Expand Up @@ -108,7 +93,7 @@ export const journeysRpcRouter = rpcAppRouter({
async ({
input: {
trainNumber,
initialDepartureDate,
initialDepartureDate = new Date(),
initialEvaNumber,
withOEV,
limit,
Expand All @@ -121,8 +106,8 @@ export const journeysRpcRouter = rpcAppRouter({
if (trainNumber) {
risPromise = findV1OrV2HafasCompatible(
trainNumber,
category,
initialDepartureDate,
category,
withOEV,
);
}
Expand Down Expand Up @@ -180,7 +165,7 @@ export const journeysRpcRouter = rpcAppRouter({
input: {
trainName,
evaNumberAlongRoute,
initialDepartureDate,
initialDepartureDate = new Date(),
journeyId,
jid,
administration,
Expand Down Expand Up @@ -223,10 +208,9 @@ export const journeysRpcRouter = rpcAppRouter({
}
const possibleJourneys = await findJourneyV1OrV2(
productDetails.trainNumber,
productDetails.category,
initialDepartureDate,
productDetails.category,
false,
undefined,
administration,
);
if (!possibleJourneys.length) {
Expand Down

0 comments on commit 94fcaf6

Please sign in to comment.