[Getan-commits] [PATCH] Added contrib/getan_report.py, a script to write a daily report by Bernhard Reiter
Wald Commits
scm-commit at wald.intevation.org
Tue Nov 4 17:46:55 CET 2014
# HG changeset patch
# User Bernhard Reiter <bernhard at intevation.de>
# Date 1415119613 -3600
# Node ID bdc4e45f9f4f5a2bcaf0353ff710b7163ba39feb
# Parent 0ad470c4bf17d3ebde84d7cf992621e0b034b6a9
Added contrib/getan_report.py, a script to write a daily report by Bernhard Reiter.
diff -r 0ad470c4bf17 -r bdc4e45f9f4f contrib/getan_report.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/getan_report.py Tue Nov 04 17:46:53 2014 +0100
@@ -0,0 +1,159 @@
+#!/usr/bin/env python3
+"""write a daily report accessing a database from http://hg.intevation.de/getan 1.1dev3."""
+# hastily done, written to learn the getan database format and its manipulation
+# Free Software under GNU GPL v>=3
+# 20130109 bernhard at intevation.de
+# 20140103 bernhard at intevation.de:
+# started from 2013/getan-writeout-timesorted.py
+# ported to python3. Removed the dependency for functions from worklog.py.
+# the timesorted variant can be uncommented in the code for now
+# 20140109 bernhard at intevation.de:
+# Total time output format improved.
+# 20140120 bernhard at intevation.de:
+# added command line options, requires argparse module now (e.g. Python>=3.2)
+# 20141104 bernhard at intevation.de:
+# migration to argparse complete, added -t option
+# TODO:
+# * use python v>=3.2 variants in the code where noted.
+
+import argparse
+import datetime
+import logging
+import sqlite3
+import sys
+
+factor = {'privat' : 0}
+
+l = logging
+l.basicConfig(level=logging.INFO,
+#l.basicConfig(level=logging.DEBUG,
+ format='%(message)s')
+# format='%(asctime)s %(levelname)s %(message)s')
+
+def hhhmm_from_timedelta(td):
+ """Return a string '-HHH:MM' from the timedelta parameter.
+
+ Accounts for way the integer division works for negative numbers:
+ -2 // 60 == -1
+ -2 % 60 == 58
+ by first working on the positive number and then adding the minus
+ to the string.
+
+ For python >=3.1. Successor of hhmm_from_timedelta() from
+ http://intevation.de/cgi-bin/viewcvs-misc.cgi/worklog.py/ .
+ """
+ total_minutes = abs(round(td.days*24*60 + td.seconds/60))
+ # variant for Python v>3.2:
+ #total_minutes = abs(round(td/datetime.timedelta(minutes=1)))
+
+ hours = total_minutes//60
+ minutes = total_minutes%60
+
+ h_string = "{}".format(hours)
+
+ if(td.days<0):
+ h_string = "-" + h_string
+
+ return "{:>3s}:{:02d}".format(h_string, minutes)
+
+def self_test():
+ """Run some simple tests on hhhmm_from_timedelta().
+
+ e.g. run like
+ python3 -c 'from getan_report_20140103 import *; self_test()'
+ """
+ l.info(hhhmm_from_timedelta(datetime.timedelta(minutes=1)))
+ l.info(hhhmm_from_timedelta(datetime.timedelta(minutes=-2)))
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("-t", action='store_true',
+ help="timesorted output and default reportday to today")
+ parser.add_argument("dbfilename")
+ parser.add_argument("reportday", nargs='?',
+ help="day to report yyyy-mm-dd")
+
+ args = parser.parse_args()
+ l.debug(args)
+
+ if args.reportday:
+ report_range_start = \
+ datetime.datetime.strptime(args.reportday, "%Y-%m-%d")
+ elif args.t:
+ # start with today 00:00
+ report_range_start = datetime.datetime.combine(
+ datetime.date.today(), datetime.time())
+ else:
+ # start with yesterday 00:00
+ report_range_start = datetime.datetime.combine(
+ datetime.date.today() - datetime.timedelta(days=1),
+ datetime.time())
+ report_range_end = report_range_start + datetime.timedelta(days=1)
+
+
+ l.info("Opening sqlite3 database '%s'" % args.dbfilename)
+ conn = sqlite3.connect(args.dbfilename, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
+ c = conn.cursor()
+
+ tasks = {}
+ task_total = {}
+
+ c.execute('select * from projects')
+ for t in c:
+ l.debug(t)
+ tasks[t[0]] = t[2]
+ task_total[t[0]] = datetime.timedelta()
+
+# from getan 1.0 20130103
+#CREATE TABLE entries (
+# id INTEGER PRIMARY KEY AUTOINCREMENT,
+# project_id INTEGER REFERENCES projects(id),
+# start_time TIMESTAMP NOT NULL,
+# stop_time TIMESTAMP NOT NULL,
+# description VARCHAR(256),
+#
+# CHECK (strftime('%s', start_time) <= strftime('%s', stop_time))
+#);
+
+ total_time = datetime.timedelta()
+
+ if args.t:
+ c.execute('select * from entries order by start_time')
+ else:
+ c.execute('select * from entries order by project_id')
+ for e in c:
+ l.debug(e)
+ length = e[3]-e[2]
+
+ desc = tasks[e[1]]
+
+ if e[2] >= report_range_start and e[2] < report_range_end:
+ if args.t:
+ print("{2}: {3} {4}\n\t\t\t{0} {1}".
+ format(e[2], e[3], desc, e[4],hhhmm_from_timedelta(length)))
+ else:
+ print("{0} {2}: {3} {4}".
+ format(e[2], e[3], desc, e[4],hhhmm_from_timedelta(length)))
+ if desc in factor:
+ ##python3.1 does not allow timedelta division.
+ ##TODO: Make python3.1 save or update to python3.2.
+ #l.info("applying factor %f to entry %s" % (factor[desc], e))
+ #length = (length * int(factor[desc]*1000))/1000
+ #Until python3.2 we only honor a factor of zero:
+ if factor[desc] == 0:
+ length = datetime.timedelta(0)
+ l.info("not counting {}".format(e))
+ else:
+ l.info("ignoring factor {}".factor[desc])
+ total_time += length
+ task_total[e[1]] += length
+
+ print("(" + hhhmm_from_timedelta(total_time).strip() + ")")
+ for t in tasks:
+ if task_total[t] != datetime.timedelta(0):
+ print("\t" + tasks[t], hhhmm_from_timedelta(task_total[t]))
+
+ c.close()
+
+if __name__ == "__main__":
+ main()
More information about the Getan-commits
mailing list