2020-11-11 23:39:26 -05:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
require('dotenv').config();
|
|
|
|
const express = require('express');
|
|
|
|
var https = require('https');
|
|
|
|
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------------------------------------
|
2020-11-11 23:39:26 -05:00
|
|
|
var options = {
|
|
|
|
key: fs.readFileSync(process.env.wd + 'key.pem'),
|
|
|
|
cert: fs.readFileSync(process.env.wd + 'cert.pem'),
|
|
|
|
passprase: []
|
|
|
|
};
|
|
|
|
|
2020-11-27 11:41:47 -05:00
|
|
|
//-----------------------------create server process--------------------------------
|
2020-11-11 23:39:26 -05:00
|
|
|
https.createServer(options, server).listen(process.env.nodeport, () => {
|
|
|
|
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();
|
|
|
|
|
2020-11-27 11:41:47 -05:00
|
|
|
//------------create a callable sql exec func that return first rows----------------
|
2020-11-11 23:39:26 -05:00
|
|
|
Postgres.FirstRow = function(inSQL, args, inResponse) {
|
|
|
|
Postgres.query(inSQL, args, (err, res) => {
|
|
|
|
if (err === null) {
|
2020-11-27 02:31:30 -05:00
|
|
|
inResponse.json(res[1].rows[0]);
|
2020-11-11 23:39:26 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(err.stack);
|
|
|
|
inResponse.json(err.stack);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-11-27 11:41:47 -05:00
|
|
|
//------------route to test if the process is running-------------------------------
|
|
|
|
server.get('/', (req, res) => res.send('pivotscale api is running'))
|
2020-11-11 23:39:26 -05:00
|
|
|
|
2020-11-27 11:41:47 -05:00
|
|
|
//------------build forecast baseline that is a mirror of the target period---------
|
2020-11-12 00:05:11 -05:00
|
|
|
server.get('/baseline', bodyParser.json(), function(req, res) {
|
2020-11-11 23:39:26 -05:00
|
|
|
|
|
|
|
var sql = "";
|
2020-11-27 20:40:34 -05:00
|
|
|
var path = './routes/baseline/baseline.sql';
|
2020-11-17 08:42:35 -05:00
|
|
|
var args = [];
|
|
|
|
|
2020-11-11 23:39:26 -05:00
|
|
|
fs.readFile(path, 'utf8', function(err, data) {
|
|
|
|
if (!err) {
|
|
|
|
callback(data);
|
|
|
|
} else {
|
|
|
|
console.log("fatal error pulling sql file")
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-24 01:24:06 -05:00
|
|
|
var app_baseline_from_date = req.body.app_baseline_from_date;
|
|
|
|
var app_baseline_to_date = req.body.app_baseline_to_date;
|
|
|
|
var app_first_forecast_date = req.body.app_first_forecast_date;
|
|
|
|
var app_openorder_cutoff = req.body.app_openorder_cutoff;
|
|
|
|
var app_plug_fromdate = req.body.app_plug_fromdate;
|
|
|
|
var app_plug_todate = req.body.app_plug_todate;
|
|
|
|
var app_openstatus_code = req.body.app_openstatus_code;
|
2020-11-21 01:51:11 -05:00
|
|
|
|
2020-11-11 23:39:26 -05:00
|
|
|
var callback = function(arg) {
|
|
|
|
sql = arg;
|
|
|
|
|
2020-11-17 08:42:35 -05:00
|
|
|
console.log(new Date().toISOString() + "-------------------------baseline build-----------------------------")
|
2020-11-11 23:39:26 -05:00
|
|
|
console.log(req.body);
|
|
|
|
//parse the where clause into the main sql statement
|
2020-11-12 00:05:11 -05:00
|
|
|
//sql = sql.replace(new RegExp("where_clause", 'g'), w)
|
2020-11-24 01:24:06 -05:00
|
|
|
sql = sql.replace(new RegExp("app_baseline_from_date", 'g'), app_baseline_from_date);
|
|
|
|
sql = sql.replace(new RegExp("app_baseline_to_date", 'g'), app_baseline_to_date);
|
|
|
|
sql = sql.replace(new RegExp("app_first_forecast_date", 'g'), app_first_forecast_date);
|
|
|
|
sql = sql.replace(new RegExp("app_openorder_cutoff", 'g'), app_openorder_cutoff);
|
|
|
|
sql = sql.replace(new RegExp("app_openstatus_code", 'g'), app_openstatus_code);
|
|
|
|
sql = sql.replace(new RegExp("app_plug_fromdate", 'g'), app_plug_fromdate);
|
|
|
|
sql = sql.replace(new RegExp("app_plug_todate", 'g'), app_plug_todate);
|
2020-11-11 23:39:26 -05:00
|
|
|
//execute the sql and send the result
|
2020-11-22 23:15:50 -05:00
|
|
|
args.push(req.body.app_baseline_from_date);
|
2020-11-11 23:39:26 -05:00
|
|
|
console.log(sql);
|
2020-11-27 02:24:20 -05:00
|
|
|
//res.send(sql);
|
|
|
|
Postgres.FirstRow(sql, [], res)
|
2020-11-11 23:39:26 -05:00
|
|
|
};
|
|
|
|
})
|