-
Notifications
You must be signed in to change notification settings - Fork 18
/
scomp.js
395 lines (379 loc) · 13.3 KB
/
scomp.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
var gDebug, gLog;
var gAnalysisPath = "../../";
var gBzBasePath = "https://bugzilla.mozilla.org/";
var gSocorroPath = "https://crash-stats.mozilla.org/";
var gSearchBase,
gSearch1,
gSearch2,
gLimit = 20,
gFetchLimit = 300;
var gSigData = {},
gSocorroAPIToken,
gBugInfo = {};
window.onload = function () {
// Socorro API token is not required, so the implementation in this code was never finished.
gSocorroAPIToken = getParameterByName("token");
// Fetch Super Search common parameters and specific parameters for query 1 and 2.
searchCommon = getParameterByName("common");
searchParam1 = getParameterByName("p1");
searchParam2 = getParameterByName("p2");
// Fetch limits from parameters.
var limit = getParameterByName("limit");
if (limit.match(/^(\d+)+$/) && limit >= 3 && limit <= 1000) {
gLimit = limit;
}
var flimit = getParameterByName("fetchlimit");
if (flimit.match(/^(\d+)+$/) && flimit >= 3 && flimit <= 1000) {
gFetchLimit = flimit;
}
gSearch1 = searchCommon + (searchParam1 ? "&" + searchParam1 : "");
gSearch2 = searchCommon + (searchParam2 ? "&" + searchParam2 : "");
gSearchBase = searchCommon;
document.getElementById("paramCommon").value = searchCommon;
document.getElementById("paramS1").value = searchParam1;
document.getElementById("paramS2").value = searchParam2;
document.getElementById("search1link").href =
gSocorroPath + "search/?" + gSearch1;
document.getElementById("search2link").href =
gSocorroPath + "search/?" + gSearch2;
if (gSearch1 && gSearch2 && gSearch1 != gSearch2) {
processData();
} else {
document.getElementById("scompParams").classList.remove("hidden");
document.getElementById("scompResult").classList.add("hidden");
}
document.getElementById("searchParamToggle").onclick = function () {
var paramArea = document.getElementById("scompParams");
if (paramArea.classList.contains("hidden")) {
paramArea.classList.remove("hidden");
} else {
paramArea.classList.add("hidden");
}
return false;
};
};
function processData() {
var tblBody = document.getElementById("scompTBody");
displayMessage("Requesting data for first search…");
// Only return the signature facet, no "normal" results (crash IDs).
fetchFile(
gSocorroPath +
"api/SuperSearch/?_facets=signature&_results_number=0" +
"&" +
gSearch1 +
"&_facets_size=" +
gFetchLimit,
"json",
function (aData1) {
if (aData1) {
var result1 = aData1.facets.signature;
var total1 = aData1.total;
displayMessage("Requesting data for second search…");
fetchFile(
gSocorroPath +
"api/SuperSearch/?_facets=signature&_results_number=0" +
"&" +
gSearch2 +
"&_facets_size=" +
gFetchLimit,
"json",
function (aData2) {
if (aData2) {
var result2 = aData2.facets.signature;
var total2 = aData2.total;
displayMessage(
"Processing " +
result1.length +
" and " +
result2.length +
" results for searches…"
);
// Assemble data into a form we can use.
for (var i = 0; i <= result1.length - 1; i++) {
gSigData[result1[i].term] = {
count1: result1[i].count,
pct1: (100 * result1[i].count) / total1,
signature: result1[i].term,
count2: 0,
pct2: 0,
};
}
// Assemble data into a form we can use.
for (var i = 0; i <= result2.length - 1; i++) {
if (!gSigData[result2[i].term]) {
gSigData[result2[i].term] = {
count1: 0,
pct1: 0,
signature: result2[i].term,
};
}
gSigData[result2[i].term].count2 = result2[i].count;
gSigData[result2[i].term].pct2 =
(100 * result2[i].count) / total2;
}
// Calculate the differences.
for (var signature in gSigData) {
gSigData[signature].pctcmp =
gSigData[signature].pct2 - gSigData[signature].pct1;
}
setTimeout(function () {
buildDataTable();
fetchBugs();
}, 0);
} else {
displayMessage("ERROR - couldn't fetch search #2!");
}
}
);
} else {
displayMessage("ERROR - couldn't fetch search #1!");
}
}
);
}
function fetchBugs() {
for (var signature in gSigData) {
gSigData[signature].bugs = [];
if (document.getElementById("sdata_" + encodeURIComponent(signature))) {
// Only actually fetch if this is actually shown.
fetchBugsForSignature(signature);
}
}
}
function fetchBugsForSignature(aSignature) {
gSigData[aSignature].bugs = [];
fetchFile(
gSocorroPath + "api/Bugs/?signatures=" + encodeURIComponent(aSignature),
"json",
function (aSignature, aData) {
if (aData) {
for (var i = 0; i <= aData.hits.length - 1; i++) {
if (aData.hits[i].signature == aSignature) {
gSigData[aSignature].bugs.push(aData.hits[i].id);
}
}
buildBugsField(aSignature);
} else {
console.log("ERROR - couldn't find bug data for " + aSignature + "!");
}
}.bind(undefined, aSignature) // Prepend signature to the argument list.
);
}
function buildDataTable() {
var msgRow = document.getElementById("message_row");
if (msgRow) {
msgRow.parentNode.removeChild(msgRow);
}
// Header
var trow = document
.getElementById("scompTHeader")
.appendChild(document.createElement("tr"));
var cell = trow.appendChild(document.createElement("th"));
cell.textContent = "#";
cell.setAttribute("title", "Rank");
var cell = trow.appendChild(document.createElement("th"));
cell.textContent = "Signature";
var cell = trow.appendChild(document.createElement("th"));
cell.textContent = "Bugs";
var cell = trow.appendChild(document.createElement("th"));
cell.textContent = "#1";
var cell = trow.appendChild(document.createElement("th"));
cell.textContent = "#2";
var cell = trow.appendChild(document.createElement("th"));
cell.textContent = "+/-";
// Body
var tblBody = document.getElementById("scompTBody");
var sigSorted = Object.keys(gSigData).sort(function (a, b) {
return Math.abs(gSigData[b].pctcmp) - Math.abs(gSigData[a].pctcmp);
});
var listNum = Math.min(gLimit, sigSorted.length);
for (var i = 0; i <= listNum - 1; i++) {
signature = sigSorted[i];
var trow = tblBody.appendChild(document.createElement("tr"));
trow.setAttribute("id", "sdata_" + encodeURIComponent(signature));
var cell = trow.appendChild(document.createElement("td"));
cell.textContent = i + 1;
cell.classList.add("rank");
var cell = trow.appendChild(document.createElement("td"));
cell.classList.add("sig");
var link = cell.appendChild(document.createElement("a"));
link.setAttribute(
"href",
gSocorroPath +
"signature?" +
gSearchBase +
"&signature=" +
encodeURIComponent(signature)
);
link.textContent = signature;
var cell = trow.appendChild(document.createElement("td"));
cell.classList.add("bugs");
var cell = trow.appendChild(document.createElement("td"));
cell.classList.add("pct");
cell.textContent = gSigData[signature].pct1.toFixed(1) + "%";
cell.setAttribute("title", gSigData[signature].count1);
if (gSigData[signature].count1 == 0) {
cell.classList.add("zero");
}
var cell = trow.appendChild(document.createElement("td"));
cell.classList.add("pct");
cell.textContent = gSigData[signature].pct2.toFixed(1) + "%";
cell.setAttribute("title", gSigData[signature].count2);
if (gSigData[signature].count2 == 0) {
cell.classList.add("zero");
}
var cell = trow.appendChild(document.createElement("td"));
cell.classList.add("pctcmp");
if (gSigData[signature].pctcmp > 0) {
cell.classList.add("plus");
} else if (gSigData[signature].pctcmp < 0) {
cell.classList.add("minus");
}
cell.textContent =
(gSigData[signature].pctcmp > 0 ? "+" : "") +
gSigData[signature].pctcmp.toFixed(1) +
"%";
}
}
function buildBugsField(aSignature) {
var sigRow = document.getElementById(
"sdata_" + encodeURIComponent(aSignature)
);
var bugsField = sigRow.querySelector(".bugs");
for (var i = 0; i <= gSigData[aSignature].bugs.length - 1; i++) {
if (i > 0) {
// Add spaces when we have multiple bugs.
bugsField.appendChild(document.createTextNode(" "));
}
var link = bugsField.appendChild(document.createElement("a"));
link.dataset["bugid"] = gSigData[aSignature].bugs[i];
link.setAttribute(
"href",
gBzBasePath + "show_bug.cgi?id=" + gSigData[aSignature].bugs[i]
);
link.textContent = gSigData[aSignature].bugs[i];
// Add Bugzilla data.
if (gBugInfo[gSigData[aSignature].bugs[i]]) {
beautifyBugzillaLink(link);
} else {
fetchFile(
gBzBasePath +
"rest/bug/" +
gSigData[aSignature].bugs[i] +
"?include_fields=id,summary,status,resolution",
"json",
function (aLink, aData) {
if (aData && aData.bugs && aData.bugs.length) {
gBugInfo[aData.bugs[0].id] = aData.bugs[0];
beautifyBugzillaLink(aLink);
} else if (aData && aData.error) {
// On error, create fake bug info.
gBugInfo[aLink.dataset["bugid"]] = {
status: "ERROR",
resolution: "",
summary: aData.message,
};
beautifyBugzillaLink(aLink);
} else {
console.log(
"ERROR - couldn't find info for bug " +
aLink.dataset["bugid"] +
"!"
);
}
}.bind(undefined, link), // Prepend link to the argument list.
true // Accept 401 responses and still return them as JSON.
);
}
}
}
function beautifyBugzillaLink(aLink) {
if (gBugInfo[aLink.dataset["bugid"]]) {
aLink.dataset["status"] = gBugInfo[aLink.dataset["bugid"]].status;
aLink.dataset["resolution"] = gBugInfo[aLink.dataset["bugid"]].resolution;
aLink.title =
gBugInfo[aLink.dataset["bugid"]].status +
" " +
gBugInfo[aLink.dataset["bugid"]].resolution +
" - " +
gBugInfo[aLink.dataset["bugid"]].summary;
} else {
console.log(
"ERROR - info for bug " +
aLink.dataset["bugid"] +
" should exist but doesn't!"
);
}
}
function displayMessage(aErrorMessage) {
var msgRow = document.getElementById("message_row");
if (msgRow) {
msgRow.parentNode.removeChild(msgRow);
}
var trow = document
.getElementById("scompTBody")
.appendChild(document.createElement("tr"));
trow.setAttribute("id", "message_row");
var cell = trow.appendChild(document.createElement("td"));
cell.textContent = aErrorMessage;
return cell;
}
function fetchFile(aURL, aFormat, aCallback, aAccept401) {
var XHR = new XMLHttpRequest();
XHR.onreadystatechange = function () {
if (XHR.readyState == 4) {
/*
gLog.appendChild(document.createElement("li"))
.appendChild(document.createTextNode(aURL + " - " + XHR.status +
" " + XHR.statusText));*/
}
if (
XHR.readyState == 4 &&
(XHR.status == 200 || (XHR.status == 401 && aAccept401))
) {
// so far so good
if (
XHR.responseXML != null &&
aFormat == "xml" &&
XHR.responseXML.getElementById("test").firstChild.data
)
aCallback(aXHR.responseXML.getElementById("test").firstChild.data);
else if (XHR.responseText != null && aFormat == "json")
aCallback(JSON.parse(XHR.responseText));
else aCallback(XHR.responseText);
} else if (XHR.readyState == 4 && XHR.status != 200) {
// fetched the wrong page or network error...
console.log("ERROR: XHR status " + XHR.status + " - " + aURL);
aCallback(null);
}
};
XHR.open("GET", aURL);
if (gSocorroAPIToken) {
// XXX: Should work but doesn't yet! We'll need to figure this out.
// Use this path when we have a token so bug 1143424 can be tested.
XHR.setRequestHeader("Auth-Token", gSocorroAPIToken);
}
if (aFormat == "json") {
XHR.setRequestHeader("Accept", "application/json");
} else if (aFormat == "xml") {
XHR.setRequestHeader("Accept", "application/xml");
}
try {
XHR.send();
} catch (e) {
console.log("ERROR: XHR send - " + e + " - " + aURL);
aCallback(null);
}
}
function getParameterByName(aName) {
// from http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
name = aName.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null
? ""
: decodeURIComponent(results[1].replace(/\+/g, " "));
}