Add some highlighting to the output for increased visibility.
This commit is contained in:
parent
853196bd65
commit
42e5e939e9
32
index.js
32
index.js
@ -16,7 +16,7 @@ var options = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
https.createServer(options, server).listen(process.env.nodeport, () => {
|
https.createServer(options, server).listen(process.env.nodeport, () => {
|
||||||
console.log(`${new Date().toISOString()}: Web service started on port ${process.env.nodeport}`)
|
console.log(`${timestamp()} Web service started on port ${process.env.nodeport}`)
|
||||||
});
|
});
|
||||||
|
|
||||||
var Postgres = new pg.Client({
|
var Postgres = new pg.Client({
|
||||||
@ -31,39 +31,47 @@ var Postgres = new pg.Client({
|
|||||||
Postgres.connect();
|
Postgres.connect();
|
||||||
|
|
||||||
Postgres.FirstRow = function(sql, response) {
|
Postgres.FirstRow = function(sql, response) {
|
||||||
console.log(`${new Date().toISOString()}: Running query:\n${sql}`);
|
console.log(`${timestamp()} Running query:\n${sql}`);
|
||||||
Postgres.query(sql, [], (err, res) => {
|
Postgres.query(sql, [], (err, res) => {
|
||||||
if (err === null) {
|
if (err === null) {
|
||||||
console.log(`${new Date().toISOString()}: Reponse: ${JSON.stringify(res.rows[0]).slice(0,100)}${JSON.stringify(res.rows[0]).length<100?'':'… (truncated)'}`);
|
console.log(`${timestamp()} Reponse: ${JSON.stringify(res.rows[0]).slice(0,100)}${JSON.stringify(res.rows[0]).length<100?'':'… (truncated)'}`);
|
||||||
response.json(res.rows[0]);
|
response.json(res.rows[0]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log(`${new Date().toISOString()}: ERROR|${JSON.stringify(err.stack)}`);
|
console.log(`${timestamp()} ${error()}${JSON.stringify(err.stack)}`);
|
||||||
response.json(err.stack);
|
response.json(err.stack);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function timestamp() {
|
||||||
|
return `\x1b[33m${new Date().toISOString()}:\x1b[0m`
|
||||||
|
}
|
||||||
|
|
||||||
|
function error() {
|
||||||
|
return `\x1b[91mERROR\x1b[0m|`
|
||||||
|
}
|
||||||
|
|
||||||
function process_route(route, description, path, res, callback) {
|
function process_route(route, description, path, res, callback) {
|
||||||
console.log(`${"▼".repeat(120)}`)
|
console.log(`\x1b[96m${"▼".repeat(120)}\x1b[0m`)
|
||||||
console.log(`${new Date().toISOString()}: ${route} (${description})`)
|
console.log(`${timestamp()} ${route} (${description})`)
|
||||||
if (!path) {
|
if (!path) {
|
||||||
callback(undefined);
|
callback(undefined);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`${new Date().toISOString()}: SQL file: ${path}`)
|
console.log(`${timestamp()} SQL file: ${path}`)
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
fs.readFile(path, 'utf8', function(err, data) {
|
||||||
if (!err) {
|
if (!err) {
|
||||||
callback(data);
|
callback(data);
|
||||||
} else {
|
} else {
|
||||||
console.log(`${new Date().toISOString()}: ERROR|Could not read sql file: ${JSON.stringify(err)}`);
|
console.log(`${timestamp()} ${error()}Could not read sql file: ${JSON.stringify(err)}`);
|
||||||
res.send('Failed to read needed SQL file.');
|
res.send('Failed to read needed SQL file.');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function build_where(req, res) {
|
function build_where(req, res) {
|
||||||
console.log(`${new Date().toISOString()}: Building WHERE from ${JSON.stringify(req.body)}`);
|
console.log(`${timestamp()} Building WHERE from ${JSON.stringify(req.body)}`);
|
||||||
var where = '';
|
var where = '';
|
||||||
for (var column in req.body.scenario) {
|
for (var column in req.body.scenario) {
|
||||||
if (where) {
|
if (where) {
|
||||||
@ -80,10 +88,10 @@ function build_where(req, res) {
|
|||||||
if (req.body.stamp) {
|
if (req.body.stamp) {
|
||||||
where = `${where}\n AND order_date >= least('${req.body.stamp}'::date,'2021-06-01')`;
|
where = `${where}\n AND order_date >= least('${req.body.stamp}'::date,'2021-06-01')`;
|
||||||
}
|
}
|
||||||
console.log(`${new Date().toISOString()}: Returning ${where}`);
|
console.log(`${timestamp()} Returning ${where}`);
|
||||||
|
|
||||||
if (where == '') {
|
if (where == '') {
|
||||||
console.log(`${new Date().toISOString()}: ERROR|Unable to create a WHERE clause.`);
|
console.log(`${timestamp()} ${error()}Unable to create a WHERE clause.`);
|
||||||
|
|
||||||
res.send('No body was sent.');
|
res.send('No body was sent.');
|
||||||
}
|
}
|
||||||
@ -110,7 +118,7 @@ server.get('/get_pool', bodyParser.json(), function(req, res) {
|
|||||||
function(arg) {
|
function(arg) {
|
||||||
if (req.body.quota_rep) {
|
if (req.body.quota_rep) {
|
||||||
// ensure backward compatibility
|
// ensure backward compatibility
|
||||||
console.log(`${new Date().toISOString()}:Converting old format… ${JSON.stringify(req.body)}`);
|
console.log(`${timestamp()} Converting old format… ${JSON.stringify(req.body)}`);
|
||||||
req.body = {'scenario':{'quota_rep_descr':req.body.quota_rep}};
|
req.body = {'scenario':{'quota_rep_descr':req.body.quota_rep}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user