This commit is contained in:
goga-m 2017-10-17 21:19:00 +03:00 committed by Josh Harvey
parent d127901992
commit 43f578d311
6 changed files with 803 additions and 4009 deletions

View file

@ -28,6 +28,7 @@ const pairing = require('./pairing')
const server = require('./server') const server = require('./server')
const transactions = require('./transactions') const transactions = require('./transactions')
const customers = require('../customers') const customers = require('../customers')
const logs = require('../logs')
const funding = require('./funding') const funding = require('./funding')
const _ = require('lodash/fp') const _ = require('lodash/fp')
@ -184,6 +185,12 @@ app.get('/api/customer/:id', (req, res, next) => {
}) })
}) })
app.get('/api/logs/:deviceId', (req, res, next) => {
return logs.getMachineLogs(req.params.deviceId)
.then(r => res.send(r))
.catch(next)
})
/** /**
* Endpoint for patching customer's data * Endpoint for patching customer's data
* *

97
lib/logs.js Normal file
View file

@ -0,0 +1,97 @@
const _ = require('lodash/fp')
const db = require('./db')
const machineLoader = require('./machine-loader')
const NUM_RESULTS = 1000
function getLastSeen (deviceId) {
const sql = `select timestamp from logs
where device_id=$1
order by timestamp desc limit 1`
return db.oneOrNone(sql, [deviceId])
.then(log => log ? log.timestamp : null)
}
function insert (log) {
console.log('inserting', log)
const sql = `insert into logs
(id, device_id, log_level, timestamp, message) values
($1, $2, $3, $4, $5) on conflict do nothing`
return db.oneOrNone(sql, [log.id, log.deviceId, log.logLevel, log.timestamp, log.message])
}
function update (deviceId, logLines) {
// Prepare logs to update
const logs = _.map(log => {
return {
id: log.id,
deviceId: deviceId,
message: log.msg,
logLevel: log.level,
timestamp: log.timestamp
}
}, logLines)
// Batch save logs
return Promise.all(_.map(insert, _.compact(logs)))
}
/**
* Get all logs by machine id
*
* @name list
* @function
*
* @param {string} deviceId Machine id to fetch the logs for
*
* @returns {array} Array of logs for the requested machinej
*/
function getMachineLogs (deviceId) {
const sql = `select * from logs
where device_id=$1
order by timestamp desc limit $2`
return db.any(sql, [ deviceId, NUM_RESULTS ])
.then(_.map(camelize))
.then(logs => {
return getMachineById(deviceId)
.then(currentMachine => {
return {
logs,
currentMachine
}
})
})
}
/**
* Find machine by id
*
* @name getMachineById
* @function
* @async
*
* @param {string} deviceId machine's id
*
* @returns {object} Found machine
*/
function getMachineById (deviceId) {
return machineLoader.getMachineNames().then(names => {
return _.find({deviceId}, names)
})
}
/**
* Camelize log fields
* Note: return null if log is undefined
*
* @name camelize
* @function
*
* @param {object} log Log with snake_case fields
* @returns {object} Camelized Log object
*/
function camelize (log) {
return log ? _.mapKeys(_.camelCase, log) : null
}
module.exports = { getMachineLogs, update, getLastSeen }

View file

@ -17,6 +17,7 @@ const poller = require('./poller')
const Tx = require('./tx') const Tx = require('./tx')
const E = require('./error') const E = require('./error')
const customers = require('./customers') const customers = require('./customers')
const logs = require('./logs')
const argv = require('minimist')(process.argv.slice(2)) const argv = require('minimist')(process.argv.slice(2))
@ -174,6 +175,18 @@ function getCustomerWithPhoneCode (req, res, next) {
.catch(next) .catch(next)
} }
function getLastSeen (req, res, next) {
return logs.getLastSeen(req.deviceId)
.then(timestamp => res.json({lastSeen: timestamp}))
.catch(next)
}
function updateLogs (req, res, next) {
return logs.update(req.deviceId, req.body.logs)
.then(status => res.json({success: status}))
.catch(next)
}
function ca (req, res) { function ca (req, res) {
const token = req.query.token const token = req.query.token
@ -291,6 +304,8 @@ app.post('/phone_code', getCustomerWithPhoneCode)
app.post('/tx', postTx) app.post('/tx', postTx)
app.get('/tx/:id', getTx) app.get('/tx/:id', getTx)
app.get('/tx', getPhoneTx) app.get('/tx', getPhoneTx)
app.get('/logs', getLastSeen)
app.post('/logs', updateLogs)
app.use(errorHandler) app.use(errorHandler)
app.use((req, res) => { app.use((req, res) => {

View file

@ -0,0 +1,18 @@
var db = require('./db')
exports.up = function (next) {
const sql =
[`create table logs (
id uuid PRIMARY KEY,
device_id text,
log_level text,
timestamp timestamptz,
message text)`
]
db.multi(sql, next)
}
exports.down = function (next) {
next()
}

File diff suppressed because one or more lines are too long

View file

@ -24,6 +24,18 @@ p {
height: 100%; height: 100%;
} }
.lamassuAdminPaneWrapper {
display: flex;
}
.lamassuAdminLeftPane {
min-width: 270px;
}
.lamassuAdminContentPane {
max-height: 100%;
}
.lamassuAdminStatusBar { .lamassuAdminStatusBar {
position: fixed; position: fixed;
bottom: 0; bottom: 0;