From 783b85ed721106b14f1b044bd4607a174455909b Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Fri, 15 Dec 2017 02:21:59 +0200 Subject: [PATCH] give lamassu support 2 full days of logs; log sharing last for 1 week --- lib/admin/admin-support.js | 2 +- lib/logs.js | 34 +++++++++++++++++++--------------- lib/support_logs.js | 4 ++-- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/admin/admin-support.js b/lib/admin/admin-support.js index 950827da..a310459a 100644 --- a/lib/admin/admin-support.js +++ b/lib/admin/admin-support.js @@ -41,7 +41,7 @@ app.get('/api/support_logs/logs', (req, res, next) => { .then(log => (!_.isNil(log) && !_.isEmpty(log)) ? log : supportLogs.batch().then(_.first)) .then(result => { const log = result || {} - return logs.getMachineLogs(log.deviceId, log.timestamp) + return logs.getUnlimitedMachineLogs(log.deviceId, log.timestamp) }) .then(r => res.send(r)) .catch(next) diff --git a/lib/logs.js b/lib/logs.js index a88168e0..336b0ca2 100644 --- a/lib/logs.js +++ b/lib/logs.js @@ -18,7 +18,7 @@ const NUM_RESULTS = 1000 * @returns {date} Last timestamp */ function getLastSeen (deviceId) { - const sql = `select timestamp from logs + const sql = `select timestamp from logs where device_id=$1 order by timestamp desc limit 1` return db.oneOrNone(sql, [deviceId]) @@ -56,23 +56,27 @@ function update (deviceId, logLines) { return db.none(sql) } -/** - * Get all logs by machine id - * and timestamp - * - * @name list - * @function - * - * @param {string} deviceId Machine id to fetch the logs for - * @param {date} until Show the logs until the date provided, defaults to now - * - * @returns {array} Array of logs for the requested machinej - */ +function getUnlimitedMachineLogs (deviceId, until = new Date().toISOString()) { + const sql = `select id, log_level, timestamp, message from logs + where device_id=$1 + and timestamp <= $2 + and timestamp > ($2 - interval '2 days') + order by timestamp asc` + + return Promise.all([db.any(sql, [ deviceId, until ]), getMachineName(deviceId)]) + .then(([logs, machineName]) => ({ + logs: _.map(_.mapKeys(_.camelCase), logs), + currentMachine: {deviceId, name: machineName} + })) +} + 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 limit $2` + order by timestamp asc + limit $2` + return Promise.all([db.any(sql, [ deviceId, NUM_RESULTS, until ]), getMachineName(deviceId)]) .then(([logs, machineName]) => ({ logs: _.map(_.mapKeys(_.camelCase), logs), @@ -80,4 +84,4 @@ function getMachineLogs (deviceId, until = new Date().toISOString()) { })) } -module.exports = { getMachineLogs, update, getLastSeen } +module.exports = { getUnlimitedMachineLogs, getMachineLogs, update, getLastSeen } diff --git a/lib/support_logs.js b/lib/support_logs.js index b3dd6b15..81d94384 100644 --- a/lib/support_logs.js +++ b/lib/support_logs.js @@ -31,7 +31,7 @@ function get (id) { * @returns {object} Newly created support_log */ function insert (deviceId) { - const sql = `insert into support_logs + const sql = `insert into support_logs (id, device_id) values ($1, $2) returning *` return db.one(sql, [uuid.v4(), deviceId]) .then(_.mapKeys(_.camelCase)) @@ -49,7 +49,7 @@ function insert (deviceId) { function batch () { const sql = `select s.id, s.device_id, s.timestamp, devices.name from support_logs as s inner join devices on s.device_id = devices.device_id - where timestamp > (now() - interval '2 days') + where timestamp > (now() - interval '1 week') order by s.timestamp desc` return db.any(sql) .then(_.map(_.mapKeys(_.camelCase)))