Sha256: 9bb83ddccd18505e008bc250f83a8f00359e6aaebf6222fd3072d34de1566cf9

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

module GitWakaTime
  # Extract Duration Data from Actions for the WAKATIME API
  class Actions
    attr_accessor :actions
    def initialize(args)
      return @actions = args[:actions] if args[:actions]
      fail if args[:project].nil?
      @project = args[:project]
      @args = args
      @session     = Wakatime::Session.new(api_key: GitWakaTime.config.api_key)
      @client      = Wakatime::Client.new(@session)
      load_actions
    end

    def load_actions
      Log.new "querying WakaTime actions for #{@project}"
      time = Benchmark.realtime do
        @actions = @client.actions(@args)
        # remove returned actions that do not have the project we want
        @actions =  @actions.keep_if do  |a|
          a['project'] == @project
        end

      end
      Log.new "API took #{time}s"
      @actions
    end

    def actions_to_durations(_project = nil, timeout = 15)
      durations = []
      current = []
      @actions.each do | action |
        # the first action just sets state and does nothing
        unless current.empty?

          # get duration since last action
          duration = action.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 action
            current['duration'] = duration

            # log current action as a duration
            durations << current
          end
        end
        # set state (re-start the clock)
        current = action
        current.delete('id')

      end
      durations
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gitwakatime-0.0.3 lib/gitwakatime/actions.rb
gitwakatime-0.0.2 lib/gitwakatime/actions.rb