class GitWakaTime::DurationsCalculator

Extract Duration Data from Heartbeats for the WAKATIME API

Attributes

heartbeats[RW]

Public Class Methods

new(args) click to toggle source
# File lib/gitwakatime/durations_calculator.rb, line 5
def initialize(args)
  return @heartbeats = args[:heartbeats] if args[:heartbeats]
  @args = args
  @heartbeats = []
end

Public Instance Methods

heartbeats_to_durations(timeout = 15) click to toggle source
# 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