2014-09-29 85 views
0

我正在處理的應用程序每晚都會運行導入任務以從外部數據庫和進程獲取信息並將其寫入本地PostgreSQL數據庫。這是可以在我們的應用程序中查看的。如何將這些多個rake任務合併爲一個任務

我們做到以下幾點:

  • 創建到外部數據庫
  • 的連接進行一些貨幣轉換
  • 請在基於平均
  • 檢查數據的一些計算,如果已經在項目存在並更新(如果已更改)
  • 同時檢查它是如何分類的,並根據我們的分類更改分類
  • 創建項目,如果不存在

所有這些數據是在外部數據庫不同的表,所以我會爲每個表(約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 

回答

0

這的確看起來重複的,你爲什麼不能拿出這樣的結構,而不是把所有的東西在下面的格式

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

我做了類似的事情,我的項目之一在這裏我從數據庫中提取數據,你也可以在這裏參考它https://github.com/ankit8898/sql-server-mapper

+0

嘿非常感謝你的回覆。我正在使用Heroku Scheduler來運行導入任務,因此它們都在一個名爲scheduler.rake的文件中。所以您認爲我應該專門創建這些模型?然後從耙子任務中調用它們? – bnussey 2014-09-29 20:27:17

+0

@bnussey,是的,我更喜歡把它放在不同的空間裏。因此,您可以創建模型並在.rake中要求它們。你可以在上面的網址 – AnkitG 2014-09-29 20:28:20

+0

中查看回購請注意。我是在尋找某種方式來幹掉整個任務,就像它正在做的所有事情,任何想法一樣?也許一次寫腳本然後以某種方式循環通過不同的模型 – bnussey 2014-09-29 20:31:23

相關問題