transiter

command module
v0.6.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 11 Imported by: 0

README

Transiter

Transiter is a backend web service that makes it easy to use realtime transit data. Transiter can be used to build things like web apps (realtimerail.nyc, closingdoors.nyc) and arrival time boards, and query transit data from other applications.

Transiter subscribes to GTFS transit data feeds from different transit agencies and provides integrated views of the data through a HTTP REST API. The endpoint for each station, for example, provides:

  • the station's static data (such as its name and GPS coordinates),

  • the station's realtime data (the list of vehicles that will arrive next, and the times they will arrive),

  • and derived data that Transiter computes automatically, such as which routes usually call at the station.

You can get a sense for the data that's available by navigating through the live demo site, or by interacting with it programmatically:

# Python snippet that finds the next train at the Rockefeller Center NYC subway station
import requests, time
data = requests.get("https://demo.transiter.dev/systems/us-ny-subway/stops/D15").json()
firstStopTime = data["stopTimes"][0]
secondsToLeave = int(firstStopTime["departure"]["time"]) - time.time()
print(f'The next train leaves in {int(secondsToLeave)} seconds and goes to {firstStopTime["trip"]["destination"]["name"]}.')

The Transiter tour contains many more examples like this.

Note that the demo site is best effort! In general if you want to use Transiter for an application you will run and own your own Transiter deployment.

Quickstart guide

This is a whirlwind version of the Transiter tour on the documentation website.

Transiter uses Postgres for persisting data, and requires Postgres to have the PostGIS Postgres extension By default Transiter assumes the database/user/password is transiter/transiter/transiter, but all these values can be configured. If you have Docker installed, you can easily spin up the right kind of Postgres instance by running the following command:

docker run \
    -e POSTGRES_USER=transiter -e POSTGRES_PASSWORD=transiter -e POSTGRES_DB=transiter \
    -p 0.0.0.0:5432:5432 \
    postgis/postgis:14-3.4

Transiter is written in Go. To build Transiter and install it run:

go install .

After this, the Transiter server is launched using:

transiter server

Transiter's HTTP API will now be available on localhost:8080. In addition to the HTTP API, you can interact with the server using Transiter client commands. For example to list all installed transit system run:

transiter list

This will show that there are no transit systems installed. The next step is install one!

San Francisco area BART:

transiter install us-ca-bart

New York City subway: If you have an MTA API key:

transiter install --arg mta_api_key=$MTA_API_KEY us-ny-subway

In either case, the server logs will show that GTFS feed updates are happening, and the HTTP API will be populated with data. If you installed the BART, you can get data about the Embarcadero station by visiting:

localhost:8080/systems/us-ca-bart/stops/place_EMBR

Development guide

This is a guide for people who are interested in developing Transiter. PRs are very welcome!

Dev requirements

The basic requirements are the same as for running Transiter, Postgres 14+ with PostGIS and Go 1.18+. Additionally, the project uses the command runner tool Just. Installing Just is optional: as an alternative, you can always manually run the commands in the justfile. However we think Just is great, and the rest of this guide assumes you've installed it. Finally, having Docker available makes it really easy to run some other things locally but is totally optional.

Changing the code

If you just change the Go code, you can build the code using go build . and run all the unit tests using go test ./... or just test. Note that the unit tests assume Postgres is up and use Postgres for a bunch of DB tests.

If you have Docker installed you can also run all the Python E2E tests by running just test-e2e. (The command just test-all runs both the unit and E2E tests). The E2E tests are in the tests/endtoend directory. Without Docker it's still possible to run the E2E tests following the instructions in that directory. However the E2E tests also run on the GitHub actions CI and it's perhaps easiest to rely on that. The E2E tests generally don't break unless you've introduced a regression. For new major features it's great to add new E2E tests.

Like many Go projects Transiter relies on some code generation:

  • Transiter uses sqlc for generating all of the DB interaction code. The DB schema and queries are stored in the db directory.

  • Transiter uses gRPC for developing the API. The HTTP REST API is generated automatically using annotations in the proto files. Buf is used for compiling the proto files to the Go code.

If you make any changes to the API (.proto files) or DB SQL statements (.sql files), you'll need to run the code generation tools.

First install the tools:

just install-tools

Then run the code generation:

just generate

License

MIT

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
db
schema
Package schema contains the migrations runner.
Package schema contains the migrations runner.
types
Package types contains Go types corresponding to database types
Package types contains Go types corresponding to database types
docs
internal
admin
Package admin contains the implementation of the Transiter admin API.
Package admin contains the implementation of the Transiter admin API.
argsflag
Package argsflag defines a flag that can accept template arguments.
Package argsflag defines a flag that can accept template arguments.
client
Package client is a client library for interacting with a Transiter server.
Package client is a client library for interacting with a Transiter server.
client/table
Package table contains a utility for printing tables on the command line.
Package table contains a utility for printing tables on the command line.
convert
Package convert contains type converters.
Package convert contains type converters.
db/constants
Package constants contains the canonical string values for constants that are persisted in the database.
Package constants contains the canonical string values for constants that are persisted in the database.
db/dbtesting
Package dbtesting is a testing util package for running unit tests against a running database.
Package dbtesting is a testing util package for running unit tests against a running database.
db/dbwrappers
Package dbwrappers contains methods that wrap the raw methods generated by sqlc and provide a nicer API.
Package dbwrappers contains methods that wrap the raw methods generated by sqlc and provide a nicer API.
gen/api
Package api is a reverse proxy.
Package api is a reverse proxy.
graph
Package graph contains type definitions for graphs and algorithms that operate on them.
Package graph contains type definitions for graphs and algorithms that operate on them.
monitoring
Package monitoring contains methods for recording metrics and reporting them through a HTTP handler
Package monitoring contains methods for recording metrics and reporting them through a HTTP handler
public
Package public contains the implementation of the Transiter public API.
Package public contains the implementation of the Transiter public API.
public/endpoints
Package endpoints contains the logic for each public API endpoint.
Package endpoints contains the logic for each public API endpoint.
public/errors
Package errors contains logic for making errors user-friendly at the API boundary.
Package errors contains logic for making errors user-friendly at the API boundary.
public/reference
Package reference contains constructors for public API reference types.
Package reference contains constructors for public API reference types.
scheduler
Package scheduler contains the periodic feed update scheduler.
Package scheduler contains the periodic feed update scheduler.
scheduler/ticker
Package ticker implements tickers for the scheduler.
Package ticker implements tickers for the scheduler.
server
Package server implements the Transiter server process.
Package server implements the Transiter server process.
servicemaps
Package servicemaps contains all of the logic for Transiter's service maps features.
Package servicemaps contains all of the logic for Transiter's service maps features.
update
Package update implements the feed update mechanism.
Package update implements the feed update mechanism.
update/common
Package common contains types used by all update code.
Package common contains types used by all update code.
update/nyctsubwaycsv
Package nyctsubwaycsv contains logic for updating the stop headsign rules from the NYCT CSV file.
Package nyctsubwaycsv contains logic for updating the stop headsign rules from the NYCT CSV file.
update/realtime
Package realtime contains the code for updating the database from a GTFS realtime feed.
Package realtime contains the code for updating the database from a GTFS realtime feed.
update/static
Package static contains the code for updating the database from a GTFS static feed.
Package static contains the code for updating the database from a GTFS static feed.
version
Package version contains the Transiter version and related information
Package version contains the Transiter version and related information
tests
performance Module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL