Module: Workarea::Metrics::RevenueChange::ClassMethods

Defined in:
app/models/workarea/metrics/revenue_change.rb

Instance Method Summary collapse

Instance Method Details

#revenue_change_medianObject



19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/workarea/metrics/revenue_change.rb', line 19

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

#revenue_change_standard_deviationObject



31
32
33
34
35
# File 'app/models/workarea/metrics/revenue_change.rb', line 31

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