Create support_logs (db & api)

This commit is contained in:
goga-m 2017-10-27 17:36:40 +03:00 committed by Josh Harvey
parent b7d6f3f419
commit 62d606cc80
8 changed files with 279 additions and 58 deletions

View file

@ -29,6 +29,7 @@ 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 logs = require('../logs')
const supportLogs = require('../support_logs')
const funding = require('./funding') const funding = require('./funding')
const _ = require('lodash/fp') const _ = require('lodash/fp')
@ -202,6 +203,43 @@ app.get('/api/logs', (req, res, next) => {
.catch(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)
.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 * Endpoint for patching customer's data
* *

View file

@ -3,9 +3,7 @@ const _ = require('lodash/fp')
const db = require('./db') const db = require('./db')
const pgp = require('pg-promise')() const pgp = require('pg-promise')()
const settingsLoader = require('./settings-loader') const getMachineName = require('./machine-loader').getMachineName
const configManager = require('./config-manager')
const NUM_RESULTS = 1000 const NUM_RESULTS = 1000
/** /**
@ -79,22 +77,4 @@ function getMachineLogs (deviceId) {
})) }))
} }
/**
* Given the machine id, get the machine name
*
* @name getMachineName
* @function
* @async
*
* @param {string} machineId machine id
* @returns {string} machine name
*/
function getMachineName (machineId) {
return settingsLoader.loadRecentConfig()
.then(config => {
const machineScoped = configManager.machineScoped(machineId, config)
return machineScoped.machineName
})
}
module.exports = { getMachineLogs, update, getLastSeen } module.exports = { getMachineLogs, update, getLastSeen }

View file

@ -6,7 +6,7 @@ const pairing = require('./pairing')
const configManager = require('./config-manager') const configManager = require('./config-manager')
const settingsLoader = require('./settings-loader') const settingsLoader = require('./settings-loader')
module.exports = {getMachines, getMachineNames, setMachine} module.exports = {getMachineName, getMachines, getMachineNames, setMachine}
function getMachines () { function getMachines () {
return db.any('select * from devices where display=TRUE order by created') return db.any('select * from devices where display=TRUE order by created')
@ -40,6 +40,24 @@ function getMachineNames (config) {
}) })
} }
/**
* Given the machine id, get the machine name
*
* @name getMachineName
* @function
* @async
*
* @param {string} machineId machine id
* @returns {string} machine name
*/
function getMachineName (machineId) {
return settingsLoader.loadRecentConfig()
.then(config => {
const machineScoped = configManager.machineScoped(machineId, config)
return machineScoped.machineName
})
}
function resetCashOutBills (rec) { function resetCashOutBills (rec) {
const sql = 'update devices set cassette1=$1, cassette2=$2 where device_id=$3' const sql = 'update devices set cassette1=$1, cassette2=$2 where device_id=$3'
return db.none(sql, [rec.cassettes[0], rec.cassettes[1], rec.deviceId]) return db.none(sql, [rec.cassettes[0], rec.cassettes[1], rec.deviceId])

View file

@ -46,12 +46,12 @@ function pair (token, deviceId, machineModel) {
.then(r => { .then(r => {
if (r.expired) return false if (r.expired) return false
const insertSql = `insert into devices (device_id) values ($1) const insertSql = `insert into devices (device_id, name) values ($1, $2)
on conflict (device_id) on conflict (device_id)
do update set paired=TRUE, display=TRUE` do update set paired=TRUE, display=TRUE`
return configureNewDevice(deviceId, r.name, machineModel) return configureNewDevice(deviceId, r.name, machineModel)
.then(() => db.none(insertSql, [deviceId])) .then(() => db.none(insertSql, [deviceId, r.name]))
.then(() => true) .then(() => true)
}) })
.catch(err => { .catch(err => {

44
lib/support_logs.js Normal file
View file

@ -0,0 +1,44 @@
const _ = require('lodash/fp')
const uuid = require('uuid')
const db = require('./db')
/**
* Insert a single support_logs row in db
*
* @name insert
* @function
* @async
*
* @param {string} deviceId Machine's id for the log
*
* @returns {object} Newly created support_log
*/
function insert (deviceId) {
const sql = `insert into support_logs
(id, device_id) values ($1, $2) returning *`
return db.one(sql, [uuid.v4(), deviceId])
.then(_.mapKeys(_.camelCase))
}
/**
* Get the latest 48-hour logs
*
* @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])
.then(_.map(_.mapKeys(_.camelCase)))
}
module.exports = { insert, batch }

View file

@ -0,0 +1,17 @@
var db = require('./db')
exports.up = function (next) {
const sql =
[`create table support_logs (
id uuid PRIMARY KEY,
device_id text,
timestamp timestamptz not null default now() )`,
'alter table logs add column server_timestamp timestamptz not null default now() '
]
db.multi(sql, next)
}
exports.down = function (next) {
next()
}

View file

@ -0,0 +1,12 @@
const db = require('./db')
exports.up = function (next) {
const sql = [
'alter table devices add column name text not null'
]
db.multi(sql, next)
}
exports.down = function (next) {
next()
}

View file

@ -34139,14 +34139,24 @@ var _user$project$Logs_Types$Log = F4(
function (a, b, c, d) { function (a, b, c, d) {
return {id: a, timestamp: b, logLevel: c, message: d}; return {id: a, timestamp: b, logLevel: c, message: d};
}); });
var _user$project$Logs_Types$SupportLog = F3(
function (a, b, c) {
return {id: a, deviceId: b, timestamp: c};
});
var _user$project$Logs_Types$Logs = F2( var _user$project$Logs_Types$Logs = F2(
function (a, b) { function (a, b) {
return {logs: a, currentMachine: b}; return {logs: a, currentMachine: b};
}); });
var _user$project$Logs_Types$Model = F2( var _user$project$Logs_Types$Model = F3(
function (a, b) { function (a, b, c) {
return {logs: a, machines: b}; return {logs: a, machines: b, supportLog: c};
}); });
var _user$project$Logs_Types$LoadSupportLog = function (a) {
return {ctor: 'LoadSupportLog', _0: a};
};
var _user$project$Logs_Types$ShareLogs = function (a) {
return {ctor: 'ShareLogs', _0: a};
};
var _user$project$Logs_Types$LoadMachines = function (a) { var _user$project$Logs_Types$LoadMachines = function (a) {
return {ctor: 'LoadMachines', _0: a}; return {ctor: 'LoadMachines', _0: a};
}; };
@ -35533,6 +35543,19 @@ var _user$project$Logs_Decoder$machinesDecoder = A2(
_elm_lang$core$Json_Decode$field, _elm_lang$core$Json_Decode$field,
'machines', 'machines',
_elm_lang$core$Json_Decode$list(_user$project$Logs_Decoder$machineDecoder)); _elm_lang$core$Json_Decode$list(_user$project$Logs_Decoder$machineDecoder));
var _user$project$Logs_Decoder$supportLogDecoder = A3(
_NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required,
'timestamp',
_elm_community$json_extra$Json_Decode_Extra$date,
A3(
_NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required,
'deviceId',
_elm_lang$core$Json_Decode$string,
A3(
_NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required,
'id',
_elm_lang$core$Json_Decode$string,
_NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$decode(_user$project$Logs_Types$SupportLog))));
var _user$project$Logs_Decoder$logDecoder = A3( var _user$project$Logs_Decoder$logDecoder = A3(
_NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required,
'message', 'message',
@ -35560,6 +35583,19 @@ var _user$project$Logs_Decoder$logsDecoder = A3(
_elm_lang$core$Json_Decode$list(_user$project$Logs_Decoder$logDecoder), _elm_lang$core$Json_Decode$list(_user$project$Logs_Decoder$logDecoder),
_NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$decode(_user$project$Logs_Types$Logs))); _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$decode(_user$project$Logs_Types$Logs)));
var _user$project$Logs_Rest$shareLogs = function (id) {
return A2(
_elm_lang$core$Platform_Cmd$map,
_user$project$Logs_Types$LoadSupportLog,
A2(
_lukewestby$elm_http_builder$HttpBuilder$send,
_krisajenkins$remotedata$RemoteData$fromResult,
A2(
_lukewestby$elm_http_builder$HttpBuilder$withExpect,
_elm_lang$http$Http$expectJson(_user$project$Logs_Decoder$supportLogDecoder),
_lukewestby$elm_http_builder$HttpBuilder$post(
A2(_elm_lang$core$Basics_ops['++'], '/api/support_logs?deviceId=', id)))));
};
var _user$project$Logs_Rest$getMachines = A2( var _user$project$Logs_Rest$getMachines = A2(
_elm_lang$core$Platform_Cmd$map, _elm_lang$core$Platform_Cmd$map,
_user$project$Logs_Types$LoadMachines, _user$project$Logs_Types$LoadMachines,
@ -35582,7 +35618,8 @@ var _user$project$Logs_Rest$getLogs = function (maybeId) {
var _user$project$Logs_State$update = F2( var _user$project$Logs_State$update = F2(
function (msg, model) { function (msg, model) {
var _p0 = msg; var _p0 = msg;
if (_p0.ctor === 'LoadLogs') { switch (_p0.ctor) {
case 'LoadLogs':
return { return {
ctor: '_Tuple2', ctor: '_Tuple2',
_0: _elm_lang$core$Native_Utils.update( _0: _elm_lang$core$Native_Utils.update(
@ -35590,7 +35627,7 @@ var _user$project$Logs_State$update = F2(
{logs: _p0._0}), {logs: _p0._0}),
_1: _elm_lang$core$Platform_Cmd$none _1: _elm_lang$core$Platform_Cmd$none
}; };
} else { case 'LoadMachines':
return { return {
ctor: '_Tuple2', ctor: '_Tuple2',
_0: _elm_lang$core$Native_Utils.update( _0: _elm_lang$core$Native_Utils.update(
@ -35598,6 +35635,23 @@ var _user$project$Logs_State$update = F2(
{machines: _p0._0}), {machines: _p0._0}),
_1: _elm_lang$core$Platform_Cmd$none _1: _elm_lang$core$Platform_Cmd$none
}; };
case 'ShareLogs':
return A2(
_elm_lang$core$Platform_Cmd_ops['!'],
model,
{
ctor: '::',
_0: _user$project$Logs_Rest$shareLogs(_p0._0.deviceId),
_1: {ctor: '[]'}
});
default:
return {
ctor: '_Tuple2',
_0: _elm_lang$core$Native_Utils.update(
model,
{supportLog: _p0._0}),
_1: _elm_lang$core$Platform_Cmd$none
};
} }
}); });
var _user$project$Logs_State$getData = function (maybeId) { var _user$project$Logs_State$getData = function (maybeId) {
@ -35615,20 +35669,54 @@ var _user$project$Logs_State$getData = function (maybeId) {
var _user$project$Logs_State$load = function (maybeId) { var _user$project$Logs_State$load = function (maybeId) {
return { return {
ctor: '_Tuple2', ctor: '_Tuple2',
_0: {logs: _krisajenkins$remotedata$RemoteData$Loading, machines: _krisajenkins$remotedata$RemoteData$Loading}, _0: {logs: _krisajenkins$remotedata$RemoteData$Loading, machines: _krisajenkins$remotedata$RemoteData$Loading, supportLog: _krisajenkins$remotedata$RemoteData$NotAsked},
_1: _user$project$Logs_State$getData(maybeId) _1: _user$project$Logs_State$getData(maybeId)
}; };
}; };
var _user$project$Logs_State$init = {logs: _krisajenkins$remotedata$RemoteData$NotAsked, machines: _krisajenkins$remotedata$RemoteData$NotAsked}; var _user$project$Logs_State$init = {logs: _krisajenkins$remotedata$RemoteData$NotAsked, machines: _krisajenkins$remotedata$RemoteData$NotAsked, supportLog: _krisajenkins$remotedata$RemoteData$NotAsked};
var _user$project$Logs_View$latestLogSnapshot = function (model) {
var _p0 = model.supportLog;
switch (_p0.ctor) {
case 'NotAsked':
return A2(
_elm_lang$html$Html$div,
{ctor: '[]'},
{ctor: '[]'});
case 'Loading':
return A2(
_elm_lang$html$Html$div,
{ctor: '[]'},
{ctor: '[]'});
case 'Failure':
return A2(
_elm_lang$html$Html$div,
{ctor: '[]'},
{
ctor: '::',
_0: _elm_lang$html$Html$text(
_elm_lang$core$Basics$toString(_p0._0)),
_1: {ctor: '[]'}
});
default:
return A2(
_elm_lang$html$Html$div,
{ctor: '[]'},
{
ctor: '::',
_0: _elm_lang$html$Html$text('Saved latest snapshot'),
_1: {ctor: '[]'}
});
}
};
var _user$project$Logs_View$maybeText = function (maybeString) { var _user$project$Logs_View$maybeText = function (maybeString) {
return _elm_lang$html$Html$text( return _elm_lang$html$Html$text(
A2(_elm_lang$core$Maybe$withDefault, '', maybeString)); A2(_elm_lang$core$Maybe$withDefault, '', maybeString));
}; };
var _user$project$Logs_View$formatDate = function (date) { var _user$project$Logs_View$formatDate = function (date) {
var _p0 = date; var _p1 = date;
if (_p0.ctor === 'Just') { if (_p1.ctor === 'Just') {
return A2(_justinmimbs$elm_date_extra$Date_Extra$toFormattedString, 'yyyy-MM-dd HH:mm', _p0._0); return A2(_justinmimbs$elm_date_extra$Date_Extra$toFormattedString, 'yyyy-MM-dd HH:mm', _p1._0);
} else { } else {
return ''; return '';
} }
@ -35761,9 +35849,24 @@ var _user$project$Logs_View$logsView = function (logs) {
_1: {ctor: '[]'} _1: {ctor: '[]'}
}); });
}; };
var _user$project$Logs_View$logsActions = function (logs) {
return A2(
_elm_lang$html$Html$button,
{
ctor: '::',
_0: _elm_lang$html$Html_Events$onClick(
_user$project$Logs_Types$ShareLogs(logs.currentMachine)),
_1: {ctor: '[]'}
},
{
ctor: '::',
_0: _elm_lang$html$Html$text('Share log snapshot'),
_1: {ctor: '[]'}
});
};
var _user$project$Logs_View$logs = function (model) { var _user$project$Logs_View$logs = function (model) {
var _p1 = model.logs; var _p2 = model.logs;
switch (_p1.ctor) { switch (_p2.ctor) {
case 'NotAsked': case 'NotAsked':
return A2( return A2(
_elm_lang$html$Html$div, _elm_lang$html$Html$div,
@ -35785,17 +35888,22 @@ var _user$project$Logs_View$logs = function (model) {
{ {
ctor: '::', ctor: '::',
_0: _elm_lang$html$Html$text( _0: _elm_lang$html$Html$text(
_elm_lang$core$Basics$toString(_p1._0)), _elm_lang$core$Basics$toString(_p2._0)),
_1: {ctor: '[]'} _1: {ctor: '[]'}
}); });
default: default:
var _p3 = _p2._0;
return A2( return A2(
_elm_lang$html$Html$div, _elm_lang$html$Html$div,
{ctor: '[]'}, {ctor: '[]'},
{ {
ctor: '::', ctor: '::',
_0: _user$project$Logs_View$logsView(_p1._0), _0: _user$project$Logs_View$logsActions(_p3),
_1: {
ctor: '::',
_0: _user$project$Logs_View$logsView(_p3),
_1: {ctor: '[]'} _1: {ctor: '[]'}
}
}); });
} }
}; };
@ -35892,8 +36000,8 @@ var _user$project$Logs_View$machinesView = function (machines) {
}); });
}; };
var _user$project$Logs_View$machines = function (model) { var _user$project$Logs_View$machines = function (model) {
var _p2 = model.machines; var _p4 = model.machines;
switch (_p2.ctor) { switch (_p4.ctor) {
case 'NotAsked': case 'NotAsked':
return A2( return A2(
_elm_lang$html$Html$div, _elm_lang$html$Html$div,
@ -35915,7 +36023,7 @@ var _user$project$Logs_View$machines = function (model) {
{ {
ctor: '::', ctor: '::',
_0: _elm_lang$html$Html$text( _0: _elm_lang$html$Html$text(
_elm_lang$core$Basics$toString(_p2._0)), _elm_lang$core$Basics$toString(_p4._0)),
_1: {ctor: '[]'} _1: {ctor: '[]'}
}); });
default: default:
@ -35924,7 +36032,7 @@ var _user$project$Logs_View$machines = function (model) {
{ctor: '[]'}, {ctor: '[]'},
{ {
ctor: '::', ctor: '::',
_0: _user$project$Logs_View$machinesView(_p2._0), _0: _user$project$Logs_View$machinesView(_p4._0),
_1: {ctor: '[]'} _1: {ctor: '[]'}
}); });
} }
@ -36011,11 +36119,15 @@ var _user$project$Logs_View$view = function (model) {
_0: _elm_lang$html$Html$text('Logs'), _0: _elm_lang$html$Html$text('Logs'),
_1: {ctor: '[]'} _1: {ctor: '[]'}
}), }),
_1: {
ctor: '::',
_0: _user$project$Logs_View$latestLogSnapshot(model),
_1: { _1: {
ctor: '::', ctor: '::',
_0: _user$project$Logs_View$logs(model), _0: _user$project$Logs_View$logs(model),
_1: {ctor: '[]'} _1: {ctor: '[]'}
} }
}
}), }),
_1: {ctor: '[]'} _1: {ctor: '[]'}
} }