give lamassu support 2 full days of logs; log sharing last for 1 week

This commit is contained in:
Josh Harvey 2017-12-15 02:21:59 +02:00
parent dc2f4e497f
commit 783b85ed72
3 changed files with 22 additions and 18 deletions

View file

@ -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(log => (!_.isNil(log) && !_.isEmpty(log)) ? log : supportLogs.batch().then(_.first))
.then(result => { .then(result => {
const log = result || {} const log = result || {}
return logs.getMachineLogs(log.deviceId, log.timestamp) return logs.getUnlimitedMachineLogs(log.deviceId, log.timestamp)
}) })
.then(r => res.send(r)) .then(r => res.send(r))
.catch(next) .catch(next)

View file

@ -18,7 +18,7 @@ const NUM_RESULTS = 1000
* @returns {date} Last timestamp * @returns {date} Last timestamp
*/ */
function getLastSeen (deviceId) { function getLastSeen (deviceId) {
const sql = `select timestamp from logs const sql = `select timestamp from logs
where device_id=$1 where device_id=$1
order by timestamp desc limit 1` order by timestamp desc limit 1`
return db.oneOrNone(sql, [deviceId]) return db.oneOrNone(sql, [deviceId])
@ -56,23 +56,27 @@ function update (deviceId, logLines) {
return db.none(sql) return db.none(sql)
} }
/** function getUnlimitedMachineLogs (deviceId, until = new Date().toISOString()) {
* Get all logs by machine id const sql = `select id, log_level, timestamp, message from logs
* and timestamp where device_id=$1
* and timestamp <= $2
* @name list and timestamp > ($2 - interval '2 days')
* @function order by timestamp asc`
*
* @param {string} deviceId Machine id to fetch the logs for return Promise.all([db.any(sql, [ deviceId, until ]), getMachineName(deviceId)])
* @param {date} until Show the logs until the date provided, defaults to now .then(([logs, machineName]) => ({
* logs: _.map(_.mapKeys(_.camelCase), logs),
* @returns {array} Array of logs for the requested machinej currentMachine: {deviceId, name: machineName}
*/ }))
}
function getMachineLogs (deviceId, until = new Date().toISOString()) { 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 limit $2` order by timestamp asc
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)])
.then(([logs, machineName]) => ({ .then(([logs, machineName]) => ({
logs: _.map(_.mapKeys(_.camelCase), logs), 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 }

View file

@ -31,7 +31,7 @@ function get (id) {
* @returns {object} Newly created support_log * @returns {object} Newly created support_log
*/ */
function insert (deviceId) { function insert (deviceId) {
const sql = `insert into support_logs const sql = `insert into support_logs
(id, device_id) values ($1, $2) returning *` (id, device_id) values ($1, $2) returning *`
return db.one(sql, [uuid.v4(), deviceId]) return db.one(sql, [uuid.v4(), deviceId])
.then(_.mapKeys(_.camelCase)) .then(_.mapKeys(_.camelCase))
@ -49,7 +49,7 @@ function insert (deviceId) {
function batch () { function batch () {
const sql = `select s.id, s.device_id, s.timestamp, devices.name from support_logs as s 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 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` order by s.timestamp desc`
return db.any(sql) return db.any(sql)
.then(_.map(_.mapKeys(_.camelCase))) .then(_.map(_.mapKeys(_.camelCase)))