Subversion Repositories DevTools

Rev

Rev 5298 | Details | Compare with Previous | Last modification | View Log | RSS feed

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