Class: GitWakaTime::DurationsCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/gitwakatime/durations_calculator.rb

Overview

Extract Duration Data from Heartbeats for the WAKATIME API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ DurationsCalculator

Returns a new instance of DurationsCalculator.



5
6
7
8
9
# File 'lib/gitwakatime/durations_calculator.rb', line 5

def initialize(args)
  return @heartbeats = args[:heartbeats] if args[:heartbeats]
  @args = args
  @heartbeats = []
end

Instance Attribute Details

#heartbeatsObject

Returns the value of attribute heartbeats.



4
5
6
# File 'lib/gitwakatime/durations_calculator.rb', line 4

def heartbeats
  @heartbeats
end

Instance Method Details

#heartbeats_to_durations(timeout = 15) ⇒ Object



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
# File 'lib/gitwakatime/durations_calculator.rb', line 11

def heartbeats_to_durations(timeout = 15)
  durations = []
  current = nil
  @heartbeats.each do |heartbeat|
    # the first heartbeat just sets state and does nothing
    unless current.nil?

      # get duration since last heartbeat
      duration = heartbeat.time.round - current.time.round

      duration = 0.0 if duration < 0

      # duration not logged if greater than the timeout
      if duration < timeout * 60

        # add duration to current heartbeat
        current.duration = duration

        # save to local db
        current.save

        # log current heartbeat as a duration
        durations << current
      end
    end
    # set state (re-start the clock)
    current = heartbeat
  end
  durations
end