Arreglados bugs de mostrado
[ttt.git] / ttt.py
1 #!/usr/bin/python
2
3 # Author: Daniel Garcia <dani@danigm.net>
4 # License: GPLv3
5
6 import datetime
7 import time
8 import sys
9 import tttdb as db
10 from tttdb import Tracker
11
12 sqlhub = db.get_hub()
13
14 def parse_time(time_seconds):
15     hour = 0
16     min = 0
17     sec = time_seconds
18     while sec >= 60:
19         min += 1
20         sec -= 60
21     while min >= 60:
22         hour += 1
23         min -= 60
24     time_str = '%02d:%02d:%02d' % (hour, min, sec)
25     return time_str
26
27 def track(task, project='', ticket=0):
28     today = datetime.datetime.now()
29     tick = time.time()
30     time_pass = 0
31     try:
32         while True:
33             tick2 = time.time()
34             time_pass = tick2 - tick
35             cadena = '\rtracking: %(task)s %(project)s %(ticket)s %(time)s' % \
36                 {'time':parse_time(time_pass),
37                  'task':task,
38                  'project':project,
39                  'ticket':ticket}
40             sys.stdout.write(cadena)
41             sys.stdout.flush()
42             time.sleep(0.5)
43     except:
44         print '\n', 'Finalizando'
45         end = datetime.datetime.now()
46         db.Tracker(project=project,
47                 task=task,
48                 ticket=ticket,
49                 start=today,
50                 end=end)
51
52 def show(trackers):
53     grouped = {}
54     total = 0
55     for i in trackers:
56         diff = i.end - i.start
57         total += diff.seconds
58         time_passed = parse_time(diff.seconds)
59
60         key = (i.task, i.project)
61         if grouped.has_key(key):
62             grouped[key] += diff.seconds
63             
64         else:
65             grouped[key] = diff.seconds
66         
67         to_show = '%-20s | %-20s | #%-6d | %s | %s'
68         print to_show % (i.project, i.task, i.ticket, i.start.ctime(),
69                                                             time_passed)
70     
71     print ''
72     for k,v in grouped.items():
73         task, project = k
74         time_passed = parse_time(v)
75         print 'Tiempo para "%s": %s' % (task, time_passed)
76
77     print '\nTotal para hoy: %s' % parse_time(total)
78
79 def show_today(delta=1):
80     # TODO yesterday no es real, hay que mirar desde las 00 del mismo dia
81     yesterday = datetime.datetime.now() - datetime.timedelta(delta)
82     today_trackers = Tracker.select(Tracker.q.start > yesterday)
83     show(today_trackers)
84
85 def show_project(project):
86     project_trackers = Tracker.select(Tracker.q.project == project)
87     sum = 0
88     for i in project_trackers:
89         sum += (i.end - i.start).seconds
90
91     print 'Tiempo para "%s": %s' % (project, parse_time(sum))
92
93 def show_sem():
94     show_today(7)
95
96 def show_today_grouped(delta=1):
97     yesterday = datetime.datetime.now() - datetime.timedelta(delta)
98     today_trackers = Tracker.select(Tracker.q.start > yesterday)
99     show_grouped(today_trackers)
100
101 def show_grouped(trackers):
102     projects = {}
103     total = 0
104     for i in trackers:
105         diff = (i.end - i.start).seconds
106         total += diff
107         if projects.has_key(i.project):
108             projects[i.project] += diff
109         else:
110             projects[i.project] = diff
111
112     for k,v in projects.items():
113         p = '"' + k + '":'
114         print 'Tiempo para %-20s %s' % (p, parse_time(v))
115
116     print '\nTotal para hoy: %s' % parse_time(total)
117
118 if __name__ == '__main__':
119     task, project, ticket = '','',0
120     args = sys.argv[1:]
121     if len(args) >= 1:
122         task = args[0]
123     if len(args) >= 2:
124         project = args[1]
125     if len(args) >= 3:
126         ticket = int(args[2])
127
128     track(task, project, ticket)