Class: Stats

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

Overview

Aggregates super useless stats given an instance of Month.

Stats.new(month).to_s

might return something like this:

Total hours:           35:30
Money made:            0
Total days:            7
Total blocks:          12
Avg hours per day:     05:04
Avg hours per block:   02:57
Longest day:           11:00
Longest block:         09:00
Most blocks in day:    3
Late nights:           0
Early mornings:        0
Consecutive days:      3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(month) ⇒ Stats


23
24
25
# File 'lib/stats.rb', line 23

def initialize(month)
  @month = month
end

Instance Attribute Details

#monthObject

Returns the value of attribute month


21
22
23
# File 'lib/stats.rb', line 21

def month
  @month
end

Instance Method Details

#average_hours_per_blockObject


69
70
71
72
# File 'lib/stats.rb', line 69

def average_hours_per_block
  return 0 if blocks.empty?
  Totals.format(month.total / total_blocks)
end

#average_hours_per_dayObject


64
65
66
67
# File 'lib/stats.rb', line 64

def average_hours_per_day
  return 0 if days.empty?
  Totals.format(month.total / total_days)
end

#average_hours_per_workday_remainingObject


95
96
97
98
99
100
101
# File 'lib/stats.rb', line 95

def average_hours_per_workday_remaining
  if workdays_left.zero?
    Totals.format remaining
  else
    Totals.format(remaining / workdays_left)
  end
end

#blocksObject


154
155
156
# File 'lib/stats.rb', line 154

def blocks
  @blocks ||= month.blocks
end

#configObject


146
147
148
# File 'lib/stats.rb', line 146

def config
  Punch.config
end

#consecutive_daysObject

Work days streak.


75
76
77
78
79
80
81
82
83
# File 'lib/stats.rb', line 75

def consecutive_days
  max = 0
  days[0..days.size - 2].each do |d|
    i = 1
    i += 1 while (d = d.next_day(month))
    max = i if i > max
  end
  max
end

#daysObject


150
151
152
# File 'lib/stats.rb', line 150

def days
  @days ||= month.days
end

#early_morningsObject

Number of blocks where the #start is before 8 am and #finish is after it.


49
50
51
52
53
54
# File 'lib/stats.rb', line 49

def early_mornings
  blocks.count do |b|
    eight = eight_am b.day
    b.start <= eight && b.finish > eight
  end
end

#eight_am(day) ⇒ Object


166
167
168
# File 'lib/stats.rb', line 166

def eight_am(day)
  Time.new(day.year, day.month, day.day, 8)
end

#hourly_payObject


142
143
144
# File 'lib/stats.rb', line 142

def hourly_pay
  @hourly_pay ||= config.hourly_pay
end

#label(str) ⇒ Object


162
163
164
# File 'lib/stats.rb', line 162

def label(str)
  "#{str}:".ljust(29).highlighted
end

#late_nightsObject

Number of blocks where #start and #finish are on different days.


44
45
46
# File 'lib/stats.rb', line 44

def late_nights
  blocks.count(&:over_midnight?)
end

#longest_blockObject


31
32
33
# File 'lib/stats.rb', line 31

def longest_block
  Totals.format blocks.map(&:total).max
end

#longest_dayObject


27
28
29
# File 'lib/stats.rb', line 27

def longest_day
  Totals.format days.map(&:total).max
end

#money_madeObject


158
159
160
# File 'lib/stats.rb', line 158

def money_made
  (hourly_pay / 3600.0 * reached).round 2
end

#monthly_goalObject


130
131
132
# File 'lib/stats.rb', line 130

def monthly_goal
  @monthly_goal ||= config.monthly_goal * 3600
end

#most_blocksObject


35
36
37
# File 'lib/stats.rb', line 35

def most_blocks
  days.map(&:block_count).max || 0
end

#nowObject


170
171
172
# File 'lib/stats.rb', line 170

def now
  @now ||= Time.now
end

#percentageObject


122
123
124
# File 'lib/stats.rb', line 122

def percentage
  (100.0 / monthly_goal * reached).round 2
end

#progressObject


85
86
87
88
89
# File 'lib/stats.rb', line 85

def progress
  diff = Totals.format(remaining)
  "#{percentage} % | #{Totals.format reached}/#{config.monthly_goal} | "\
    "Diff: #{diff}"
end

#reachedObject


134
135
136
# File 'lib/stats.rb', line 134

def reached
  @reached ||= month.total
end

#remainingObject


138
139
140
# File 'lib/stats.rb', line 138

def remaining
  @remaining ||= monthly_goal - reached
end

#to_sObject


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/stats.rb', line 103

def to_s
  <<-EOS
#{label "Progress"}#{progress}
#{label "Workdays left"}#{workdays_left}
#{label "Hours per workday remaining"}#{average_hours_per_workday_remaining}
#{label "Money made"}#{total_money_made}
#{label "Avg hours per day"}#{average_hours_per_day}
#{label "Avg hours per block"}#{average_hours_per_block}
#{label "Longest day"}#{longest_day}
#{label "Longest block"}#{longest_block}
#{label "Most blocks in day"}#{most_blocks}
#{label "Late nights"}#{late_nights}
#{label "Early mornings"}#{early_mornings}
#{label "Consecutive days"}#{consecutive_days}
#{label "Total days"}#{total_days}
#{label "Total blocks"}#{total_blocks}
  EOS
end

#total_blocksObject


60
61
62
# File 'lib/stats.rb', line 60

def total_blocks
  blocks.count
end

#total_daysObject


56
57
58
# File 'lib/stats.rb', line 56

def total_days
  days.count
end

#total_money_madeObject


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

def total_money_made
  "CHF #{money_made}"
end

#workdaysObject


126
127
128
# File 'lib/stats.rb', line 126

def workdays
  @workdays ||= month.workdays
end

#workdays_leftObject


91
92
93
# File 'lib/stats.rb', line 91

def workdays_left
  @worddays_left = workdays.count { |d| d > now }
end