about summary refs log tree commit diff
path: root/app/lib/admin/metrics/measure/tag_accounts_measure.rb
blob: ef773081bed94f579a0b7280dc8552cc203e0b60 (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
# frozen_string_literal: true

class Admin::Metrics::Measure::TagAccountsMeasure < Admin::Metrics::Measure::BaseMeasure
  def self.with_params?
    true
  end

  def key
    'tag_accounts'
  end

  def total
    tag.history.aggregate(time_period).accounts
  end

  def previous_total
    tag.history.aggregate(previous_time_period).accounts
  end

  def data
    time_period.map { |date| { date: date.to_time(:utc).iso8601, value: tag.history.get(date).accounts.to_s } }
  end

  protected

  def tag
    @tag ||= Tag.find(params[:id])
  end

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

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

  def params
    @params.permit(:id)
  end
end