-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
43 lines (39 loc) · 1.36 KB
/
worker.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
const md5File = require('md5-file/promise');
const fs = require('fs');
const crypto = require('crypto');
const path = require('path');
const alasql = require('alasql');
const _ = require('lodash');
const moment = require('moment');
alasql.fn.moment = moment;
module.exports.md5File = md5File;
module.exports.countLines = function(filePath) {
return new Promise((resolve, reject) => {
let count = 0;
fs.createReadStream(filePath)
.on('data', function(chunk) {
for (let i = 0; i < chunk.length; ++i)
if (chunk[i] == 10) count++;
})
.on('end', function() {
resolve(count);
});
})
}
module.exports.fileStat = function(filePath) {
const stats = fs.statSync(filePath);
return stats;
}
module.exports.alasql = async function(sql, params, outputDataPath) {
const startTime = new Date();
const results = await alasql.promise(sql, params);
const hash = crypto.createHash('md5').update(`${sql}-${JSON.stringify(params)}`).digest("hex");
const resultsPath = path.join(outputDataPath, `latest-results.json`);
fs.writeFileSync(resultsPath, JSON.stringify(results), 'utf8');
const executionTime = new Date() - startTime;
return {
itemCount: _.size(results),
executionTime,
preview: _.slice(results, 0, 500)
};
};