Subversion Repositories DevTools

Rev

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

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