Class: Did::Action::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/did/action/report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReport

Returns a new instance of Report.



9
10
# File 'lib/did/action/report.rb', line 9

def initialize
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



6
7
8
# File 'lib/did/action/report.rb', line 6

def format
  @format
end

#rangeObject

Returns the value of attribute range.



7
8
9
# File 'lib/did/action/report.rb', line 7

def range
  @range
end

Instance Method Details

#duration_to_s(duration) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/did/action/report.rb', line 58

def duration_to_s(duration)
  total_seconds = duration.floor
  milliseconds = duration - total_seconds
  seconds = total_seconds % 60
  minute_seconds = (total_seconds - seconds) % 3600
  hour_seconds = total_seconds - seconds - minute_seconds
  
  "%03d:%02d:%02d" %  [hour_seconds / 3600, minute_seconds / 60, seconds]
end

#performObject



12
13
14
15
16
17
18
19
# File 'lib/did/action/report.rb', line 12

def perform
  case @format
  when :timeline
    perform_timeline
  when :tags
    perform_tags
  end
end

#perform_tagsObject



25
26
27
# File 'lib/did/action/report.rb', line 25

def perform_tags
  tags_report(Span.find_for_day(@range))
end

#perform_timelineObject



21
22
23
# File 'lib/did/action/report.rb', line 21

def perform_timeline
  timeline_report(Span.find_for_day(@range))
end

#tags_report(spans) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/did/action/report.rb', line 44

def tags_report(spans)
  tag_totals = Hash.new{|hash, key| hash[key] = 0}
  spans.each do |span|
    span.tags.each do |tag|
      tag_totals[tag.label]+= span.duration
    end
  end
  max_length = tag_totals.keys.map(&:length).max
  tag_totals = tag_totals.sort{|a, b| a[1] <=> b[1]}.reverse
  tag_totals.each do |tag_label, duration|
    puts "#{tag_label.ljust(max_length + 3)}  #{duration_to_s(duration)}"
  end
end

#timeline_report(spans) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/did/action/report.rb', line 29

def timeline_report(spans)
  spans.each do |span|
    sitting_char = "|"
    if span.entire_sitting?
      sitting_char = "]"
    elsif span.sitting_start?
      sitting_char = "\\"
    elsif span.sitting_end?
      sitting_char = "/"
    end
    
    puts "#{span.start_time} - #{span.end_time} (#{duration_to_s(span.duration)}): #{sitting_char} #{span.tags.map{|t| t.label}.join(", ")}"
  end
end