diff --git a/lib/logs.js b/lib/logs.js index 278f8b30..11e948ee 100644 --- a/lib/logs.js +++ b/lib/logs.js @@ -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)]) diff --git a/migrations/1514981004673-add_serial_to_logs.js b/migrations/1514981004673-add_serial_to_logs.js new file mode 100644 index 00000000..560b6549 --- /dev/null +++ b/migrations/1514981004673-add_serial_to_logs.js @@ -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() +}