]>
Commit | Line | Data |
---|---|---|
06a3d686 BB |
1 | /*global $:false, _:false, Morris:false, __report:false, __history:false, __options: false */ |
2 | /*jshint browser:true*/ | |
3 | ||
4 | $(function(){ | |
5 | "use strict"; | |
6 | ||
7 | // bootstrap popover | |
8 | $('[rel=popover]').popover(); | |
9 | ||
10 | // @todo put client side templates into a JST | |
11 | var fileGraphTemplate = _.template( | |
12 | '<div class="threshold-<%= threshold %>">' + | |
13 | '<label><%= label %></label>' + | |
14 | '<span class="horizontal-bar" style="width:<%= width %>px"></span>' + | |
15 | '<span class="chart-value"><%= value %></span>' + | |
16 | '</div>' | |
17 | ); | |
18 | ||
19 | var horizontalBar = function(orig, width, label, thresholds){ | |
20 | var threshold = 0; | |
21 | for (var i = thresholds.length - 1; i > -1; i--) { | |
22 | if (orig > thresholds[i]) { | |
23 | threshold = i + 1; | |
24 | break; | |
25 | } | |
26 | } | |
27 | return fileGraphTemplate({ | |
28 | width : width, | |
29 | label : label, | |
30 | threshold : threshold, | |
31 | value : orig | |
32 | }); | |
33 | }; | |
34 | ||
35 | function drawFileCharts() { | |
36 | // @todo make a jQuery plugin to accomodate the horizontalBar function | |
37 | $('.js-file-chart').each(function(){ | |
38 | var el = $(this), | |
39 | width = el.width() - 130; // @todo establish max width of graph in plugin | |
40 | ||
41 | el.empty(); | |
42 | ||
43 | var value = el.data('complexity'); | |
44 | el.append(horizontalBar(value, Math.min(value * 2, width),'complexity', [5,10])); | |
45 | ||
46 | value = el.data('sloc'); | |
47 | el.append(horizontalBar(value, Math.min(value, width), 'sloc', [400,600])); | |
48 | ||
49 | value = el.data('bugs'); | |
50 | el.append(horizontalBar(value, Math.min(value * 5, width), 'est errors', [1,5])); | |
51 | ||
52 | value = el.data('lint'); | |
53 | el.append(horizontalBar(value, Math.min(value * 5, width), 'lint errors', [1,10])); | |
54 | }); | |
55 | } | |
56 | ||
57 | function drawOverviewCharts(reports) { | |
58 | ||
59 | var maintainability = { | |
60 | element: 'chart_maintainability', | |
61 | data: [], | |
62 | xkey: 'label', | |
63 | ykeys: ['value'], | |
64 | ymax : 100, | |
65 | ymin : 0, | |
66 | labels: ['Maintainability'], | |
67 | barColors : ['#ff9b40'] | |
68 | }; | |
69 | var sloc = { | |
70 | element: 'chart_sloc', | |
71 | data: [], | |
72 | xkey: 'label', | |
73 | ykeys: ['value'], | |
74 | ymax : 400, | |
75 | labels: ['Lines'], | |
76 | barColors : ['#1f6b75'] | |
77 | }; | |
78 | var bugs = { | |
79 | element: 'chart_bugs', | |
80 | data: [], | |
81 | xkey: 'label', | |
82 | ykeys: ['value'], | |
83 | labels: ['Errors'], | |
84 | ymax: 20, | |
85 | barColors : ['#ff9b40'] | |
86 | }; | |
87 | var lint = { | |
88 | element: 'chart_lint', | |
89 | data: [], | |
90 | xkey: 'label', | |
91 | ykeys: ['value'], | |
92 | labels: ['Errors'], | |
93 | ymax: 20, | |
94 | barColors : ['#1f6b75'] | |
95 | }; | |
96 | ||
97 | reports.forEach(function(report){ | |
98 | ||
99 | // @todo shouldn't need this, 'auto [num]' doesn't seem to work : https://github.com/oesmith/morris.js/issues/201 | |
100 | sloc.ymax = Math.max(sloc.ymax, report.complexity.aggregate.complexity.sloc.physical); | |
101 | bugs.ymax = Math.max(bugs.ymax, report.complexity.aggregate.complexity.halstead.bugs.toFixed(2)); | |
102 | ||
103 | ||
104 | sloc.data.push({ | |
105 | value : report.complexity.aggregate.complexity.sloc.physical, | |
106 | label : report.info.fileShort | |
107 | }); | |
108 | bugs.data.push({ | |
109 | value : report.complexity.aggregate.complexity.halstead.bugs.toFixed(2), | |
110 | label : report.info.fileShort | |
111 | }); | |
112 | maintainability.data.push({ | |
113 | value : report.complexity.maintainability ? report.complexity.maintainability.toFixed(2) : 0, | |
114 | label : report.info.fileShort | |
115 | }); | |
116 | lint.data.push({ | |
117 | value : report.jshint && report.jshint.messages, | |
118 | label : report.info.fileShort | |
119 | }); | |
120 | }); | |
121 | ||
122 | function onGraphClick(i){ | |
123 | document.location = __report.reports[i].info.link; | |
124 | } | |
125 | ||
126 | var charts = [ | |
127 | Morris.Bar(bugs), | |
128 | Morris.Bar(sloc), | |
129 | Morris.Bar(maintainability) | |
130 | ]; | |
131 | ||
132 | if (__options.flags.jshint) charts.push(Morris.Bar(lint)); | |
133 | ||
134 | charts.forEach(function(chart){ | |
135 | chart.on('click', onGraphClick); | |
136 | }); | |
137 | return charts; | |
138 | } | |
139 | ||
140 | function drawHistoricalChart(history) { | |
141 | var data = _.map(history,function(record){ | |
142 | var date = new Date(record.date); | |
143 | return { | |
144 | date : date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate(), | |
145 | average_maintainability : parseFloat(record.average.maintainability), | |
146 | average_sloc : record.average.sloc | |
147 | }; | |
148 | }).slice(-20); | |
149 | Morris.Line({ | |
150 | element: 'chart_historical_sloc', | |
151 | data: data, | |
152 | xkey: 'date', | |
153 | ykeys: ['average_sloc'], | |
154 | labels: ['Average Lines'], | |
155 | parseTime : false | |
156 | }); | |
157 | Morris.Line({ | |
158 | element: 'chart_historical_maint', | |
159 | data: data, | |
160 | xkey: 'date', | |
161 | ykeys: ['average_maintainability'], | |
162 | labels: ['Maintainability'], | |
163 | ymax: 100, | |
164 | parseTime : false | |
165 | }); | |
166 | } | |
167 | ||
168 | function drawCharts() { | |
169 | $('.js-chart').empty(); | |
170 | drawHistoricalChart(__history); | |
171 | drawOverviewCharts(__report.reports); | |
172 | drawFileCharts(__report.reports); | |
173 | } | |
174 | ||
175 | drawCharts(); | |
176 | ||
177 | $(window).on('resize', _.debounce(drawCharts,200)); | |
178 | }); | |
179 | ||
180 | ||
181 |