Andy Mikhailenko  committed a4f4a6d

ext.time: Added argument --sum to command show_history.

  • Participants
  • Parent commits 5d01f94
  • Branches default

Comments (0)

Files changed (1)

File orgtool/ext/time/

 import re
 from itertools import chain
+from dateutil.relativedelta import relativedelta
 from tool.cli import (
     # commands
 @arg('--count', default=False,
      help='only display the number of matching events')
+@arg('--sum', default=False,
+     help='only display the total duration of matching events')
 @arg('-q', '--query', help='events must match this regex')
 @arg('--format', help='a Python string format for events')
 def show_history(args):
     .. _Python string formatting:
+    assert not (args.count and args.sum), 'choose one aggregate at a time'
     fix_unicode(args, 'query', 'format')
     # TODO this lists *all* events, whether related to time tracking or not.
     # here we only need a subset of them: the *user's* activities!
                   'return slightly different results')
     db = default_storage()
-    events = Event.objects(db).order_by('date_time')
+    events = db.find(Event).order_by('date_time')
     if args.query:
         events = events.where(summary__matches_caseless=args.query)
     if args.count:
         yield events.count()
+    elif args.sum:
+        durations = (e.get_duration() for e in events)
+        seconds = sum(d.seconds for d in durations if d)
+        yield relativedelta(seconds=seconds)
         for event in events:
             template = args.format or u'{date_time} {summary}'