From c1ddbd474dda1192fe286e193462ad960d18d160 Mon Sep 17 00:00:00 2001 From: Caleb Denio Date: Sun, 11 Oct 2026 13:05:38 -0600 Subject: [PATCH] frontrunner track prediction --- cmd/main.go | 171 ++++++++++++++++++++++++++++++++++++++++++++++- go.mod | 1 + go.sum | 6 ++ pkg/gtfs/gtfs.go | 38 +++++++++++ 4 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 pkg/gtfs/gtfs.go diff --git a/cmd/main.go b/cmd/main.go index dcc1961..5d6aea7 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -11,14 +11,17 @@ import ( "os" "strconv" "strings" + "time" + "github.com/cjdenio/uta-trax-api/pkg/gtfs" pb "github.com/cjdenio/uta-trax-api/proto" + "github.com/jmoiron/sqlx" "google.golang.org/protobuf/proto" _ "github.com/mattn/go-sqlite3" ) -var scheduleDb *sql.DB +var scheduleDb *sqlx.DB var headsignMappings map[string]string = map[string]string{ "to medical": "To University Medical", @@ -322,10 +325,173 @@ func shapeHandler(w http.ResponseWriter, r *http.Request) { w.Write(response) } +type Route struct { + ID string `json:"routeId"` + ShortName string `json:"shortName"` + LongName string `json:"longName"` +} + +type Departure struct { + Route Route `json:"route"` + DepartureTime time.Time `json:"departureTime"` + Headsign string `json:"headsign"` + PredictedPlatform int `json:"predictedPlatform,omitempty"` +} + +type ApiDepartures struct { + Departures []Departure `json:"departures"` + To time.Time `json:"to"` +} + +type DbDeparture struct { + RouteId string `db:"route_id"` + RouteShortName string `db:"route_short_name"` + RouteLongName string `db:"route_long_name"` + DepartureTime string `db:"departure_time"` + TripHeadsign string `db:"trip_headsign"` + TripId string `db:"trip_id"` + DirectionId int `db:"direction_id"` + StopId string `db:"stop_id"` + PassingTrain bool `db:"passing_train"` +} + +var predictionData map[string][4]int = map[string][4]int{ + "21760": {1, 2, 0, 0}, // Roy Station + "21757": {0, 0, 1, 1}, // Clearfield Station + "21772": {1, 2, 0, 0}, // Layton Station + "21758": {0, 0, 1, 1}, // Farmington Station + "21759": {0, 0, 1, 1}, // Woods Cross Station + "23113": {1, 2, 1, 2}, // North Temple Station + "21771": {1, 2, 1, 2}, // Salt Lake Central Station + "23080": {2, 1, 1, 1}, // Murray Central Station + "23082": {0, 0, 2, 2}, // South Jordan Station + "23081": {1, 2, 0, 0}, // Draper Station + "23074": {1, 2, 1, 1}, // Lehi Station + "23071": {0, 0, 2, 2}, // American Fork Station + "25330": {2, 1, 2, 2}, // Vineyard Station + "23076": {0, 0, 2, 2}, // Orem Central Station +} + +func predictPlatform(d DbDeparture) int { + if d.RouteShortName != "750" { + return 0 + } + + data, ok := predictionData[d.StopId] + if !ok { + return 0 + } + + idx := 0 + if !d.PassingTrain { + idx += 2 + } + + if d.DirectionId == 1 { + idx += 1 + } + return data[idx] +} + +func upcomingDeparturesHandler(w http.ResponseWriter, r *http.Request) { + stationIds := strings.Split(r.URL.Query().Get("stationIds"), ",") + if r.URL.Query().Get("stationIds") == "" || len(stationIds) == 0 { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("missing stationIds param")) + return + } + + loc, _ := time.LoadLocation("America/Denver") // ew this shouldn't be hardcoded + + fromStr := r.URL.Query().Get("from") + from := time.Now().In(loc) + if fromStr != "" { + fromInt, err := strconv.ParseInt(fromStr, 10, 64) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("invalid `from` param, expected UNIX timestamp")) + return + } + + from = time.Unix(fromInt, 0).In(loc) + } + + // get current service IDs + serviceIds, err := gtfs.GetActiveServiceIDs(&from, scheduleDb.DB) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("internal server error")) + return + } + + to := from.Add(60 * time.Minute) + + // get upcoming departures + query, args, err := sqlx.In(`select + routes.route_id, + routes.route_short_name, + routes.route_long_name, + stop_times.departure_time, + trips.trip_headsign, + trips.trip_id, + trips.direction_id, + stop_times.stop_id, + EXISTS ( + SELECT * from stop_times st2 inner join trips t2 on t2.trip_id = st2.trip_id where st2.stop_id = stop_times.stop_id and t2.service_id IN (?) and (st2.departure_timestamp <= stop_times.departure_timestamp + 300) and (st2.departure_timestamp >= stop_times.departure_timestamp - 300 and stop_times.trip_id != st2.trip_id AND trips.direction_id != t2.direction_id) + ) AS passing_train + from stop_times + inner join trips on trips.trip_id = stop_times.trip_id + inner join routes on routes.route_id = trips.route_id + where trips.service_id IN (?) and stop_times.stop_id IN (?) and stop_times.departure_time >= ? and stop_times.departure_time < ? + order by stop_times.departure_time asc`, serviceIds, serviceIds, stationIds, from.Format("15:04:05"), to.Format("15:04:05")) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("internal server error")) + return + } + + dbDepartures := []DbDeparture{} + err = scheduleDb.Select(&dbDepartures, query, args...) + if err != nil { + log.Println(err) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("internal server error")) + return + } + + departures := make([]Departure, 0, len(dbDepartures)) + + for _, departure := range dbDepartures { + t, err := time.Parse("15:04:05", departure.DepartureTime) + if err != nil { + continue + } + + departures = append(departures, Departure{ + Route: Route{ + ID: departure.RouteId, + ShortName: departure.RouteShortName, + LongName: departure.RouteLongName, + }, + DepartureTime: time.Date(from.Year(), from.Month(), from.Day(), t.Hour(), t.Minute(), t.Second(), 0, loc), + Headsign: departure.TripHeadsign, + PredictedPlatform: predictPlatform(departure), + }) + } + + response, _ := json.Marshal(ApiDepartures{ + Departures: departures, + To: to, + }) + + w.Header().Add("Content-Type", "application/json") + w.Write(response) +} + func main() { fmt.Println("Opening database...") - _db, err := sql.Open("sqlite3", "uta-gtfs.db") + _db, err := sqlx.Open("sqlite3", "uta-gtfs.db") if err != nil { log.Fatal(err) } @@ -342,6 +508,7 @@ func main() { http.HandleFunc("/api.json", vehicleHandler("json")) http.HandleFunc("/api/trips/{trip}", tripHandler) http.HandleFunc("/api/shapes/{shape}", shapeHandler) + http.HandleFunc("/api/upcoming-departures", upcomingDeparturesHandler) port := "3000" if portEnv, ok := os.LookupEnv("PORT"); ok { diff --git a/go.mod b/go.mod index c2018e8..54ae7d8 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/cjdenio/uta-trax-api go 1.24.2 require ( + github.com/jmoiron/sqlx v1.4.0 // indirect github.com/mattn/go-sqlite3 v1.14.28 // indirect google.golang.org/protobuf v1.36.6 // indirect ) diff --git a/go.sum b/go.sum index cc9af62..27fe9d8 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,9 @@ +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= +github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= +github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A= github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= diff --git a/pkg/gtfs/gtfs.go b/pkg/gtfs/gtfs.go new file mode 100644 index 0000000..234d006 --- /dev/null +++ b/pkg/gtfs/gtfs.go @@ -0,0 +1,38 @@ +package gtfs + +import ( + "database/sql" + "time" +) + +func GetActiveServiceIDs(t *time.Time, db *sql.DB) ([]string, error) { + weekday := t.Weekday() + + rows, err := db.Query(`select service_id from calendar + WHERE ( + case $1 + when 0 then sunday + when 1 then monday + when 2 then tuesday + when 3 then wednesday + when 4 then thursday + when 5 then friday + when 6 then saturday + end + ) AND $2 >= start_date AND $2 <= end_date + union + select service_id from calendar_dates where date = $2;`, weekday, t.Format("20060102")) + + if err != nil { + return nil, err + } + + result := []string{} + for rows.Next() { + var serviceId string + rows.Scan(&serviceId) + result = append(result, serviceId) + } + + return result, nil +}