-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgetReportedSentences.js
76 lines (66 loc) · 2.1 KB
/
getReportedSentences.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
const fs = require('fs');
const path = require('path');
const csv = require('fast-csv');
const { saveStatsToDisk } = require('./processStats');
/**
* Download all reported sentences for each language
*
* @param {Object} db database connection
* @param {array} localeDirs array of locale directories
* @param {string} releaseName name of current release
*
* @return {Object} key/value pairs of locale { reportedSentences: # }
* to merge w Stats obj
*/
const getReportedSentences = (db, localeDirs, releaseName) => {
const QUERY_FILE = path.join(
__dirname,
'queries',
'getReportedSentences.sql',
);
const TSV_OPTIONS = {
headers: true,
delimiter: '\t',
quote: false,
};
return new Promise((resolve) => {
const reportedSentences = {};
db.query(fs.readFileSync(QUERY_FILE, 'utf-8'))
.on('result', (row) => {
// if locale is not included in current set of releases, skip
if (!localeDirs.includes(row.locale)) return;
// initiate locale in results object
if (reportedSentences[row.locale] === undefined) {
reportedSentences[row.locale] = [];
}
// add reported sentence
reportedSentences[row.locale].push({
...row,
sentence: row.sentence.split('\r').join(' '),
});
})
.on('end', () => {
// When complete, write all reported sentences to TSV
Object.keys(reportedSentences).forEach((locale) => {
const localePath = path.join(
__dirname,
releaseName,
locale,
'reported.tsv',
);
csv
.write(reportedSentences[locale], TSV_OPTIONS)
.pipe(fs.createWriteStream(localePath));
reportedSentences[locale] = {
reportedSentences: reportedSentences[locale].length,
};
});
// Merge with existing stats and return reported sentence counts
saveStatsToDisk(releaseName, { locales: reportedSentences });
resolve(reportedSentences);
});
});
};
module.exports = {
getReportedSentences,
};