This repository has been archived by the owner on Feb 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
logcruncher.js
98 lines (88 loc) · 2.54 KB
/
logcruncher.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
importScripts("workerApi.js");
onmessage = messageAdapter(new LogReader());
function LogReader() {}
LogReader.prototype.read = read(readFile);
LogReader.prototype.readURL = read(readURL);
function read(reader) {
return (data) => {
return reader(data)
.then((logData) => {return getLogType(logData)();});
};
}
function getLogType(logData) {
var parsed = null;
var mozlogParser = () => parseMozlog(logData, testsFilter);
try {
parsed = JSON.parse(logData);
} catch (e) {
return mozlogParser;
}
if (!parsed.hasOwnProperty("results")) {
return mozlogParser;
}
return () => parseRunnerJSON(parsed);
}
function parseMozlog(rawtext, filter) {
return new Promise(function (resolve, reject) {
var JSONArray = [];
var lines = rawtext.split('\n');
for (var i = 0; i < lines.length; i++) {
if (lines[i] === "") {
continue;
}
var json = JSON.parse(lines[i]);
if (filter(json)) {
JSONArray.push(json);
}
}
resolve(JSONArray);
});
}
function parseRunnerJSON(parsedJson) {
return new Promise((resolve, reject) => {
var JSONArray = [];
parsedJson.results.forEach((result) => {
JSONArray.push({"action": "test_start",
"test": result.test});
result.subtests.forEach((subtest) => {
JSONArray.push({"action": "test_status",
"test": result.test,
"subtest": subtest.name,
"status": subtest.status,
"message": subtest.message});
});
JSONArray.push({"action": "test_end",
"test": result.test,
"status": result.status,
"message": result.message});
});
resolve(JSONArray);
});
}
function testsFilter(parsedLine) {
var pattr = /^test_/;
var pattr2 = /^(?:error)|(?:critical)/i;
return (pattr.test(parsedLine.action) || (parsedLine.action === "log" && pattr2.test(parsedLine.level)));
}
function readFile(file) {
return new Promise(function(resolve, reject) {
var reader = new FileReaderSync();
resolve(reader.readAsText(file, "UTF-8"));
});
}
function readURL(url) {
return new Promise(function(resolve, reject) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState === 4) {
if (xhttp.status === 200) {
resolve(xhttp.responseText);
} else {
reject("HTTP request failed!");
}
}
};
xhttp.open('GET', url, false);
xhttp.send();
});
}