add serials to machine logging

This commit is contained in:
Josh Harvey 2018-01-03 13:01:02 +00:00
parent 547e9c08b6
commit beb2a2136d
2 changed files with 18 additions and 7 deletions

View file

@ -18,11 +18,11 @@ const NUM_RESULTS = 1000
* @returns {date} Last timestamp
*/
function getLastSeen (deviceId) {
const sql = `select id, timestamp from logs
const sql = `select id, timestamp, serial from logs
where device_id=$1
order by timestamp desc limit 1`
order by timestamp desc, serial desc limit 1`
return db.oneOrNone(sql, [deviceId])
.then(log => log ? {timestamp: log.timestamp, id: log.id} : null)
.then(log => log ? {timestamp: log.timestamp, serial: log.serial, id: log.id} : null)
}
/**
@ -39,7 +39,7 @@ function getLastSeen (deviceId) {
*/
function update (deviceId, logLines) {
const cs = new pgp.helpers.ColumnSet([
'id', 'device_id', 'log_level', 'timestamp', 'message'],
'id', 'device_id', 'log_level', 'timestamp', 'serial', 'message'],
{table: 'logs'})
const logs = _.map(log => {
@ -48,7 +48,8 @@ function update (deviceId, logLines) {
deviceId: deviceId,
message: log.msg,
logLevel: _.contains('error', _.lowerCase(log.msg)) ? 'error' : 'info',
timestamp: log.timestamp
timestamp: log.timestamp,
serial: log.serial || 0
}
return _.mapKeys(_.snakeCase, formatted)
}, logLines)
@ -61,7 +62,7 @@ function getUnlimitedMachineLogs (deviceId, until = new Date().toISOString()) {
where device_id=$1
and timestamp <= $2
and timestamp > ($2 - interval '2 days')
order by timestamp asc`
order by timestamp asc, serial asc`
return Promise.all([db.any(sql, [ deviceId, until ]), getMachineName(deviceId)])
.then(([logs, machineName]) => ({
@ -74,7 +75,7 @@ function getMachineLogs (deviceId, until = new Date().toISOString()) {
const sql = `select id, log_level, timestamp, message from logs
where device_id=$1
and timestamp <= $3
order by timestamp asc
order by timestamp asc, serial asc
limit $2`
return Promise.all([db.any(sql, [ deviceId, NUM_RESULTS, until ]), getMachineName(deviceId)])

View file

@ -0,0 +1,10 @@
const db = require('./db')
exports.up = function (next) {
const sql = ['alter table logs add column serial integer not null default 0']
db.multi(sql, next)
}
exports.down = function (next) {
next()
}