-
Notifications
You must be signed in to change notification settings - Fork 14
/
MockBackend.js
392 lines (331 loc) · 10.1 KB
/
MockBackend.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
var util = require('util'),
mock = require('./mockdata');
// Constructor
function MockBackend(name, config, callback) {
var self = this;
this.name = name;
this.config = config;
var numFailures = config.numFailures;
callback();
}
// cb is getTests
function getCommits(cb) {
var queryCB = function (err, results) {
if (err) {
cb(err);
} else if (!results || !results.rows) {
this.commits = [];
cb(null);
} else {
this.commits = results.rows;
cb(null);
}
};
queryCB.bind(this);
var args = [];
// get commits to tids
var cql = 'select * from commits_to_tid;\n';
this.client.execute(cql, args, this.consistencies.write, queryCB);
}
// cb is initTestPQ
function getTests(cb) {
var queryCB = function (err, results) {
if (err) {
cb(err);
} else if (!results || !results.rows) {
this.testsList = [];
cb(null, 0, 0);
} else {
this.testsList = testsList;
cb(null, 0, results.rows.length);
}
};
queryCB.bind(this);
var args = [];
// get tests
var cql = 'select * from tests;\n';
// And finish it off
this.client.execute(cql, args, this.consistencies.write, queryCB);
}
function initTestPQ(commitIndex, numTestsLeft, cb) {
var queryCB = function (err, results) {
if (err) {
cb(err);
} else if (!results || !results.rows || results.rows.length === 0) {
cb(null);
} else {
for (result in results) {
if (!(result.test in this.testHash)) {
// construct resultObj
var resultObj = {test: test, score: score, commitIndex: commitIndex};
this.testArr.push(resultObj);
this.testHash.push(result.test)
numTestsLeft--;
}
}
if (numTestsLeft == 0 || this.commits[commitIndex].isSnapshot) {
cb(null);
}
initTestPQ(commitIndex+1, numTestsLeft, cb);
}
};
queryCB.bind(this);
var lastCommit = this.commits[commitIndex].hash;
var args = [lastCommit];
var cql = 'select (test, score, commit) from test_by_score where commit = ?';
this.client.execute(cql, args, this.consistencies.write, queryCB);
}
/**
* Get the number of regressions based on the previous commit
*
* @param commit1 object {
* hash: <git hash string>
* timestamp: <git commit timestamp date object>
* }
* @param cb function (err, num) - num is the number of regressions for the last commit
*/
// MockBackend.prototype.getNumRegFix = function (commit, cb) {
// calcRegressionFixes(function(err, reg, fix) {
// cb(null, reg.length, fix.length);
// });
// };
/**
* Get the next title to test
*
* @param commit object {
* hash: <git hash string>
* timestamp: <git commit timestamp date object>
* }
* @param cb function (err, test) with test being an object that serializes to
* JSON, for example [ 'enwiki', 'some title', 12345 ]
*/
MockBackend.prototype.getTest = function (commit, cb) {
/*
// check running queue for any timed out tests.
for (testObj in runningQueue) {
// if any timed out,
if (testObj timed out) {
if (testObj.tries < threshold) {
// increment tries, return it;
testObj.tries++;
cb(null, testObj.test);
} else {
// pop it; (discard the result)
runningQueue.pop();
}
}
}
// pop test from test queue
// push test into running queue
// increment tries, return test;
*/
cb([ 'enwiki', 'some title', 12345 ]);
};
/**
* Get results ordered by score
*
* @param cb- (err, result), result is defined below
*
*/
MockBackend.prototype.getStatistics = function(cb) {
/**
* @param results
* object {
* tests: <test count>,
* noskips: <tests without skips>,
* nofails: <tests without fails>,
* noerrors: <tests without error>,
*
* latestcommit: <latest commit hash>,
* beforelatestcommit: <commit before latest commit>,
*
* averages: {
* errors: <average num errors>,
* fails: <average num fails>,
* skips: <average num skips>,
* scores: <average num scores>
* },
*
* crashes: <num crashes>,
* regressions: <num regressions>,
* fixes: <num fixes>
* }
*
*/
var results = mock;
calcRegressionFixes(function(err, reg, fix) {
results.numFixes= fix.length;
results.numReg = reg.length;
cb(null, results);
});
}
/**
* getRegressionRows mock method returns the mock data of the fake regressions
*/
var regressionsHeaderData = ['Title', 'New Commit', 'Errors|Fails|Skips', 'Old Commit', 'Errors|Fails|Skips'];
var statsScore = function(skip, fail, error) {
return error*1000000+fail*1000+skip;
}
/**
This method calculates all the scores data from the tests table
**/
function calcRegressionFixes(cb) {
var data = mock.testdata;
var regData = [];
var fixData = [];
for(var y in data) {
var x = data[y];
var newtest = statsScore(x.skips, x.fails, x.errors);
var oldtest = statsScore(x.old_skips, x.old_fails, x.old_errors);
/*if they differ then we're going to push it in either the regression or fixes*/
if(newtest !== oldtest) {
/*if the new is better than the old then it's a fix, otherwise regress*/
(newtest < oldtest) ?fixData.push(x) : regData.push(x);
}
}
//console.log("data: " + JSON.stringify(regData, null, '\t') + "\n" + JSON.stringify(fixData,null,'\t'));
cb (null, regData, fixData);
}
MockBackend.prototype.getRegressions = function(r1, r2, prefix, page, cb) {
calcRegressionFixes(function(err, regressions, fix) {
var mydata = {
page: page,
urlPrefix: prefix,
urlSuffix: '',
heading: "Total regressions between selected revisions: " + regressions.length, /*change this with mock's num regresssions*/
headingLink: {url: "/topfixes/between/" + r1 + "/" + r2, name: 'topfixes'},
header: regressionsHeaderData
};
for (var i = 0; i < regressions.length; i++) {
regressions[i].old_commit= r2;
regressions[i].new_commit= r1;
}
//console.log("json: " + JSON.stringify(regressions, null, '\t'));
cb(null, regressions, mydata);
});
}
/**
* getRegressionRows mock method returns the mock data of the fake regressions
*/
MockBackend.prototype.getFixes = function(r1, r2, prefix, page, cb) {
calcRegressionFixes(function(err, regressions, fixes) {
var mydata = {
page: page,
urlPrefix: prefix,
urlSuffix: '',
heading: "Total fixes between selected revisions: " + fixes.length, /*change this with mock's num regresssions*/
headingLink: {url: '/regressions/between/' + r1 + '/' + r2, name: 'regressions'},
header: regressionsHeaderData
};
for (var i = 0; i < fixes.length; i++) {
fixes[i].old_commit= r2;
fixes[i].new_commit= r1;
}
cb(null, fixes, mydata);
});
}
/**
* Add a result to storage
*
* @param test string representing what test we're running
* @param commit object {
* hash: <git hash string>
* timestamp: <git commit timestamp date object>
* }
* @param result string (JUnit XML typically)
* @param cb callback (err) err or null
*/
MockBackend.prototype.addResult = function(test, commit, result, cb) {
var tid = commit.timestamp; // fix
var skipCount = result.match( /<skipped/g ),
failCount = result.match( /<failure/g ),
errorCount = result.match( /<error/g );
// Build up the CQL
// Simple revison table insertion only for now
var cql = 'BEGIN BATCH ',
args = [],
score = statsScore(skipCount, failCount, errorCount);
// Insert into results
cql += 'insert into results (test, tid, result)' +
'values(?, ?, ?);\n';
args = args.concat([
test,
tid,
result
]);
// Check if test score changed
if (testScores[test] == score) {
// If changed, update test_by_score
cq += 'insert into test_by_score (commit, score, test)' +
'values(?, ?, ?);\n';
args = args.concat([
commit,
score,
test
]);
// Update scores in memory;
testScores[test] = score;
}
// And finish it off
cql += 'APPLY BATCH;';
this.client.execute(cql, args, this.consistencies.write, cb);
}
var statsScore = function(skipCount, failCount, errorCount) {
// treat <errors,fails,skips> as digits in a base 1000 system
// and use the number as a score which can help sort in topfails.
return errorCount*1000000+failCount*1000+skipCount;
};
var calcTopFails = function(cb) {
}
/**
* Computes everything in the order of top fails first and displays them back to the user
* "Everything" includes all commits, old and new
*
* @param offset (for pagination)
* @param limit (for pagination)
* @param cb
*
*/
MockBackend.prototype.getTopFails = function(offset, limit, cb) {
/**
* cb
*
* @param results array [
* object {
* commit: <commit hash>,
* prefix: <prefix>,
* title: <title>
* status: <status> // 'perfect', 'skip', 'fail', or null
* skips: <skip count>,
* fails: <fails count>,
* errors: <errors count>
* }
* ]
*/
var results = [];
for (var i = offset; i < limit; i++) {
var current = topFails[i];
var score = current.score;
var errorsCount = score % 1000000;
score = score - errorsCount * 1000000;
var failsCount = score % 1000;
score = score - failsCount * 1000;
var skipsCount = score;
if ( skipsCount === 0 && failsCount === 0 && errorsCount === 0 ) {
return 'perfect';
} else if ( errors_count > 0 || failsCount > 0 ) {
return 'fail';
} else {
return 'skip';
}
var result = {
commit: current.commit, prefix: current.test.prefix, title: current.test.title, status: status, skips: skipsCount,
fails: failsCount, errors: errorsCount
}
results.append(result);
}
cb(results);
}
// Node.js module exports. This defines what
// require('./MockBackend.js'); evaluates to.
module.exports = MockBackend;