Sha256: 917b8da88a68eb3158d2c5e075c3142ab9e5620c8dce2993a2e397cc46c94079

Contents?: true

Size: 1.18 KB

Versions: 62

Compression:

Stored size: 1.18 KB

Contents

module Workarea
  module Metrics
    module RevenueChange
      extend ActiveSupport::Concern

      included do
        field :revenue, type: Float, default: 0.0
        field :previous_week_revenue, type: Float, default: 0.0
        field :revenue_change, type: Float, default: 0.0

        index(revenue_change: 1)

        scope :top_sellers, -> { desc(:revenue) }
        scope :improved_revenue, -> { where(:revenue_change.gt => 0) }
        scope :declined_revenue, -> { where(:revenue_change.lt => 0) }
      end

      module ClassMethods
        def revenue_change_median
          sort = if scoped.selector.merge(declined_revenue.selector) == scoped.selector
            :desc
          else
            :asc
          end

          skip = (scoped.count / 2.to_f).floor
          skip = skip < 0 ? 0 : skip
          scoped.order_by(revenue_change: sort).skip(skip).first&.revenue_change.to_i
        end

        def revenue_change_standard_deviation
          grouped = scoped.group(id: nil, result: { '$stdDevPop' => '$revenue_change' })
          results = collection.aggregate(grouped.pipeline).to_a
          results.empty? ? 0 : results.first['result']
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.4.13 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.4.12 app/models/workarea/metrics/revenue_change.rb