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 monthly_totals = report_month(year: year, month: month)
25 monthly_totals.each do |route, count|
26 total = route_totals[route] || 0
28 route_totals[route] = total
33 route_totals.each do |_route, count|
37 puts "ANNUAL_TOTAL;#{annual_total};#{route_totals.inspect}"
40 def route_origin_stop_ids
41 @route_origin_stop_ids ||= @db.query(
42 "SELECT stop_id FROM stop_times WHERE stop_sequence=1 GROUP BY 1 ORDER BY 1;"
46 # Returns a hash of route_num => trip_count for the given date
47 def trip_counts(date_hash: nil)
50 print "Checking for trips on #{date_hash[:year]}-#{date_hash[:month]}-#{date_hash[:day]}"
52 route_origin_stop_ids.each do |stop_id|
56 route_trips = @gtfs.trips(stop_id: stop_id, date_hash: date_hash, originating_only: true).map do |k, v|
63 end.to_h.each do |route, trips|
64 count = result[route] || 0
74 def report_month(year:, month:)
77 last_day = DAYS_IN_MONTH[month]
78 last_day += 1 if (month == 2) && (0 == year % 4) # leap year (doesn't account for 1900, 2100, ...)
80 (1..last_day).each do |day|
81 date_hash = {year: year, month: month, day: day}
82 date_string = "#{year}-#{month.to_s.rjust(2, "0")}-#{day.to_s.rjust(2, "0")}"
84 counts = trip_counts(date_hash: date_hash)
87 counts.each do |route, count|
88 route_total = monthly_totals[route] || 0
90 monthly_totals[route] = route_total
95 puts "#{date_string};#{daily_total};#{counts.inspect}"