Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 0c9158c

Browse files
authored
Merge pull request #1264 from transitland/rsp-headways
route_stop_patterns/headways
2 parents 17c794f + 4ea2ad7 commit 0c9158c

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

app/controllers/api/v1/route_stop_patterns_controller.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,40 @@ def self.model
33
RouteStopPattern
44
end
55

6+
def headways
7+
set_model
8+
render :json => rsp_headways([@model]).map { |k,v| [k.join(':'), v] }.to_h
9+
end
10+
611
private
712

13+
def rsp_headways(rsps)
14+
# headway_* query parameters
15+
dates = (params[:headway_dates] || "").split(",")
16+
between = (params[:headway_departure_between] || "").split(",")
17+
departure_span = params[:headway_departure_span].presence
18+
h = params[:headway_percentile].presence
19+
headway_percentile = h ? h.to_f : 0.5
20+
headways = {}
21+
begin
22+
headways = ScheduleStopPair.headways({
23+
dates: dates,
24+
key: [:route_stop_pattern_id, :origin_id, :destination_id],
25+
q: {route_stop_pattern_id: rsps.map(&:id)},
26+
departure_start: between[0],
27+
departure_end: between[1],
28+
departure_span: departure_span,
29+
headway_percentile: headway_percentile
30+
})
31+
rescue StandardError => e
32+
return {}
33+
end
34+
sids = Stop.select([:id, :onestop_id]).where(id: headways.keys.map { |k| k[1..-1] }.flatten.sort.uniq ).map { |s| [s.id, s.onestop_id] }.to_h
35+
rids = rsps.map { |i| [i.id, i.onestop_id] }.to_h
36+
headways.map { |k,v| [[rids[k[0]],sids[k[1]],sids[k[2]]], v]}.to_h
37+
end
38+
39+
840
def index_query
941
super
1042
if params[:traversed_by].present?

app/models/schedule_stop_pair.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def self.headways(dates: [], q: {}, departure_start: nil, departure_end: nil, de
287287
ScheduleStopPair
288288
.where(q)
289289
.where_service_on_date(date)
290-
.select([:id, :route_id, :origin_id, :destination_id, :origin_arrival_time, :origin_departure_time, :destination_arrival_time, :destination_departure_time, :frequency_start_time, :frequency_end_time, :frequency_headway_seconds])
290+
.select([:id, :route_id, :route_stop_pattern_id, :origin_id, :destination_id, :origin_arrival_time, :origin_departure_time, :destination_arrival_time, :destination_departure_time, :frequency_start_time, :frequency_end_time, :frequency_headway_seconds])
291291
.find_each do |ssp|
292292
ssp.expand_frequency.each do |ssp|
293293
t = GTFS::WideTime.parse(ssp.origin_arrival_time).to_seconds

config/routes.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@
4949
get 'aggregate'
5050
end
5151
end
52-
resources :route_stop_patterns, only: [:index, :show]
52+
resources :route_stop_patterns, only: [:index, :show] do
53+
member do
54+
get 'headways'
55+
end
56+
end
5357
resources :schedule_stop_pairs, only: [:index]
5458
resources :feeds, only: [:index, :show] do
5559
member do

0 commit comments

Comments
 (0)