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.5.27 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.5.26 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.4.45 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.5.25 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.5.23 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.4.44 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.5.22 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.4.43 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.5.21 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.4.42 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.5.20 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.4.41 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.5.19 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.4.40 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.5.18 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.4.39 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.5.17 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.4.38 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.5.16 app/models/workarea/metrics/revenue_change.rb
workarea-core-3.4.37 app/models/workarea/metrics/revenue_change.rb