Subversion Repositories DevTools

Rev

Rev 4381 | Rev 4765 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4381 tlittlef 1
import json, urllib2, sys, datetime
4380 tlittlef 2
 
3
CRUCIBLE_URL_TEMPLATE = 'http://cds:8060/rest-service/reviews-v1'
4
RESULTS_KEY = u'results'
5
GEOMETRY_KEY = u'geometry'
6
LOCATION_KEY = u'location'
7
LAT_KEY = u'lat'
8
LNG_KEY = u'lng'
9
 
10
 
11
class CrucibleExtractor :
12
    def __init__(self) :
13
        pass
14
        self.selected_reviews = {}
15
        self.runtime_defects = {}
16
        self.maintenance_defects = {}
17
        self.security_defects = {}
18
        self.unclassified_defect_reviews = [ ]
19
        self.total_defects = 0
4420 tlittlef 20
        self.verbose = False
4380 tlittlef 21
    def url_to_object(self, url, top=None) :
22
        req = urllib2.Request(url)
23
        req.add_header('Accept','application/json')
24
        req.add_header('Authorization', 'Basic dGxpdHRsZWY6MCRtb3JQRVRI')
25
        resp = urllib2.urlopen(req).read()
26
        retval = json.loads(resp)
27
        if top : 
28
            retval = retval[top]
29
        return retval
30
    def get_selected_review_ids(self,min_create_date, max_create_date) :
31
        url = CRUCIBLE_URL_TEMPLATE
32
        reviewData = self.url_to_object(url, "reviewData")
33
        for r in reviewData :
34
            review_id = r["permaId"]["id"] 
35
            review_create_date = r["createDate"]
36
            if review_create_date>=min_create_date and review_create_date<=max_create_date :
37
                self.selected_reviews[review_id] = [ r[u'projectKey'], None, ]
38
            else :
39
                pass
40
        return self.selected_reviews.keys()
41
    def get_review_summary(self,review_id) :
42
        url = CRUCIBLE_URL_TEMPLATE + "/" + review_id + "/comments"
43
        projectKey = str(self.selected_reviews[review_id][0])
44
        reviewDetails = self.url_to_object(url,u'comments')
45
        retval = [ 
46
            projectKey,
47
            0, # non-defect comments
48
            0, # runtime defects
49
            0, # maintenance defects
50
            0, # security defects
51
            0, # unclassified defects
52
        ]
53
        for c in reviewDetails:
54
            if c[u'defectRaised'] is False :
55
                retval[1] += 1
56
            elif len(c[u'metrics'].keys()) != 1 : # unclassified - either none of the drop downs or more than one selected
57
                retval[5] += 1 
58
                self.unclassified_defect_reviews += [ review_id ]
59
                self.total_defects += 1
60
            else :
61
                defect_category_code =  c[u'metrics'].keys()[0]
62
                defect_type = c[u'metrics'][defect_category_code][u'value']
63
                category_defect_map = None
64
                if defect_category_code == u'metric-57' : # maintenance
65
                    retval[3] += 1
66
                    category_defect_map = self.maintenance_defects
67
                elif defect_category_code == u'metric-95' : # runtime
68
                    retval[2] += 1
69
                    category_defect_map = self.runtime_defects
4420 tlittlef 70
                elif defect_category_code == u'metric-97' : # security
4380 tlittlef 71
                    retval[4] += 1
72
                    category_defect_map = self.security_defects
4420 tlittlef 73
                else :
74
                    print u"Unexpected defect_category_code %s" % ( c[u'metrics'].keys()[0] )
75
                    retval[5] += 1 
76
                    self.unclassified_defect_reviews += [ review_id ]
77
                    self.total_defects += 1
4380 tlittlef 78
                if defect_type in category_defect_map.keys() :
79
                    category_defect_map[defect_type] += 1
80
                else :
81
                    category_defect_map[defect_type] = 1
82
 
83
                self.total_defects += 1
84
        self.selected_reviews[review_id][1] = retval[2:]    
85
        return retval
4381 tlittlef 86
    def summarize_by_project(self, month_prefix) :
87
        start_date = month_prefix + "-00"
88
        end_date = month_prefix + "-99"
4420 tlittlef 89
        if self.verbose : 
90
            print "Getting review ids"
