This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulkMatch.js
67 lines (59 loc) · 1.78 KB
/
bulkMatch.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
'use strict';
const fs = require( 'fs' );
const process = require( 'process' );
const readline = require( 'readline' );
const parser = require( './lib/sparqlParser' );
const defaultQueryHandlerChain = require( './lib/defaultQueryHandlerChain' );
function getResultForQuery( query, handlers ) {
let parsedQuery;
try {
parsedQuery = parser.parse( query );
} catch ( _e ) {
return 'parseError';
}
for ( const handler of handlers ) {
try {
const request = handler.handle( query, parsedQuery );
if ( request ) {
return request.constructor.name;
}
} catch ( _e ) {
return 'handlerError';
}
}
return 'default';
}
const rd = readline.createInterface( {
input: fs.createReadStream( process.argv[ 2 ] ),
output: process.stdout,
terminal: false,
} );
const handlers = defaultQueryHandlerChain.queryHandlers;
const resultStats = {};
rd.on( 'line', ( line ) => {
const [ count_, averageTime_, encodedQuery ] = line.split( ',' );
if ( count_ === 'count' ) {
return; // header line
}
const count = parseInt( count_ );
const averageTime = parseFloat( averageTime_ );
const query = decodeURIComponent( encodedQuery.replace( /\+/g, '%20' ) );
const resultForQuery = getResultForQuery( query, handlers );
if ( !( resultForQuery in resultStats ) ) {
resultStats[ resultForQuery ] = { distinct: 0, count: 0, time: 0 };
}
resultStats[ resultForQuery ].distinct++;
resultStats[ resultForQuery ].count += count;
resultStats[ resultForQuery ].time += count * averageTime;
} );
rd.on( 'close', () => {
const total = Object.values( resultStats )
.reduce( ( acc, stats ) => {
acc.distinct += stats.distinct;
acc.count += stats.count;
acc.time += stats.time;
return acc;
}, { distinct: 0, count: 0, time: 0 } );
resultStats.total = total;
console.log( resultStats );
} );