<commit_before>require 'ruby-progressbar'
require 'supermarket/community_site'

namespace :supermarket do
  namespace :amend do
    desc 'Update imported cookbook data'
    task :cookbooks => ['supermarket:cull:all', :environment] do
      bar = Supermarket::Import.debug do
        ProgressBar.create(
          title: "Amending Cookbook Data",
          total: ::Cookbook.count,
          format: '%t: (%c/%C) |%B|'
        )
      end

      ::Cookbook.where.not(legacy_id: nil).find_in_batches do |batch|
        batch.each do |cookbook|
          begin
            id = cookbook.legacy_id
            record = Supermarket::CommunitySite::CookbookRecord.find(id)

            if record && record.updated_at.utc > cookbook.updated_at.utc
              cookbook.assign_attributes(
                category: record.supermarket_category,
                owner: record.supermarket_owner,
                source_url: record.sanitized_external_url.to_s,
                updated_at: record.updated_at
              )
              cookbook.record_timestamps = false
              cookbook.save!
            end

            Supermarket::Import.debug { bar.increment }
          rescue => e
            Supermarket::Import.debug { bar.decrement }

            Supermarket::Import.report(e) { |m| bar.log(m) }
          end
        end
      end
    end

    desc 'Update Users registered before the migration'
    task :users => ['supermarket:cull:all', :environment] do
      base_scope = ::Account.for('chef_oauth2').joins(:user).where('legacy_id IS ?', nil)

      bar = Supermarket::Import.debug do
        ProgressBar.create(
          title: "Amending Account Data",
          total: base_scope.count,
          format: '%t: (%c/%C) |%B|'
        )
      end

      base_scope.find_each do |account|
        begin
          legacy_user = Supermarket::CommunitySite::UserRecord.query(
            unique_name: account.username
          ).first

          if legacy_user
            user = account.user
            user.record_timestamps = false
            user.legacy_id = legacy_user.id
            user.save!
          end

          Supermarket::Import.debug { bar.increment }
        rescue => e
          Supermarket::Import.debug { bar.decrement }

          Supermarket::Import.report(e) { |m| bar.log(m) }
        end
      end
    end
  end
end
<commit_msg>Lightly refactor the cookbook amendment task<commit_after>require 'ruby-progressbar'
require 'supermarket/community_site'

namespace :supermarket do
  namespace :amend do
    desc 'Update imported cookbook data'
    task :cookbooks => ['supermarket:cull:all', :environment] do
      base_scope = ::Cookbook.where('legacy_id IS NOT ?', nil)

      bar = Supermarket::Import.debug do
        ProgressBar.create(
          title: "Amending Cookbook Data",
          total: base_scope.count,
          format: '%t: (%c/%C) |%B|'
        )
      end

      base_scope.find_in_batches do |batch|
        batch.each do |cookbook|
          begin
            id = cookbook.legacy_id
            record = Supermarket::CommunitySite::CookbookRecord.find(id)

            if record && record.updated_at.utc > cookbook.updated_at.utc
              cookbook.assign_attributes(
                category: record.supermarket_category,
                owner: record.supermarket_owner,
                source_url: record.sanitized_external_url.to_s,
                updated_at: record.updated_at
              )
              cookbook.record_timestamps = false
              cookbook.save!
            end

            Supermarket::Import.debug { bar.increment }
          rescue => e
            Supermarket::Import.debug { bar.decrement }

            Supermarket::Import.report(e) { |m| bar.log(m) }
          end
        end
      end
    end

    desc 'Update Users registered before the migration'
    task :users => ['supermarket:cull:all', :environment] do
      base_scope = ::Account.for('chef_oauth2').joins(:user).where('legacy_id IS ?', nil)

      bar = Supermarket::Import.debug do
        ProgressBar.create(
          title: "Amending Account Data",
          total: base_scope.count,
          format: '%t: (%c/%C) |%B|'
        )
      end

      base_scope.find_each do |account|
        begin
          legacy_user = Supermarket::CommunitySite::UserRecord.query(
            unique_name: account.username
          ).first

          if legacy_user
            user = account.user
            user.record_timestamps = false
            user.legacy_id = legacy_user.id
            user.save!
          end

          Supermarket::Import.debug { bar.increment }
        rescue => e
          Supermarket::Import.debug { bar.decrement }

          Supermarket::Import.report(e) { |m| bar.log(m) }
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.09843912591050989, 'fraction_numerical': 0.0004162330905306972, 'mean_word_length': 1.919805589307412, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '1534549', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1355, 'n_words': 291}