<commit_before>class DiscourseChat::Channel < DiscourseChat::PluginModel
  KEY_PREFIX = 'channel:'

  # Setup ActiveRecord::Store to use the JSON field to read/write these values
  store :value, accessors: [ :provider, :error_key, :data ], coder: JSON

  after_initialize :init_data

  def init_data
    self.data = {} if self.data.nil?
  end

  after_destroy :destroy_rules
  def destroy_rules
    rules.destroy_all()
  end

  validate :provider_valid?, :data_valid?

  def provider_valid?
    # Validate provider
    if not ::DiscourseChat::Provider.provider_names.include? provider
      errors.add(:provider, "#{provider} is not a valid provider")
      return
    end
  end

  def data_valid?
    # If provider is invalid, don't try and check data
    return unless ::DiscourseChat::Provider.provider_names.include? provider

    params = ::DiscourseChat::Provider.get_by_name(provider)::CHANNEL_PARAMETERS

    unless params.map {|p| p[:key]}.sort == data.keys.sort
      errors.add(:data, "data does not match the required structure for provider #{provider}")
      return
    end

    check_unique = false
    matching_channels = DiscourseChat::Channel.all

    data.each do |key, value|
      regex_string = params.find{|p| p[:key] == key}[:regex]<commit_msg>Ignore self when checking for duplicate rule<commit_after>class DiscourseChat::Channel < DiscourseChat::PluginModel
  KEY_PREFIX = 'channel:'

  # Setup ActiveRecord::Store to use the JSON field to read/write these values
  store :value, accessors: [ :provider, :error_key, :data ], coder: JSON

  after_initialize :init_data

  def init_data
    self.data = {} if self.data.nil?
  end

  after_destroy :destroy_rules
  def destroy_rules
    rules.destroy_all()
  end

  validate :provider_valid?, :data_valid?

  def provider_valid?
    # Validate provider
    if not ::DiscourseChat::Provider.provider_names.include? provider
      errors.add(:provider, "#{provider} is not a valid provider")
      return
    end
  end

  def data_valid?
    # If provider is invalid, don't try and check data
    return unless ::DiscourseChat::Provider.provider_names.include? provider

    params = ::DiscourseChat::Provider.get_by_name(provider)::CHANNEL_PARAMETERS

    unless params.map {|p| p[:key]}.sort == data.keys.sort
      errors.add(:data, "data does not match the required structure for provider #{provider}")
      return
    end

    check_unique = false
    matching_channels = DiscourseChat::Channel.where.not(id: id)

    data.each do |key, value|
      regex_string = params.find{|p| p[:key] == key}[:regex]
{'fraction_non_alphanumeric': 0.1030608291359938, 'fraction_numerical': 0.0, 'mean_word_length': 3.9180952380952383, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2718550', 'n_tokens_mistral': 773, 'n_tokens_neox': 752, 'n_words': 218}