add serials to machine logging
This commit is contained in:
parent
547e9c08b6
commit
beb2a2136d
2 changed files with 18 additions and 7 deletions
15
lib/logs.js
15
lib/logs.js
|
|
@ -18,11 +18,11 @@ const NUM_RESULTS = 1000
|
||||||
* @returns {date} Last timestamp
|
* @returns {date} Last timestamp
|
||||||
*/
|
*/
|
||||||
function getLastSeen (deviceId) {
|
function getLastSeen (deviceId) {
|
||||||
const sql = `select id, timestamp from logs
|
const sql = `select id, timestamp, serial from logs
|
||||||
where device_id=$1
|
where device_id=$1
|
||||||
order by timestamp desc limit 1`
|
order by timestamp desc, serial desc limit 1`
|
||||||
return db.oneOrNone(sql, [deviceId])
|
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) {
|
function update (deviceId, logLines) {
|
||||||
const cs = new pgp.helpers.ColumnSet([
|
const cs = new pgp.helpers.ColumnSet([
|
||||||
'id', 'device_id', 'log_level', 'timestamp', 'message'],
|
'id', 'device_id', 'log_level', 'timestamp', 'serial', 'message'],
|
||||||
{table: 'logs'})
|
{table: 'logs'})
|
||||||
|
|
||||||
const logs = _.map(log => {
|
const logs = _.map(log => {
|
||||||
|
|
@ -48,7 +48,8 @@ function update (deviceId, logLines) {
|
||||||
deviceId: deviceId,
|
deviceId: deviceId,
|
||||||
message: log.msg,
|
message: log.msg,
|
||||||
logLevel: _.contains('error', _.lowerCase(log.msg)) ? 'error' : 'info',
|
logLevel: _.contains('error', _.lowerCase(log.msg)) ? 'error' : 'info',
|
||||||
timestamp: log.timestamp
|
timestamp: log.timestamp,
|
||||||
|
serial: log.serial || 0
|
||||||
}
|
}
|
||||||
return _.mapKeys(_.snakeCase, formatted)
|
return _.mapKeys(_.snakeCase, formatted)
|
||||||
}, logLines)
|
}, logLines)
|
||||||
|
|
@ -61,7 +62,7 @@ function getUnlimitedMachineLogs (deviceId, until = new Date().toISOString()) {
|
||||||
where device_id=$1
|
where device_id=$1
|
||||||
and timestamp <= $2
|
and timestamp <= $2
|
||||||
and timestamp > ($2 - interval '2 days')
|
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)])
|
return Promise.all([db.any(sql, [ deviceId, until ]), getMachineName(deviceId)])
|
||||||
.then(([logs, machineName]) => ({
|
.then(([logs, machineName]) => ({
|
||||||
|
|
@ -74,7 +75,7 @@ function getMachineLogs (deviceId, until = new Date().toISOString()) {
|
||||||
const sql = `select id, log_level, timestamp, message from logs
|
const sql = `select id, log_level, timestamp, message from logs
|
||||||
where device_id=$1
|
where device_id=$1
|
||||||
and timestamp <= $3
|
and timestamp <= $3
|
||||||
order by timestamp asc
|
order by timestamp asc, serial asc
|
||||||
limit $2`
|
limit $2`
|
||||||
|
|
||||||
return Promise.all([db.any(sql, [ deviceId, NUM_RESULTS, until ]), getMachineName(deviceId)])
|
return Promise.all([db.any(sql, [ deviceId, NUM_RESULTS, until ]), getMachineName(deviceId)])
|
||||||
|
|
|
||||||
10
migrations/1514981004673-add_serial_to_logs.js
Normal file
10
migrations/1514981004673-add_serial_to_logs.js
Normal 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()
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue