Files: 2e24f63f3e975823d94bc8e83243dd998d9caa5f / apis / progress.js
870 bytesRaw
1 | var u = require('yap-util') |
2 | |
3 | function round (n, p) { |
4 | return Math.round(n * p) / p |
5 | } |
6 | |
7 | function percent (n) { |
8 | return (round(n, 1000)*100).toString().substring(0, 4)+'%' |
9 | } |
10 | |
11 | function rate (prog) { |
12 | if(prog.target == prog.current) return 1 |
13 | return (prog.current - prog.start) / (prog.target - prog.start) |
14 | } |
15 | |
16 | module.exports = function (sbot) { |
17 | return function (opts, apply) { |
18 | return function (cb) { |
19 | sbot.progress(function (err, prog) { |
20 | var s = '', r = 1 |
21 | for(var k in prog) |
22 | if(prog[k].current <= prog[k].target) { |
23 | var _r = rate(prog[k]) |
24 | r = Math.min(r, _r) |
25 | s += (s ? ', ' : '') + k +': ' + percent(r) |
26 | } |
27 | cb(null, ['progress', Object.assign( |
28 | {value: r, max: 1, title: s}, |
29 | apply.cacheAttrs('/progress', 'prog') |
30 | ) |
31 | ]) |
32 | }) |
33 | } |
34 | } |
35 | } |
36 | |
37 | |
38 | |
39 |
Built with git-ssb-web