-
Notifications
You must be signed in to change notification settings - Fork 0
/
idonethis-report.rb
executable file
·80 lines (70 loc) · 2.06 KB
/
idonethis-report.rb
1
2
3
4
5
6
7
8
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
37
38
39
40
41
42
43
44
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
#!/usr/bin/env ruby
require 'date'
require 'rdoc'
include RDoc::Text
require 'bundler/inline'
gemfile do
source "https://rubygems.org"
gem 'idonethis'
gem 'netrc'
gem 'terminal-table'
end
given_date = ARGV.shift
user, token = Netrc.read['idonethis.com']
if user.nil? || token.nil?
warn "You need to store a user and token in your ~/.netrc file. Here's an example:"
warn <<~NETRC
machine idonethis.com
login <your iDoneThis email/username>
password <token from https://idonethis.com/api/token/>
NETRC
abort
end
period_end = Date.parse("2016-01-23")
current_period_end = given_date ? Date.parse(given_date) : Date.today
current_period_end -= 1 until (Date::DAYNAMES[current_period_end.wday] == "Saturday")
current_period_end += 7 unless ((current_period_end - period_end) % 14).zero?
current_period_start = current_period_end - 13
client = IDoneThis::Client.new(token)
request = {
done_date_after: "#{current_period_start}",
done_date_before: "#{current_period_end}",
page_size: 200,
page: 0,
}
dones = []
while
request[:page] += 1
response = client.dones(request)
dones += response['results']
break unless response['next']
end
dones = dones.group_by {|d| d['owner'] }
dones.each_value do |v|
v.map! do |done|
raw_text = wrap(done['raw_text'], 100)
time, desc = raw_text.split(' ', 2)
desc.gsub!(/&/, '&')
desc.gsub!(/&(quot|rquo|lquo);/, '"')
minutes = if (time =~ /hr?$/)
time[0..-2].to_f * 60
else
time[0..-2].to_f
end.to_i
hours = minutes./(60.0).round(2)
[done['done_date'], hours, desc]
end
v.sort_by!(&:first)
end
dones.each do |owner, rows|
total_hours = rows.reduce(0) {|s, d| s + d[1] }
minutes = (total_hours.modulo(1.0) * 60).round
rows.each{|r| r[1] = "%0.2f" % r[1] }
table = Terminal::Table.new headings: %w(Date Hours Description), rows: rows
table.title = "#{owner} (#{current_period_start}-#{current_period_end})"
table.add_separator
table.add_row ["", "%dh %dm" % [total_hours, minutes], "$#{"%0.2f" % (total_hours * 150)}"]
puts table
puts
puts
end