summaryrefslogtreecommitdiffstats
path: root/scripts/abrt-bz-stats
blob: ea44f5bda2f4f8f54869dd0f0a71bce18c4ed18b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/python
# -*- mode:python -*-
# ABRT Bugzilla Statistics script
#
# Please do not run this script unless it's neccessary to do so.
# It forces Bugzilla to send info about thousands of bug reports.

from bugzilla import RHBugzilla
from optparse import OptionParser
import sys
import os.path
import subprocess
from datetime import datetime
import pickle

#
# Parse the command line input
#
parser = OptionParser(version="%prog 1.0")
parser.add_option("-u", "--user", dest="user",
                  help="Bugzilla user name (REQUIRED)", metavar="USERNAME")
parser.add_option("-p", "--password", dest="password",
                  help="Bugzilla password (REQUIRED)", metavar="PASSWORD")
parser.add_option("-b", "--bugzilla", dest="bugzilla",
                  help="Bugzilla URL (defaults to Red Hat Bugzilla)", metavar="URL")
# Weekly stats shows the impact of changes in ABRT early.
parser.add_option("-w", "--weekly", help="Generate weekly report instead of monthly",
                  action="store_true", default=False, dest="weekly")
# HTML output for blogs etc.
parser.add_option("-t", "--html", help="Generate HTML output",
                  action="store_true", default=False, dest="html")
parser.add_option("-i", "--wiki", help="Generate output in wiki syntax",
                  action="store_true", default=False, dest="wiki")
# Newest stats first
parser.add_option("-r", "--reversed", help="Display the newest stats first",
                  action="store_true", default=False, dest="reversed")
(options, args) = parser.parse_args()
if not options.user or len(options.user) == 0:
  parser.error("User name is required.\nTry {0} --help".format(sys.argv[0]))
if not options.password or len(options.password) == 0:
  parser.error("Password is required.\nTry {0} --help".format(sys.argv[0]))
if not options.bugzilla or len(options.bugzilla) == 0:
  options.bugzilla = "https://bugzilla.redhat.com/xmlrpc.cgi"

#
# Connect to Bugzilla and get the list of all bugs reported by ABRT
#
bz = RHBugzilla()
bz.connect(options.bugzilla)
bz.login(options.user, options.password)

buginfos = bz.query({'status_whiteboard_type':'allwordssubstr','status_whiteboard':'abrt_hash'})
total = len(buginfos)
print "{0} bugs found.".format(total)

#
# Load cache from previous run. Speeds up the case Bugzilla closes connection.
#
buginfos_loaded = {}
CACHE_FILE = "abrt-bz-stats-cache.tmp"
if os.path.isfile(CACHE_FILE):
  f = open(CACHE_FILE, 'r')
  buginfos_loaded = pickle.load(f)
  f.close()

def save_to_cache():
  global buginfos_loaded
  f = open(CACHE_FILE, 'w')
  pickle.dump(buginfos_loaded, f, 2)
  f.close()

#
# Load data from Bugzilla
#
count = 0
for buginfo in buginfos:
  count += 1
  print "{0}/{1}".format(count, total)

  if count % 100 == 0:
    save_to_cache()

  if buginfos_loaded.has_key(buginfo.bug_id):
    continue

  # creation date, format YEAR-MONTH-DAY
  created = buginfo.creation_ts[0:10].replace(".", "-")
  # last change to bug, format YEAR-MONTH-DAY
  lastchange = buginfo.delta_ts[0:10].replace(".", "-")
  status = buginfo.bug_status # status during the last change
  if buginfo.resolution != "":
    status += "_" + buginfo.resolution
  buginfos_loaded[buginfo.bug_id] = {
    'created':created,
    'lastchange':lastchange,
    'status':status,
    'component':buginfo.component}

bz.logout()
save_to_cache()

