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
|
# File 'lib/harvest_slack_report.rb', line 8
def self.fetch_harvest_data(from_date)
domain = ENV.fetch 'HARVEST_DOMAIN'
username = ENV.fetch 'HARVEST_USERNAME'
password = ENV.fetch 'HARVEST_PASSWORD'
puts "Collecting Harvest data for #{domain}..."
harvest = Harvest.hardy_client(subdomain: domain,
username: username,
password: password
)
ignore_users = if ENV['IGNORE_USERS'].present?
ENV['IGNORE_USERS'].split(',').map{ |user_id| user_id.to_i }
else
[]
end
people = harvest.users.all.select { |u| u.is_active? && !ignore_users.include?(u.id) }
projects = harvest.projects.all
puts 'Aggregating data...'
report = []
n_people = people.count
people.each_with_index do |person, i|
entries = harvest.reports.time_by_user(person.id, from_date, Time.now)
name = "#{person.first_name} #{person.last_name}"
if entries && entries.any?
total_hours = entries.map { |x| x.hours }.sum.round(2)
hours_by_project = entries.group_by { |x| x.project_id }.map do |project_id, es|
proj = projects.find { |pr| pr.id == project_id }
title = "#{proj.code.present? ? "[#{proj.code}] " : ''}#{proj.name}"
{ title: title, value: es.map { |h| h.hours }.sum.round(2), short: true }
end
color_code = case total_hours
when 0..2
"#D0021B"
when 2..4
"#F59423"
when 4..5
"#F8C61C"
else
"#72D321"
end
report << { fallback: "#{name} logged #{total_hours} hours",
text: "#{name} logged #{total_hours} hours",
fields: hours_by_project,
color: color_code
}
else
report << { fallback: "#{name} logged no time", text: "#{name} logged no time", color: "#4A4A4A" }
end
puts "#{i+1}/#{n_people}"
end
report
end
|