Class: Twstats::Runner

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

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/twstats/runner.rb', line 9

def initialize(file = nil)
  # Load interactive console
  @prompt = TTY::Prompt.new
  puts WELLCOME_MESSAGE.bright
  # Ask for the csv file
  file = read_file file
  # Check if the file exists
  @csv = CSVReader.new(file)
  @hourly_rate = HourlyRate.new(@csv)
  loop do
    option = @prompt.select('Choose an option', Twstats::MENU_CHOICES, cycle: true)
    case option
    when :stats
      show_stats_menu
    when :timesheet
      timesheet
    when :weekly
      show_weekly_report
    when :billing
      billing
    when :quit
      break
    else
      puts 'Option not recognized!'
    end
  end
  @current = nil
end

Instance Method Details

#billingObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/twstats/runner.rb', line 67

def billing
  add_non_billable = @prompt.yes? 'Do you want to include non-billable time?'
  @hourly_rate.set_user_hourly_rate(@prompt)
  if @prompt.yes? 'Was it a closed quote?', default: true
    quoted_price = @prompt.ask 'What was the quoted price for this project?', convert: :float
  end
  price = @hourly_rate.get_billed_time(user_hours_billed(add_non_billable))
  puts ' - Billed amount '.bright.blue + ' | ' + price.round(2).to_s + ' € '
  if quoted_price
    diff = quoted_price - price
    if diff >= 0
      puts " - The project has a proffit of: #{diff.round(2)} €, #{(diff * 100 / quoted_price).round(2)}".bright.green
    else
      puts " - The project shows a loss: #{diff.round(2)} €, #{(diff * 100 / quoted_price).round(2)}%".bright.red
    end
    alltime = @csv.get_total_time(:all, nil, !add_non_billable)
    flat_price = add_non_billable ? (alltime * 46).round(2) : (alltime[0] * 46).round(2)
    puts alltime, flat_price
    diff = quoted_price - flat_price
    if diff >= 0
      puts "The project is profitable using a flat rate of 46 €/hour by #{quoted_price - flat_price}".bright.green
    else
      puts "The project should have been quoted in #{flat_price} using the flat rate of 46 €/hour".bright.red
    end
  end
end


154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/twstats/runner.rb', line 154

def print_table(log_list)
  167.times { print '-' }
  puts "\n|" + 'Date'.center(12, ' ').bright + '|' + 'Time'.center(6, ' ').bright + '|' + 'Who'.center(20, ' ').bright +
       '|' + 'Description'.center(78, ' ').bright + '|' + 'Tags'.center(24, ' ').bright + '|' + 'Task'.center(20, ' ').bright + '|'
  167.times { print '-' }
  puts ''
  log_list.each do |log|
    puts table_line(log)
  end
  167.times { print '-' }
  puts ''
end

#ranking_from_not_tagged(list) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/twstats/runner.rb', line 145

def ranking_from_not_tagged(list)
  result = {}
  list[:list].each do |log|
    result[log.who] ||= 0
    result[log.who] += log.decimal_time
  end
  result.sort_by { |_k, v| v }.reverse.to_h
end

#read_file(file = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/twstats/runner.rb', line 38

def read_file(file = nil)
  # Check if the file exsis
  unless file.nil?
    if File.exist? file
      return file
    else
      file = @prompt.ask('The file you have supplied does not exsists. Try again or type .q to exit twstats.') do |input|
        input.modify :chomp
      end
      if file == '.q'
        exit 0
      else
        read_file file
      end
    end
  end
  # Now ask for the file if needed
  file = @prompt.ask('Specify the CSV file from a Teamwork time log export', default: 'exportTimeLog.csv') do |input|
    input.modify :chomp
  end
  read_file file
end

#section(text) ⇒ Object



214
215
216
217
# File 'lib/twstats/runner.rb', line 214

def section(text)
  puts ''
  puts (' ' + text + ' ').center(70, '*').bright.orange
end

#show_full_statsObject



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/twstats/runner.rb', line 176

def show_full_stats
  billable, non_billable = @csv.get_total_time(:all, nil, true)
  section 'Total time spent'
  puts ' - Billable      '.bright.blue + ' | ' + billable.round(2).to_s
  puts ' - Non-billable  '.bright.blue + ' | ' + non_billable.round(2).to_s
  section 'People involved'
  show_stats :people, true
  section 'Tags used'
  show_stats :tags, true
  show_not_tagged_section(@csv.logs, true)
  show_metrics
end

#show_metrics(logs = nil) ⇒ Object



219
220
221
222
223
224
225
226
227
228
# File 'lib/twstats/runner.rb', line 219

def show_metrics(logs = nil)
  logs = @csv.logs if logs.nil?
  stats = LogStats.new(logs)
  section 'Usefull metrics for the selected logs'
  puts "A total of #{logs.size} time logs have been analyzed."
  puts ' - Mean time logged:   '.bright.blue + stats.mean_log_time.round(2).to_s
  puts ' - Mean time per task: '.bright.blue + stats.mean_task_time(@csv.tasks).round(2).to_s
  puts ' - Maximum time logged: '.bright.blue + stats.max.to_s
  puts ' - Minimum time logged: '.bright.blue + stats.min.to_s
end

#show_not_tagged_section(logs, table) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/twstats/runner.rb', line 199

