Sha256: 6b1cdce5a8c96647e741a531599452136ad477cc90bc71d8430fb93270715382

Contents?: true

Size: 1.7 KB

Versions: 34

Compression:

Stored size: 1.7 KB

Contents

require 'active_support/testing/time_helpers'

namespace :workarea do
  namespace :insights do
    desc 'Creates metrics and insights based on orders'
    task generate: :environment do
      include ActiveSupport::Testing::TimeHelpers
      batch_size = ENV['WORKAREA_INSIGHTS_BATCH_SIZE'].presence || 1000

      Workarea::Order
        .placed
        .each_by(batch_size.to_i) { |o| Workarea::SaveOrderMetrics.perform(o) }

      8.times do |i|
        travel_to (i.weeks.ago.beginning_of_week + 1.hour)
        Workarea::GenerateInsights.generate_all!
      end

      puts "Success! Generated #{Workarea::Insights::Base.count} insights."
    end

    # Clear the metrics/insights environment - deletes lots of data, this task
    # is very dangerous! Useful for testing/debugging.
    task reset: :environment do
      Workarea::Order
        .where(:metrics_saved_at.gt => 0)
        .update_all(metrics_saved_at: nil)


      Workarea::Metrics::CategoryByDay.delete_all
      Workarea::Metrics::CountryByDay.delete_all
      Workarea::Metrics::DiscountByDay.delete_all
      Workarea::Metrics::MenuByDay.delete_all
      Workarea::Metrics::ProductByDay.delete_all
      Workarea::Metrics::ProductByWeek.delete_all
      Workarea::Metrics::ProductForLastWeek.delete_all
      Workarea::Metrics::SalesByDay.delete_all
      Workarea::Metrics::SearchByDay.delete_all
      Workarea::Metrics::SearchByWeek.delete_all
      Workarea::Metrics::SearchForLastWeek.delete_all
      Workarea::Metrics::SkuByDay.delete_all
      Workarea::Metrics::TrafficReferrerByDay.delete_all
      Workarea::Metrics::User.delete_all
      Workarea::Insights::Base.delete_all

      puts "Success! Insights and metrics have been cleared."
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
workarea-core-3.4.45 lib/tasks/insights.rake
workarea-core-3.4.44 lib/tasks/insights.rake
workarea-core-3.4.43 lib/tasks/insights.rake
workarea-core-3.4.42 lib/tasks/insights.rake
workarea-core-3.4.41 lib/tasks/insights.rake
workarea-core-3.4.40 lib/tasks/insights.rake
workarea-core-3.4.39 lib/tasks/insights.rake
workarea-core-3.4.38 lib/tasks/insights.rake
workarea-core-3.4.37 lib/tasks/insights.rake
workarea-core-3.4.36 lib/tasks/insights.rake
workarea-core-3.4.35 lib/tasks/insights.rake
workarea-core-3.4.34 lib/tasks/insights.rake
workarea-core-3.4.33 lib/tasks/insights.rake
workarea-core-3.4.32 lib/tasks/insights.rake
workarea-core-3.4.31 lib/tasks/insights.rake
workarea-core-3.4.30 lib/tasks/insights.rake
workarea-core-3.4.29 lib/tasks/insights.rake
workarea-core-3.4.28 lib/tasks/insights.rake
workarea-core-3.4.27 lib/tasks/insights.rake
workarea-core-3.4.26 lib/tasks/insights.rake