-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathserver-mysql.js
executable file
·1409 lines (1255 loc) · 43 KB
/
server-mysql.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
( function () {
"use strict";
var express = require( 'express' ),
optimist = require( 'optimist' ),
hbs = require( 'handlebars' );
// Default options
var defaults = {
'host': 'localhost',
'port': 3306,
'database': 'parsoid',
'user': 'parsoid',
'password': 'parsoidpw',
'debug': false,
'fetches': 6,
'tries': 6,
'cutofftime': 600,
'batch': 50
};
// Command line options
var argv = optimist.usage( 'Usage: $0 [connection parameters]' )
.options( 'help', {
'boolean': true,
'default': false,
describe: "Show usage information."
} )
.options( 'h', {
alias: 'host',
describe: 'Hostname of the database server.'
} )
.options( 'P', {
alias: 'port',
describe: 'Port number to use for connection.'
} )
.options( 'D', {
alias: 'database',
describe: 'Database to use.'
} )
.options( 'u', {
alias: 'user',
describe: 'User for MySQL login.'
} )
.options( 'p', {
alias: 'password',
describe: 'Password.'
} )
.options( 'd', {
alias: 'debug',
'boolean': true,
describe: "Output MySQL debug data."
} )
.options( 'f', {
alias: 'fetches',
describe: "Number of times to try fetching a page."
} )
.options( 't', {
alias: 'tries',
describe: "Number of times an article will be sent for testing " +
"before it's considered an error."
} )
.options( 'c', {
alias: 'cutofftime',
describe: "Time in seconds to wait for a test result."
} )
.options( 'config', {
describe: "Config file path"
} )
.options( 'b', {
alias: 'batch',
describe: "Number of titles to fetch from database in one batch."
} )
.argv;
if ( argv.help ) {
optimist.showHelp();
process.exit( 0 );
}
// Settings file
var settings;
try {
var settingsPath = argv.config || './server-mysql.settings.js';
settings = require( settingsPath );
} catch ( e ) {
settings = {};
}
var getOption = function( opt ) {
var value;
// Check possible options in this order: command line, settings file, defaults.
if ( argv.hasOwnProperty( opt ) ) {
value = argv[ opt ];
} else if ( settings.hasOwnProperty( opt ) ) {
value = settings[ opt ];
} else if ( defaults.hasOwnProperty( opt ) ) {
value = defaults[ opt ];
} else {
return undefined;
}
// Check the boolean options, 'false' and 'no' should be treated as false.
// Copied from mediawiki.Util.js.
if ( opt === 'debug' ) {
if ( ( typeof value ) === 'string' &&
/^(no|false)$/i.test( value ) ) {
return false;
}
}
return value;
};
var // The maximum number of tries per article
maxTries = getOption( 'tries' ),
// The maximum number of fetch retries per article
maxFetchRetries = getOption( 'fetches' ),
// The time to wait before considering a test has failed
cutOffTime = getOption( 'cutofftime' ),
// The number of pages to fetch at once
batchSize = getOption( 'batch' ),
debug = getOption( 'debug' );
var mysql = require( 'mysql' );
var db = mysql.createConnection({
host : getOption( 'host' ),
port : getOption( 'port' ),
database : getOption( 'database' ),
user : getOption( 'user' ),
password : getOption( 'password'),
multipleStatements : true,
charset : 'UTF8_BIN',
debug : debug
} );
var queues = require( 'mysql-queues' );
queues( db, debug );
// Try connecting to the database.
process.on( 'exit', function() {
db.end();
} );
db.connect( function( err ) {
if ( err ) {
console.error( "Unable to connect to database, error: " + err.toString() );
process.exit( 1 );
}
} );
// ----------------- The queries --------------
var dbGetTitle =
'SELECT id, title, prefix, claim_hash, claim_num_tries ' +
'FROM pages ' +
'WHERE num_fetch_errors < ? AND ' +
'( claim_hash != ? OR ' +
'( claim_num_tries < ? AND claim_timestamp < ? ) ) ' +
'ORDER BY claim_num_tries DESC, latest_score DESC, ' +
'claim_timestamp ASC LIMIT ? ' +
// Stop other transactions from reading until we finish this one.
'FOR UPDATE';
var dbIncrementFetchErrorCount =
'UPDATE pages SET ' +
'claim_hash = ?, ' +
'num_fetch_errors = num_fetch_errors + 1, ' +
'claim_num_tries = 0 ' +
'WHERE title = ? AND prefix = ?';
var dbInsertCommit =
'INSERT IGNORE INTO commits ( hash, timestamp ) ' +
'VALUES ( ?, ? )';
var dbFindPage =
'SELECT id ' +
'FROM pages ' +
'WHERE title = ? AND prefix = ?';
var dbUpdatePageClaims =
'UPDATE pages SET claim_hash = ?, claim_timestamp = ?, claim_num_tries = claim_num_tries + 1 ' +
'WHERE id IN ( ? )';
var dbInsertResult =
'INSERT INTO results ( page_id, commit_hash, result ) ' +
'VALUES ( ?, ?, ? ) ' +
'ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID( id ), ' +
'result = VALUES( result )';
var dbInsertStats =
'INSERT INTO stats ' +
'( skips, fails, errors, score, page_id, commit_hash ) ' +
'VALUES ( ?, ?, ?, ?, ?, ? ) ' +
'ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID( id ), ' +
'skips = VALUES( skips ), fails = VALUES( fails ), ' +
'errors = VALUES( errors ), score = VALUES( score )';
var dbInsertPerfStatsStart =
'INSERT INTO perfstats ' +
'( page_id, commit_hash, type, value ) VALUES ';
var dbInsertPerfStatsEnd =
' ON DUPLICATE KEY UPDATE value = VALUES( value )';
var dbUpdatePageLatestResults =
'UPDATE pages ' +
'SET latest_stat = ?, latest_score = ?, latest_result = ?, ' +
'claim_hash = ?, claim_timestamp = NULL, claim_num_tries = 0 ' +
'WHERE id = ?';
var dbUpdateCrashersClearTries =
'UPDATE pages ' +
'SET claim_num_tries = 0 ' +
'WHERE claim_hash != ? AND claim_num_tries >= ?';
var dbStatsQuery =
'SELECT ' +
'(select hash from commits order by timestamp desc limit 1) as maxhash, ' +
'(select hash from commits order by timestamp desc limit 1 offset 1) as secondhash, ' +
'(select count(*) from stats where stats.commit_hash = ' +
'(select hash from commits order by timestamp desc limit 1)) as maxresults, ' +
'(select avg(stats.errors) from stats join pages on ' +
'pages.latest_stat = stats.id) as avgerrors, ' +
'(select avg(stats.fails) from stats join pages on ' +
'pages.latest_stat = stats.id) as avgfails, ' +
'(select avg(stats.skips) from stats join pages on ' +
'pages.latest_stat = stats.id) as avgskips, ' +
'(select avg(stats.score) from stats join pages on ' +
'pages.latest_stat = stats.id) as avgscore, ' +
'count(*) AS total, ' +
'count(CASE WHEN stats.errors=0 THEN 1 ELSE NULL END) AS no_errors, ' +
'count(CASE WHEN stats.errors=0 AND stats.fails=0 '+
'then 1 else null end) AS no_fails, ' +
'count(CASE WHEN stats.errors=0 AND stats.fails=0 AND stats.skips=0 '+
'then 1 else null end) AS no_skips, ' +
// get regression count between last two commits
'(SELECT count(*) ' +
'FROM pages p ' +
'JOIN stats AS s1 ON s1.page_id = p.id ' +
'JOIN stats AS s2 ON s2.page_id = p.id ' +
'WHERE s1.commit_hash = (SELECT hash ' +
'FROM commits ORDER BY timestamp DESC LIMIT 1 ) ' +
'AND s2.commit_hash = (SELECT hash ' +
'FROM commits ORDER BY timestamp DESC LIMIT 1 OFFSET 1) ' +
'AND s1.score > s2.score ) as numregressions, ' +
// get fix count between last two commits
'(SELECT count(*) ' +
'FROM pages ' +
'JOIN stats AS s1 ON s1.page_id = pages.id ' +
'JOIN stats AS s2 ON s2.page_id = pages.id ' +
'WHERE s1.commit_hash = (SELECT hash FROM commits ORDER BY timestamp DESC LIMIT 1 ) ' +
'AND s2.commit_hash = (SELECT hash FROM commits ORDER BY timestamp DESC LIMIT 1 OFFSET 1 ) ' +
'AND s1.score < s2.score ) as numfixes, ' +
// Get latest commit crashers
'(SELECT count(*) ' +
'FROM pages ' +
'WHERE claim_hash = (SELECT hash FROM commits ORDER BY timestamp DESC LIMIT 1) ' +
'AND claim_num_tries >= ? ' +
'AND claim_timestamp < ?) AS crashers ' +
'FROM pages JOIN stats on pages.latest_stat = stats.id';
var dbPerWikiStatsQuery =
'SELECT ' +
'(select hash from commits order by timestamp desc limit 1) as maxhash, ' +
'(select hash from commits order by timestamp desc limit 1 offset 1) as secondhash, ' +
'(select count(*) from stats join pages on stats.page_id = pages.id ' +
'where stats.commit_hash = ' +
'(select hash from commits order by timestamp desc limit 1) ' +
'and pages.prefix = ?) as maxresults, ' +
'(select avg(stats.errors) from stats join pages on ' +
'pages.latest_stat = stats.id where pages.prefix = ?) as avgerrors, ' +
'(select avg(stats.fails) from stats join pages on ' +
'pages.latest_stat = stats.id where pages.prefix = ?) as avgfails, ' +
'(select avg(stats.skips) from stats join pages on ' +
'pages.latest_stat = stats.id where pages.prefix = ?) as avgskips, ' +
'(select avg(stats.score) from stats join pages on ' +
'pages.latest_stat = stats.id where pages.prefix = ?) as avgscore, ' +
'count(*) AS total, ' +
'count(CASE WHEN stats.errors=0 THEN 1 ELSE NULL END) AS no_errors, ' +
'count(CASE WHEN stats.errors=0 AND stats.fails=0 '+
'then 1 else null end) AS no_fails, ' +
'count(CASE WHEN stats.errors=0 AND stats.fails=0 AND stats.skips=0 '+
'then 1 else null end) AS no_skips, ' +
// get regression count between last two commits
'(SELECT count(*) ' +
'FROM pages p ' +
'JOIN stats AS s1 ON s1.page_id = p.id ' +
'JOIN stats AS s2 ON s2.page_id = p.id ' +
'WHERE s1.commit_hash = (SELECT hash ' +
'FROM commits ORDER BY timestamp DESC LIMIT 1 ) ' +
'AND s2.commit_hash = (SELECT hash ' +
'FROM commits ORDER BY timestamp DESC LIMIT 1 OFFSET 1) ' +
'AND p.prefix = ? ' +
'AND s1.score > s2.score ) as numregressions, ' +
// get fix count between last two commits
'(SELECT count(*) ' +
'FROM pages ' +
'JOIN stats AS s1 ON s1.page_id = pages.id ' +
'JOIN stats AS s2 ON s2.page_id = pages.id ' +
'WHERE s1.commit_hash = (SELECT hash FROM commits ORDER BY timestamp DESC LIMIT 1 ) ' +
'AND s2.commit_hash = (SELECT hash FROM commits ORDER BY timestamp DESC LIMIT 1 OFFSET 1 ) ' +
'AND pages.prefix = ? ' +
'AND s1.score < s2.score ) as numfixes, ' +
// Get latest commit crashers
'(SELECT count(*) ' +
'FROM pages WHERE prefix = ? ' +
'AND claim_hash = (SELECT hash FROM commits ORDER BY timestamp DESC LIMIT 1) ' +
'AND claim_num_tries >= ? ' +
'AND claim_timestamp < ?) AS crashers ' +
'FROM pages JOIN stats on pages.latest_stat = stats.id WHERE pages.prefix = ?';
var dbFailsQuery =
'SELECT pages.title, pages.prefix, commits.hash, stats.errors, stats.fails, stats.skips ' +
'FROM stats ' +
'JOIN (' +
' SELECT MAX(id) AS most_recent FROM stats GROUP BY page_id' +
') AS s1 ON s1.most_recent = stats.id ' +
'JOIN pages ON stats.page_id = pages.id ' +
'JOIN commits ON stats.commit_hash = commits.hash ' +
'ORDER BY stats.score DESC ' +
'LIMIT 40 OFFSET ?' ;
var dbGetOneResult =
'SELECT result FROM results ' +
'JOIN commits ON results.commit_hash = commits.hash ' +
'JOIN pages ON pages.id = results.page_id ' +
'WHERE pages.title = ? AND pages.prefix = ? ' +
'ORDER BY commits.timestamp DESC LIMIT 1' ;
var dbGetResultWithCommit =
'SELECT result FROM results ' +
'JOIN pages ON pages.id = results.page_id ' +
'WHERE results.commit_hash = ? AND pages.title = ? AND pages.prefix = ?';
var dbFailedFetches =
'SELECT title, prefix FROM pages WHERE num_fetch_errors >= ?';
var dbCrashers =
'SELECT pages.title, pages.prefix, pages.claim_hash, commits.timestamp ' +
'FROM pages JOIN commits ON (pages.claim_hash = commits.hash) ' +
'WHERE claim_num_tries >= ? ' +
'AND claim_timestamp < ? ' +
'ORDER BY commits.timestamp DESC';
var dbFailsDistribution =
'SELECT fails, count(*) AS num_pages ' +
'FROM stats ' +
'JOIN pages ON pages.latest_stat = stats.id ' +
'GROUP by fails';
var dbSkipsDistribution =
'SELECT skips, count(*) AS num_pages ' +
'FROM stats ' +
'JOIN pages ON pages.latest_stat = stats.id ' +
'GROUP by skips';
var dbCommits =
'SELECT hash, timestamp, ' +
//// get the number of fixes column
// '(SELECT count(*) ' +
// 'FROM pages ' +
// 'JOIN stats AS s1 ON s1.page_id = pages.id ' +
// 'JOIN stats AS s2 ON s2.page_id = pages.id ' +
// 'WHERE s1.commit_hash = (SELECT hash FROM commits c2 where c2.timestamp < c1.timestamp ORDER BY timestamp DESC LIMIT 1 ) ' +
// 'AND s2.commit_hash = c1.hash AND s1.score < s2.score) as numfixes, ' +
//// get the number of regressions column
// '(SELECT count(*) ' +
// 'FROM pages ' +
// 'JOIN stats AS s1 ON s1.page_id = pages.id ' +
// 'JOIN stats AS s2 ON s2.page_id = pages.id ' +
// 'WHERE s1.commit_hash = (SELECT hash FROM commits c2 where c2.timestamp < c1.timestamp ORDER BY timestamp DESC LIMIT 1 ) ' +
// 'AND s2.commit_hash = c1.hash AND s1.score > s2.score) as numregressions, ' +
//// get the number of tests for this commit column
'(select count(*) from stats where stats.commit_hash = c1.hash) as numtests ' +
'FROM commits c1 ' +
'ORDER BY timestamp DESC';
var dbCommitHashes =
'SELECT hash FROM commits ORDER BY timestamp DESC';
var dbFixesBetweenRevs =
'SELECT pages.title, pages.prefix, ' +
's1.commit_hash AS new_commit, s1.errors AS errors, s1.fails AS fails, s1.skips AS skips, ' +
's2.commit_hash AS old_commit, s2.errors AS old_errors, s2.fails AS old_fails, s2.skips AS old_skips ' +
'FROM pages ' +
'JOIN stats AS s1 ON s1.page_id = pages.id ' +
'JOIN stats AS s2 ON s2.page_id = pages.id ' +
'WHERE s1.commit_hash = ? AND s2.commit_hash = ? AND s1.score < s2.score ' +
'ORDER BY s1.score - s2.score ASC ' +
'LIMIT 40 OFFSET ?';
var dbNumFixesBetweenRevs =
'SELECT count(*) as numFixes ' +
'FROM pages ' +
'JOIN stats AS s1 ON s1.page_id = pages.id ' +
'JOIN stats AS s2 ON s2.page_id = pages.id ' +
'WHERE s1.commit_hash = ? AND s2.commit_hash = ? AND s1.score < s2.score ';
var dbRegressionsBetweenRevs =
'SELECT pages.title, pages.prefix, ' +
's1.commit_hash AS new_commit, s1.errors AS errors, s1.fails AS fails, s1.skips AS skips, ' +
's2.commit_hash AS old_commit, s2.errors AS old_errors, s2.fails AS old_fails, s2.skips AS old_skips ' +
'FROM pages ' +
'JOIN stats AS s1 ON s1.page_id = pages.id ' +
'JOIN stats AS s2 ON s2.page_id = pages.id ' +
'WHERE s1.commit_hash = ? AND s2.commit_hash = ? AND s1.score > s2.score ' +
'ORDER BY s1.score - s2.score DESC ' +
'LIMIT 40 OFFSET ?';
var dbNumRegressionsBetweenRevs =
'SELECT count(*) as numRegressions ' +
'FROM pages ' +
'JOIN stats AS s1 ON s1.page_id = pages.id ' +
'JOIN stats AS s2 ON s2.page_id = pages.id ' +
'WHERE s1.commit_hash = ? AND s2.commit_hash = ? AND s1.score > s2.score ';
var dbResultsQuery =
'SELECT result FROM results';
var dbResultsPerWikiQuery =
'SELECT result FROM results ' +
'JOIN pages ON pages.id = results.page_id ' +
'WHERE pages.prefix = ?';
var dbPerfStatsTypes =
'SELECT DISTINCT type FROM perfstats';
var dbLastPerfStatsStart =
'SELECT prefix, title, ';
var dbLastPerfStatsEnd =
' FROM pages JOIN perfstats ON pages.id = perfstats.page_id ' +
'WHERE perfstats.commit_hash = ' +
'(SELECT hash FROM commits ORDER BY timestamp DESC LIMIT 1) ' +
'GROUP BY pages.id ';
var dbPagePerfStatsStart =
'SELECT commits.hash, commits.timestamp, ';
var dbPagePerfStatsEnd =
' FROM (perfstats JOIN pages ON perfstats.page_id = pages.id) ' +
'JOIN commits ON perfstats.commit_hash = commits.hash ' +
'WHERE pages.prefix = ? AND pages.title = ? ' +
'GROUP BY commits.hash ' +
'ORDER BY commits.timestamp DESC ' +
'LIMIT 0, ?';
var transFetchCB = function( msg, trans, failCb, successCb, err, result ) {
if ( err ) {
trans.rollback( function () {
if ( failCb ) {
failCb ( msg? msg + err.toString() : err, result );
}
} );
} else if ( successCb ) {
successCb( result );
}
};
var fetchPages = function( commitHash, cutOffTimestamp, cb ) {
var trans = db.startTransaction();
trans.query( dbGetTitle,
[ maxFetchRetries, commitHash, maxTries, cutOffTimestamp, batchSize ],
transFetchCB.bind( null, "Error getting next titles", trans, cb, function ( rows ) {
if ( !rows || rows.length === 0 ) {
trans.commit( cb.bind( null, null, rows ) );
} else {
// Process the rows: Weed out the crashers.
var pages = [];
var pageIds = [];
for ( var i = 0; i < rows.length; i++ ) {
var row = rows[i];
pageIds.push( row.id );
pages.push( { id: row.id, prefix: row.prefix, title: row.title } );
}
trans.query( dbUpdatePageClaims, [ commitHash, new Date(), pageIds ],
transFetchCB.bind( null, "Error updating claims", trans, cb, function () {
trans.commit( cb.bind( null, null, pages ));
} ) );
}
} ) ).execute();
};
var fetchedPages = [];
var lastFetchedCommit = null;
var lastFetchedDate = new Date(0);
var knownCommits;
var getTitle = function ( req, res ) {
var commitHash = req.query.commit;
var commitDate = new Date( req.query.ctime );
var knownCommit = knownCommits && knownCommits[ commitHash ];
req.connection.setTimeout(300 * 1000);
res.setHeader( 'Content-Type', 'text/plain; charset=UTF-8' );
// Keep track of known commits so we can discard clients still on older
// versions. If we don't know about the commit, then record it
// Use a transaction to make sure we don't start fetching pages until
// we've done this
if ( !knownCommit ) {
var trans = db.startTransaction();
if ( !knownCommits ) {
knownCommits = {};
trans.query( dbCommitHashes, null, function ( err, resCommitHashes ) {
if ( err ) {
console.log( "Error fetching known commits", err );
} else {
resCommitHashes.forEach( function ( v ) {
knownCommits[ v.hash ] = commitDate;
} );
}
} );
}
// New commit, record it
knownCommits[ commitHash ] = commitDate;
trans.query( dbInsertCommit, [ commitHash, new Date() ], function ( err, commitInsertResult ) {
if ( err ) {
console.error( "Error inserting commit " + commitHash );
} else if ( commitInsertResult.affectedRows > 0 ) {
// If this is a new commit, we need to clear the number of times a
// crasher page has been sent out so that each title gets retested
trans.query( dbUpdateCrashersClearTries, [ commitHash, maxTries ] );
}
} );
trans.commit();
}
if ( knownCommit && commitHash !== lastFetchedCommit ) {
// It's an old commit, tell the client so it can restart.
// HTTP status code 426 Update Required
res.send( "Old commit", 426 );
return;
}
var fetchCb = function ( err, pages ) {
if ( err ) {
res.send( "Error: " + err.toString(), 500 );
return;
}
if ( pages ) {
// Get the pages that aren't already fetched, to guard against the
// case of clients not finishing the whole batch in the cutoff time
var newPages = pages.filter( function( p ) {
return fetchedPages.every( function ( f ) {
return f.id !== p.id;
} );
} );
// Append the new pages to the already fetched ones, in case there's
// a parallel request.
fetchedPages = fetchedPages.concat( newPages );
}
if ( fetchedPages.length === 0 ) {
res.send( 'No available titles that fit the constraints.', 404 );
} else {
var page = fetchedPages.pop();
console.log( ' ->', page.prefix + ':' + page.title );
res.send( page, 200 );
}
};
// Look if there's a title available in the already fetched ones.
// Ensure that we load a batch when the commit has changed.
if ( fetchedPages.length === 0 ||
commitHash !== lastFetchedCommit ||
( lastFetchedDate.getTime() + ( cutOffTime * 1000 ) ) < Date.now() ) {
// Select pages that were not claimed in the 10 minutes.
// If we didn't get a result from a client 10 minutes after
// it got a rt claim on a page, something is wrong with the client
// or with parsing the page.
//
// Hopefully, no page takes longer than 10 minutes to parse. :)
lastFetchedCommit = commitHash;
lastFetchedDate = new Date();
fetchPages( commitHash, new Date( Date.now() - ( cutOffTime * 1000 ) ), fetchCb );
} else {
fetchCb();
}
};
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 parsePerfStats = function( text ) {
var regexp = /<perfstat[\s]+type="([\w\:]+)"[\s]*>([\d]+)/g;
var perfstats = [];
for ( var match = regexp.exec( text ); match !== null; match = regexp.exec( text ) ) {
perfstats.push( { type: match[ 1 ], value: match[ 2 ] } );
}
return perfstats;
};
var transUpdateCB = function( title, prefix, hash, type, res, trans, success_cb, err, result ) {
if ( err ) {
trans.rollback();
var msg = "Error inserting/updating " + type + " for page: " + prefix + ':' + title + " and hash: " + hash;
console.error( msg );
if ( res ) {
res.send( msg, 500 );
}
} else if ( success_cb ) {
success_cb( result );
}
};
var insertPerfStats = function( db, pageId, commitHash, perfstats, cb ) {
// If empty, just return
if ( !perfstats || perfstats.length === 0 ) {
if ( cb ) {
return cb( null, null );
}
return;
}
// Build the query to insert all the results in one go:
var dbStmt = dbInsertPerfStatsStart;
for ( var i = 0; i < perfstats.length; i++ ) {
if ( i !== 0 ) {
dbStmt += ", ";
}
dbStmt += "( " + pageId.toString() + ", '" + commitHash + "', '" +
perfstats[i].type + "', " + perfstats[i].value + ' )';
}
dbStmt += dbInsertPerfStatsEnd;
// Make the query using the db arg, which could be a transaction
db.query( dbStmt, null, cb );
};
var receiveResults = function ( req, res ) {
req.connection.setTimeout(300 * 1000);
var title = req.params[ 0 ],
result = req.body.results,
skipCount = result.match( /<skipped/g ),
failCount = result.match( /<failure/g ),
errorCount = result.match( /<error/g );
var prefix = req.params[1];
var commitHash = req.body.commit;
var perfstats = parsePerfStats( result );
skipCount = skipCount ? skipCount.length : 0;
failCount = failCount ? failCount.length : 0;
errorCount = errorCount ? errorCount.length : 0;
res.setHeader( 'Content-Type', 'text/plain; charset=UTF-8' );
var trans = db.startTransaction();
//console.warn("got: " + JSON.stringify([title, commitHash, result, skipCount, failCount, errorCount]));
if ( errorCount > 0 && result.match( 'DoesNotExist' ) ) {
// Page fetch error, increment the fetch error count so, when it goes over
/// maxFetchRetries, it won't be considered for tests again.
console.log( 'XX', prefix + ':' + title );
trans.query( dbIncrementFetchErrorCount, [commitHash, title, prefix],
transUpdateCB.bind( null, title, prefix, commitHash, "page fetch error count", res, trans, null ) )
.commit( function( err ) {
if ( err ) {
console.error( "Error incrementing fetch count: " + err.toString() );
}
res.send( '', 200 );
} );
} else {
trans.query( dbFindPage, [ title, prefix ], function ( err, pages ) {
if ( !err && pages && pages.length === 1 ) {
// Found the correct page, fill the details up
var page = pages[0];
var score = statsScore( skipCount, failCount, errorCount );
var latest_resultId = 0,
latest_statId = 0;
// Insert the result
trans.query( dbInsertResult, [ page.id, commitHash, result ],
transUpdateCB.bind( null, title, prefix, commitHash, "result", res, trans, function( insertedResult ) {
latest_resultId = insertedResult.insertId;
// Insert the stats
trans.query( dbInsertStats, [ skipCount, failCount, errorCount, score, page.id, commitHash ],
transUpdateCB.bind( null, title, prefix, commitHash, "stats", res, trans, function( insertedStat ) {
latest_statId = insertedStat.insertId;
// And now update the page with the latest info
trans.query( dbUpdatePageLatestResults, [ latest_statId, score, latest_resultId, commitHash, page.id ],
transUpdateCB.bind( null, title, prefix, commitHash, "latest result", res, trans, function() {
trans.commit( function() {
console.log( '<- ', prefix + ':' + title, ':', skipCount, failCount,
errorCount, commitHash.substr(0,7) );
// Insert the performance stats, ignoring errors for now
insertPerfStats( db, page.id, commitHash, perfstats, null );
// Maybe the perfstats aren't committed yet, but it shouldn't be a problem
res.send('', 200);
} );
} ) );
} ) );
} ) );
} else {
trans.rollback( function() {
res.send( "Did not find claim for title: " + prefix + ':' + title, 500);
} );
}
} ).execute();
}
};
var pageListData = [
{ url: '/topfails', title: 'Results by title' },
{ url: '/failedFetches', title: 'Non-existing test pages' },
{ url: '/failsDistr', title: 'Histogram of failures' },
{ url: '/skipsDistr', title: 'Histogram of skips' },
{ url: '/commits', title: 'List of all tested commits' },
{ url: '/perfstats', title: 'Performance stats of last commit' }
];
hbs.registerHelper('formatPerfStat', function (type, value) {
if ( type.match( /^time/ ) ) {
// Show time in seconds
value = Math.round( (value / 1000) * 100 ) / 100;
return value.toString() + "s";
} else if ( type.match( /^size/ ) ) {
// Show sizes in KiB
value = Math.round( value / 1024 );
return value.toString() + "KiB";
} else {
// Other values go as they are
return value.toString();
}
});
var pageStatus = function(row) {
var hasStatus = row.hasOwnProperty( 'skips' ) &&
row.hasOwnProperty( 'fails' ) &&
row.hasOwnProperty( 'errors' );
if (hasStatus) {
if ( row.skips === 0 && row.fails === 0 && row.errors === 0 ) {
return 'perfect';
} else if ( row.errors > 0 || row.fails > 0 ) {
return 'fail';
} else {
return 'skip';
}
}
return null;
};
var pageTitleData = function(row){
var prefix = encodeURIComponent( row.prefix ),
title = encodeURIComponent( row.title );
return {
title: row.prefix + ':' + row.title,
titleUrl: 'http://parsoid.wmflabs.org/_rt/' + prefix + '/' + title,
lh: 'http://localhost:8000/_rt/' + prefix + '/' + title,
latest: '/latestresult/' + prefix + '/' + title,
perf: '/pageperfstats/' + prefix + '/' + title
};
};
var displayPageList = function(res, data, makeRow, err, rows){
console.log( "GET " + data.urlPrefix + "/" + data.page + data.urlSuffix );
if ( err ) {
res.send( err.toString(), 500 );
} else if ( !rows || rows.length <= 0 ) {
res.send( "No entries found", 404 );
} else {
var tableRows = [];
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
var tableRow = {status: pageStatus(row), tableData: makeRow(row)};
tableRows.push(tableRow);
}
var tableData = data;
tableData.paginate = true;
tableData.row = tableRows;
tableData.prev = data.page > 0;
tableData.next = rows.length === 40;
hbs.registerHelper('prevUrl', function (urlPrefix, urlSuffix, page) {
return urlPrefix + "/" + ( page - 1 ) + urlSuffix;
});
hbs.registerHelper('nextUrl', function (urlPrefix, urlSuffix, page) {
return urlPrefix + "/" + ( page + 1 ) + urlSuffix;
});
res.render('table.html', tableData);
}
};
var statsWebInterface = function ( req, res ) {
var query, queryParams;
var cutoffDate = new Date( Date.now() - ( cutOffTime * 1000 ) );
var prefix = req.params[1] || null;
// Switch the query object based on the prefix
if ( prefix !== null ) {
query = dbPerWikiStatsQuery;
queryParams = [ prefix, prefix, prefix, prefix,
prefix, prefix, prefix, prefix,
prefix, maxTries, cutoffDate ];
} else {
query = dbStatsQuery;
queryParams = [ maxTries, cutoffDate ];
}
// Fetch stats for commit
db.query( query, queryParams, function ( err, row ) {
if ( err || !row ) {
var msg = "Stats query returned nothing!";
msg = err ? msg + "\n" + err.toString() : msg;
console.error("Error: " + msg);
res.send( msg, 500 );
} else {
res.status( 200 );
var tests = row[0].total,
errorLess = row[0].no_errors,
skipLess = row[0].no_skips,
numRegressions = row[0].numregressions,
numFixes = row[0].numfixes,
noErrors = Math.round( 100 * 100 * errorLess / ( tests || 1 ) ) / 100,
perfects = Math.round( 100* 100 * skipLess / ( tests || 1 ) ) / 100,
syntacticDiffs = Math.round( 100 * 100 *
( row[0].no_fails / ( tests || 1 ) ) ) / 100;
var width = 800;
var data = {
prefix: prefix,
results: {
tests: tests,
noErrors: noErrors,
syntacticDiffs: syntacticDiffs,
perfects: perfects
},
graphWidths: {
perfect: width * perfects / 100 || 0,
syntacticDiff: width * ( syntacticDiffs - perfects ) / 100 || 0,
semanticDiff: width * ( 100 - syntacticDiffs ) / 100 || 0
},
latestRevision: [
{ description: 'Git SHA1', value: row[0].maxhash },
{ description: 'Test Results', value: row[0].maxresults },
{ description: 'Crashers', value: row[0].crashers,
url: '/crashers' },
{ description: 'Regressions', value: numRegressions,
url: '/regressions/between/' + row[0].secondhash + '/' + row[0].maxhash },
{ description: 'Fixes', value: numFixes,
url: '/topfixes/between/' + row[0].secondhash + '/' + row[0].maxhash },
],
averages: [
{ description: 'Errors', value: row[0].avgerrors },
{ description: 'Fails', value: row[0].avgfails },
{ description: 'Skips', value: row[0].avgskips },
{ description: 'Score', value: row[0].avgscore }
],
pages: pageListData
};
// round numeric data, but ignore others
hbs.registerHelper('round', function (val) {
if ( isNaN(val) ) {
return val;
} else {
return Math.round( val * 100 ) / 100;
}
});
res.render('index.html', data);
}
});
};
var failsWebInterface = function ( req, res ) {
var page = ( req.params[0] || 0 ) - 0,
offset = page * 40;
var makeFailsRow = function(row) {
return [
pageTitleData(row),
commitLinkData(row.hash, row.title, row.prefix),
row.skips,
row.fails,
row.errors === null ? 0 : row.errors
];
};
var data = {
page: page,
urlPrefix: '/topfails',
urlSuffix: '',
heading: 'Results by title',
header: ['Title', 'Commit', 'Syntactic diffs', 'Semantic diffs', 'Errors']
};
db.query( dbFailsQuery, [ offset ],
displayPageList.bind( null, res, data, makeFailsRow ) );
};
var resultsWebInterface = function ( req, res ) {
var query, queryParams,
prefix = req.params[1] || null;
if ( prefix !== null ) {
query = dbResultsPerWikiQuery;
queryParams = [ prefix ];
} else {
query = dbResultsQuery;
queryParams = [];
}
db.query( query, queryParams, function ( err, rows ) {
var i;
if ( err ) {
console.error( err );
res.send( err.toString(), 500 );
} else {
if ( rows.length === 0 ) {
res.send( '', 404 );
} else {
res.setHeader( 'Content-Type', 'text/xml; charset=UTF-8' );
res.status( 200 );
res.write( '<?xml-stylesheet href="/static/result.css"?>\n' );
res.write( '<testsuite>' );
for ( i = 0; i < rows.length; i++ ) {
res.write( rows[i].result );
}
res.end( '</testsuite>' );
}
}
} );
};
var resultWebCallback = function( req, res, err, row ) {
if ( err ) {
console.error( err );
res.send( err.toString(), 500 );
} else if ( row && row.length > 0 ) {
res.setHeader( 'Content-Type', 'text/xml; charset=UTF-8' );
res.status( 200 );
res.write( '<?xml-stylesheet href="/static/result.css"?>\n' );
res.end( row[0].result );
} else {
res.send( 'no results for that page at the requested revision', 404 );
}
};
var resultWebInterface = function( req, res ) {
var commit = req.params[2] ? req.params[0] : null;
var title = commit === null ? req.params[1] : req.params[2];
var prefix = commit === null ? req.params[0] : req.params[1];
if ( commit !== null ) {
db.query( dbGetResultWithCommit, [ commit, title, prefix ], resultWebCallback.bind( null, req, res ) );
} else {
db.query( dbGetOneResult, [ title, prefix ], resultWebCallback.bind( null, req, res ) );
}
};
var GET_failedFetches = function( req, res ) {
db.query( dbFailedFetches, [maxFetchRetries], function ( err, rows ) {
if ( err ) {
console.error( err );
res.send( err.toString(), 500 );
} else {
res.status( 200 );
var n = rows.length;
var pageData = [];
for (var i = 0; i < n; i++) {
var prefix = rows[i].prefix, title = rows[i].title;
var name = prefix + ':' + title;
pageData.push({
url: prefix.replace( /wiki$/, '' ) + '.wikipedia.org/wiki/' + title,
linkName: name.replace('&', '&')
});
}
var heading = n === 0 ? 'No titles returning 404! All\'s well with the world!' :
'The following ' + n + ' titles return 404';
var data = {
alt: n === 0,
heading: heading,
items: pageData
};
hbs.registerHelper('formatUrl', function (url) {
return 'http://' + encodeURI(url).replace('&', '&');
});
res.render('list.html', data);
}
} );
};
var GET_crashers = function( req, res ) {
var cutoffDate = new Date( Date.now() - ( cutOffTime * 1000 ) );