<commit_before># == Schema Information
#
# Table name: change_payloads
#
#  id           :integer          not null, primary key
#  payload      :json
#  changeset_id :integer
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#
# Indexes
#
#  index_change_payloads_on_changeset_id  (changeset_id)
#

class ChangePayload < ActiveRecord::Base
  include HasAJsonPayload

  belongs_to :changeset

  after_initialize :set_default_values
  validate :validate_payload

  onestop_id_format_proc = -> (onestop_id, expected_entity_type) do
    is_a_valid_onestop_id, onestop_id_errors = OnestopId.validate_onestop_id_string(onestop_id, expected_entity_type: expected_entity_type)
    raise JSON::Schema::CustomFormatError.new(onestop_id_errors.join(', ')) if !is_a_valid_onestop_id
  end
  JSON::Validator.schema_reader = JSON::Schema::Reader.new(accept_uri: false, accept_file: true)
  JSON::Validator.register_format_validator('operator-onestop-id', -> (onestop_id) {
    onestop_id_format_proc.call(onestop_id, 'operator')
  })
  JSON::Validator.register_format_validator('stop-onestop-id', -> (onestop_id) {
    onestop_id_format_proc.call(onestop_id, 'stop')
  })
  JSON::Validator.register_format_validator('feed-onestop-id', -> (onestop_id) {
    onestop_id_format_proc.call(onestop_id, 'feed')
  })
  JSON::Validator.register_format_validator('route-onestop-id', -> (onestop_id) {
    onestop_id_format_proc.call(onestop_id, 'route')
  })
  JSON::Validator.register_format_validator('vehicle-type', -> (vehicle_type) {
    if vehicle_type.is_a?(Integer) || vehicle_type.match(/\A\d+\z/)
      GTFS::Route::VEHICLE_TYPES.keys.map {|i| i.to_s.to_i }.include?(vehicle_type.to_i)
    else
      GTFS::Route::VEHICLE_TYPES.values.map { |s| s.to_s.parameterize('_') }.include?(vehicle_type.to_s)
    end
  })
  JSON::Validator.register_format_validator('sha1', -> (sha1) {
    !!sha1.match(/^[0-9a-f]{5,40}$/)
  })

  def apply!
    cache = {}
    changes = []
    entity_types = {
      feed: Feed,
      stop: Stop,
      operator: Operator,
      route: Route,
      schedule_stop_pair: ScheduleStopPair,
      route_stop_pattern: RouteStopPattern
    }
    (payload_as_ruby_hash[:changes] || []).each do |change|
      (entity_types.keys & change.keys).each do |entity_type|
        changes << [entity_type, change[:action], change[entity_type]]
      end
    end
    changes
      .chunk { |entity_type, action, change| [entity_type, action] }
      .each { | chunk_key, chunked_changes |
        entity_type, action = chunk_key
        puts "Applying... #{entity_type}, #{action}, #{chunked_changes.size}"
        entity_types[entity_type].apply_changes(
          changeset: changeset,
          action: action,
          changes: chunked_changes.map(&:last)
        )<commit_msg>Remove debugging line<commit_after># == Schema Information
#
# Table name: change_payloads
#
#  id           :integer          not null, primary key
#  payload      :json
#  changeset_id :integer
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#
# Indexes
#
#  index_change_payloads_on_changeset_id  (changeset_id)
#

class ChangePayload < ActiveRecord::Base
  include HasAJsonPayload

  belongs_to :changeset

  after_initialize :set_default_values
  validate :validate_payload

  onestop_id_format_proc = -> (onestop_id, expected_entity_type) do
    is_a_valid_onestop_id, onestop_id_errors = OnestopId.validate_onestop_id_string(onestop_id, expected_entity_type: expected_entity_type)
    raise JSON::Schema::CustomFormatError.new(onestop_id_errors.join(', ')) if !is_a_valid_onestop_id
  end
  JSON::Validator.schema_reader = JSON::Schema::Reader.new(accept_uri: false, accept_file: true)
  JSON::Validator.register_format_validator('operator-onestop-id', -> (onestop_id) {
    onestop_id_format_proc.call(onestop_id, 'operator')
  })
  JSON::Validator.register_format_validator('stop-onestop-id', -> (onestop_id) {
    onestop_id_format_proc.call(onestop_id, 'stop')
  })
  JSON::Validator.register_format_validator('feed-onestop-id', -> (onestop_id) {
    onestop_id_format_proc.call(onestop_id, 'feed')
  })
  JSON::Validator.register_format_validator('route-onestop-id', -> (onestop_id) {
    onestop_id_format_proc.call(onestop_id, 'route')
  })
  JSON::Validator.register_format_validator('vehicle-type', -> (vehicle_type) {
    if vehicle_type.is_a?(Integer) || vehicle_type.match(/\A\d+\z/)
      GTFS::Route::VEHICLE_TYPES.keys.map {|i| i.to_s.to_i }.include?(vehicle_type.to_i)
    else
      GTFS::Route::VEHICLE_TYPES.values.map { |s| s.to_s.parameterize('_') }.include?(vehicle_type.to_s)
    end
  })
  JSON::Validator.register_format_validator('sha1', -> (sha1) {
    !!sha1.match(/^[0-9a-f]{5,40}$/)
  })

  def apply!
    cache = {}
    changes = []
    entity_types = {
      feed: Feed,
      stop: Stop,
      operator: Operator,
      route: Route,
      schedule_stop_pair: ScheduleStopPair,
      route_stop_pattern: RouteStopPattern
    }
    (payload_as_ruby_hash[:changes] || []).each do |change|
      (entity_types.keys & change.keys).each do |entity_type|
        changes << [entity_type, change[:action], change[entity_type]]
      end
    end
    changes
      .chunk { |entity_type, action, change| [entity_type, action] }
      .each { | chunk_key, chunked_changes |
        entity_type, action = chunk_key
        # puts "Applying... #{entity_type}, #{action}, #{chunked_changes.size}"
        entity_types[entity_type].apply_changes(
          changeset: changeset,
          action: action,
          changes: chunked_changes.map(&:last)
        )
{'fraction_non_alphanumeric': 0.12854609929078015, 'fraction_numerical': 0.0028368794326241137, 'mean_word_length': 3.879757785467128, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '94376', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1981, 'n_words': 296}