Skip to content

Commit

Permalink
Provide option GTFS_RANGE to configure schedules included
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dzfranklin committed Aug 6, 2024
1 parent a78992b commit dd26faf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/cli/OutputGTFSCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions src/cli/ShowHelpCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
`);

Expand Down
7 changes: 5 additions & 2 deletions src/gtfs/repository/CIFRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ScheduleResults> {
public async getSchedules(range: string): Promise<ScheduleResults> {
const scheduleBuilder = new ScheduleBuilder();
const [[lastSchedule]] = await this.db.query<{id: number}>("SELECT id FROM schedule ORDER BY id desc LIMIT 1");

Expand All @@ -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
Expand Down

0 comments on commit dd26faf

Please sign in to comment.