2020-11-11 23:39:26 -05:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
require('dotenv').config();
|
|
|
|
const express = require('express');
|
2022-05-12 00:52:00 -04:00
|
|
|
//var https = require('https');
|
2020-11-11 23:39:26 -05:00
|
|
|
var bodyParser = require('body-parser');
|
|
|
|
const server = express();
|
|
|
|
const pg = require('pg');
|
|
|
|
var fs = require('fs');
|
|
|
|
var readline = require('readline');
|
|
|
|
|
2020-11-27 11:41:47 -05:00
|
|
|
//-----------------------------point to ssl info------------------------------------
|
2022-05-12 00:52:00 -04:00
|
|
|
//var options = {
|
|
|
|
// key: fs.readFileSync(process.env.wd + 'key.pem'),
|
|
|
|
// cert: fs.readFileSync(process.env.wd + 'cert.pem'),
|
|
|
|
// passprase: []
|
|
|
|
//};
|
2020-11-11 23:39:26 -05:00
|
|
|
|
2020-11-27 11:41:47 -05:00
|
|
|
//-----------------------------create server process--------------------------------
|
2022-05-12 00:52:00 -04:00
|
|
|
//https.createServer( /*options,*/ server).listen(process.env.nodeport, () => {
|
|
|
|
server.listen(process.env.nodeport, () => {
|
2020-11-11 23:39:26 -05:00
|
|
|
console.log('started on ' + process.env.nodeport)
|
|
|
|
});
|
|
|
|
|
2020-11-27 11:41:47 -05:00
|
|
|
//-----------------------------create permanent database connetion------------------
|
2020-11-11 23:39:26 -05:00
|
|
|
var Postgres = new pg.Client({
|
|
|
|
user: process.env.user,
|
|
|
|
password: process.env.password,
|
|
|
|
host: process.env.host,
|
|
|
|
port: process.env.port,
|
|
|
|
database: process.env.database,
|
|
|
|
ssl: false,
|
2020-11-27 20:40:34 -05:00
|
|
|
application_name: "ps_api"
|
2020-11-11 23:39:26 -05:00
|
|
|
});
|
|
|
|
Postgres.connect();
|
|
|
|
|
|
|
|
|
2022-05-06 23:07:42 -04:00
|
|
|
const SQLTemplate = (inSql, inJSON) => {
|
|
|
|
for (key in inJSON) {
|
|
|
|
inSql = inSql.replace(new RegExp(key, 'g'), inJSON[key]);
|
|
|
|
}
|
|
|
|
return inSql;
|
|
|
|
};
|
2022-05-12 00:27:31 -04:00
|
|
|
const build_where = (req, c, w) => {
|
2020-12-05 00:10:47 -05:00
|
|
|
//loop through each top level item expected to be a simple key/value list reflecting the column and the target value
|
|
|
|
// "part":"XFRM500", "customer":"Sanford and Son" --> SQL --> part = 'XFRM500'
|
|
|
|
// AND customer = 'Sanford and Son'
|
2022-05-12 00:27:31 -04:00
|
|
|
var d = 1;
|
|
|
|
var args = [];
|
|
|
|
|
2020-12-05 01:19:17 -05:00
|
|
|
for (var i in req.body.app_scenario) {
|
2020-12-05 00:10:47 -05:00
|
|
|
//console.log(i);
|
|
|
|
///console.log(req.body[i]);
|
|
|
|
//this step applies the AND seperator only
|
|
|
|
if (c > 1) {
|
|
|
|
w = w +
|
|
|
|
`
|
2022-05-06 23:07:42 -04:00
|
|
|
AND `;
|
2020-12-05 00:10:47 -05:00
|
|
|
}
|
2020-12-05 01:19:17 -05:00
|
|
|
if (Array.isArray(req.body.app_scenario[i])) {
|
2020-12-05 00:10:47 -05:00
|
|
|
//if the scenario key has a value that is an array of items, push it into an `IN` statement
|
|
|
|
//iter = [stage1, stage2] --> SQL --> iter IN ('stag1', stage2')
|
|
|
|
w = w + i + " IN (";
|
2020-12-05 01:19:17 -05:00
|
|
|
for (var j in req.body.app_scenario[i]) {
|
2020-12-05 00:10:47 -05:00
|
|
|
if (d > 1) {
|
|
|
|
w = w + ",";
|
|
|
|
}
|
2020-12-05 01:19:17 -05:00
|
|
|
w = w + "'" + req.body.app_scenario[i][j] + "'";
|
2020-12-05 00:10:47 -05:00
|
|
|
d = d + 1;
|
|
|
|
}
|
|
|
|
w = w + ")";
|
|
|
|
} else {
|
2020-12-05 01:19:17 -05:00
|
|
|
w = w + i + " = '" + req.body.app_scenario[i] + "'";
|
2020-12-05 00:10:47 -05:00
|
|
|
}
|
2020-12-05 01:19:17 -05:00
|
|
|
args.push(req.body.app_scenario[i]);
|
2020-12-05 00:10:47 -05:00
|
|
|
c = c + 1;
|
|
|
|
};
|
2022-05-12 00:27:31 -04:00
|
|
|
return { c, w };
|
2020-12-05 00:10:47 -05:00
|
|
|
}
|
2022-05-06 23:07:42 -04:00
|
|
|
|
|
|
|
//------------route to test if the process is running-------------------------------
|
|
|
|
server.get('/', (req, res) => res.send('pivotscale api is running'))
|
|
|
|
|
|
|
|
//------------build forecast baseline that is a mirror of the target period---------
|
|
|
|
server.get('/baseline', bodyParser.json(), function(req, res) {
|
|
|
|
|
|
|
|
var sql = "";
|
|
|
|
var path = './routes/baseline/baseline.sql';
|
|
|
|
var args = [];
|
|
|
|
|
2022-05-12 01:33:32 -04:00
|
|
|
Postgres.query("SELECT t FROM fc.sql WHERE cmd = 'baseline'", []).then((result, error) => {
|
2022-05-06 23:07:42 -04:00
|
|
|
|
2022-05-12 01:33:32 -04:00
|
|
|
if (error) {
|
2022-05-06 23:07:42 -04:00
|
|
|
console.log("fatal error pulling sql file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-12 01:33:32 -04:00
|
|
|
sql = result.rows[0].t
|
|
|
|
|
2022-05-06 23:07:42 -04:00
|
|
|
console.log(new Date().toISOString() + "-------------------------baseline build-----------------------------")
|
|
|
|
|
2022-05-12 01:33:32 -04:00
|
|
|
sql = SQLTemplate(sql, {
|
2022-05-06 23:07:42 -04:00
|
|
|
...req.body,
|
|
|
|
app_req: JSON.stringify(req.body)
|
|
|
|
});
|
|
|
|
|
2022-05-12 01:33:32 -04:00
|
|
|
console.log(sql)
|
|
|
|
|
2022-05-06 23:07:42 -04:00
|
|
|
Postgres.query(sql, []).then((result, error) => {
|
|
|
|
res.json(result[1].rows[0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
//------------scale a selected slice by the specified amounts-----------------------
|
|
|
|
server.get('/scale', bodyParser.json(), function(req, res) {
|
|
|
|
|
|
|
|
var sql = "";
|
2022-05-12 00:27:31 -04:00
|
|
|
var where = ""; //holds the where
|
|
|
|
var check = 1; //flag if body is empty
|
2022-05-06 23:07:42 -04:00
|
|
|
var path = './routes/scale/scale.sql';
|
|
|
|
|
|
|
|
|
2022-05-12 01:33:32 -04:00
|
|
|
Postgres.query("SELECT t FROM fc.sql WHERE cmd = 'scale'", []).then((result, error) => {
|
2022-05-06 23:07:42 -04:00
|
|
|
|
2022-05-12 01:33:32 -04:00
|
|
|
if (error) {
|
2022-05-06 23:07:42 -04:00
|
|
|
console.log("fatal error pulling sql file")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-12 01:33:32 -04:00
|
|
|
sql = result.rows[0].t;
|
|
|
|
|
2022-05-12 00:27:31 -04:00
|
|
|
({ c: check, w: where } = build_where(req, check, where));
|
2022-05-06 23:07:42 -04:00
|
|
|
//if there was no body sent, return with nothing
|
|
|
|
//if (c == 1) {
|
|
|
|
// res.send("no body was sent");
|
|
|
|
// return;
|
|
|
|
//}
|
|
|
|
console.log(new Date().toISOString() + "-------------------------scale type adjustment----------------------")
|
|
|
|
|
2022-05-12 01:33:32 -04:00
|
|
|
sql = SQLTemplate(sql, {
|
2022-05-06 23:07:42 -04:00
|
|
|
app_req: JSON.stringify(req.body),
|
|
|
|
app_forecast_name: req.body.app_forecast_name,
|
|
|
|
app_where: where
|
|
|
|
});
|
|
|
|
|
|
|
|
//execute the sql and send the result
|
|
|
|
|
|
|
|
Postgres.query(sql, []).then((result, error) => {
|
|
|
|
res.json(result.rows[0]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|