Skip to content

Commit

Permalink
add stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
falconindy committed Jul 2, 2010
1 parent 16f4b78 commit 8b777f3
Showing 1 changed file with 44 additions and 40 deletions.
84 changes: 44 additions & 40 deletions clockwerk
Expand Up @@ -29,21 +29,25 @@ LOCKFILE="${CW_DATADIR}/clockwerk.lock"


debug() { debug() {
local mesg=$1; shift local mesg=$1; shift
printf "%s\n" "$mesg" printf "debug: %s\n" "$mesg"
} }


die() { die() {
: local mesg=$1; shift
printf "error: %s\n" "$mesg"
} }


warn() { warn() {
: local mesg=$1; shift
printf "warn: %s\n" "$mesg"
} }


info() { info() {
: :
} }


# return 0 on success
# return 1 on failure
initialize_db() { initialize_db() {
local sql="CREATE TABLE job(j_id INTEGER PRIMARY KEY AUTOINCREMENT, local sql="CREATE TABLE job(j_id INTEGER PRIMARY KEY AUTOINCREMENT,
j_category TEXT NOT NULL, j_category TEXT NOT NULL,
Expand All @@ -54,7 +58,9 @@ initialize_db() {
CREATE TABLE category(c_name TEXT PRIMARY KEY, CREATE TABLE category(c_name TEXT PRIMARY KEY,
c_desc TEXT);" c_desc TEXT);"


exec_query "$sql" exec_query "$sql" || return 1

return 0
} }


exec_query() { exec_query() {
Expand All @@ -69,52 +75,44 @@ lock_acquire() {
touch "$LOCKFILE" && return 0 || return 1 touch "$LOCKFILE" && return 0 || return 1
} }


# returns nonzero (job_id) on success # returns 0 on success
# returns 0 on failure # returns non-zero on failure
lock_release() { lock_release() {
debug "Releasing lock" debug "Releasing lock"
[[ ! -f $LOCKFILE ]] && return 0 [[ ! -f $LOCKFILE ]] && return 1
local job_id=$(< "$LOCKFILE") cat "$LOCKFILE"
rm "$LOCKFILE" || return 0 rm "$LOCKFILE"
return $job_id
} }


# return 0 on success # return 0 on success
# return 1 on failure to acquire lock # return 1 on failure to acquire lock
# return 2 on sql error # return 2 on sql error
job_start() { job_start() {
lock_acquire || return 1 lock_acquire || return 1
local jobstart=$(date +%s)
local category=$1
local comment=$2

# insert into table
sql="INSERT INTO job(job_category, job_start, job_comment)
VALUES('$category', $jobstart, '$comment');"


exec_query "$sql" || return 2 local jobstart=$(date +%s) category=$1 comment=$2
local jobid=$(exec_query 'select max(job_id) from jobs;')


echo "$jobid $jobstart" > "$LOCKFILE" echo "$jobstart|$category|$comment" > "$LOCKFILE"
} }


# return 0 on success # return 0 on success
# return 1 on no job recorded (too short) # return 1 on error
# return 2 on error
job_stop() { job_stop() {
local jobid jobstart jobstop=$(date +%s) [[ ! -f $LOCKFILE ]] && return 1
read jobid jobstart <<< "$(lock_release)" local category jobstart comment jobstop=$(date +%s)


if [[ $(get_duration $jobstart $jobstop) -gt 60 ]]; then IFS=$'|' read jobstart category comment <<< "$(lock_release || echo -1)"
job_delete $job_id
return 1
fi


local sql="UPDATE job SET job_stop = '$jobstart' WHERE job_id = '$jobid';" [[ $jobstart -eq -1 ]] && return 1

[[ $(get_duration $jobstart $jobstop) -gt 60 ]] && return 2

local sql="INSERT INTO job(j_category, j_start, j_stop, j_comment)
VALUES('$category', '$jobstart', '$jobstop', 'comment');"


exec_query "$sql" &>/dev/null exec_query "$sql" &>/dev/null


[[ $jobid -eq 0 ]] && return 2 [[ $jobid -eq 0 ]] && return 3
} }


# returns duration in seconds # returns duration in seconds
Expand All @@ -136,15 +134,11 @@ date_to_seconds() {
seconds_to_hms() { seconds_to_hms() {
local seconds=$1 hours=0 minutes=0 local seconds=$1 hours=0 minutes=0


while (( seconds >= 3600 )); do hours=$(( seconds / 3600 ))
(( ++hours )) seconds=$(( seconds - hours * 3600 ))
(( seconds -= 3600 ))
done


while (( seconds >= 60 )); do minutes=$(( seconds / 60 ))
(( ++minutes )) seconds=$(( seconds - minutes * 60 ))
(( seconds -= 60 ))
done


echo $hours $minutes $seconds echo $hours $minutes $seconds
} }
Expand All @@ -168,29 +162,39 @@ category_del() {


do_job() { do_job() {
local action=$1; shift local action=$1; shift

case $action in case $action in
"start") "start")
[[ $# -gt 0 ]] && job_start "$@" || usage [[ -z $1 ]] && die "missing job category"
job_start "$@"
;; ;;
"stop") "stop")
job_stop job_stop
;; ;;
*) usage *) usage
;; ;;
esac esac

} }


do_category() { do_category() {
local action=$1; shift local action=$1; shift

case $action in
"add") category_add "$@" ;;
*) die "Not implemented" ;;
esac

} }


do_report() { do_report() {
local action=$1; shift local action=$1; shift

} }


#sanity checks #sanity checks
[[ ! -d $CW_DATADIR ]] && mkdir -p "$CW_DATADIR" [[ ! -d $CW_DATADIR ]] && mkdir -p "$CW_DATADIR"
exec_query 'SELECT * FROM job' &>/dev/null || initialize_db exec_query 'SELECT * FROM job' &>/dev/null || initialize_db || die "failed to initialize database"


# option parsing # option parsing
action=$1; shift action=$1; shift
Expand Down

0 comments on commit 8b777f3

Please sign in to comment.