def show_not_tagged_section(logs, table)
  not_tagged = @csv.not_tagged_tasks logs
  unless not_tagged[:list].empty?
    section('Logs not tagged in this report')
    puts "A total of #{not_tagged[:list].size} logs have not been tagged:"
    puts " - A total time of #{not_tagged[:total_time]} is not tagged properly."
    puts ' - Impact by employee: '
    ranking_from_not_tagged(not_tagged).each do |user, time|
      puts "\t - #{user.ljust(30, ' ').bright.blue} | #{time}"
    end
    table = @prompt.yes?('Do you want to see what tasks have not been tagged?', default: true) if table.nil?
    print_table not_tagged[:list] if table
  end
end

#show_stats(obj, filter_billable = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/twstats/runner.rb', line 110

def show_stats(obj, filter_billable = nil)
  puts "Time logged vs #{obj}:" if filter_billable.nil?
  toshow = {}
  max = 0
  filter_billable ||= @prompt.yes?('Do you want to filter billable and non-billable time?', default: true)
  do_all = @prompt.yes? 'Do you want to analyze all the elements?', default: true
  objects = @csv.send(obj)
  list = objects
  if objects.size > 1 && !do_all
    list = @prompt.multi_select "Which ones of the following do you want to see? Select or unselect them with the space key\n", filter: true do |menu|
      menu.choices objects
    end
  end
  selected_logs = []
  list.each do |element|
    toshow[element] = @csv.get_total_time(obj, element, filter_billable)
    max = element.size if max < element.size
    selected_logs += @csv.get_logs(obj, element)
  end
  toshow.sort_by { |_k, v| v }.reverse.to_h.each do |k, v|
    if filter_billable
      if v[0].zero?
        puts " - #{k.ljust(max, ' ').bright.blue} | #{'Non-billable:'.bright} #{v[1].round(2)}" # unless v[1].zero?
      else
        puts " - #{k.ljust(max, ' ').bright.blue} | #{'Billable:'.ljust(13, ' ').bright} #{v[0].round(2)} (#{((v[0] / (v[0] + v[1])) * 100).round(2)} %)"
        puts "   #{''.ljust(max, ' ').bright.blue} | #{'Non-billable:'.bright} #{v[1].round(2)}" unless v[1].zero?
      end
    else
      puts " - #{k.ljust(max, ' ').bright.blue} | #{v.round(2)}" unless v.zero?
    end
  end
  show_not_tagged_section selected_logs, false
  show_metrics selected_logs
end

#show_stats_menuObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/twstats/runner.rb', line 94

def show_stats_menu
  loop do
    option = @prompt.select('Select what time logging stats you want to see', Twstats::STATS_MENU_CHOICES, cycle: true)
    case option
    when :projects, :people, :tags, :tasks
      show_stats option
    when :fullstats
      show_full_stats
    when :back
      return
    else
      puts 'Option not recognized'
    end
  end
end

#show_weekly_reportObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/twstats/runner.rb', line 230

def show_weekly_report
  unless @csv.is_weekly?
    puts 'The CSV file provided has logged times that differ more than a week.'
    unless @prompt.ask 'Are you sure you want to continue?', default: true
      return
    end
  end
  hours = @prompt.ask 'What is the weekly amount of hours worked?', default: 40, convert: :int
  info = {}
  @csv.people.each do |person|
    billable, non_billable = @csv.get_total_time(:people, person, true)
    info[person] = { rate: billable * 100 / hours,
                     not_billed: hours - billable - non_billable,
                     billable: billable,
                     non_billable: non_billable }
  end
  info.sort_by { |_x, v| v[:rate] }.reverse_each do |person, data|
    puts ' - ' + person.bright.blue
    puts "\tBillable time: ".ljust(20, ' ').bright + data[:billable].round(2).to_s
    puts "\tBillable rate: ".ljust(20, ' ').bright + data[:rate].round(2).to_s + ' % '
    puts "\tNot logged time: ".ljust(20, ' ').bright + data[:not_billed].round(2).to_s
  end
end

#table_line(log) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/twstats/runner.rb', line 167

def table_line(log)
  '|' + log.date.strftime('%d/%m/%Y').to_s.truncate(10).center(12, ' ') +
    '|' + log.decimal_time.round(2).to_s.truncate(4).center(6, ' ') +
    '|' + log.who.to_s.truncate(18).center(20, ' ') +
    '|' + log.description.to_s.truncate(76).ljust(78, ' ').tr("\n", ' ') +
    '|' + log.tags.join(', ').to_s.truncate(22).center(24, ' ') +
    '|' + log.task.to_s.truncate(18).center(20, ' ') + '|'
end

#timesheetObject



61
62
63
64
65
# File 'lib/twstats/runner.rb', line 61

def timesheet
  ## Takes care of using the current CSV file to be imported into timesheets.
  # Makes it easier to do it than manually going through all of them
  TimeSheetExport.new(@csv, @prompt).execute
end

#user_hours_billed(nonb) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/twstats/runner.rb', line 189

def user_hours_billed(nonb)
  uh = {}
  @csv.people.each do |user|
    val = @csv.get_total_time :people, user, !nonb
    uh[user] = val
    uh[user] = val[0] unless nonb
  end
  uh
end