<commit_before>require 'restclient'
require 'fileutils'
require 'erb'
require 'readline'

namespace :db do
  desc "Seed with data (task manually created during the 3.0 upgrade, as it went missing)"
  task :seed => :environment do
    load(Rails.root.join("db", "seeds.rb"))
  end

  task :migrate => :environment do
    migration_db_name = [COUCHDB_CONFIG[:db_prefix], "migration", COUCHDB_CONFIG[:db_suffix]].join
    db = COUCHDB_SERVER.database!(migration_db_name)
    migration_ids = db.documents["rows"].select{|row| !row["id"].include?("_design")}.map{|row| row["id"]}
    migration_names = migration_ids.map{|id| db.get(id)[:name]}

    migrations_dir = "db/migration"
    Dir.new(Rails.root.join(migrations_dir)).entries.select { |file| file.ends_with? ".rb" }.sort.each do |file|
      if migration_names.include?(file) 
        puts "skipping migration: #{file} - already applied"
      else
        puts "Applying migration: #{file}"
        load(Rails.root.join(migrations_dir, file))
        db.save_doc({:name => file})
      end
    end
  end

  desc "Create system administrator for couchdb. This is needed only if you are interested to test out replications"
  task :create_couch_sysadmin do
    host = "http://127.0.0.1"
    port = "5984"
    puts "
      **************************************************************

        Welcome to RapidFTR couchdb system administrator setup!

      **************************************************************
      RapidFTR uses couchdb _users and _replication database for couchdb master to master replication.
      If you don't want to try the replication feature please hit CTRL + C.

      Else go on...
    "
    is_admin_available = get "Does your couchdb have admin credentials(yes/no) "
    raise "Invalid value #{is_admin_available}. Needed one of yes/no" unless %w[yes no].include?(is_admin_available)
    if is_admin_available == "yes"
      user_name = get "Enter username of your couchdb "
      password = get "Enter password of your couchdb "
    else
      user_name = ENV["COUCHDB_USERNAME"] || "rapidftr"
      password  = ENV["COUCHDB_PASSWORD"] || "<PASSWORD>"
    end

    puts "
        Assuming you are running your couchdb server at http://127.0.0.1:5984/.
        If you are not, please change this @ #{__FILE__ }
         "

    begin
      RestClient.post "#{host}:#{port}/_session", "name=#{user_name}&password=#{password}", {:content_type => 'application/x-www-form-urlencoded'}
    rescue RestClient::Request::Unauthorized
      full_host = "#{host}:#{port}/_config/admins/#{user_name}"
      RestClient.put full_host, "\""+password+"\"", {:content_type => :json}
    end
    Rake::Task["db:create_couchdb_yml"].invoke(user_name, password)
  end

  desc "Create/Copy couchdb.yml from cocuhdb.yml.example"
  task :create_couchdb_yml, :user_name, :password  do |t, args|
    default_env = ENV['RAILS_ENV'] || "development"
    environments = ["development", "test", "cucumber", "production", "uat", "standalone", default_env].uniq
    user_name = ENV['couchdb_user_name'] || args[:user_name] || ""
    password = ENV['<PASSWORD>'] || args[:password] || ""

    default_config = {
      "host" => "localhost",
      "port" => 5984,
      "https_port" => 6984,
      "database_prefix" => "rapidftr_",
      "username" => user_name,
      "password" => password,
      "ssl" => false
    }

    couchdb_config = {}
    environments.each do |env|
      couchdb_config[env] = default_config.merge("database_suffix" => "_#{env}")
    end

    write_file Rails.root.to_s+"/config/couchdb.yml", couchdb_config.to_yaml
  end<commit_msg>1703 | Subhas/Kavitha | Fix CouchDB Admin task to be more automation friendly<commit_after>require 'restclient'
require 'fileutils'
require 'erb'
require 'readline'

namespace :db do
  desc "Seed with data (task manually created during the 3.0 upgrade, as it went missing)"
  task :seed => :environment do
    load(Rails.root.join("db", "seeds.rb"))
  end

  task :migrate => :environment do
    migration_db_name = [COUCHDB_CONFIG[:db_prefix], "migration", COUCHDB_CONFIG[:db_suffix]].join
    db = COUCHDB_SERVER.database!(migration_db_name)
    migration_ids = db.documents["rows"].select{|row| !row["id"].include?("_design")}.map{|row| row["id"]}
    migration_names = migration_ids.map{|id| db.get(id)[:name]}

    migrations_dir = "db/migration"
    Dir.new(Rails.root.join(migrations_dir)).entries.select { |file| file.ends_with? ".rb" }.sort.each do |file|
      if migration_names.include?(file)
        puts "skipping migration: #{file} - already applied"
      else
        puts "Applying migration: #{file}"
        load(Rails.root.join(migrations_dir, file))
        db.save_doc({:name => file})
      end
    end
  end

  desc "Create system administrator for couchdb. This is needed only if you are interested to test out replications"
  task :create_couch_sysadmin, :user_name, :password do |t, args|
    puts "
      **************************************************************
          Welcome to RapidFTR couchdb system administrator setup
      **************************************************************
    "

    url       = "http://localhost:5984"
    user_name = args[:user_name] || get("Enter username for CouchDB: ")
    password  = args[:password]  || get("Enter password for CouchDB: ")

    begin
      RestClient.post "#{url}/_session", "name=#{user_name}&password=#{password}", {:content_type => 'application/x-www-form-urlencoded'}
      puts "Administrator account #{user_name} is already existing and verified"
    rescue RestClient::Request::Unauthorized
      full_host = "#{url}/_config/admins/#{user_name}"
      RestClient.put full_host, "\""+password+"\"", {:content_type => :json}
      puts "Administrator account #{user_name} has been created"
    end

    Rake::Task["db:create_couchdb_yml"].invoke(user_name, password)
  end

  desc "Create/Copy couchdb.yml from cocuhdb.yml.example"
  task :create_couchdb_yml, :user_name, :password  do |t, args|
    default_env = ENV['RAILS_ENV'] || "development"
    environments = ["development", "test", "cucumber", "production", "uat", "standalone", default_env].uniq
    user_name = ENV['couchdb_user_name'] || args[:user_name] || ""
    password = ENV['<PASSWORD>'] || args[:password] || ""

    default_config = {
      "host" => "localhost",
      "port" => 5984,
      "https_port" => 6984,
      "database_prefix" => "rapidftr_",
      "username" => user_name,
      "password" => password,
      "ssl" => false
    }

    couchdb_config = {}
    environments.each do |env|
      couchdb_config[env] = default_config.merge("database_suffix" => "_#{env}")
    end

    write_file Rails.root.to_s+"/config/couchdb.yml", couchdb_config.to_yaml
  end
{'fraction_non_alphanumeric': 0.17255655774064765, 'fraction_numerical': 0.00709744196362561, 'mean_word_length': 3.7768361581920904, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '5585509', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2145, 'n_words': 504}