/
clockwerk
executable file
·308 lines (236 loc) · 6.76 KB
/
clockwerk
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash
CW_DATADIR="${XDG_DATA_HOME:-$HOME/.local/share}/clockwerk"
DBFILE="${CW_DATADIR}/clockwerk.db"
LOCKFILE="${CW_DATADIR}/clockwerk.lock"
usage() {
cat << EOF
clockwerk <action> [arguments]
actions: job
category
report
arguments: job start <category> [comment]
stop
delete <jobid>
category add <name> [description]
edit <name> <newname> [description]
delete <name>
report [-c category] [-f from] [-t to]
EOF
exit 0
}
debug() {
local mesg=$1; shift
printf "debug: %s\n" "$mesg" >&2
}
die() {
local mesg=$1; shift
printf "error: %s\n" "$mesg" >&2
exit 1
}
warn() {
local mesg=$1; shift
printf "warn: %s\n" "$mesg" >&2
}
info() {
local mesg=$1; shift
printf ":: %s\n" "$mesg"
}
date_to_seconds() {
local seconds=$(date --date="$1" +%s || echo "0")
return seconds
}
# prints HH MM SS
seconds_to_hms() {
local seconds=$1 hours=0 minutes=0
hours=$(( seconds / 3600 ))
seconds=$(( seconds - hours * 3600 ))
minutes=$(( seconds / 60 ))
seconds=$(( seconds - minutes * 60 ))
echo $hours $minutes $seconds
}
# prints HH MM rounded to nearest minute
seconds_to_hm() {
local seconds=$1 hours=0 minutes=0
hours=$(( seconds / 3600 ))
seconds=$(( seconds - hours * 3600 ))
minutes=$(( seconds / 60 ))
seconds=$(( seconds - minutes * 60 ))
(( seconds >= 30 )) && (( ++minutes ))
echo $hours $minutes
}
# return 0 on success
# return 1 on failure
initialize_db() {
local sql="CREATE TABLE job(j_id INTEGER PRIMARY KEY AUTOINCREMENT,
j_category TEXT NOT NULL,
j_start INTEGER NOT NULL,
j_stop INTEGER DEFAULT 0,
j_comment NULL,
FOREIGN KEY (j_category) REFERENCES category (c_name));
CREATE TABLE category(c_id INTEGER PRIMARY KEY AUTOINCREMENT,
c_name TEXT,
c_desc TEXT);"
exec_query "$sql" || return 1
return 0
}
exec_query() {
sqlite3 "$DBFILE" "$@"
}
# returns 0 on success
# returns non-zero on failure
lock_acquire() {
debug "Acquiring lock"
[[ -f $LOCKFILE ]] && return 1
touch "$LOCKFILE" || return 2
return 0
}
# returns 0 on success
# returns non-zero on failure
lock_release() {
debug "Releasing lock"
[[ ! -f $LOCKFILE ]] && return 1
cat "$LOCKFILE"
rm "$LOCKFILE"
}
#################################
# job functions #
#################################
# return 0 on success
# return 1 on failure to acquire lock
# return 2 on sql error
# parameters: category, opt:comment
job_start() {
# verify category exists
# do we want to create the category if it doesn't exist?
category_verify "$1"
local catnum=$?
[[ $catnum -eq 0 ]] && die "Invalid category: '$1'"
lock_acquire || return 1
local ret=$?
[[ $ret -eq 1 ]] && die "A job is already running"
[[ $ret -eq 2 ]] && die "Unable to create lock file"
local jobstart=$(date +%s) comment=$2
echo "$jobstart|$catnum|$comment" > "$LOCKFILE"
info "Job started at $(date)"
}
# return 0 on success
# return 1 on error
job_stop() {
[[ ! -f $LOCKFILE ]] && return 1
local category jobstart comment jobstop=$(date +%s)
IFS=$'|' read jobstart category comment < <(lock_release || echo -1)
[[ $jobstart = "-1" ]] && return 1
if (( jobstop - jobstart < 60 )); then
warn "Job too short. Discarded."
return 2
fi
local sql="INSERT INTO job(j_category, j_start, j_stop, j_comment)
VALUES('$category', '$jobstart', '$jobstop', '$comment');"
exec_query "$sql" &>/dev/null || return 3
info "Job complete: duration %s %s" $(seconds_to_hm $(( jobstop - jobstart )))
}
job_modify() {
:
}
#################################
# category functions #
#################################
# return 0 on success
# return 1 on category exists
# return 2 on sql error
category_add() {
local sql="INSERT INTO category(c_name, c_desc) VALUES('$1', '$2');"
local result=$(exec_query "$sql" 2>&1)
[[ $result = "Error: column c_name is not unique" ]] && return 1
[[ $result =~ ^Error:* ]] && return 2
return 0
}
category_delete() {
die "Not implemented"
# check for existance of category, fail if it doesn't exist
# how to handle removal of a category when jobs use it?
}
category_list() {
local sql="SELECT DISTINCT j_category FROM job"
while IFS=$'|' read c_name c_desc; do
printf "%-20s%-20s\n" "$c_name" "$c_desc"
done < <(exec_query "$sql")
}
# returns zero on not found else c_id
# parameter: category name
category_verify() {
local sql result
sql="SELECT c_id from category where c_name = '$1';"
result=$(exec_query "$sql")
[[ -z $result ]] && return 0 || return $result
}
#################################
# action dispatchers #
#################################
do_job() {
local action=$1; shift
case $action in
"start")
[[ -z $1 ]] && die "missing job category"
job_start "$@"
;;
"stop")
job_stop
;;
"status")
job_status
;;
"delete")
die "Not implemented"
;;
*) usage
;;
esac
}
do_category() {
local action=$1; shift
type -p category_$action && category_$action "$@" || usage
}
do_report() {
local action=$1; shift
local from to cat
while getopts "f:t:c:" opt; do
case "$opt" in
f) from=$(date --date="$OPTARG" +%s 2>/dev/null) || die "Invalid from date: '$OPTARG'" ;;
t) to=$(date --date="$OPTARG" +%s 2>/dev/null) || die "Invalid to date: '$OPTARG'" ;;
c) category_verify "$OPTARG" && cat="$OPTARG" || die "Category not found: '$OPTARG'" ;;
\?) die "Unrecognized option -$OPTARG" ;;
esac
done
# ensure a default date range
from=${from:-0}
to=${to:-$(date +%s)}
cat=${cat:-ALL}
printf "%5s %-10s %-10s %s\n\n" ID Category Duration Comment
while IFS=$'|' read id cat start stop comment; do
printf "%5s %-10s %-10s %s\n" "$id" "$cat" "$(seconds_to_hms $(( stop - start )))" "$comment"
done < <(exec_query "select * from job")
}
do_status() {
if [[ -f $LOCKFILE ]]; then
local jobstart category comment dur
IFS=$'|' read jobstart category comment < "$LOCKFILE"
dur=$(( $(date +%s) - jobstart ))
echo "Job in Progress:"
echo "Category: $category"
[[ -n $comment ]] && echo "Comment: $comment"
printf "Elapsed time: %sh %sm %ss\n" $(seconds_to_hms $dur)
else
info "No job currently running"
fi
}
#############
# main loop #
#############
# sanity checks
[[ ! -d $CW_DATADIR ]] && mkdir -p "$CW_DATADIR"
exec_query 'SELECT * FROM job' &>/dev/null || initialize_db || die "failed to initialize database"
# option parsing
action=$1; shift
type -p do_$action || usage
do_$action "$@"