This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·64 lines (54 loc) · 2.09 KB
/
cli.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
#!/usr/bin/env node
'use strict'
// npm
const sample = require('lodash.samplesize')
const debug = require('debug')('yummy')
// self
const utils = require('./lib/utils')
const userChanges = () => {
const usersFeed = utils.userDB.follow({ include_docs: true, since: 'now' })
usersFeed.on('change', (change) => {
if (change.doc && change.doc.contribs) { return }
if (change.delete) { return }
if (change.id.indexOf('_design/') === 0) { return }
debug('2) CHANGE %s %s', change.id, change.seq)
utils.fetchContribs(change.doc.name)
.then((contribs) => {
change.doc.contribs = contribs
utils.putUser(change.doc)
.then((body) => debug('BODY %s %s', body.id, body.rev))
.catch((err) => debug('insert user contribs error: %s', err))
})
})
usersFeed.follow()
}
const dailyUpdates = (options) => utils.userDB.view(
'app', 'probs', options || { descending: true },
(err, body) => {
if (err) { return debug('dailyUpdates error: %s', err) }
const data = sample(body.rows, Math.floor(body.rows.length / 4))
const delay = 600
data.forEach((r, k) => {
debug('setup contrib updates for %s', r.id)
setTimeout((name) => {
debug('updating contrib for %s', name)
utils.refresh(name)
}, k * delay, utils.couchUserToName(r))
})
setTimeout((name) => {
utils.cdb.db.compact('u2', (err, body) => {
if (err) { return debug('Compacting Error: %s', err) }
debug('(%s) Compacting OK: %s', new Date().toISOString(), JSON.stringify(body))
})
}, (10 + data.length) * delay)
debug('(%s) nItems: %s; delay: %s', new Date().toISOString(), data.length, delay)
}
)
const dailySome = dailyUpdates.bind(null, { descending: true, endkey: 6 }) // 5000
const dailyWorked = dailyUpdates.bind(null, { descending: true, endkey: 75 }) // 200-400
const dailyZero = dailyUpdates.bind(null, { endkey: 75 }) // 4000
userChanges()
dailySome()
setTimeout(() => setInterval(dailySome, utils.dayUnit / 13), utils.dayUnit / 13)
setInterval(dailyWorked, utils.dayUnit / 31)
setInterval(dailyZero, utils.dayUnit)