Class: Herodot::Worklog

Inherits:
Object
  • Object
show all
Defined in:
lib/herodot/worklog.rb

Constant Summary collapse

END_TRACK_EVENTS =
[:work_end, :lunch_break_start, :after_last_dates_end].freeze
START_TRACK_EVNETS =
[:work_start, :lunch_break_end, :before_first_dates_start].freeze
EVENTS =
(END_TRACK_EVENTS + START_TRACK_EVNETS).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Worklog

Returns a new instance of Worklog.



7
8
9
10
11
12
# File 'lib/herodot/worklog.rb', line 7

def initialize(config)
  @raw_logs = []
  @branches = {}
  @dates = []
  @config = config
end

Instance Attribute Details

#branchesObject (readonly)

Returns the value of attribute branches.



2
3
4
# File 'lib/herodot/worklog.rb', line 2

def branches
  @branches
end

Instance Method Details

#actual_id(current_id, id) ⇒ Object



82
83
84
# File 'lib/herodot/worklog.rb', line 82

def actual_id(current_id, id)
  END_TRACK_EVENTS.include?(id) ? id : current_id || id
end

#add_entry(time, project_path, branch) ⇒ Object



14
15
16
17
18
19
# File 'lib/herodot/worklog.rb', line 14

def add_entry(time, project_path, branch)
  project = project_path.gsub(@config.projects_directory.to_s, '')
  id = "#{project}:#{branch}"
  @raw_logs << { time: time, id: id }
  @branches[id] = { branch: branch, project: project, path: project_path }
end

#branch(id) ⇒ Object



55
56
57
# File 'lib/herodot/worklog.rb', line 55

def branch(id)
  @branches.fetch(id, {})
end

#datesObject



59
60
61
# File 'lib/herodot/worklog.rb', line 59

def dates
  @raw_logs.map { |log| log[:time].to_date }.uniq.sort
end

#logs_with_eventsObject



21
22
23
24
25
26
27
# File 'lib/herodot/worklog.rb', line 21

def logs_with_events
  filtered_logs = @raw_logs.chunk { |x| x[:id] }.map(&:last).map(&:first)
  filtered_logs += work_time_events
  filtered_logs << { time: Time.new(0), id: :before_first_dates_start }
  filtered_logs << { time: Time.now, id: :after_last_dates_end }
  filtered_logs.sort_by { |log| log[:time] }
end

#logs_with_timesObject



29
30
31
32
33
34
35
36
37
# File 'lib/herodot/worklog.rb', line 29

def logs_with_times
  current_id = nil
  logs_with_events.each_cons(2).map do |log, following_log|
    current_id = log[:id] unless EVENTS.include?(log[:id])
    log.merge id: actual_id(current_id, log[:id]),
              time: time_between(log, following_log),
              date: log[:time].to_date
  end
end

#logs_with_times_cleanedObject



39
40
41
# File 'lib/herodot/worklog.rb', line 39

def logs_with_times_cleaned
  logs_with_times.reject { |log| EVENTS.include?(log[:id]) }
end

#same_date?(log_entry, other_log_entry) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/herodot/worklog.rb', line 73

def same_date?(log_entry, other_log_entry)
  log_entry[:time].to_date == other_log_entry[:time].to_date
end

#time_between(log_entry, following_entry) ⇒ Object



77
78
79
80
# File 'lib/herodot/worklog.rb', line 77

def time_between(log_entry, following_entry)
  return 0 unless same_date?(log_entry, following_entry)
  following_entry[:time] - log_entry[:time]
end

#totalsObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/herodot/worklog.rb', line 43

def totals
  grouped = logs_with_times_cleaned.group_by { |time| time[:date] }
  dates.map do |date|
    time_sums = grouped[date].each_with_object({}) do |time, sums|
      id = time[:id]
      sums[id] ||= { time: 0, **branch(id) }
      sums[id][:time] += time[:time]
    end
    [date, time_sums.values]
  end
end

#work_time_eventsObject



63
64
65
66
67
68
69
70
71
# File 'lib/herodot/worklog.rb', line 63

def work_time_events
  dates.flat_map do |date|
    @config.work_times.map { |event, (hour, minute)|
      time = Time.new(date.year, date.month, date.day, hour, minute)
      next if time > Time.now
      { id: event, time: time }
    }.compact
  end
end