about summary refs log tree commit diff
path: root/app/lib/admin/metrics/measure/base_measure.rb
blob: 4c336a69e689c82e1a1661fe226be7aafa10490d (plain) (blame)
1
2
3
4
5
6
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

class Admin::Metrics::Measure::BaseMeasure
  def initialize(start_at, end_at)
    @start_at = start_at&.to_datetime
    @end_at   = end_at&.to_datetime
  end

  def key
    raise NotImplementedError
  end

  def total
    raise NotImplementedError
  end

  def previous_total
    raise NotImplementedError
  end

  def data
    raise NotImplementedError
  end

  def self.model_name
    self.class.name
  end

  def read_attribute_for_serialization(key)
    send(key) if respond_to?(key)
  end

  protected

  def time_period
    (@start_at...@end_at)
  end

  def previous_time_period
    ((@start_at - length_of_period)...(@end_at - length_of_period))
  end

  def length_of_period
    @length_of_period ||= @end_at - @start_at
  end
end