Files: 8c686fb2b73c28246604759c61d6636e2812d871 / apis / progress.js
830 bytesRaw
1 | var u = require('../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 () { |
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', {value: r, max: 1, title: s}, |
28 | u.cacheTag('/progress', 'prog', Date.now() + 1000) |
29 | ]) |
30 | }) |
31 | } |
32 | } |
33 | } |
34 | |
35 | |
36 |
Built with git-ssb-web