All files / builds/1000i100/quiz-maker/generated/maintainability/assets/scripts plato-overview.js

0% Statements 0/190
0% Branches 0/1
0% Functions 0/1
0% Lines 0/190

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191                                                                                                                                                                                                                                                                                                                                                                                             
/*global $:false, _:false, Morris:false, __report:false, __history:false, __options: false */
/*jshint browser:true*/

$(function() {
	'use strict';

  // bootstrap popover
	$('[rel=popover]').popover();

  // @todo put client side templates into a JST
	var fileGraphTemplate = _.template(
    '<div class="threshold-<%= threshold %>">' +
    '<label><%= label %></label>' +
    '<span class="horizontal-bar" style="width:<%= width %>px"></span>' +
    '<span class="chart-value"><%= value %></span>' +
    '</div>'
  );

	var horizontalBar = function(orig, width, label, thresholds) {
		var threshold = 0;
		for (var i = thresholds.length - 1; i > -1; i--) {
			if (orig > thresholds[i]) {
				threshold = i + 1;
				break;
			}
		}
		return fileGraphTemplate({
			width: width,
			label: label,
			threshold: threshold,
			value: orig
		});
	};

	function drawFileCharts() {
    // @todo make a jQuery plugin to accomodate the horizontalBar function
		$('.js-file-chart').each(function() {
			var el = $(this),
				width = el.width() - 130; // @todo establish max width of graph in plugin

			el.empty();

			var value = el.data('complexity');
			el.append(horizontalBar(value, Math.min(value * 2, width), 'complexity', [5, 10]));

			value = el.data('sloc');
			el.append(horizontalBar(value, Math.min(value, width), 'sloc', [400, 600]));

			value = el.data('bugs');
			el.append(horizontalBar(value, Math.min(value * 5, width), 'est errors', [1, 5]));

			if (__options.flags.eslint) {
				value = el.data('lint');
				el.append(horizontalBar(value, Math.min(value * 5, width), 'lint errors', [1, 10]));
			}
		});
	}

	function drawOverviewCharts(reports) {

		var maintainability = {
			element: 'chart_maintainability',
			data: [],
			xkey: 'label',
			ykeys: ['value'],
			ymax: 100,
			ymin: 0,
			labels: ['Maintainability'],
			barColors: ['#ff9b40']
		};
		var sloc = {
			element: 'chart_sloc',
			data: [],
			xkey: 'label',
			ykeys: ['value'],
			ymax: 400,
			labels: ['Lines'],
			barColors: ['#1f6b75']
		};
		var bugs = {
			element: 'chart_bugs',
			data: [],
			xkey: 'label',
			ykeys: ['value'],
			labels: ['Errors'],
			ymax: 20,
			barColors: ['#ff9b40']
		};
		var lint = {
			element: 'chart_lint',
			data: [],
			xkey: 'label',
			ykeys: ['value'],
			labels: ['Errors'],
			ymax: 20,
			barColors: ['#1f6b75']
		};

		reports.forEach(function(report) {

      // @todo shouldn't need this, 'auto [num]' doesn't seem to work : https://github.com/oesmith/morris.js/issues/201
			sloc.ymax = Math.max(sloc.ymax, report.complexity.aggregate.complexity.sloc.physical);
			bugs.ymax = Math.max(bugs.ymax, report.complexity.aggregate.complexity.halstead.bugs.toFixed(2));


			sloc.data.push({
				value: report.complexity.aggregate.complexity.sloc.physical,
				label: report.info.fileShort
			});
			bugs.data.push({
				value: report.complexity.aggregate.complexity.halstead.bugs.toFixed(2),
				label: report.info.fileShort
			});
			maintainability.data.push({
				value: report.complexity.maintainability ? report.complexity.maintainability.toFixed(2) : 0,
				label: report.info.fileShort
			});

			console.log(report);

			lint.data.push({
				value: report.eslint && report.eslint.messages,
				label: report.info.fileShort
			});
		});

		function onGraphClick(i) {
      // If the i is not set, we jump to the last file in the list. This
      // preserves a behavior from Morris v1. I expect Plato V1 to be deprecated
      // and this hack is mearly to preserve the casper tests.
			if (i == null || isNaN(i)) {
				i = __report.reports.length - 1;
			}
			document.location = __report.reports[i].info.link;
		}

		var charts = [
			Morris.Bar(bugs),
			Morris.Bar(sloc),
			Morris.Bar(maintainability)
		];

		if (__options.flags.eslint) charts.push(Morris.Bar(lint));

		charts.forEach(function(chart) {
			chart.on('click', onGraphClick);
		});
		return charts;
	}

	function drawHistoricalChart(history) {
		var data = _.map(history, function(record) {
			var date = new Date(record.date);
			return {
				date: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate(),
				average_maintainability: parseFloat(record.average.maintainability),
				average_sloc: record.average.sloc
			};
		}).slice(-20);
		Morris.Line({
			element: 'chart_historical_sloc',
			data: data,
			xkey: 'date',
			ykeys: ['average_sloc'],
			labels: ['Average Lines'],
			parseTime: false
		});
		Morris.Line({
			element: 'chart_historical_maint',
			data: data,
			xkey: 'date',
			ykeys: ['average_maintainability'],
			labels: ['Maintainability'],
			ymax: 100,
			parseTime: false
		});
	}

	function drawCharts() {

		$('.js-chart').empty();
		drawHistoricalChart(__history);
		drawOverviewCharts(__report.reports);
		drawFileCharts(__report.reports);
	}

	drawCharts();

	$(window).on('resize', _.debounce(drawCharts, 200));
});