如何将这些许多rake任务合并为一个任务

时间:2014-09-29 16:25:14

标签: ruby-on-rails rake-task

我正在处理的应用程序每晚运行导入任务以从外部数据库获取信息并处理并将其写入本地PostgreSQL数据库。这样就可以在我们的应用程序中查看。

我们执行以下操作:

  • 创建与外部数据库的连接
  • 执行一些货币转换
  • 根据平均值
  • 对数据进行一些计算
  • 检查项目是否已存在,如果已更改则更新
  • 同时检查其分类方式并更改类别以符合我们的分类
  • 创建项目(如果项目不存在)

所有这些数据都在外部数据库的不同表中,所以我为每个表创建了一个任务(大约30个),但是我正在重复自己,所以寻找关于如何干这个坏男孩的一些指导

是否可以在外部数据库表名称的集合上创建某种循环?我已经尝试了几次,但没有取得任何成功。请在下面找到两个不同导入脚本示例的代码。

desc 'Imports ABC Data'
task :abcdeal_import => :environment do

    # Connect to legacy db

    class OldDb < ActiveRecord::Base
        establish_connection :cleardb
        self.table_name = 'abcdeal'
    end

    class Import < OldDb
        self.table_name = 'abcdeal'
    end

    # Currency Conversion

    require 'money'
    require 'money/bank/google_currency'

    # set the seconds after than the current rates are automatically expired
    Money::Bank::GoogleCurrency.ttl_in_seconds = 86400
    # set default bank to instance of GoogleCurrency
    bank = Money::Bank::GoogleCurrency.new

        @gbp = bank.get_rate(:USD, :GBP).to_f
        @eur = bank.get_rate(:USD, :EUR).to_f
        @aud = bank.get_rate(:USD, :AUD).to_f      
        @cad = bank.get_rate(:USD, :CAD).to_f    
        @nzd = bank.get_rate(:USD, :NZD).to_f      

    Import.all.find_each do |u|

        # Define Variables

        @price = u.UnitPrice
        @interest = u.UnitSold
        #@interest_changes = u.unitsold_changes
        @company = u.Company
        @legacy_id = u.id
        @end_date = u.EndDate
        @category = u.Category
        @revenue = u.Revenue

        # Calculate average

        @company_average = company_average(@company)        

        if @company_average == 0
            @company_average = 1
        end

        # Does item exist?
        if Dailydeal.exists?(:legacy_id => @legacy_id, :company => @company)
        # It exists.
            @historical_interest = Product.where(:legacy_id => @legacy_id).pluck(:interest)                
            # Another statement to check that the Interest != UnitSold
            if @interest != @historical_interest
            Product.where(:legacy_id => @legacy_id, :company => @company).update_all(:interest => @interest, :end_date => @end_date, :interest_changes => @interest_changes, :status => average_change_interest, :revenue => @revenue, :revenue_eur => eur_revenue_currency_conversion, :revenue_aud => aud_revenue_currency_conversion, :revenue_gbp => gbp_revenue_currency_conversion, :revenue_cad => cad_revenue_currency_conversion, :revenue_nzd => nzd_revenue_currency_conversion)
            end
        else
        # Doesn't exist

        # Set Category            

        case @category
            when 'Apparel'
                @category = 'Apparel & Footwear'

            when 'Gadgets'
                @category = 'Electronics & Accessories'

            when 'Gifts'
                @category = 'Miscellaneous'

            when 'Health & Cosmetics'
                @category = 'Health & Beauty'

            when 'Home'
                @category = 'Home & Furniture'

            when 'Other'
                @category = 'Miscellaneous'

            when 'Toys'
                @category = 'Toys & Kids'

            else

                @category

            end

        Product.create(
              :name => u.ProductName,
              :link => u.ProductLink,
              :image_url => u.ImageUrl,
              :price => u.UnitPrice,
              :interest => u.UnitSold,
              :start_date => u.StartDate,
              :end_date => u.EndDate,
              :revenue => u.Revenue,
              :company => u.Company,
              :category => @category,
              :country => u.Country,
              :price_eur => eur_currency_conversion,
              :price_aud => aud_currency_conversion,
              :price_gbp => gbp_currency_conversion,
              :price_cad => cad_currency_conversion,
              :price_nzd => nzd_currency_conversion,
              :revenue_eur => eur_revenue_currency_conversion,
              :revenue_aud => aud_revenue_currency_conversion,
              :revenue_gbp => gbp_revenue_currency_conversion,
              :revenue_cad => cad_revenue_currency_conversion,
              :revenue_nzd => nzd_revenue_currency_conversion,
              :status => average_change_interest,
              :legacy_id => u.id)
        end
    end

    puts "Successfully imported data from abc"

end

