Sha256: a8c83ba21b2d8c4a848081332ae376592f3bbe4b06244ec819d5f070b8240d97

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'open3'
require 'date'

module Tyme
  class Last
    def initialize( last_output = nil )
      if last_output
        @stdin = nil
        @stdout = last_output
        @stderr = nil
      else
        @stdin, @stdout, @stderr = Open3.popen3('last --time-format iso -R | grep tty')
      end
    end

    def process
      lines = @stdout.readlines
      return nil if lines.empty?
      data = lines.select { |line| not line.include? 'gone' }.map { |line| parse_line(line) }
      consolidate(data)
    end

    private
      def consolidate( data )
        ret={}
        data.each do |elt|
          user = elt[:user]
          date = elt[:date]
          ret[user] ||= {}
          ret[user][date] ||= 0
          ret[user][date] += elt[:duration] 
        end
        ret
      end
      
      def parse_line( line )
        values = line.scan( /(\S+)\s+tty\d+\s+(\S+)\s+-?\s+\S+\s+\((\d+):(\d+)\)/ )[0]
        date = DateTime.parse( values[1] ).strftime('%F')
        { user: values[0].to_sym,  date: date.to_sym, duration: values[2].to_i*60 + values[3].to_i }
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tyme-0.1.4 lib/tyme/last.rb