4380 tlittlef 91
        review_ids = self.get_selected_review_ids(start_date, end_date)
92
        project_summaries = {}
93
        overall_summary = [ 0, ] * 6
94
        for review_id in sorted(review_ids) :
95
            if True :
96
                review_summary = self.get_review_summary(review_id)
4420 tlittlef 97
                if self.verbose : 
98
                    print "%-12s: %s" % ( review_id, self.selected_reviews[review_id][1] )
4380 tlittlef 99
                project_id = review_summary[0]
100
                if  project_id in project_summaries.keys() :
101
                    project_summary = project_summaries[project_id]
102
                else :
103
                    project_summary = [ 0,] * 6
104
                project_summary[0] += 1
105
                overall_summary[0] += 1
106
                for i in range(1,6) :
107
                    project_summary[i] += review_summary[i]
108
                    overall_summary[i] += review_summary[i]
109
                project_summaries[project_id] = project_summary
110
        REPORT_PRINTF_FORMAT = "%-12s %4s %4s %4s %4s %4s %4s"
111
        REPORT_FORMAT_SEPARATOR = ( '-------','---','---','---','---','---','---')
112
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
113
        print REPORT_PRINTF_FORMAT % ( "Project", "rev", "com", "run", "mnt", "sec", "oth" )
114
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
115
        for project_id in sorted(project_summaries.keys()) :
116
            ps = project_summaries[project_id]
117
            print REPORT_PRINTF_FORMAT % ( project_id, ps[0], ps[1], ps[2], ps[3], ps[4], ps[5] )
118
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
119
        ps = overall_summary
120
        print REPORT_PRINTF_FORMAT % ( "TOTAL", ps[0], ps[1], ps[2], ps[3], ps[4], ps[5] )
121
    def summarize_for_category(self,category_name, category_types) :
122
        REPORT_PRINTF_FORMAT = "%-75s %6s %6s"
123
        REPORT_FORMAT_SEPARATOR = ( '-----------------------','------','------')
124
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
125
        print REPORT_PRINTF_FORMAT % ( "%s defects" % (category_name),"count","%")
126
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
127
        count_for_category = 0
128
        percent_for_category = 0.0
129
        for d in sorted(category_types.keys()) :
130
            count_for_type = category_types[d]
131
            percent_for_type = int( (1000.0 * count_for_type)/self.total_defects) * 0.1
132
            count_for_category += count_for_type
133
            percent_for_category += percent_for_type
134
            d = d.replace(u"\u2013",u"-")
135
            print REPORT_PRINTF_FORMAT % ( d, count_for_type, percent_for_type )
136
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
137
        print REPORT_PRINTF_FORMAT % ( "Total %s" % (category_name,), count_for_category, percent_for_category )
138
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
139
    def summarize_by_defect_type(self) :
140
        self.summarize_for_category("maintenance",self.maintenance_defects)
141
        self.summarize_for_category("runtime",self.runtime_defects)
142
        self.summarize_for_category("security",self.security_defects)
143
    def report_on_unclassified_defects(self) : 
144
        print "Reviews with unclassified defects: %s" % (self.unclassified_defect_reviews,)
145
 
146
if __name__ == "__main__" :
147
    extractor = CrucibleExtractor()
4420 tlittlef 148
 
149
    if sys.argv[1] == "--usage" :
150
        print "crucible_reporting.py YYYY-MM"
151
        print "   report stats for specified month"
152
        print "crucible_reporting.py"
153
        print "   report stats for previous month"
154
        sys.exit(0)
155
    elif sys.argv[1] == "--verbose" :
156
        extractor.verbose = True
157
        sys.argv = sys.argv[1:]
158
 
4381 tlittlef 159
    if len(sys.argv) < 2 :
160
        today = datetime.date.today()
161
        if today.month!= 1 :
162
            month_prefix = "%04d-%02d" % (today.year, today.month-1)
163
        else :
164
            month_prefix = "%04d-%02d" % (today.year-1, 12)
165
        print "Generating report for previous month (" + month_prefix + ")"
166
    else :
167
        month_prefix = sys.argv[1]
168
 
169
    extractor.summarize_by_project(month_prefix)
4380 tlittlef 170
    extractor.summarize_by_defect_type()
171
    extractor.report_on_unclassified_defects()
172
 
173