1 # frozen_string_literal: true
10 # Warning: doesn't account for leap years
11 # J F M A M J J A S O N D
12 DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
22 (1..12).each do |month|
23 last_day = DAYS_IN_MONTH[month]
24 last_day += 1 if (month == 2) && (0 == year % 4) # leap year (doesn't account for 2000)
26 (1..last_day).each do |day|
27 date_hash = {year: year, month: month, day: day}
28 date_string = "#{year}-#{month.to_s.rjust(2, "0")}-#{day.to_s.rjust(2, "0")}"
30 counts = trip_counts(date_hash: date_hash)
33 counts.each do |route, count|
34 route_total = route_totals[route] || 0
36 route_totals[route] = route_total
41 puts "#{date_string};#{daily_total};#{counts.inspect}"
46 route_totals.each do |_route, count|
50 puts "ANNUAL_TOTAL;#{annual_total};#{route_totals.inspect}"
53 def route_origin_stop_ids
54 @route_origin_stop_ids ||= @db.query(
55 "SELECT stop_id FROM stop_times WHERE stop_sequence=1 GROUP BY 1 ORDER BY 1;"
59 # Returns a hash of route_num => trip_count for the given date
60 def trip_counts(date_hash: nil)
63 print "Checking for trips on #{date_hash[:year]}-#{date_hash[:month]}-#{date_hash[:day]}"
65 route_origin_stop_ids.each do |stop_id|
69 route_trips = @gtfs.trips(stop_id: stop_id, date_hash: date_hash, originating_only: true).map do |k, v|
76 end.to_h.each do |route, trips|
77 count = result[route] || 0