-
Notifications
You must be signed in to change notification settings - Fork 37
/
progress.js
51 lines (40 loc) · 1.54 KB
/
progress.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
'use strict';
const pino = require('pino');
const log = logger.child({ module: 'stats/progress' });
// TODO: Add status for replication and other stuff?
/**
* Continuously monitor the analyzer progress, printing information such as the analysis %.
*
* @param {Nano} npmNano - The npm nano client instance.
* @param {Nano} npmsNano - The npms nano client instance.
*/
function statProgress(npmNano, npmsNano) {
// Do nothing if loglevel is higher than info
if (log.levelVal > pino.levels.values.info) {
return;
}
let pending = false;
setInterval(() => {
if (pending) {
log.info('Progress stat is still being retrieved..');
return;
}
pending = true;
Promise.props({
npmDocsCount: npmNano.infoAsync().then((res) => res.doc_count),
npmDesignDocsCount: npmNano.listAsync({ startkey: '_design/', endkey: '_design0' }).then((res) => res.rows.length),
npmsPackagesCount: npmsNano.viewAsync('npms-analyzer', 'packages-evaluation', { reduce: true })
.then((res) => res.rows[0] ? res.rows[0].value : 0),
})
.finally(() => { pending = false; })
.then((result) => {
const analysis = `${(result.npmsPackagesCount / (result.npmDocsCount - result.npmDesignDocsCount) * 100).toFixed(4)}%`;
log.info({ analysis }, 'Progress stat');
}, (err) => {
log.error({ err }, 'Progress stat failed');
})
.done();
}, 15000)
.unref();
}
module.exports = statProgress;