Class: Athar::Dashboard::Sparkline

Inherits:
Object
  • Object
show all
Defined in:
lib/athar/dashboard/sparkline.rb

Constant Summary collapse

DAYS =
14

Instance Method Summary collapse

Constructor Details

#initialize(model:, now: Time.current) ⇒ Sparkline

Returns a new instance of Sparkline.



8
9
10
11
# File 'lib/athar/dashboard/sparkline.rb', line 8

def initialize(model:, now: Time.current)
  @model = model
  @now = now
end

Instance Method Details

#bucketsObject

rubocop:disable Metrics/AbcSize



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/athar/dashboard/sparkline.rb', line 13

def buckets # rubocop:disable Metrics/AbcSize
  rows = connection.select_all(<<~SQL).to_a
    SELECT date_trunc('day', deleted_at) AS day, COUNT(*) AS n
    FROM #{Athar::DELETIONS_TABLE_NAME}
    WHERE deleted_at > #{quote(@now - DAYS.days)} #{model_scope}
    GROUP BY day ORDER BY day
  SQL

  by_day = rows.to_h do |row|
    [row["day"].to_date, row["n"].to_i]
  end

  Array.new(DAYS) do |index|
    day = (@now.to_date - (DAYS - 1 - index))
    by_day.fetch(day, 0)
  end
end