Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
4766 tlittlef 1
mport 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
4765 tlittlef 78
                if category_defect_map is None :
79
                    pass
80
                elif defect_type in category_defect_map.keys() :
4380 tlittlef 81
                    category_defect_map[defect_type] += 1
82
                else :
83
                    category_defect_map[defect_type] = 1
84
                self.total_defects += 1
85
        self.selected_reviews[review_id][1] = retval[2:]    
86
        return retval
4381 tlittlef 87
    def summarize_by_project(self, month_prefix) :
88
        start_date = month_prefix + "-00"
89
        end_date = month_prefix + "-99"
4420 tlittlef 90
        if self.verbose : 
91
            print "Getting review ids"
4380 tlittlef 92
        review_ids = self.get_selected_review_ids(start_date, end_date)
93
        project_summaries = {}
94
        overall_summary = [ 0, ] * 6
95
        for review_id in sorted(review_ids) :
96
            if True :
97
                review_summary = self.get_review_summary(review_id)
4420 tlittlef 98
                if self.verbose : 
99
                    print "%-12s: %s" % ( review_id, self.selected_reviews[review_id][1] )
4380 tlittlef 100
                project_id = review_summary[0]
101
                if  project_id in project_summaries.keys() :
102
                    project_summary = project_summaries[project_id]
103
                else :
104
                    project_summary = [ 0,] * 6
105
                project_summary[0] += 1
106
                overall_summary[0] += 1
107
                for i in range(1,6) :
108
                    project_summary[i] += review_summary[i]
109
                    overall_summary[i] += review_summary[i]
110
                project_summaries[project_id] = project_summary
111
        REPORT_PRINTF_FORMAT = "%-12s %4s %4s %4s %4s %4s %4s"
112
        REPORT_FORMAT_SEPARATOR = ( '-------','---','---','---','---','---','---')
113
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
114
        print REPORT_PRINTF_FORMAT % ( "Project", "rev", "com", "run", "mnt", "sec", "oth" )
115
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
116
        for project_id in sorted(project_summaries.keys()) :
117
            ps = project_summaries[project_id]
118
            print REPORT_PRINTF_FORMAT % ( project_id, ps[0], ps[1], ps[2], ps[3], ps[4], ps[5] )
119
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
120
        ps = overall_summary
121
        print REPORT_PRINTF_FORMAT % ( "TOTAL", ps[0], ps[1], ps[2], ps[3], ps[4], ps[5] )
122
    def summarize_for_category(self,category_name, category_types) :
123
        REPORT_PRINTF_FORMAT = "%-75s %6s %6s"
124
        REPORT_FORMAT_SEPARATOR = ( '-----------------------','------','------')
125
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
126
        print REPORT_PRINTF_FORMAT % ( "%s defects" % (category_name),"count","%")
127
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
128
        count_for_category = 0
129
        percent_for_category = 0.0
130
        for d in sorted(category_types.keys()) :
131
            count_for_type = category_types[d]
132
            percent_for_type = int( (1000.0 * count_for_type)/self.total_defects) * 0.1
133
            count_for_category += count_for_type
134
            percent_for_category += percent_for_type
135
            d = d.replace(u"\u2013",u"-")
136
            print REPORT_PRINTF_FORMAT % ( d, count_for_type, percent_for_type )
137
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
138
        print REPORT_PRINTF_FORMAT % ( "Total %s" % (category_name,), count_for_category, percent_for_category )
139
        print REPORT_PRINTF_FORMAT % REPORT_FORMAT_SEPARATOR
140
    def summarize_by_defect_type(self) :
141
        self.summarize_for_category("maintenance",self.maintenance_defects)
142
        self.summarize_for_category("runtime",self.runtime_defects)
143
        self.summarize_for_category("security",self.security_defects)
144
    def report_on_unclassified_defects(self) : 
145
        print "Reviews with unclassified defects: %s" % (self.unclassified_defect_reviews,)
146
 
147
if __name__ == "__main__" :
148
    extractor = CrucibleExtractor()
4420 tlittlef 149
 
150
    if sys.argv[1] == "--usage" :
151
        print "crucible_reporting.py YYYY-MM"
152
        print "   report stats for specified month"
153
        print "crucible_reporting.py"
154
        print "   report stats for previous month"
155
        sys.exit(0)
156
    elif sys.argv[1] == "--verbose" :
157
        extractor.verbose = True
158
        sys.argv = sys.argv[1:]
159
 
4381 tlittlef 160
    if len(sys.argv) < 2 :
161
        today = datetime.date.today()
162
        if today.month!= 1 :
163
            month_prefix = "%04d-%02d" % (today.year, today.month-1)
164
        else :
165
            month_prefix = "%04d-%02d" % (today.year-1, 12)
166
        print "Generating report for previous month (" + month_prefix + ")"
167
    else :
168
        month_prefix = sys.argv[1]
169
 
170
    extractor.summarize_by_project(month_prefix)
4380 tlittlef 171
    extractor.summarize_by_defect_type()
172
    extractor.report_on_unclassified_defects()
173
 
174