-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdash.js
37 lines (32 loc) · 981 Bytes
/
dash.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
const Fastify = require('fastify')
const prettyMs = require('pretty-ms')
const { format } = require('timeago.js')
module.exports = function(foke) {
const fastify = Fastify({
logger: foke.options.dashLogger
})
fastify.get('/', async (req, reply) => {
const data = foke.fokes.map(
({ name, interval, runCount, runAt, isRunning, spider }) => {
const waiting = interval - (Date.now() - runAt)
const data = {
name,
runCount,
isRunning,
runAt: format(new Date(runAt)),
interval: prettyMs(interval),
waiting: !isRunning && runAt && waiting > 0 ? prettyMs(waiting) : 0
}
if (spider) {
data.stats = spider.stats
}
return data
}
)
reply.send(JSON.stringify(data, null, 4))
})
fastify.listen(foke.options.dashPort, '0.0.0.0', (err, address) => {
if (err) throw err
fastify.log.info(`server listening on ${address}`)
})
}