Mercurial > getan
comparison scripts/getan_report.py @ 380:20fde79f8e12
Move all scripts in a common scripts directory
Currently console scripts were kept in several directories. Now use a common
directory for all scripts.
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Mon, 05 Jan 2015 10:54:20 +0100 |
parents | contrib/getan_report.py@bdc4e45f9f4f |
children |
comparison
equal
deleted
inserted
replaced
379:47890f38e558 | 380:20fde79f8e12 |
---|---|
1 #!/usr/bin/env python3 | |
2 """write a daily report accessing a database from http://hg.intevation.de/getan 1.1dev3.""" | |
3 # hastily done, written to learn the getan database format and its manipulation | |
4 # Free Software under GNU GPL v>=3 | |
5 # 20130109 bernhard@intevation.de | |
6 # 20140103 bernhard@intevation.de: | |
7 # started from 2013/getan-writeout-timesorted.py | |
8 # ported to python3. Removed the dependency for functions from worklog.py. | |
9 # the timesorted variant can be uncommented in the code for now | |
10 # 20140109 bernhard@intevation.de: | |
11 # Total time output format improved. | |
12 # 20140120 bernhard@intevation.de: | |
13 # added command line options, requires argparse module now (e.g. Python>=3.2) | |
14 # 20141104 bernhard@intevation.de: | |
15 # migration to argparse complete, added -t option | |
16 # TODO: | |
17 # * use python v>=3.2 variants in the code where noted. | |
18 | |
19 import argparse | |
20 import datetime | |
21 import logging | |
22 import sqlite3 | |
23 import sys | |
24 | |
25 factor = {'privat' : 0} | |
26 | |
27 l = logging | |
28 l.basicConfig(level=logging.INFO, | |
29 #l.basicConfig(level=logging.DEBUG, | |
30 format='%(message)s') | |
31 # format='%(asctime)s %(levelname)s %(message)s') | |
32 | |
33 def hhhmm_from_timedelta(td): | |
34 """Return a string '-HHH:MM' from the timedelta parameter. | |
35 | |
36 Accounts for way the integer division works for negative numbers: | |
37 -2 // 60 == -1 | |
38 -2 % 60 == 58 | |
39 by first working on the positive number and then adding the minus | |
40 to the string. | |
41 | |
42 For python >=3.1. Successor of hhmm_from_timedelta() from | |
43 http://intevation.de/cgi-bin/viewcvs-misc.cgi/worklog.py/ . | |
44 """ | |
45 total_minutes = abs(round(td.days*24*60 + td.seconds/60)) | |
46 # variant for Python v>3.2: | |
47 #total_minutes = abs(round(td/datetime.timedelta(minutes=1))) | |
48 | |
49 hours = total_minutes//60 | |
50 minutes = total_minutes%60 | |
51 | |
52 h_string = "{}".format(hours) | |
53 | |
54 if(td.days<0): | |
55 h_string = "-" + h_string | |
56 | |
57 return "{:>3s}:{:02d}".format(h_string, minutes) | |
58 | |
59 def self_test(): | |
60 """Run some simple tests on hhhmm_from_timedelta(). | |
61 | |
62 e.g. run like | |
63 python3 -c 'from getan_report_20140103 import *; self_test()' | |
64 """ | |
65 l.info(hhhmm_from_timedelta(datetime.timedelta(minutes=1))) | |
66 l.info(hhhmm_from_timedelta(datetime.timedelta(minutes=-2))) | |
67 | |
68 def main(): | |
69 parser = argparse.ArgumentParser(description=__doc__) | |
70 parser.add_argument("-t", action='store_true', | |
71 help="timesorted output and default reportday to today") | |
72 parser.add_argument("dbfilename") | |
73 parser.add_argument("reportday", nargs='?', | |
74 help="day to report yyyy-mm-dd") | |
75 | |
76 args = parser.parse_args() | |
77 l.debug(args) | |
78 | |
79 if args.reportday: | |
80 report_range_start = \ | |
81 datetime.datetime.strptime(args.reportday, "%Y-%m-%d") | |
82 elif args.t: | |
83 # start with today 00:00 | |
84 report_range_start = datetime.datetime.combine( | |
85 datetime.date.today(), datetime.time()) | |
86 else: | |
87 # start with yesterday 00:00 | |
88 report_range_start = datetime.datetime.combine( | |
89 datetime.date.today() - datetime.timedelta(days=1), | |
90 datetime.time()) | |
91 report_range_end = report_range_start + datetime.timedelta(days=1) | |
92 | |
93 | |
94 l.info("Opening sqlite3 database '%s'" % args.dbfilename) | |
95 conn = sqlite3.connect(args.dbfilename, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) | |
96 c = conn.cursor() | |
97 | |
98 tasks = {} | |
99 task_total = {} | |
100 | |
101 c.execute('select * from projects') | |
102 for t in c: | |
103 l.debug(t) | |
104 tasks[t[0]] = t[2] | |
105 task_total[t[0]] = datetime.timedelta() | |
106 | |
107 # from getan 1.0 20130103 | |
108 #CREATE TABLE entries ( | |
109 # id INTEGER PRIMARY KEY AUTOINCREMENT, | |
110 # project_id INTEGER REFERENCES projects(id), | |
111 # start_time TIMESTAMP NOT NULL, | |
112 # stop_time TIMESTAMP NOT NULL, | |
113 # description VARCHAR(256), | |
114 # | |
115 # CHECK (strftime('%s', start_time) <= strftime('%s', stop_time)) | |
116 #); | |
117 | |
118 total_time = datetime.timedelta() | |
119 | |
120 if args.t: | |
121 c.execute('select * from entries order by start_time') | |
122 else: | |
123 c.execute('select * from entries order by project_id') | |
124 for e in c: | |
125 l.debug(e) | |
126 length = e[3]-e[2] | |
127 | |
128 desc = tasks[e[1]] | |
129 | |
130 if e[2] >= report_range_start and e[2] < report_range_end: | |
131 if args.t: | |
132 print("{2}: {3} {4}\n\t\t\t{0} {1}". | |
133 format(e[2], e[3], desc, e[4],hhhmm_from_timedelta(length))) | |
134 else: | |
135 print("{0} {2}: {3} {4}". | |
136 format(e[2], e[3], desc, e[4],hhhmm_from_timedelta(length))) | |
137 if desc in factor: | |
138 ##python3.1 does not allow timedelta division. | |
139 ##TODO: Make python3.1 save or update to python3.2. | |
140 #l.info("applying factor %f to entry %s" % (factor[desc], e)) | |
141 #length = (length * int(factor[desc]*1000))/1000 | |
142 #Until python3.2 we only honor a factor of zero: | |
143 if factor[desc] == 0: | |
144 length = datetime.timedelta(0) | |
145 l.info("not counting {}".format(e)) | |
146 else: | |
147 l.info("ignoring factor {}".factor[desc]) | |
148 total_time += length | |
149 task_total[e[1]] += length | |
150 | |
151 print("(" + hhhmm_from_timedelta(total_time).strip() + ")") | |
152 for t in tasks: | |
153 if task_total[t] != datetime.timedelta(0): | |
154 print("\t" + tasks[t], hhhmm_from_timedelta(task_total[t])) | |
155 | |
156 c.close() | |
157 | |
158 if __name__ == "__main__": | |
159 main() |