-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateAnalytics.js
128 lines (101 loc) · 3.19 KB
/
StateAnalytics.js
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
"use strict";
var Promise = require("es6-promise").Promise;
var FileCategories = require("./FileCategories.js");
var type = require("typed");
var StateAnalytics = function(){
var NOT_FOUND = "NotFound";
var getAnalyticsOfStates= function(states){
// Extends the input object
return new Promise(function(resolve, reject){
states.map(function(state){
state.files = state.files.map(function(file){
var analyticsFile = type.create("FileAnalytics");
analyticsFile.name = file.name;
analyticsFile.markers = file.markers;
analyticsFile.tests = file.tests;
analyticsFile.foundTests = file.foundTests;
analyticsFile.foundMarkers= file.foundMarkers;
analyticsFile.numberOfLines = getLineCount(file);
analyticsFile.numberOfMarkers = getMarkerCount(file);
analyticsFile.numberOfFailedTests = getFailedTestsCount(file);
analyticsFile.numberOfTests = getTotalTestsCount(file);
analyticsFile.packageName = getPackageName(file);
analyticsFile.type = getType(file);
analyticsFile.contentName = getContentName(file);
//Note: depends on already gathered information
analyticsFile.categories = getCategoryRelations(analyticsFile);
return analyticsFile;
});
state.files = state.files.filter(function(file){
return _isValidFile(file);
});
});
resolve(states);
});
};
var _isValidFile = function(file){
return file.numberOfLines >0;
}
var getLineCount = function(file){
if(file !== undefined){
return file.fileContents.split("\n").length - 1;
}
};
var getMarkerCount = function(file){
return file.markers.length;
};
var getFailedTestsCount = function(file){
var failedTests = "?";
if(file.tests !== undefined && file.tests.length !== undefined){
failedTests = file.tests.reduce(function(p, c){
if(c.result === "Failure" || c.result === "Error"){
return p + 1;
}
return p;
}, 0);
}
return failedTests;
};
var getTotalTestsCount = function(file){
if(file.tests === undefined || file.tests.length === undefined){
return "?";
}
return file.tests.length;
};
var getPackageName = function(file){
var packageMatch = file.fileContents.match(/\s*package ([æ|ø|\w|\.]+);/);
var packageName = NOT_FOUND;
if(packageMatch !== null && packageMatch[1] !== null){
packageName = packageMatch[1];
}
return packageName;
};
var getType = function(file){
var classMatch = file.fileContents.match(/\s*public class (\w+)/);
var interfaceMatch = file.fileContents.match(/\s*public interface (\w+)/);
if(classMatch !== null){
return "class";
}
if(interfaceMatch !== null){
return "interface";
}
return NOT_FOUND; //Implement others on demand
};
var getContentName = function(file){
var classMatch = file.fileContents.match(/\s*public (?:class|interface) (\w+)/);
var className = NOT_FOUND;
if(classMatch !== null && classMatch[1] !== null){
className = classMatch[1];
}
return className;
};
var getCategoryRelations = function(file){
var categories = FileCategories.getCategoriesForFile(file);
return categories;
};
return {
getLineCount: getLineCount,
getAnalyticsOfStates: getAnalyticsOfStates
};
};
module.exports = new StateAnalytics();