]> piware.de Git - bin.git/blobdiff - workitems.py
workitems: add by-assignee report to HTML output
[bin.git] / workitems.py
index 928c941b1e0f969f5761527f2657690fef720b5b..7b22e60111fdf07fa49d9382c6feb3534418b4ca 100755 (executable)
@@ -20,12 +20,13 @@ def get_db(dbpath):
         cur = db.cursor()
         cur.execute('''CREATE TABLE work_items (
             blueprint VARCHAR(255) NOT NULL,
-            workitem VARCHAR(255) NOT NULL,
+            workitem VARCHAR(1000) NOT NULL,
             status VARCHAR(20) NOT NULL,
+            assignee VARCHAR(200) NOT NULL,
             date TIMESTAMP NOT NULL)''')
-        cur.execute('''CREATE TABLE statuses (
+        cur.execute('''CREATE TABLE status (
             blueprint VARCHAR(255) NOT NULL,
-            status VARCHAR(20) NOT NULL,
+            status VARCHAR(1000) NOT NULL,
             date TIMESTAMP NOT NULL)''')
         db.commit()
 
@@ -88,14 +89,28 @@ def get_blueprints(url, name_pattern):
 def get_blueprint_workitems(blueprint_url):
     '''Collect work items from a particular blueprint URL.
 
-    This will return a list of ('item', 'state') pairs.
+    This will return a list of ('item', 'status', 'assignee') tuples.
     '''
     work_items_re = re.compile('(<p>|^)work items:\s*<br />', re.I)
+    assignee_re = re.compile('<a href="https://.*launchpad.net/~([a-zA-Z0-9_-]+)" class=".*person">')
 
     found_workitems = False
+    found_assignee = False
     result = []
+    default_assignee = 'nobody'
     for l in urllib.urlopen(blueprint_url):
         end = False
+
+        if '<dt>Assignee:' in l:
+            found_assignee = True
+            continue
+
+        if found_assignee and not found_workitems:
+            m = assignee_re.search(l)
+            if m:
+                default_assignee = m.group(1)
+                found_assignee = False
+
         if not found_workitems:
             if work_items_re.search(l):
                 found_workitems = True
@@ -127,7 +142,18 @@ def get_blueprint_workitems(blueprint_url):
             print >> sys.stderr, 'ERROR: invalid state "%s" for work item "%s"' % (
                 state, desc)
             continue
-        result.append((desc, state))
+
+        if desc.startswith('['):
+            try:
+                off = desc.index(']')
+                assignee = desc[1:off]
+                desc = desc[off+1:].strip()
+            except ValueError:
+                print >> sys.stderr, 'ERROR: missing closing "]" for assignee for work item "%s"' % desc
+        else:
+            assignee = default_assignee
+
+        result.append((desc, state, assignee))
 
         if end:
             break
@@ -169,7 +195,7 @@ def get_moin_workitems(url):
 
     Every line starting with "|| " is treated as a work item.
 
-    Return a list of ('item', 'state') pairs.
+    Return a list of ('item', 'status') pairs.
     '''
     result = []
     for line in urllib.urlopen(url):
@@ -193,22 +219,32 @@ def dump(db):
     '''Dump database contents.'''
 
     cur = db.cursor()
-    cur.execute('SELECT w.*, s.status FROM work_items w LEFT JOIN statuses s on w.blueprint = s.blueprint')
-    for (blueprint, workitem, status, date) in cur:
-        print '%s [%s]\t%s: %s' % (date, blueprint, workitem, status)
+    cur.execute('SELECT * FROM work_items')
+    print '== Work items: =='
+    for (blueprint, workitem, item_status, assignee, date) in cur:
+        print '%s [%s, %s]\t%s: %s' % (date, blueprint, assignee, workitem, item_status)
+
+    print '\n== Status =='
+    cur = db.cursor()
+    cur.execute('SELECT * FROM status')
+    for (blueprint, status, date) in cur:
+        print '%s: %s [%s]' % (blueprint, status, date)
 
-def add_work_item(db, blueprint, item, state):
+def add_work_item(db, blueprint, item, status, assignee):
     '''Add work item to database.'''
 
     cur = db.cursor()
-    cur.execute('INSERT INTO work_items VALUES (?, ?, ?, date(CURRENT_TIMESTAMP))',
-            (blueprint, item, state))
+    cur.execute('INSERT INTO work_items VALUES (?, ?, ?, ?, date(CURRENT_TIMESTAMP))',
+            (blueprint, item, status, assignee))
 
 def add_status(db, blueprint, status):
     '''Add blueprint status to database.'''
 
+    if not status:
+        return
+
     cur = db.cursor()
-    cur.execute('INSERT INTO statuses VALUES (?, ?, date(CURRENT_TIMESTAMP))',
+    cur.execute('INSERT INTO status VALUES (?, ?, date(CURRENT_TIMESTAMP))',
             (blueprint, status))
 
 def import_lp(db, name_pattern, release):
@@ -219,7 +255,7 @@ def import_lp(db, name_pattern, release):
 
     cur = db.cursor()
     cur.execute('DELETE FROM work_items WHERE date = date(CURRENT_TIMESTAMP)')
-    cur.execute('DELETE FROM statuses WHERE date = date(CURRENT_TIMESTAMP)')
+    cur.execute('DELETE FROM status WHERE date = date(CURRENT_TIMESTAMP)')
 
     for bp in blueprints:
         #print 'Checking', bp
@@ -228,10 +264,8 @@ def import_lp(db, name_pattern, release):
         status = get_blueprint_status(bp)
         if not work_items:
             print >> sys.stderr, 'WARNING: %s has no work items' % bpname
-        if not status:
-            print >> sys.stderr, 'WARNING: %s has no status' % bpname
-        for (item, state) in work_items:
-            add_work_item(db, bpname, item, state)
+        for (item, state, assignee) in work_items:
+            add_work_item(db, bpname, item, state, assignee)
         add_status(db, bpname, status)
 
 def workitems_over_time(db):
@@ -248,10 +282,10 @@ def workitems_over_time(db):
             data.setdefault(date, {})[s] = num
     return data
 
-def blueprint_stats(db):
-    '''Determine current blueprint stats.
+def blueprint_completion(db):
+    '''Determine current blueprint completion.
 
-    Return blueprint -> [todo, done, postponed] mapping.
+    Return blueprint -> [todo, done, postponed, status] mapping.
     '''
     data = {}
 
@@ -264,18 +298,42 @@ def blueprint_stats(db):
     for s in valid_states:
         cur = db.cursor()
         cur.execute('SELECT w.blueprint, count(w.workitem), s.status FROM work_items w '
-                'LEFT JOIN statuses s ON w.blueprint = s.blueprint '
+                'LEFT JOIN status s ON w.blueprint = s.blueprint '
                 'WHERE w.status = ? AND w.date = ? GROUP BY w.blueprint',
                 (s, last_date))
         for (bp, num, status) in cur:
-            data.setdefault(bp, [0, 0, 0, ""])[index] = num
-            data[bp][-1] = status
+            data.setdefault(bp, [0, 0, 0, ''])[index] = num
+            data[bp][-1] = status or ''
+        index += 1
+
+    return data
+
+def assignee_completion(db):
+    '''Determine current by-assignee completion.
+
+    Return assignee -> [todo, done, postponed] mapping.
+    '''
+    data = {}
+
+    # last date
+    cur = db.cursor()
+    cur.execute('SELECT max(date) FROM work_items')
+    (last_date,) = cur.fetchone()
+
+    index = 0
+    for s in valid_states:
+        cur = db.cursor()
+        cur.execute('SELECT assignee, count(workitem) FROM work_items '
+                'WHERE date=? and status=? GROUP BY assignee',
+                (last_date, s))
+        for (a, num) in cur:
+            data.setdefault(a, [0, 0, 0])[index] = num
         index += 1
 
     return data
 
 def text(db):
-    '''Print work item stats as text.'''
+    '''Print work item completion as text.'''
 
     data = workitems_over_time(db)
 
@@ -283,8 +341,8 @@ def text(db):
     for d in sorted(data.keys()):
         print d, data[d]
 
-    print '\nBlueprint stats:'
-    data = blueprint_stats(db)
+    print '\nBlueprint completion:'
+    data = blueprint_completion(db)
     for (bp, (todo, done, postponed, status)) in data.iteritems():
         # TODO print status
         print '%s: %i/%i (%i%%)' % (bp, postponed+done, todo+done+postponed, 
@@ -307,6 +365,8 @@ def csv(db, from_date, to_date):
     data = workitems_over_time(db)
 
     dates = sorted(data.keys())
+    if not dates:
+        return
 
     f = _fromstr(from_date or dates[0])
     t = _fromstr(to_date or dates[-1])
@@ -349,7 +409,7 @@ def html(db):
   <tr><th>Blueprint</th> <th>todo/postponed/done</th> <th>Completion</th> <th>Status</th></tr>
 '''
 
-    data = blueprint_stats(db)
+    data = blueprint_completion(db)
 
     completion = []
     for (bp, (todo, done, postponed, status)) in data.iteritems():
@@ -359,13 +419,38 @@ def html(db):
     completion.sort(key=lambda k: k[1], reverse=True)
 
     for (bp, percent) in completion:
-        print '  <tr><td><a href="%s/ubuntu/+spec/%s">%s</a></td> <td>%i/%i/%i</td> <td>%i%%</td> <td>%s</td></tr>' % (
-                blueprints_base_url, bp, bp, data[bp][0], data[bp][2],
+        if bp.startswith('http:'):
+            url = bp
+        else:
+            url = '%s/ubuntu/+spec/%s' % (blueprints_base_url, bp)
+        print '  <tr><td><a href="%s">%s</a></td> <td>%i/%i/%i</td> <td>%i%%</td> <td>%s</td></tr>' % (
+                url, bp, data[bp][0], data[bp][2],
                 data[bp][1], percent,
                 data[bp][-1])
 
     print '</table>'
 
+    print '''
+<h1>Status by assignee</h1>
+<table>
+  <tr><th>Assignee</th> <th>todo/postponed/done</th> <th>Completion</th></tr>
+'''
+    data = assignee_completion(db)
+
+    completion = []
+    for (a, (todo, done, postponed)) in data.iteritems():
+        completion.append((a,
+            int(float(postponed+done)/(todo+done+postponed)*100 + 0.5)))
+
+    completion.sort(key=lambda k: k[0], reverse=False)
+
+    for (a, percent) in completion:
+        url = '%s/~%s/+specs?role=assignee' % (blueprints_base_url, a)
+        print '  <tr><td><a href="%s">%s</a></td> <td>%i/%i/%i</td> <td>%i%%</td></tr>' % (
+                url, a, data[a][0], data[a][2],
+                data[a][1], percent)
+    print '</table>'
+
     print '</body></html>'
 
 def import_moin(db, urls):
@@ -373,7 +458,7 @@ def import_moin(db, urls):
 
     for url in urls:
         for (d, s) in get_moin_workitems(url):
-            add_work_item(db, url, d, s)
+            add_work_item(db, url, d, s, 'nobody')
 
 #
 # main