desc 'Imports def Data'
task :def_import => :environment do

    # Connect to legacy db

    class OldDb < ActiveRecord::Base
        establish_connection :cleardb
        self.table_name = 'def'
    end

    class Import < OldDb
        self.table_name = 'def'
    end

    # Currency Conversion

    require 'money'
    require 'money/bank/google_currency'

    # set the seconds after than the current rates are automatically expired
    Money::Bank::GoogleCurrency.ttl_in_seconds = 86400
    # set default bank to instance of GoogleCurrency
    bank = Money::Bank::GoogleCurrency.new

        @gbp = bank.get_rate(:USD, :GBP).to_f
        @eur = bank.get_rate(:USD, :EUR).to_f
        @aud = bank.get_rate(:USD, :AUD).to_f      
        @cad = bank.get_rate(:USD, :CAD).to_f    
        @nzd = bank.get_rate(:USD, :NZD).to_f      

    Import.all.find_each do |u|

        # Define Variables

        @price = u.UnitPrice
        @interest = u.UnitSold
        #@interest_changes = u.unitsold_changes
        @company = u.Company
        @legacy_id = u.id
        @end_date = u.EndDate
        @category = u.Category
        @revenue = u.Revenue

        # Calculate average

        @company_average = company_average(@company)

        if @company_average == 0
            @company_average = 1
        end

        # Does item exist?
        if Product.exists?(:legacy_id => @legacy_id, :company => @company)
        # It exists.
            @historical_interest = Product.where(:legacy_id => @legacy_id).pluck(:interest)                
            # Another statement to check that the Interest != UnitSold
            if @interest != @historical_interest
                Product.where(:legacy_id => @legacy_id, :company => @company).update_all(:interest => @interest, :end_date => @end_date, :interest_changes => @interest_changes, :status => average_change_interest, :revenue => @revenue, :revenue_eur => eur_revenue_currency_conversion, :revenue_aud => aud_revenue_currency_conversion, :revenue_gbp => gbp_revenue_currency_conversion, :revenue_cad => cad_revenue_currency_conversion, :revenue_nzd => nzd_revenue_currency_conversion)
            end
        else
        # Doesn't exist

        # Set Categories    

        case @category

            when 'Adult Products'
                @category

            when 'Arts & Crafts', 'Furniture', 'Garden', 'Large Appliances', 'Lighting', 'Outdoor & Patio', 'Stationary', 'Storage & Organization'
                @category = 'Home & Furniture'

            when 'Audio & Audio Accessories', 'Electronics Accessories', 'Gadgets', 'GPS & Car Accessories', 'Tools & Hardware', 'Tablets'
                @category = 'Electronics & Accessories'

            when 'Automotive Services'
                @category = 'Automotive'

            when 'Bath', 'Bedding'
                @category = 'Bed & Bath'

            when 'Books & Media', 'Canada', 'Gifts', 'Miscellaneous', 'Recreational'
                @category = 'Miscellaneous'

            when 'Cafe, Bakery & Treats', 'Kitchen'
                @category = 'Kitchen'

            when 'Casual Restaurants', 'Cleaning Services', 'Dance Classes', 'Fast Food', 'Fine Dining', 'Home Services', 'Local Exhibits & Shows', 'Other Services', 'Other Workshops & Classes', 'Outdoor Adventures', 'Other Activities', 'Tours & Sightseeing', 'Fitness Classes', 'Massage', 'Med Spa', 'Salon & Hair Care Services', 'Spa Services', 'Yoga, Pilates & Aerobics'
                @category = 'Experiences'

            when 'Cats', 'Dogs', 'Pets', 'Pets Accessories'
                @category = 'Pets Accessories'

            when 'Clothing', 'Clothing, Fashion & Accessories', 'Fashion & Accessories', 'Footwear', "Men's", 'Unisex', "Women's"
                @category = 'Apparel & Footwear'

            when 'Earrings', 'Fashion Accessories'
                @category = 'Jewellery & Accessories'

            when 'Fitness', 'Hair Care', 'Hair Removal', 'Health Care', 'Manicure & Pedicure', 'Other Beauty & Spa', 'Other Health & Fitness', 'Personal Care', 'Teeth Whitening', 'Wellness & Nutrition', 'Facial'
            @category = 'Health & Beauty'

            when 'Golf', 'Sports', 'Football', 'Baseball', 'Hockey', 'Ice Hockey'
                @category = 'Sporting Accessories'

        else

            @category

        end            

        Product.create(
              :name => u.ProductName,
              :link => u.ProductLink,
              :image_url => u.ImageUrl,
              :price => u.UnitPrice,
              :interest => u.UnitSold,
              :start_date => u.StartDate,
              :end_date => u.EndDate,
              :revenue => u.Revenue,
              :company => u.Company,
              :category => @category,
              :country => u.Country,
              :price_eur => eur_currency_conversion,
              :price_aud => aud_currency_conversion,
              :price_gbp => gbp_currency_conversion,
              :price_cad => cad_currency_conversion,
              :price_nzd => nzd_currency_conversion,
              :revenue_eur => eur_revenue_currency_conversion,
              :revenue_aud => aud_revenue_currency_conversion,
              :revenue_gbp => gbp_revenue_currency_conversion,
              :revenue_cad => cad_revenue_currency_conversion,
              :revenue_nzd => nzd_revenue_currency_conversion,
              :status => average_change_interest,
              :legacy_id => u.id)
        end
    end

    puts "Successfully imported data from def"

end

1 个答案:

答案 0 :(得分:0)

这确实看起来很重复,为什么你不能想出这样的结构而不是将所有内容都放在格式不足

.
├── Rakefile
└── models
    ├── import.rb
    └── old_db.rb

我做了类似的事情,对于我从数据库中提取数据的项目之一,你也可以在这里引用https://github.com/ankit8898/sql-server-mapper