Skip to content

Commit

Permalink
Fix #9: Add time to previous task for status cmd
Browse files Browse the repository at this point in the history
This commit updates the `status` command to include the time spent on
the previous command. It also adds some unit tests for the old
`get_previous_task()` and the new `get_previous_task_and_time()`
functions.
  • Loading branch information
Cartroo committed Mar 26, 2013
1 parent 7d8f042 commit 7e6f4d8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
8 changes: 4 additions & 4 deletions ttrack/bin/ttrack
Expand Up @@ -75,8 +75,7 @@ def format_duration(secs):


def format_duration_since_datetime(dt):
now = datetime.datetime.now()
delta = now - dt
delta = datetime.datetime.now() - dt
return format_duration(delta.days * 86400 + delta.seconds)


Expand Down Expand Up @@ -659,11 +658,12 @@ class CommandHandler(cmd.Cmd):
"""

try:
prev = self.db.get_previous_task()
prev = self.db.get_previous_task_and_time()
if prev is None:
print "No previous task."
else:
print "Previous task: %s" % (prev,)
dur_str = format_duration(prev[1])
print "Previous task: %s (for %s)" % (prev[0], dur_str)
task = self.db.get_current_task()
if task is None:
print "No current task."
Expand Down
22 changes: 15 additions & 7 deletions ttrack/lib/tracklib.py
Expand Up @@ -444,20 +444,21 @@ def _get_current_task_with_id(self):
return row


def _get_previous_task_with_id(self):
def _get_previous_task_and_time_with_id(self):
"""Return tuple of (log entry id, task name) or None."""

task = self._get_current_task_with_id()
cur = self.conn.cursor()
if task is None:
cur.execute("SELECT L.id, T.name FROM tasklog AS L"
cur.execute("SELECT L.id, T.name, L.start, L.end FROM tasklog AS L"
" INNER JOIN tasks AS T ON L.task=T.id"
" WHERE L.end is NOT NULL"
" ORDER BY L.end DESC LIMIT 1")
else:
cur_task_id = self.tasks.get_id(task[1])
cur.execute("SELECT L.id, T.name FROM tasklog AS L"
cur.execute("SELECT L.id, T.name, L.start, L.end FROM tasklog AS L"
" INNER JOIN tasks AS T ON L.task=T.id"
" WHERE end IS NOT NULL AND L.task!=?"
" WHERE L.end IS NOT NULL AND L.task!=?"
" ORDER BY L.end DESC LIMIT 1", (cur_task_id,))
row = cur.fetchone()
if row is None:
Expand All @@ -479,11 +480,18 @@ def get_current_task(self):
def get_previous_task(self):
"""Returns the name of the highest completed task."""

task = self._get_previous_task_with_id()
if task is None:
task_and_time = self.get_previous_task_and_time()
return task_and_time[0] if task_and_time is not None else None


def get_previous_task_and_time(self):
"""As get_previous_task() but returns a tuple: (task, start, end)."""

task_and_time = self._get_previous_task_and_time_with_id()
if task_and_time is None:
return None
else:
return task[1]
return (task_and_time[1], task_and_time[3] - task_and_time[2])


def get_last_created_task(self):
Expand Down
11 changes: 11 additions & 0 deletions ttrack/test/test_tracklib.py
Expand Up @@ -446,6 +446,17 @@ def test_current_task(self):
self.assertEqual(self.db.get_current_task(), None)


def test_previous_task(self):
self.db.tasks.add("task1")
self.db.tasks.add("task2")
self.db.start_task("task1", datetime.datetime(2013, 3, 26, 10, 0))
self.db.stop_task(datetime.datetime(2013, 3, 26, 10, 30))
self.db.start_task("task2", datetime.datetime(2013, 3, 26, 11, 0))
self.assertEqual(self.db.get_current_task(), "task2")
self.assertEqual(self.db.get_previous_task(), "task1")
self.assertEqual(self.db.get_previous_task_and_time(), ("task1", 1800))


def test_add_task_tags(self):
self.db.tasks.add("task1")
self.db.tasks.add("task2")
Expand Down

0 comments on commit 7e6f4d8

Please sign in to comment.