add machine uptime

This commit is contained in:
Josh Harvey 2016-12-20 22:36:17 +02:00
parent d4035de4d6
commit ed8a51af46
3 changed files with 215 additions and 137 deletions

View file

@ -5,7 +5,28 @@ const settingsLoader = require('../settings-loader')
const db = require('../db')
const CONSIDERED_UP = 30000
const CONSIDERED_UP_SECS = 30
function machinesLastPing () {
const sql = `select name, min(extract(epoch from (now() - machine_events.created))) as age
from machine_events, devices
where machine_events.device_id = devices.device_id
group by name`
return db.any(sql)
.then(r => {
const downRows = r.filter(row => row.age > CONSIDERED_UP_SECS)
if (downRows.length === 0) return 'All machines are up'
if (downRows.length === 1) {
const row = downRows[0]
const age = moment.duration(row.age, 'seconds')
return `${row.name} down for ${age.humanize()}`
}
return 'Multiple machines down'
})
}
function status () {
const sql = `select extract(epoch from (now() - created)) as age
@ -14,12 +35,12 @@ function status () {
order by created desc
limit 1`
return db.oneOrNone(sql, ['ping'])
.then(row => {
if (!row) return {up: false, lastPing: null}
return Promise.all([db.oneOrNone(sql, ['ping']), machinesLastPing()])
.then(([statusRow, machineStatus]) => {
if (!statusRow) return {up: false, lastPing: null, machineStatus}
const age = moment.duration(row.age, 'seconds')
const up = age.asMilliseconds() < CONSIDERED_UP
const age = moment.duration(statusRow.age, 'seconds')
const up = statusRow.age < CONSIDERED_UP_SECS
const lastPing = age.humanize()
return settingsLoader.loadLatest()
@ -31,7 +52,7 @@ function status () {
bid: parseFloat(ratesRec.rates.bid),
ask: parseFloat(ratesRec.rates.ask)
}]
return {up, lastPing, rates}
return {up, lastPing, rates, machineStatus}
})
})
})