This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.rb
194 lines (170 loc) · 4.82 KB
/
app.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# frozen_string_literal: true
require 'sinatra'
require 'sequel'
require 'terminal-table'
require 'date'
require 'slack-notifier'
require_relative 'db'
use Rack::Auth::Basic, 'Protected Area' do |username, password|
username == 'admin' && Digest::SHA2.hexdigest(password) == ENV['TIME_CARD_ADMIN_PASSWORD_HASH']
end
class Report
def initialize(entries_by_worker, description: '')
@entries_by_worker = entries_by_worker
@description = description
end
def self.monthly(year:, month:)
new(
entries('EXTRACT(year FROM date)::INTEGER = ? AND EXTRACT(month FROM date)::INTEGER = ?', year, month),
description: "#{'%04d' % year}-#{'%02d' % month}"
)
end
def self.bi_weekly(year:, month:, day:)
period_end = Date.new(2016, 1, 23)
current_period_end = Date.new(year, month, day)
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
new(
entries('date >= ? AND date <= ?', current_period_start, current_period_end),
description: "#{current_period_start} to #{current_period_end}"
)
end
def self.entries(*query)
Entry
.where(*query)
.order_by(:worker_id, :date)
.enum_for
.group_by(&:worker_id)
.values
end
def all
@entries_by_worker.map do |entries|
[entries.first.worker, entries]
end
end
def to_a
all.map do |worker, entries|
{
worker: worker.to_hash,
entries: entries,
minutes: entries.reduce(0) { |acc, elem| acc + elem.minutes }
}
end
end
def to_text
all.reduce(+'') do |text, (worker, entries)|
table = Terminal::Table.new headings: %w[Date Hours Description]
table.title = "#{worker.user_name} (#{@description})"
total_minutes = 0
add_row = ->(date, minutes, description) { table.add_row [date&.to_date, '%dh %dm' % [minutes./(60.0), minutes.modulo(60)], description] }
entries.each do |e|
total_minutes += e.minutes
add_row[e.date, e.minutes, word_wrap(e.message)]
end
table.add_separator
add_row[nil, total_minutes, "$#{'%0.2f' % (total_minutes * 2.5)}"]
text << table.to_s << "\n\n"
end
end
def word_wrap(text, line_width: 80, break_sequence: "\n")
text.split("\n").collect! do |line|
line.length > line_width ? line.gsub(
/(.{1,#{line_width}})(\s+|$)/,
"\\1#{break_sequence}"
).strip : line
end * break_sequence
end
end
def notify_slack!(entry)
return unless url = ENV['SLACK_NOTIFICATION_WEBHOOK_URL']
notifier = Slack::Notifier.new(url, username: 'time_card')
attachments = [
{
text: notifier.escape(entry.message),
color: 'good',
fields: [
{
title: 'worker',
value: entry.worker.user_name,
short: true
},
{
title: 'date',
value: entry.date.to_date,
short: true
},
{
title: 'time',
value: '%dh %dm' % [entry.minutes./(60.0), entry.minutes.modulo(60)],
short: true
}
]
}
]
notifier.ping attachments: attachments
end
get '/' do
content_type 'application/json'
Entry.all.to_json
end
get '/report/monthly/:year-:month.?:format?' do
year = params[:year].to_i
month = params[:month].to_i
report = Report.monthly(year: year, month: month)
case params[:format]
when 'json'
content_type 'application/json'
content_type :json
report.to_a.to_json
else
content_type :text
report.to_text
end
end
get '/report/biweekly/:year-:month-:day.?:format?' do
year, month, day = params.values_at(:year, :month, :day).map(&:to_i)
report = Report.bi_weekly(year: year, month: month, day: day)
case params[:format]
when 'json'
content_type 'application/json'
content_type :json
report.to_a.to_json
else
content_type :text
report.to_text
end
end
post '/entries' do
data = JSON.parse(request.body.read)
data['worker_id'] = Worker.find_or_create(user_name: data.delete('worker')).id
data['date'] ||= DateTime.now
e = Entry.create data
notify_slack!(e)
redirect "/entries/#{e.id}"
end
get '/entries/:id' do
content_type 'application/json'
Entry[params[:id]].to_json(include: :worker)
end
put '/entries/:id' do
content_type 'application/json'
Entry[params[:id]].update(JSON.parse(request.body.read))
end
delete '/entries/:id' do
Entry[params[:id]].delete
end
get '/workers/:id' do
content_type 'application/json'
Worker[params[:id]].to_json(include: :entries)
end
put '/workers/:id' do
content_type 'application/json'
worker = Worker[params[:id]]
worker.update(JSON.parse(request.body.read))
worker.to_json
end
get '/workers' do
content_type 'application/json'
Worker.all.to_json
end