<commit_before>$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
$LOAD_PATH.unshift File.expand_path('..', __FILE__)

require 'concurrent'
require 'date'
require 'faker'
require 'logger'
require 'logger_colored'

require 'seed_base'

module Faker
  class Base
    class << self
      def fetch(key, options = {})
        fetched = translate("faker.#{key}", options)
        fetched = fetched.sample if fetched.respond_to?(:sample)
        if fetched.match(/^\//) and fetched.match(/\/$/) # A regex
          regexify(fetched)
        else
          fetched
        end
      end
    end
  end
end

module KillBillIntegrationSeed
  class TestSeedKaui < TestSeedBase

    def setup
      log_dir = '/var/tmp/'
      now = Time.now.strftime('%Y-%m-%d-%H:%M')
      kb_log_path = "#{log_dir}/kaui_seed.#{now}.killbill.log"
      KillBillClient.logger = Logger.new(kb_log_path)
      KillBillClient.logger.level = Logger::DEBUG

      @logger = Logger.new(STDOUT)
      @logger.level = Logger::INFO

      @pool = Concurrent::CachedThreadPool.new

      @base_subscriptions = Concurrent::Array.new
      @ao_subscriptions = Concurrent::Hash.new
      @subs_mutex = Mutex.new

      tenant_info = {
          :use_multi_tenant => true,
          :api_key => ENV['api_key'] || 'bob',
          :api_secret => ENV['api_secret'] || 'lazar'
      }

      @start_date = ENV['start_date'] || Date.today.to_s
      setup_base('kaui_seed', tenant_info, "#{@start_date}T08:00:00.000Z")
      upload_catalog(ENV['catalog'], true, @user, @options) if ENV['catalog']
    end

    def teardown
      @pool.shutdown
      teardown_base
    end

    def test_seed_kaui
      initial_date = DateTime.parse(@start_date).to_date
      last_date = DateTime.now.to_date

      run_with_clock(initial_date, last_date) { |date| run_one_day(date) }
    end

    private

    def run_one_day(date)
      # Notes:
      # * The config is global, so set it before creating accounts in parallel
      # * Stick to a few "safe" locales (rather than sampling I18n.available_locales) to ensure having enough data in Faker
      Faker::Config.locale = [:de, :en, :es, :fa, :fr, :it, :ja, :ko, :ru].sample

      task = lambda { create_account_with_subscription }<commit_msg>seed_kaui: make KB address configurable<commit_after>$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
$LOAD_PATH.unshift File.expand_path('..', __FILE__)

require 'concurrent'
require 'date'
require 'faker'
require 'logger'
require 'logger_colored'

require 'seed_base'

module Faker
  class Base
    class << self
      def fetch(key, options = {})
        fetched = translate("faker.#{key}", options)
        fetched = fetched.sample if fetched.respond_to?(:sample)
        if fetched.match(/^\//) and fetched.match(/\/$/) # A regex
          regexify(fetched)
        else
          fetched
        end
      end
    end
  end
end

module KillBillIntegrationSeed
  class TestSeedKaui < TestSeedBase

    def setup
      log_dir = '/var/tmp/'
      now = Time.now.strftime('%Y-%m-%d-%H:%M')
      kb_log_path = "#{log_dir}/kaui_seed.#{now}.killbill.log"
      KillBillClient.logger = Logger.new(kb_log_path)
      KillBillClient.logger.level = Logger::DEBUG

      @logger = Logger.new(STDOUT)
      @logger.level = Logger::INFO

      @pool = Concurrent::CachedThreadPool.new

      @base_subscriptions = Concurrent::Array.new
      @ao_subscriptions = Concurrent::Hash.new
      @subs_mutex = Mutex.new

      tenant_info = {
          :use_multi_tenant => true,
          :api_key => ENV['api_key'] || 'bob',
          :api_secret => ENV['api_secret'] || 'lazar'
      }

      @start_date = ENV['start_date'] || Date.today.to_s
      setup_base('kaui_seed', tenant_info, "#{@start_date}T08:00:00.000Z", ENV['kb_host'] || DEFAULT_KB_ADDRESS, ENV['kb_port'] || DEFAULT_KB_PORT)
      upload_catalog(ENV['catalog'], true, @user, @options) if ENV['catalog']
    end

    def teardown
      @pool.shutdown
      teardown_base
    end

    def test_seed_kaui
      initial_date = DateTime.parse(@start_date).to_date
      last_date = DateTime.now.to_date

      run_with_clock(initial_date, last_date) { |date| run_one_day(date) }
    end

    private

    def run_one_day(date)
      # Notes:
      # * The config is global, so set it before creating accounts in parallel
      # * Stick to a few "safe" locales (rather than sampling I18n.available_locales) to ensure having enough data in Faker
      Faker::Config.locale = [:de, :en, :es, :fa, :fr, :it, :ja, :ko, :ru].sample

      task = lambda { create_account_with_subscription }
{'fraction_non_alphanumeric': 0.1286740692357936, 'fraction_numerical': 0.004789897670367951, 'mean_word_length': 2.9298545765611634, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2880129', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1540, 'n_words': 330}