Skip to content

Commit 8eed3a5

Browse files
committed
fix: multiple fixes, lint cleanup
Fixed bug where date filter was not applied to history and summary got old data. Fixed bug where issues with 0 messages were reported in top 10. Filter duplicates from resolved reporting. Print output indicating no items were created/resolved/had activity. Variable name changes. add second CLI argument to set interval add example output to comment document format of main accumulator variables remove math import - used len(str(max num)) rther than log base 10. lint: replace id variable, sort imports
1 parent f976878 commit 8eed3a5

File tree

1 file changed

+76
-27
lines changed

1 file changed

+76
-27
lines changed

scripts/weekly-report

Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,106 @@
33
# This script generates a simple report outlining the activity in one
44
# tracker for the most recent week.
55

6+
# A second argument is the negative interval to change period
7+
# of time.
8+
69
# This script is free software, you may redistribute it
710
# and/or modify under the same terms as Python.
811

12+
#Example output
13+
#CREATED:
14+
#2702: new item
15+
#
16+
#RESOLVED:
17+
#1995: Where is my Power plugs
18+
#2501: Can you help me with Sanity
19+
#459: I need Sanity
20+
#
21+
#TOP TEN MOST DISCUSSED:
22+
#2 - 491: Can you help me with Sanity
23+
#1 - 1995: Where is my Power plugs
24+
925
from __future__ import print_function
10-
import sys, math
11-
from roundup import instance, date
26+
27+
import sys
28+
29+
from roundup import date, instance
30+
31+
# position for arguments
32+
tracker_home_pos = 1
33+
optional_interval_pos = 2
34+
35+
# gather args
36+
arg_len = len(sys.argv)
37+
# map pos to length by adding 1.
38+
if arg_len not in [tracker_home_pos + 1, optional_interval_pos + 1]:
39+
print('Usage: %s tracker-home [interval -1w]' % sys.argv[0])
40+
if (arg_len < tracker_home_pos + 1 ):
41+
print(' You need to specify a tracker home directory')
42+
sys.exit(1)
43+
instance_home = sys.argv[tracker_home_pos]
44+
lookback_interval = sys.argv[optional_interval_pos] if \
45+
len(sys.argv) == optional_interval_pos + 1 else '-1w'
1246

1347
# open the instance
14-
if len(sys.argv) != 2:
15-
print('You need to specify an instance home dir')
16-
instance_home = sys.argv[1]
1748
instance = instance.open(instance_home)
1849
db = instance.open('admin')
1950

20-
old = date.Date('-1w')
51+
old = date.Date(lookback_interval)
2152

22-
created = []
23-
summary = {}
24-
messages = []
53+
created = [] # [issue_id_created_issue]
54+
summary = {} # {status_id: [issue_ids in that status]}
55+
messages = [] # [(number_of_messages, issue_id)]
2556

2657
# loop through all the recently-active issues
2758
for issue_id in db.issue.filter(None, {'activity': '-1w;'}):
28-
num = 0
29-
for x,ts,userid,action,data in db.issue.history(issue_id):
30-
if ts < old: continue
59+
message_count = 0
60+
for _x,ts,_userid,action,data in db.issue.history(issue_id):
61+
if ts < old: # history occurred before our current window
62+
continue
3163
if action == 'create':
3264
created.append(issue_id)
3365
elif action == 'set' and 'messages' in data:
34-
num += 1
35-
summary.setdefault(db.issue.get(issue_id, 'status'), []).append(issue_id)
36-
messages.append((num, issue_id))
66+
message_count += 1
67+
summary.setdefault(db.issue.get(issue_id, 'status'),
68+
[]).append(issue_id)
69+
messages.append((message_count, issue_id))
3770

38-
#print 'STATUS SUMMARY:'
71+
#print('STATUS SUMMARY:')
3972
#for k,v in summary.items():
40-
# print k, len(v)
73+
# print(k, len(v))
4174

4275
print('\nCREATED:')
43-
print('\n'.join(['%s: %s'%(id, db.issue.get(id, 'title'))
44-
for id in created]))
76+
if created:
77+
print('\n'.join(['%s: %s'%(itemid, db.issue.get(itemid, 'title'))
78+
for itemid in created]))
79+
else:
80+
print("No issue created in interval %s" % lookback_interval)
4581

4682
print('\nRESOLVED:')
4783
resolved_id = db.status.lookup('resolved')
48-
print('\n'.join(['%s: %s'%(id, db.issue.get(id, 'title'))
49-
for id in summary.get(resolved_id, [])]))
84+
if summary:
85+
# deduplicate - duplicates happen when issue with resolved status
86+
# has multiple history items (e.g. message or other
87+
# change after resolution)
88+
resolved_ids = sorted(set(summary.get(resolved_id, [])), key=int)
89+
print('\n'.join(['%s: %s' % (itemid, db.issue.get(itemid, 'title'))
90+
for itemid in resolved_ids ]))
91+
else:
92+
print("No issue resolved in interval %s" % lookback_interval)
5093

5194
print('\nTOP TEN MOST DISCUSSED:')
52-
messages.sort()
53-
messages.reverse()
54-
nmax = messages[0][0]
55-
fmt = '%%%dd - %%s: %%s'%(int(math.log(nmax, 10)) + 1)
56-
print('\n'.join([fmt%(num, id, db.issue.get(id, 'title'))
57-
for num, id in messages[:10]]))
95+
96+
# filter out issues with no messages
97+
messages = [ message for message in messages if message[0] > 0 ]
98+
if messages:
99+
messages.sort()
100+
messages.reverse()
101+
nmax = messages[0][0] or 1
102+
fmt = '%%%dd - %%s: %%s'%(len(str(nmax)))
103+
print('\n'.join([fmt%(num, itemid, db.issue.get(itemid, 'title'))
104+
for num, itemid in messages[:10]]))
105+
else:
106+
print("No issues discussed in interval %s" % lookback_interval)
58107

59108
# vim: set filetype=python ts=4 sw=4 et si

0 commit comments

Comments
 (0)