Provide machine name on logs snapshots batch

This commit is contained in:
goga-m 2017-10-31 16:30:46 +02:00 committed by Josh Harvey
parent 62d606cc80
commit 4f9cc88a5e
4 changed files with 819 additions and 273 deletions

View file

@ -203,51 +203,26 @@ app.get('/api/logs', (req, res, next) => {
.catch(next)
})
/**
* Get support_logs for the last 2 days
*
* @name get
* @function
* @async
*
* @param {string} '/api/support_logs/ URl to handle
* @param {object} req Request object
* @param {object} res Response object
* @param {function} next Callback
*/
app.get('/api/support_logs/:timestamp', (req, res, next) => {
const timestamp = req.params.timestamp || new Date().toISOString()
return supportLogs.batch(timestamp)
app.get('/api/support_logs', (req, res, next) => {
return supportLogs.batch()
.then(supportLogs => res.send({ supportLogs }))
.catch(next)
})
app.get('/api/support_logs/logs', (req, res, next) => {
return supportLogs.get(req.query.supportLogId)
.then(log => (!_.isNil(log) && !_.isEmpty(log)) ? log : supportLogs.batch().then(_.first))
.then(log => logs.getMachineLogs(log.deviceId, log.timestamp))
.then(r => res.send(r))
.catch(next)
})
/**
* Create a new support log
*
* @name post
* @function
* @async
*
* @param {string} '/api/support_logs' URL Endpoint
* @param {object} req Request object
* @param {object} res Response object
* @param {function} next Callback
*/
app.post('/api/support_logs', (req, res, next) => {
return supportLogs.insert(req.query.deviceId)
.then(r => res.send(r))
.catch(next)
})
/**
* Endpoint for patching customer's data
*
* @param {string} '/api/customer/ Url to handle
* @param {object} req Request object
* @param {object} res Response object
* @param {function} next Callback
*/
app.patch('/api/customer/:id', (req, res, next) => {
if (!req.params.id) return res.status(400).send({Error: 'Requires id'})
const token = req.token || req.cookies.token

View file

@ -58,19 +58,22 @@ function update (deviceId, logLines) {
/**
* 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 getMachineLogs (deviceId) {
function getMachineLogs (deviceId, until = new Date().toISOString()) {
const sql = `select id, log_level, timestamp, message from logs
where device_id=$1
order by timestamp desc limit $2`
return Promise.all([db.any(sql, [ deviceId, NUM_RESULTS ]), getMachineName(deviceId)])
and timestamp <= $3
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),
currentMachine: {deviceId, name: machineName}

View file

@ -3,6 +3,22 @@ const uuid = require('uuid')
const db = require('./db')
/**
* Get single support_log by id
*
* @name get
* @function
*
* @param {string} phone Customer's phone number
*
* @returns {object} Customer
*/
function get (id) {
if (!id || _.isEmpty(id)) return Promise.resolve()
const sql = 'select * from support_logs where id=$1'
return db.oneOrNone(sql, [id])
.then(_.mapKeys(_.camelCase))
}
/**
* Insert a single support_logs row in db
*
@ -22,23 +38,21 @@ function insert (deviceId) {
}
/**
* Get the latest 48-hour logs
* Get the latest 48-hour logs snapshots
*
* @name batch
* @function
* @async
*
* @param {string} deviceId Machine's id
* @param {date} timestamp Fetch the last 48-hour logs before this timestamp
*
* @returns {array} List of all support_logs rows
*/
function batch (timestamp) {
const sql = `select * from support_logs
where timestamp > $1 - interval '2 days'
order by timestamp desc`
return db.oneOrNone(sql, [timestamp])
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')
order by s.timestamp desc`
return db.any(sql)
.then(_.map(_.mapKeys(_.camelCase)))
}
module.exports = { insert, batch }
module.exports = { get, insert, batch }

File diff suppressed because it is too large Load diff