Skip to content

Commit

Permalink
main.js: Add service to parse coala_json output
Browse files Browse the repository at this point in the history
Closes coala#34
  • Loading branch information
hemangsk committed Aug 18, 2017
1 parent 34cb6b8 commit 6f67174
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 31 deletions.
74 changes: 44 additions & 30 deletions coalahtml/_coalahtml/app/controllers/main.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,52 @@
'use strict';

angular.module('coalaHtmlApp')
.controller('MainCtrl',["$scope", "$rootScope", "$http",
function($scope, $rootScope, $http) {
var parseCoalaProject = function() {
$scope.data = [];
var knownFiles = {};
var parseResult = function(result) {
result.affected_code.forEach(function(sourceRange) {
if (!(sourceRange.file in knownFiles)) {
knownFiles[sourceRange.file] = [];
}
knownFiles[sourceRange.file].push({
"start": sourceRange.start.line,
"end": sourceRange.end.line,
"diffs": result.diffs,
"message": result.message,
"origin": result.origin,
"severity": result.severity
});
.factory('coalaJSON', function($http, $rootScope){
var parseResult = function (result) {
result.affected_code.forEach(function(sourceRange) {
if (!(sourceRange.file in $rootScope.knownFiles)) {
$rootScope.knownFiles[sourceRange.file] = [];
}
$rootScope.knownFiles[sourceRange.file].push({
"start": sourceRange.start.line,
"end": sourceRange.end.line,
"diffs": result.diffs,
"message": result.message,
"origin": result.origin,
"severity": result.severity
});
};
});
};
return {
async: function () {
var promise =
$http.get($rootScope.CONSTANTS.data + $rootScope.CONSTANTS.coala)
.then(function(coala_json) {
$rootScope.COALA_JSON = coala_json.data;
$rootScope.logsCount = coala_json.data.logs.length;
var resultsCount = 0;
for (var section in $rootScope.COALA_JSON.results) {
$rootScope.COALA_JSON.results[section].forEach(parseResult);
resultsCount += $rootScope.COALA_JSON.results[section].length;
}
$rootScope.resultsCount = resultsCount;
$scope.data.push(knownFiles);
$rootScope.resultFiles = knownFiles;
.then(function (response) {
return response;
});
return promise;
},
parseJSONResults: function (coala_json) {
$rootScope.COALA_JSON = coala_json.data;
$rootScope.logsCount = coala_json.data.logs.length;
var resultsCount = 0;
for (var section in $rootScope.COALA_JSON.results) {
$rootScope.COALA_JSON.results[section].forEach(parseResult);
resultsCount += $rootScope.COALA_JSON.results[section].length;
}
$rootScope.resultsCount = resultsCount;
$rootScope.data.push($rootScope.knownFiles);
$rootScope.resultFiles = $rootScope.knownFiles;
}
};
})
.controller('MainCtrl',["$scope", "$rootScope", "$http", "coalaJSON",
function($scope, $rootScope, $http, coalaJSON) {
var parseCoalaProject = function() {
$rootScope.data = [];
$rootScope.knownFiles = {};
coalaJSON.async().then(function (coala_json) {
coalaJSON.parseJSONResults(coala_json);
});
};

Expand Down
17 changes: 16 additions & 1 deletion tests/specs/controllers/MainCtrlSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,26 @@ describe('MainCtrl', function() {
describe('search', function () {

beforeEach(module('coalaHtmlApp'));
beforeEach(inject(function ($controller) {
beforeEach(inject(function ($controller, $injector, _$rootScope_) {
$httpBackend = $injector.get('$httpBackend');
rootScope = _$rootScope_;
scope = _$rootScope_.$new();
$httpBackend.whenGET('data/Constants.json')
.respond(200, CONSTANTS);
$httpBackend.whenGET(CONSTANTS.data + CONSTANTS.coala)
.respond(200, COALA_JSON);
$httpBackend.whenGET(CONSTANTS.data + CONSTANTS.file_data)
.respond(200, FILE_DATA);
$httpBackend.whenGET(CONSTANTS.data + CONSTANTS.files)
.respond(200, FILES);
$httpBackend.whenGET(CONSTANTS.roothome)
.respond(200, ROOTHOME);
$httpBackend.flush();
$controller('MainCtrl', {
$scope: scope,
$rootScope: rootScope
});
$httpBackend.flush();
}));

it('test search matching', function() {
Expand Down

0 comments on commit 6f67174

Please sign in to comment.