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) .catch(next)
}) })
/** app.get('/api/support_logs', (req, res, next) => {
* Get support_logs for the last 2 days return supportLogs.batch()
* .then(supportLogs => res.send({ supportLogs }))
* @name get .catch(next)
* @function })
* @async
* app.get('/api/support_logs/logs', (req, res, next) => {
* @param {string} '/api/support_logs/ URl to handle return supportLogs.get(req.query.supportLogId)
* @param {object} req Request object .then(log => (!_.isNil(log) && !_.isEmpty(log)) ? log : supportLogs.batch().then(_.first))
* @param {object} res Response object .then(log => logs.getMachineLogs(log.deviceId, log.timestamp))
* @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)
.then(r => res.send(r)) .then(r => res.send(r))
.catch(next) .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) => { app.post('/api/support_logs', (req, res, next) => {
return supportLogs.insert(req.query.deviceId) return supportLogs.insert(req.query.deviceId)
.then(r => res.send(r)) .then(r => res.send(r))
.catch(next) .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) => { app.patch('/api/customer/:id', (req, res, next) => {
if (!req.params.id) return res.status(400).send({Error: 'Requires id'}) if (!req.params.id) return res.status(400).send({Error: 'Requires id'})
const token = req.token || req.cookies.token const token = req.token || req.cookies.token

View file

@ -58,19 +58,22 @@ function update (deviceId, logLines) {
/** /**
* Get all logs by machine id * Get all logs by machine id
* and timestamp
* *
* @name list * @name list
* @function * @function
* *
* @param {string} deviceId Machine id to fetch the logs for * @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 * @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 const sql = `select id, log_level, timestamp, message from logs
where device_id=$1 where device_id=$1
order by timestamp desc limit $2` and timestamp <= $3
return Promise.all([db.any(sql, [ deviceId, NUM_RESULTS ]), getMachineName(deviceId)]) order by timestamp asc limit $2`
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),
currentMachine: {deviceId, name: machineName} currentMachine: {deviceId, name: machineName}

View file

@ -3,6 +3,22 @@ const uuid = require('uuid')
const db = require('./db') 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 * 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 * @name batch
* @function * @function
* @async * @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 * @returns {array} List of all support_logs rows
*/ */
function batch (timestamp) { function batch () {
const sql = `select * from support_logs const sql = `select s.id, s.device_id, s.timestamp, devices.name from support_logs as s
where timestamp > $1 - interval '2 days' inner join devices on s.device_id = devices.device_id
order by timestamp desc` where timestamp > (now() - interval '2 days')
return db.oneOrNone(sql, [timestamp]) order by s.timestamp desc`
return db.any(sql)
.then(_.map(_.mapKeys(_.camelCase))) .then(_.map(_.mapKeys(_.camelCase)))
} }
module.exports = { insert, batch } module.exports = { get, insert, batch }

File diff suppressed because it is too large Load diff