Class: BBNW::TimeSink

Inherits:
Object
  • Object
show all
Includes:
Activities, DateTimeUtils
Defined in:
lib/adapters/time_sink.rb

Constant Summary collapse

APPLICATION =
0
START_TIME =
1
END_TIME =
2
TIME_SINK_HEADERS =
{'Application'                  => APPLICATION,
'Foreground Begin (Unix Time)' => START_TIME,
'Foreground End (Unix Time)'   => END_TIME}

Instance Attribute Summary

Attributes included from Activities

#activities

Instance Method Summary collapse

Methods included from DateTimeUtils

#is_number?, #long_date, #long_date_time, #long_time, #parse_time, #short_date, #short_time, #time_description

Constructor Details

#initialize(file_path, start_date, end_date, debug = false) ⇒ TimeSink

Returns a new instance of TimeSink.



21
22
23
24
25
26
# File 'lib/adapters/time_sink.rb', line 21

def initialize(file_path, start_date, end_date, debug = false)
  @files      = Dir.glob(file_path)
  @debug      = debug
  @start_date =  Time.new(start_date.year, start_date.month, start_date.day, 0, 0, 0)
  @end_date   =  Time.new(end_date.year, end_date.month, end_date.day, 23, 59, 59)
end

Instance Method Details

#columns(header) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/adapters/time_sink.rb', line 32

def columns(header)
  hs = {}
  TIME_SINK_HEADERS.each do |k,v|
    header.each_with_index do |header_item,i|
      if header_item == k
        hs[v] = i
        break
      end
    end
  end
  hs
end

#get_activitiesObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/adapters/time_sink.rb', line 45

def get_activities
  activities = []
  prev_e     = nil
  blank_time = Time.at(0.0)

  @files.each do |file|
    csv     = CSV::parse(File.open(file) { |f| f.read })
    headers = csv.shift
    cols    = columns(headers)

    csv.each_with_index do |row, i|
      activity    = row[cols[APPLICATION]]
      s           = row[cols[START_TIME]]
      e           = row[cols[END_TIME]]

      next unless prev_e.nil? ||  s >= prev_e

      start_time  = parse_time(s)
      #next if start_time == blank_time

      end_time    = parse_time(e)
      #next if end_time == blank_time

      next if end_time < @start_date
      next if start_time > @end_date
      #start_time = @start_date if start_time < @start_date && end_time < @end_date
      #end_time   = @end_date if start_time > @start_date && end_time > @end_date

      time_entry  = BBNW::Activity.new(activity, start_time, end_time)
      activities << time_entry

      prev_e      = e
    end
  end
  activities
end

#log(message) ⇒ Object



28
29
30
# File 'lib/adapters/time_sink.rb', line 28

def log(message)
  puts message if @debug
end