From dd26faf9859150285e40e7bde46526c5e9d17128 Mon Sep 17 00:00:00 2001 From: Daniel Franklin Date: Mon, 5 Aug 2024 12:21:44 +0100 Subject: [PATCH] Provide option GTFS_RANGE to configure schedules included The environment variable GTFS_RANGE will configure the schedules included. If not specified the default is the previously hardcoded value, 3 months. Because mysql makes it annoying to provide an interval as an argument I just string interpolate it. The docs have a warning not to allow untrusted input. --- src/cli/OutputGTFSCommand.ts | 7 ++++++- src/cli/ShowHelpCommand.ts | 4 ++++ src/gtfs/repository/CIFRepository.ts | 7 +++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/cli/OutputGTFSCommand.ts b/src/cli/OutputGTFSCommand.ts index 60e9ee8d..3df1c978 100644 --- a/src/cli/OutputGTFSCommand.ts +++ b/src/cli/OutputGTFSCommand.ts @@ -31,8 +31,13 @@ export class OutputGTFSCommand implements CLICommand { throw new Error(`Output path ${this.baseDir} does not exist.`); } + if (Object.hasOwn(process.env, "GTFS_RANGE")) { + console.log(`Using GTFS_RANGE = ${process.env.GTFS_RANGE}\n`); + } + const range = process.env.GTFS_RANGE || "3 MONTH"; + const associationsP = this.repository.getAssociations(); - const scheduleResultsP = this.repository.getSchedules(); + const scheduleResultsP = this.repository.getSchedules(range); const transfersP = this.copy(this.repository.getTransfers(), "transfers.txt"); const stopsP = this.copy(this.repository.getStops(), "stops.txt"); const agencyP = this.copy(agencies, "agency.txt"); diff --git a/src/cli/ShowHelpCommand.ts b/src/cli/ShowHelpCommand.ts index 54f08c94..d99e394c 100644 --- a/src/cli/ShowHelpCommand.ts +++ b/src/cli/ShowHelpCommand.ts @@ -37,6 +37,10 @@ The --get-* and --download-* commands require SFTP environment properties: SFTP_USERNAME SFTP username SFTP_PASSWORD SFTP password SFTP_HOSTNAME SFTP hostname (defaults to dtd.atocrsp.org) + +The --gtfs and --gtfs-zip commands take the following environment properties: + + GTFS_RANGE A mysql interval expression for the scedules to include. This is NOT SANITIZED so it cannot be untrusted user input (defaults to '3 MONTH') `); diff --git a/src/gtfs/repository/CIFRepository.ts b/src/gtfs/repository/CIFRepository.ts index ef3b0fb8..7627ee06 100644 --- a/src/gtfs/repository/CIFRepository.ts +++ b/src/gtfs/repository/CIFRepository.ts @@ -80,8 +80,11 @@ export class CIFRepository { * * The second query selects all the z-trains (usually replacement buses) within three months. They already use CRS * codes as the location so avoid the disaster above. + * + * The argument range is a mysql expression like '3 MONTH'. + * It is NOT SANITIZED so it cannot be untrusted user input. */ - public async getSchedules(): Promise { + public async getSchedules(range: string): Promise { const scheduleBuilder = new ScheduleBuilder(); const [[lastSchedule]] = await this.db.query<{id: number}>("SELECT id FROM schedule ORDER BY id desc LIMIT 1"); @@ -103,7 +106,7 @@ export class CIFRepository { ( stop_time.id IS NULL OR crs_code IS NOT NULL ) - AND runs_from < CURDATE() + INTERVAL 3 MONTH + AND runs_from < CURDATE() + INTERVAL ${range} AND runs_to >= CURDATE() AND scheduled_pass_time is null ORDER BY stp_indicator DESC, id, stop_id