<commit_before>require_relative 'gtfs_parser'
include GtfsParser

# DepartureComparator compares the scheduled departures from the GTFS data and
# the departures obtained from the realtime feed.
module DepartureComparator

  @messages = []
  @statuses = {
    feed_down: false,
    missing_routes: [],
    incorrect_times: []
  }

  # How many hours in the future can we expect the realtime feed to return
  # departures on a given route?
  DEPARTURE_FUTURE_HOURS = 3

  def compare
    begin
      avail_times = Bojangles.get_avail_departure_times!
    rescue SocketError
      report_feed_down
    end
    gtfs_times = soonest_departures_within DEPARTURE_FUTURE_HOURS
    # Look through each scheduled route, and make sure that each route is present,
    # and that the next reported departure has the correct scheduled time.
    gtfs_times.each do |route_number, (last_time, next_time)|
      if avail_times.key? route_number
        avail_time = avail_times[route_number]
        # if Avail's returned SDT is before now, check that it's the last scheduled
        # departure from the stop (i.e. the bus is running late).
        if avail_time < Time.now && avail_time != last_time
          report_incorrect_departure route_number, last_time, avail_time
        # if the returned SDT is after now, check that it's the next scheduled departure
        elsif avail_time >= Time.now && avail_time != next_time
          report_incorrect_departure route_number, next_time, avail_time
        end
      else report_missing_route route_number, gtfs_time
      end
    end
    [@messages, @statuses]
  end

  def report_feed_down
    @messages << <<-message
      The realtime feed is inaccessible via HTTP.
    message
    @statuses[:feed_down] = true
  end

  def report_missing_route(route_number, gtfs_time)
    @messages << <<-message
      Route #{route_number} is missing:
      Expected to be departing from Studio Arts Building
      Expected scheduled departure time #{gtfs_time}
    message
    @statuses[:missing_routes] << route_number
  end

  def report_incorrect_departure(route_number, gtfs_time, avail_time)
    @messages << <<-message
      Incorrect route #{route_number} departure:
      Expected next scheduled departure time #{gtfs_time}
      Received next scheduled departure time #{avail_time}
    message
    @statuses[:incorrect_times] << route_number
  end

end<commit_msg>Error messages tailored to whether we're looking in the past or not<commit_after>require_relative 'gtfs_parser'
include GtfsParser

# DepartureComparator compares the scheduled departures from the GTFS data and
# the departures obtained from the realtime feed.
module DepartureComparator

  @messages = []
  @statuses = {
    feed_down: false,
    missing_routes: [],
    incorrect_times: []
  }

  # How many hours in the future can we expect the realtime feed to return
  # departures on a given route?
  DEPARTURE_FUTURE_HOURS = 3

  def compare
    begin
      avail_times = Bojangles.get_avail_departure_times!
    rescue SocketError
      report_feed_down
    end
    gtfs_times = soonest_departures_within DEPARTURE_FUTURE_HOURS
    # Look through each scheduled route, and make sure that each route is present,
    # and that the next reported departure has the correct scheduled time.
    gtfs_times.each do |route_number, (last_time, next_time)|
      if avail_times.key? route_number
        avail_time = avail_times[route_number]
        # if Avail's returned SDT is before now, check that it's the last scheduled
        # departure from the stop (i.e. the bus is running late).
        if avail_time < Time.now && avail_time != last_time
          report_incorrect_departure route_number, last_time, avail_time, 'last'
        # if the returned SDT is after now, check that it's the next scheduled departure
        elsif avail_time >= Time.now && avail_time != next_time
          report_incorrect_departure route_number, next_time, avail_time, 'next'
        end
      else report_missing_route route_number, gtfs_time
      end
    end
    [@messages, @statuses]
  end

  def email_format(time)
    time.strftime '%r'
  end

  def report_feed_down
    @messages << <<-message
      The realtime feed is inaccessible via HTTP.
    message
    @statuses[:feed_down] = true
  end

  def report_missing_route(route_number, gtfs_time)
    @messages << <<-message
      Route #{route_number} is missing:
      Expected to be departing from Studio Arts Building
      Expected scheduled departure time #{email_format gtfs_time}
    message
    @statuses[:missing_routes] << route_number
  end

  def report_incorrect_departure(route_number, gtfs_time, avail_time, type)
    @messages << <<-message
      Incorrect route #{route_number} departure:
      Expected #{type} scheduled departure time #{email_format gtfs_time}
      Received #{type} scheduled departure time #{email_format avail_time}
    message
    @statuses[:incorrect_times] << route_number
  end

end
{'fraction_non_alphanumeric': 0.06270096463022508, 'fraction_numerical': 0.00040192926045016077, 'mean_word_length': 3.4556848701880036, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1752796', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1457, 'n_words': 479}