Class: Extractors::Change

Inherits:
Base
  • Object
show all
Defined in:
lib/prosperity/extractors/change.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.keyObject



3
4
5
# File 'lib/prosperity/extractors/change.rb', line 3

def self.key
  'change'
end

Instance Method Details

#to_aObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/prosperity/extractors/change.rb', line 7

def to_a
  data = []

  period.each_period(start_time, end_time) do |start_time|
    if metric.sql?
      new = count_up_to_date_with_sql(start_time)
      last = count_up_to_date_with_sql(start_time - period.duration)
    elsif metric.ruby?
      new = metric.value_at.call(start_time, period)
      last = metric.value_at.call(start_time - period.duration, period)
    else
      new = aggregate.apply(scope.where("#{metric.group_by} < ?", start_time))
      last = aggregate.apply(scope.where("#{metric.group_by} < ?", start_time - period.duration))
    end

    change = if last && last > 0
               ((new.to_f / last) - 1.0) * 100
             else
               0.0
             end

    data << change
  end

  data
end