-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkimai
executable file
·251 lines (226 loc) · 6.4 KB
/
kimai
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env bash
set -euo pipefail
shopt -s inherit_errexit
# shellcheck source=/home/janne/.config/kimai
. ~/.config/kimai
# kimaiURL=
# token=
# projectID=
# activityID=
# sickDays=
# hoursPerDay=
# holidayActivity=
# Run an authenticated curl
_curl() {
curl -sfH 'Content-Type: application/json' -H "Authorization: Bearer ${token}" "${@}"
}
# Return the ID of the currently active timesheet or "" if
# there is none
getActiveID() {
local json
json="$(_curl -X GET "${kimaiURL}/api/timesheets/active")"
[[ "$(jq length <<< "${json}")" = 0 ]] && return # No active timesheet
jq '.[].id' <<< "${json}"
}
# Return the edit URL of a timesheet by ID
editURL() {
local id="${1}"
echo "${kimaiURL}/en/timesheet/${id}/edit"
}
# Opens $EDITOR with the current description
editDescription() {
local id="${1}"
local desc newJSON
tmpfile="$(mktemp)"
trap 'rm "${tmpfile}"' INT TERM EXIT
# Get the current description
desc="$(_curl -X GET "${kimaiURL}/api/timesheets/${id}" | jq -r .description)"
if [[ "${desc}" == null ]]; then
desc=
fi
# Edit the description
echo "${desc}" > "${tmpfile}"
"${EDITOR:-vim}" "${tmpfile}"
# Write it back to Kimai
desc="$(< "${tmpfile}")"
newJSON="{ \"description\": \"${desc//\"/\\\"}\" }"
_curl -X PATCH -d "${newJSON}" "${kimaiURL}/api/timesheets/${id}" >/dev/null
}
# Start a new timesheet and return its ID
newTimesheet() {
local json
json="$(cat <<-EOF
{
"project": ${projectID},
"activity": ${activityID},
"description": ""
}
EOF
)"
_curl -X POST -d "${json}" "${kimaiURL}/api/timesheets" | jq .id
}
# Stop the timesheet given by ID
stopTimesheet() {
local id="${1}"
local json description newDescription newJSON
json="$(_curl -X GET "${kimaiURL}/api/timesheets/${id}")"
# Ensure a description
description="$(jq -r .description <<< "${json}")"
if [[ "${description}" = null ]]; then
echo "Please add a description for the timesheet you did."
echo "It started at $(jq -r .begin <<< "${json}")"
newDescription=
while [[ -z "${newDescription:-}" ]]; do
read -r -p "Description: " newDescription
done
newJSON="{ \"description\": \"${newDescription//\"/\\\"}\" }"
_curl -X PATCH -d "${newJSON}" "${kimaiURL}/api/timesheets/${id}" >/dev/null
fi
# Stop the timesheet
_curl -X PATCH "${kimaiURL}/api/timesheets/${id}/stop" >/dev/null
echo "Stopped $(editURL "${id}")"
}
secondsToHuman() {
raw="${1}"
sign=+
seconds="$(bc <<< "${raw} % 60")"
minutes="$(bc <<< "${raw} / 60 % 60")"
hours="$(bc <<< "${raw} / 60 / 60")"
if [[ "${seconds}" -lt 0 ]]; then
seconds="$((seconds * -1))"
sign=-
fi
if [[ "${minutes}" -lt 0 ]]; then
minutes="$((minutes * -1))"
sign=-
fi
if [[ "${hours}" -lt 0 ]]; then
hours="$((hours * -1))"
sign=-
fi
printf "%s%02d:%02d:%02d\n" "${sign}" "${hours}" "${minutes}" "${seconds}"
}
renderDaily() {
pagesize=100
start="$(date +%Y-%m-%dT00:00:00)"
end="$(date +%Y-%m-%dT%H:%M:%S)"
i=1
sum=0
while :; do
out="$(_curl --no-fail -i "${kimaiURL}/api/timesheets?begin=${start}&end=${end}&size=${pagesize}&page=${i}")"
code="$(grep ^HTTP/ <<< "${out}" | cut -d' ' -f2)"
if [[ "${code}" == 404 ]]; then
break
fi
if [[ "${code}" != 200 ]]; then
echo "Unexpected HTTP response: ${code}"
echo "${out}"
fi
durationSum="$(grep '^\[' <<< "${out}" | jq "[.[] | .duration] | add")"
((sum += durationSum))
i="$((i + 1))"
done
daySeconds="$(_curl -X GET "${kimaiURL}/api/users/me" | jq -r '.preferences[] | select(.name == "daily_working_time") | .value')"
echo "Only stopped timesheets are counted!"
echo "You should have worked $(secondsToHuman "${daySeconds}") by the end of this day"
echo "You have actually worked $(secondsToHuman "${sum}")"
echo "Diff in worktime right now: $(secondsToHuman "$((sum - daySeconds))")"
}
renderYearly() {
# Sum up duration
begin="$(date +%y)-01-01T00:00:00"
end="$(date +%Y-%m-%dT%H:%M:%S)"
pagesize=100
i=1
sum=0
holidaySum=0
while :; do
out="$(_curl --no-fail -i "${kimaiURL}/api/timesheets?begin=${begin}&end=${end}&size=${pagesize}&page=${i}")"
code="$(grep ^HTTP/ <<< "${out}" | cut -d' ' -f2)"
if [[ "${code}" == 404 ]]; then
break
fi
if [[ "${code}" != 200 ]]; then
echo "Unexpected HTTP response: ${code}"
echo "${out}"
fi
durationSum="$(grep '^\[' <<< "${out}" | jq "[.[] | select(.activity != ${holidayActivity}) | .duration] | add")"
((sum += durationSum))
# Count holiday days
out="$(_curl --no-fail -i "${kimaiURL}/api/timesheets?begin=${begin}&end=${end}&size=${pagesize}&page=${i}")"
code="$(grep ^HTTP/ <<< "${out}" | cut -d' ' -f2)"
if [[ "${code}" != 200 ]]; then
echo "Unexpected HTTP response: ${code}"
echo "${out}"
exit 1
fi
holidays="$(grep '^\[' <<< "${out}" | jq "[.[] | select(.activity == ${holidayActivity})] | length")"
holidaySum="$((holidaySum + holidays))"
i="$((i + 1))"
done
# Calculate seconds we should have spent working
daysSoFar=0
d="$(date +%Y)-01-01"
tomorrow="$(date -d 'today + 1 day' +%Y-%m-%d)"
while :; do
weekday="$(date -d "${d}" +%w)"
if [[ "${weekday}" != 0 && "${weekday}" != 6 ]]; then
daysSoFar="$((daysSoFar + 1))"
fi
d="$(date -d "${d} + 1 day" +%Y-%m-%d)"
if [[ "${d}" == "${tomorrow}" ]]; then
break
fi
done
sickDays="${sickDays:-0}"
hoursPerDay="${hoursPerDay:-8}"
daysSoFar="$((daysSoFar - sickDays - holidaySum))"
hoursSoFar="$((daysSoFar * hoursPerDay))"
secondsSoFar="$((hoursSoFar * 60 * 60))"
echo "Only stopped timesheets are counted!"
echo "You should have worked ${daysSoFar}d by the end of this day, this is $(secondsToHuman "${secondsSoFar}")"
echo "You have actually worked $(secondsToHuman "${sum}")"
echo "Diff in worktime right now: $(secondsToHuman "$((sum - secondsSoFar))")"
}
action="${1:-}"
currentID="$(getActiveID)"
case "${action}" in
start)
if [[ -n "${currentID}" ]]; then
echo "You are already doing something: $(editURL "${currentID}")"
exit 1
fi
editURL "$(newTimesheet)"
;;
stop)
if [[ -z "${currentID}" ]]; then
echo "You are not doing anything"
exit 1
fi
stopTimesheet "${currentID}"
;;
switch)
if [[ -z "${currentID}" ]]; then
echo "You are not doing anything"
exit 1
fi
stopTimesheet "${currentID}"
editURL "$(newTimesheet)"
;;
desc)
if [[ -z "${currentID}" ]]; then
echo "You are not doing anything"
exit 1
fi
editDescription "${currentID}"
;;
yearly)
renderYearly
;;
daily)
renderDaily
;;
*)
echo "Usage: ${0} [start|stop|switch|desc|yearly|daily]" >&2
exit 1
esac