Sha256: 50e33fedb8a6314741ddca294eb416adc49cefff131e326075d0ad0ead5c4d6c

Contents?: true

Size: 1.47 KB

Versions: 23

Compression:

Stored size: 1.47 KB

Contents

require 'active_support/testing/time_helpers'

module Workarea
  class InsightsSeeds
    include ActiveSupport::Testing::TimeHelpers

    def perform
      puts 'Adding insights...'

      add_views
      add_searches
      add_orders

      process
    end

    def add_views
      Catalog::Product.limit(100).pluck(:id).each do |id|
        12.times do |weeks|
          travel_to weeks.weeks.ago
          rand(10).times { Metrics::ProductByDay.inc(key: { product_id: id }, views: 10) }
          travel_back
        end
      end

      Catalog::Category.limit(100).pluck(:id).each do |id|
        12.times do |weeks|
          travel_to weeks.weeks.ago
          rand(10).times { Metrics::CategoryByDay.inc(key: { category_id: id }, views: 10) }
          travel_back
        end
      end
    end

    def add_searches
      terms = Catalog::Product
        .asc(:name)
        .limit(20)
        .flat_map { |product| product.name.split(' ') }
        .uniq

      terms.each do |term|
        12.times do |weeks|
          travel_to weeks.weeks.ago

          rand(3).times { Metrics::SearchByDay.save_search(term, 0) }
          rand(3).times do
            Metrics::SearchByDay.save_search(term, rand(100))
          end

          travel_back
        end
      end
    end

    def add_orders
      Order.all.each_by(1000) { |order| SaveOrderMetrics.perform(order) }
    end

    def process
      ProcessProductRecommendations.new.perform
      GenerateInsights.generate_all!
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
workarea-core-3.5.5 app/seeds/workarea/insights_seeds.rb
workarea-core-3.4.27 app/seeds/workarea/insights_seeds.rb
workarea-core-3.5.4 app/seeds/workarea/insights_seeds.rb
workarea-core-3.4.26 app/seeds/workarea/insights_seeds.rb
workarea-core-3.5.3 app/seeds/workarea/insights_seeds.rb
workarea-core-3.4.25 app/seeds/workarea/insights_seeds.rb
workarea-core-3.5.2 app/seeds/workarea/insights_seeds.rb
workarea-core-3.4.24 app/seeds/workarea/insights_seeds.rb
workarea-core-3.5.1 app/seeds/workarea/insights_seeds.rb
workarea-core-3.4.23 app/seeds/workarea/insights_seeds.rb
workarea-core-3.4.22 app/seeds/workarea/insights_seeds.rb
workarea-core-3.5.0 app/seeds/workarea/insights_seeds.rb
workarea-core-3.4.21 app/seeds/workarea/insights_seeds.rb
workarea-core-3.5.0.beta.1 app/seeds/workarea/insights_seeds.rb
workarea-core-3.4.20 app/seeds/workarea/insights_seeds.rb
workarea-core-3.4.19 app/seeds/workarea/insights_seeds.rb
workarea-core-3.4.18 app/seeds/workarea/insights_seeds.rb
workarea-core-3.4.17 app/seeds/workarea/insights_seeds.rb
workarea-core-3.4.16 app/seeds/workarea/insights_seeds.rb
workarea-core-3.4.15 app/seeds/workarea/insights_seeds.rb