<commit_before>require 'pavlov'

module Interactors
  class NormalizeSiteUrl
    include Pavlov::Interactor

    def initialize site_id, normalizer_class_name
      @site = ::Site[site_id]
      @normalizer_class = Kernel.const_get(normalizer_class_name.to_s)
    end

    def execute
      normalized_url = @normalizer_class.normalize @site.url

      if not save_site_with_new_url(@site, normalized_url)
        new_site = ::Site.find_or_create_by url: normalized_url, url_normalizer_class: @normalizer_class
        move_facts_to_other_site @site, new_site
        cleanup_site_if_empty @site
      end
    end

    private

    def cleanup_site_if_empty site
      site.delete if site.facts.count == 0
    end

    def save_site_with_new_url site, url
      site.url = url
      site.save
    end

    def move_facts_to_other_site old_site, new_site
      old_site.facts.each do |f|
        f.site = new_site
        f.save
      end
    end
  end
end<commit_msg>prefer unless over if not<commit_after>require 'pavlov'

module Interactors
  class NormalizeSiteUrl
    include Pavlov::Interactor

    def initialize site_id, normalizer_class_name
      @site = ::Site[site_id]
      @normalizer_class = Kernel.const_get(normalizer_class_name.to_s)
    end

    def execute
      normalized_url = @normalizer_class.normalize @site.url

      unless save_site_with_new_url(@site, normalized_url)
        new_site = ::Site.find_or_create_by url: normalized_url, url_normalizer_class: @normalizer_class
        move_facts_to_other_site @site, new_site
        cleanup_site_if_empty @site
      end
    end

    private

    def cleanup_site_if_empty site
      site.delete if site.facts.count == 0
    end

    def save_site_with_new_url site, url
      site.url = url
      site.save
    end

    def move_facts_to_other_site old_site, new_site
      old_site.facts.each do |f|
        f.site = new_site
        f.save
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.05858170606372045, 'fraction_numerical': 0.0010277492291880781, 'mean_word_length': 2.863095238095238, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6435610', 'n_tokens_mistral': 663, 'n_tokens_neox': 638, 'n_words': 142}