#
# Interpret data from Bugzilla
#
# Bugs reported this month/week by ABRT
# Bugs closed as useful this month/week by ABRT
# Bugs closed as waste this month/week by ABRT
# Top crashers this month/week.
#
class TimeSpan:
  """
  It's either a week or month.
  """
  def __init__(self):
    # Number of bugs reported to certain component this month.
    self.components = {}

    self.closed_as_useful = 0
    self.closed_as_waste = 0
    self.closed_as_other = 0

  def bugs_reported(self):
    result = 0
    for component in self.components.values():
      result += component
    return result

  def top_crashers(self, n = 10):
    """
    Top n components causing crash this month.
    Returns list of tuples (component, number of crashes)
    """
    result = sorted(self.components.items(), key=lambda x: x[1])
    result.reverse()
    return result[0:n]

  def closed_as_useful_percentage(self):
    return int(100 * self.closed_as_useful / self.closed())

  def closed_as_waste_percentage(self):
    return int(100 * self.closed_as_waste / self.closed())

  def closed_as_other_percentage(self):
    return 100 - self.closed_as_useful_percentage() \
        - self.closed_as_waste_percentage()

  def closed(self):
    return self.closed_as_useful + self.closed_as_waste + self.closed_as_other

  def add_component_crash(self, component):
    if component in self.components:
      self.components[component] += 1
    else:
      self.components[component] = 1

  def add_resolution(self, resolution):
    # Catches only resolutions starting with "CLOSED_"
    if resolution in ["CLOSED_CURRENTRELEASE", "CLOSED_RAWHIDE", "CLOSED_ERRATA",
                      "CLOSED_UPSTREAM", "CLOSED_NEXTRELEASE"]:
      self.closed_as_useful += 1
    elif resolution in ["CLOSED_DUPLICATE", "CLOSED_CANTFIX",
                        "CLOSED_INSUFFICIENT_DATA"]:
      self.closed_as_waste += 1
    elif resolution in ["CLOSED_NOTABUG", "CLOSED_WONTFIX",
                           "CLOSED_DEFERRED", "CLOSED_WORKSFORME"]:
      self.closed_as_other += 1

  def __str__(self):
    def bug(count):
      if count == 1:
        return "%d bug" % count
      else:
        return "%d bugs" % count

    def crash(count):
      if count == 1:
        return "%d crash" % count
      else:
        return "%d crashes" % count

    start = ""
    bugs_reported = " - %s reported\n"
    bugs_closed = " - %s closed\n"
    bugs_cl_useful =  "   - %s (%d%%) as fixed, so ABRT was useful\n"
    bugs_cl_notuseful = "   - %s (%d%%) as duplicate, can't fix, insuf. data, so ABRT was not useful\n"
    bugs_cl_other = "   - %s (%d%%) as notabug, wontfix, worksforme\n"
    bugs_closed_end = ""
    top_crashers = " - top crashers:\n"
    top_crasher_item = "    # %s: %s\n"
    top_crashers_end = ""
    end = ""
    if options.html:
      start = "<ul>\n"
      bugs_reported = "<li>%s reported</li>\n"
      bugs_closed = "<li>%s closed\n<ul>\n"
      bugs_cl_useful = "  <li>%s (%d%%) as fixed, so ABRT was useful</li>\n"
      bugs_cl_notuseful = "   <li>%s (%d%%) as duplicate, can't fix, insuf. data, so ABRT was not useful</li>\n"
      bugs_cl_other = "   <li>%s (%d%%) as notabug, wontfix, worksforme</li>\n"
      bugs_closed_end = "</ul></li>\n"
      top_crashers = "<li>top crashers:\n<ol>\n"
      top_crasher_item = "    <li>%s: %s</li>\n"
      top_crashers_end = "</ol></li>\n"
      end = "</ul>\n"
    elif options.wiki:
      start = ""
      bugs_reported = "* %s reported\n"
      bugs_closed = "* %s closed\n"
      bugs_cl_useful =  "** %s (%d%%) as fixed, so ABRT was useful\n"
      bugs_cl_notuseful = "** %s (%d%%) as duplicate, can't fix, insuf. data, so ABRT was not useful\n"
      bugs_cl_other = "** %s (%d%%) as notabug, wontfix, worksforme\n"
      bugs_closed_end = ""
      top_crashers = "* top crashers:\n"
      top_crasher_item = "*# %s: %s\n"
      top_crashers_end = ""
      end = ""


    str = start
    str += bugs_reported % bug(self.bugs_reported())
    if self.closed() > 0:
      str += bugs_closed % bug(self.closed())
      if self.closed_as_useful > 0:
        str += bugs_cl_useful % (bug(self.closed_as_useful), self.closed_as_useful_percentage())
      if self.closed_as_waste > 0:
        str += bugs_cl_notuseful % (bug(self.closed_as_waste), self.closed_as_waste_percentage())
      if self.closed_as_other > 0:
        str += bugs_cl_other % (bug(self.closed_as_other), self.closed_as_other_percentage())
      str += bugs_closed_end
    if len(self.top_crashers()) > 0:
      str += top_crashers
      for (component, num_crashes) in self.top_crashers():
        str += top_crasher_item % (component, crash(num_crashes))
      str += top_crashers_end
    str += end
    return str

monthly_stats = {} # key == YEAR-MONTH, value == Month()
weekly_stats = {} # key == YEAR-WEEK, value == Month()

def get_month(month):
  global monthly_stats
  if month in monthly_stats:
    return monthly_stats[month]
  else:
    monthly_stats[month] = TimeSpan()
    return monthly_stats[month]

def get_week(week):
  global weekly_stats
  if week in weekly_stats:
    return weekly_stats[week]
  else:
    weekly_stats[week] = TimeSpan()
    return weekly_stats[week]

for buginfo in buginfos_loaded.values():
  # Bugs reported this month by ABRT
  # Top crashers this month
  month_key = buginfo['created'][0:7]
  month = get_month(month_key)
  month.add_component_crash(buginfo['component'])

  # Bugs reported this week by ABRT
  # Top crashers this week
  week_key = datetime.strptime(buginfo['created'], "%Y-%m-%d").strftime("%Y-%W")
  week = get_week(week_key)
  week.add_component_crash(buginfo['component'])

  # Bugs closed as useful this month by ABRT
  # Bugs closed as waste this month by ABRT
  month_key = buginfo['lastchange'][0:7]
  month = get_month(month_key)
  month.add_resolution(buginfo['status'])

  # Bugs closed as useful this week by ABRT
  # Bugs closed as waste this week by ABRT
  week_key = datetime.strptime(buginfo['lastchange'], "%Y-%m-%d").strftime("%Y-%W")
  week = get_week(week_key)
  week.add_resolution(buginfo['status'])

#
# Print interpreted data
#
print "STATS"
print "=========================================================================="
if not options.weekly:
  months = monthly_stats.keys()
  months.sort()
  if options.reversed:
    months.reverse()
  for month in months:
    m = monthly_stats[month]
    if options.html:
      print "<h2>Month %s</h2>" % month
    elif options.wiki:
      print "==Month %s==" % month
    else:
      print "MONTH %s" % month
    print m
else:
  weeks = weekly_stats.keys()
  weeks.sort()
  if options.reversed:
    weeks.reverse()
  for week in weeks:
    w = weekly_stats[week]
    if options.html:
      print "<h2>Week %s</h2>" % week
    elif options.wiki:
      print "==Week %s==" % week
    else:
      print "WEEK %s" % week
    print w