Sha256: 9719fd73a1c3b574012ddc13fc0de0647111c35fd38d42a1e7209145de3ca39c

Contents?: true

Size: 1.75 KB

Versions: 49

Compression:

Stored size: 1.75 KB

Contents

module Workarea
  module Insights
    class CustomerAcquisition < Base
      class << self
        def dashboards
          %w(people marketing)
        end

        def generate_monthly!
          aggregation_results = find_results

          results = Array.new(days_last_month) do |i|
            result = aggregation_results.detect { |r| r['_id']['day'] == i + 1 } || {}

            {
              new_customers: result['new_customers'] || 0,
              date: Time.zone.local(
                beginning_of_last_month.year,
                beginning_of_last_month.month,
                i + 1
              )
            }
          end

          create!(results: results) if results.present?
        end

        def days_last_month
          (end_of_last_month.to_date - beginning_of_last_month.to_date).to_i + 1
        end

        def find_results
          Metrics::User.collection.aggregate([filter_date_range, group_by_time]).to_a
        end

        def filter_date_range
          {
            '$match' => {
              'first_order_at' => {
                '$gte' => beginning_of_last_month.utc,
                '$lte' => end_of_last_month.utc
              }
            }
          }
        end

        def group_by_time
          {
            '$group' => {
              '_id' => {
                'year' => { '$year' => first_order_at_in_time_zone },
                'month' => { '$month' => first_order_at_in_time_zone },
                'day' => { '$dayOfMonth' => first_order_at_in_time_zone }
              },
              'new_customers' => { '$sum' => 1 }
            }
          }
        end

        def first_order_at_in_time_zone
          { 'date' => '$first_order_at', 'timezone' => Time.zone.tzinfo.name }
        end
      end
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.5.26 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.4.45 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.5.25 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.5.23 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.4.44 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.5.22 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.4.43 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.5.21 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.4.42 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.5.20 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.4.41 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.5.19 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.4.40 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.5.18 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.4.39 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.5.17 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.4.38 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.5.16 app/models/workarea/insights/customer_acquisition.rb
workarea-core-3.4.37 app/models/workarea/insights/customer_acquisition.rb