<commit_before>namespace :cartoset do
  desc "Creates the cartodb schema"
  task :create_schema => :environment do
    errors = []

    puts ''
    puts '##############################################'
    puts 'Creating table features...'

    hydra = Typhoeus::Hydra.new

    hydra.queue Typhoeus::Request.new("http://#{CARTODB[:host]}/api/json/tables.json",
                                                 :method => :post,
                                                 :params => {
                                                   :api_key => CARTODB[:api_key],
                                                   :name => 'features'
                                                 },
                                                 :on_complete => lambda do |response|
                                                   if response.success?
                                                       table_data = JSON.parse(response.body)

                                                       table_id = table_data['id']

                                                       if table_id
                                                         create_column = Typhoeus::Request.new("http://#{CARTODB[:host]}/api/json/tables/#{table_id}/update_schema.json",
                                                                                           :method => :put,
                                                                                           :params => {
                                                                                             :api_key => CARTODB[:api_key],
                                                                                             :what    => "add",
                                                                                             :column  => {
                                                                                                :name => "description",
                                                                                                :type => "text"
                                                                                             }
                                                                                           })
                                                         hydra.queue create_column

                                                         hydra.run
                                                         puts '... done!'
                                                       end
                                                     else
                                                       json_response = JSON.parse(response.body)
                                                       if json_response && json_response['errors']
                                                         errors += json_response['errors']
                                                       end
                                                     end
                                                   end)

    hydra.run

    unless errors.empty?
      puts ''
      puts 'Errors creating table features:'
      errors.each do |error|
        puts "- #{error}"
      end
      puts '##############################################'
    end
  end

  desc "Drops the cartodb schema"
  task :drop_schema => :environment do
    errors = []

    puts ''
    puts '##############################################'
    puts 'Droping table features...'

    hydra = Typhoeus::Hydra.new

    list_tables_request = Typhoeus::Request.new("http://#{CARTODB[:host]}/api/json/tables.json",
                                                :method => :get,
                                                :params => {
                                                  :api_key => CARTODB[:api_key]
                                                })

    list_tables_request.on_complete do |response|
      if response.success?
        tables_list = JSON.parse(response.body)
        table_data = tables_list.select{|t| t['name'].downcase.match(/features/)}.first

        if table_data && table_data['id']
          table_id = table_data['id']

          delete_table_request = Typhoeus::Request.new("http://#{CARTODB[:host]}/api/json/tables/#{table_id}.json",
                                                      :method => :delete,
                                                      :params => {
                                                        :api_key => CARTODB[:api_key]
                                                      })
          delete_table_request.on_complete do |response|
            if response.success?
              puts '... done!'
            else
              json_response = JSON.parse(response.body)
              if json_response && json_response['errors']
                errors += json_response['errors']
              end
            end
          end

          hydra.queue delete_table_request
          hydra.run

        else
          errors << 'Table features does not exist'
        end
      else
        json_response = JSON.parse(response.body)
        if json_response && json_response['errors']
          errors += json_response['errors']
        end
      end
    end

    hydra.queue list_tables_request
    hydra.run

    unless errors.empty?
      puts ''
      puts 'Errors creating table features:'
      errors.each do |error|
        puts "- #{error}"
      end
      puts '##############################################'
    end
  end
end<commit_msg>Improves schema creation/deletion tasks<commit_after>namespace :cartoset do
  desc "Creates the cartodb schema"
  task :create_schema => :environment do
    errors = []

    puts ''
    puts '##############################################'
    puts 'Creating table features...'

    hydra = Typhoeus::Hydra.new

    create_table_request = Typhoeus::Request.new("http://#{CARTODB[:host]}/api/json/tables.json",
                                                 :method => :post,
                                                 :params => {
                                                   :api_key => CARTODB[:api_key],
                                                   :name => 'features'
                                                 })

    create_table_request.on_complete do |response|

      if response.success?
        puts '... done!'
      else
        errors += parse_errors(response)
      end
    end

    hydra.queue create_table_request
    hydra.run

    print_errors(errors)
  end

  desc "Drops the cartodb schema"
  task :drop_schema => :environment do
    errors = []

    puts ''
    puts '##############################################'
    puts 'Droping table features...'

    hydra = Typhoeus::Hydra.new

    list_tables_request = Typhoeus::Request.new("http://#{CARTODB[:host]}/api/json/tables.json",
                                                :method => :get,
                                                :params => {
                                                  :api_key => CARTODB[:api_key]
                                                })

    list_tables_request.on_complete do |response|
      if response.success?
        tables_list = JSON.parse(response.body)
        table_data = tables_list.select{|t| t['name'].downcase.match(/features/)}.first

        if table_data && table_data['id']
          table_id = table_data['id']

          delete_table_request = Typhoeus::Request.new("http://#{CARTODB[:host]}/api/json/tables/#{table_id}.json",
                                                      :method => :delete,
                                                      :params => {
                                                        :api_key => CARTODB[:api_key]
                                                      })
          delete_table_request.on_complete do |response|
            if response.success?
              puts '... done!'
            else
              errors += parse_errors(response)
            end
          end

          hydra.queue delete_table_request
          hydra.run

        else
          errors << 'Table features does not exist'
        end
      else
        errors += parse_errors(response)
      end
    end

    hydra.queue list_tables_request
    hydra.run

    print_errors(errors)
  end

  def parse_errors(response)
    json_response = JSON.parse(response.body)
    return json_response['errors'] if json_response && json_response['errors']
  end

  def print_errors(errors)
    unless errors.empty?
      puts ''
      puts 'Errors creating table features:'
      errors.each do |error|
        puts "- #{error}"
      end
      puts '##############################################'
    end
  end
end
{'fraction_non_alphanumeric': 0.11742510862108392, 'fraction_numerical': 0.0, 'mean_word_length': 0.909825327510917, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 122, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4367324', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1726, 'n_words': 358}