Class: Obst::GroupByDays

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/obst/group_by_days.rb

Constant Summary collapse

ONE_DAY =
60 * 60 * 24

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ GroupByDays

Returns a new instance of GroupByDays.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/obst/group_by_days.rb', line 10

def initialize(**opts)
  duration = ONE_DAY * (opts[:days] || 1)
  latest = opts[:before] ? Time.parse(opts[:before]) : Time.parse(Time.now.strftime('%F 23:59:59'))

  @timeline = Enumerator.new do |y|
    curr = latest
    loop do
      y << curr.strftime('%F')
      curr -= duration
    end
  end

  @log = PackLog.new(**opts) do |committed_at|
    that_time = Time.parse(committed_at)
    n_durations = ((latest - that_time) / duration).to_i
    n_durations_before = latest - (n_durations * duration)
    n_durations_before.strftime('%F')
  end.to_enum
end

Instance Method Details

#each(&block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/obst/group_by_days.rb', line 30

def each(&block)
  return self unless block

  current_time = @timeline.next
  record = @log.next

  loop do
    break unless record

    if record.time == current_time
      block.call(record)
      record = @log.next
    else
      block.call(PackLog::Record.new(current_time, {}))
    end

    current_time = @timeline.next
  end
end