forecast_api/index.js

376 lines
11 KiB
JavaScript
Raw Normal View History

2019-01-31 16:32:34 -05:00
require('dotenv').config();
2019-01-31 14:19:42 -05:00
const express = require('express');
var bodyParser = require('body-parser');
2019-01-31 14:19:42 -05:00
const server = express();
2019-01-31 16:32:34 -05:00
const pg = require('pg');
2019-03-01 15:31:31 -05:00
//---------read sql files into variables----------------
var fs = require('fs');
var readline = require('readline');
//-------------------------------------------------------
2019-01-31 16:32:34 -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,
application_name: "osm_api"
});
Postgres.connect();
2019-03-01 15:31:31 -05:00
Postgres.FirstRow = function (inSQL, args, inResponse) {
Postgres.query(inSQL, args, (err, res) => {
if (err === null) {
2019-01-31 16:32:34 -05:00
inResponse.json(res.rows[0]);
return;
}
2019-03-12 15:31:24 -04:00
console.log(err.stack);
2019-01-31 16:32:34 -05:00
inResponse.json(err.stack);
});
};
2019-03-05 23:57:31 -05:00
server.get('/', (req, res) => res.send('node.js express is up and running'))
server.get('/test_sql', function(req, res){
2019-03-06 05:54:30 -05:00
var path = './route_meta/scenario_package.sql'
2019-03-05 23:57:31 -05:00
var callback = function(arg){
res.send(arg)
};
fs.readFile(path, 'utf8', function(err, data){
if (!err){
callback(data);
} else {
callback(err);
}
});
});
2019-03-05 23:58:32 -05:00
server.get('/get_pool', bodyParser.json(), function (req, res) {
2019-03-12 15:03:21 -04:00
var sql = "";
2019-03-05 23:58:32 -05:00
var args = [req.body.quota_rep];
2019-03-12 15:03:21 -04:00
var path = './route_sql/get_pool.sql';
var callback = function(arg){
sql = arg;
console.log("-------------------------get pool:----------------------------");
2019-03-12 15:03:21 -04:00
console.log(req.body.quota_rep);
sql = sql.replace("rep_replace",req.body.quota_rep);
Postgres.FirstRow(sql,[],res)
};
fs.readFile(path, 'utf8', function(err, data){
if (!err){
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
}
});
2019-03-05 23:58:32 -05:00
})
2019-03-01 15:37:00 -05:00
server.get('/scenario_package', bodyParser.json(), function (req, res) {
2019-03-12 15:31:24 -04:00
var sql = "";
2019-03-01 15:37:00 -05:00
var w = "";
var c = 1;
var args = [];
2019-03-12 15:31:24 -04:00
var path = './route_sql/scenario_package.sql';
2019-03-01 15:37:00 -05:00
2019-03-12 15:31:24 -04:00
var callback = function(arg){
sql = arg;
console.log("-------------------------get scenario:------------------------------")
2019-03-12 15:31:24 -04:00
console.log(req.body);
//parse request body into a where clause
for (var i in req.body) {
if (c > 1) {
w = w +
`
AND `
}
w = w + i + " = '" + req.body[i] + "'";
args.push(req.body[i]);
c = c + 1;
};
//if there was no body sent, return with nothing
if (c == 1) {
res.send("no body was sent");
return;
2019-03-01 15:37:00 -05:00
}
2019-03-12 15:31:24 -04:00
//parse the where clause into the main sql statement
sql = sql.replace(new RegExp("where_clause",'g'),w)
2019-03-12 15:31:24 -04:00
//execute the sql and send the result
//console.log(sql);
2019-03-12 15:31:24 -04:00
Postgres.FirstRow(sql,[],res)
2019-03-01 15:37:00 -05:00
};
2019-03-12 15:31:24 -04:00
fs.readFile(path, 'utf8', function(err, data){
if (!err){
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
}
});
2019-03-01 15:37:00 -05:00
})
server.post('/addmonth_v', bodyParser.json(), function (req, res) {
2019-03-06 05:54:30 -05:00
2019-03-12 17:19:19 -04:00
var sql = "";
2019-03-06 05:54:30 -05:00
var w = "";
var c = 1; //counts iterations through each scaenario key
var d = 1; //counts cycles in scenario key values which are arrays
2019-03-06 05:54:30 -05:00
var args = [];
2019-03-12 17:19:19 -04:00
var path = './route_sql/addmonth_vd.sql';
2019-03-12 17:19:19 -04:00
var callback = function(arg){
sql = arg
2019-03-06 09:53:33 -05:00
2019-03-12 17:19:19 -04:00
for (var i in req.body.scenario) {
//console.log(i);
///console.log(req.body[i]);
if (c > 1) {
w = w +
`
AND `
}
if (Array.isArray(req.body.scenario[i])){
//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 (";
for (var j in req.body.scenario[i]){
if (d>1){
w = w + ",";
}
w = w + "'" + req.body.scenario[i][j] + "'";
d = d + 1;
}
w = w + ")"
} else {
w = w + i + " = '" + req.body.scenario[i] + "'";
}
2019-03-12 17:19:19 -04:00
args.push(req.body.scenario[i]);
c = c + 1;
};
if (c == 1) {
res.send("no body was sent");
return;
2019-03-06 09:53:33 -05:00
}
console.log("-----------------------------add month volume:---------------------------------");
2019-03-12 17:19:19 -04:00
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("scenario = target_scenario",'g'),w);
sql = sql.replace(new RegExp("target_increment",'g'),req.body.amount);
sql = sql.replace(new RegExp("target_month",'g'),req.body.month);
sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
sql = sql.replace(new RegExp("replace_iteration",'g'),"tool volume");
sql = sql.replace(new RegExp("replace_iterdet",'g'),"addmonth_vd");
sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
2019-03-12 17:19:19 -04:00
//console.log(sql);
console.log(sql)
2019-03-12 17:19:19 -04:00
Postgres.FirstRow(sql,[],res)
2019-03-06 09:53:33 -05:00
}
2019-03-06 10:10:02 -05:00
2019-03-12 17:19:19 -04:00
fs.readFile(path, 'utf8', function(err, data){
if (!err){
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
2019-03-06 10:10:02 -05:00
}
2019-03-12 17:19:19 -04:00
});
2019-03-06 10:10:02 -05:00
})
server.post('/addmonth_vp', bodyParser.json(), function (req, res) {
var sql = "";
var w = "";
var c = 1;
var args = [];
var path = './route_sql/addmonth_vupd.sql';
var callback = function(arg){
sql = arg
for (var i in req.body.scenario) {
//console.log(i);
///console.log(req.body[i]);
if (c > 1) {
w = w +
`
AND `
}
w = w + i + " = '" + req.body.scenario[i] + "'";
args.push(req.body.scenario[i]);
c = c + 1;
};
if (c == 1) {
res.send("no body was sent");
return;
}
console.log("------------------add month volume and price:-------------------");
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
sql = sql.replace(new RegExp("target_volume",'g'),req.body.qty);
sql = sql.replace(new RegExp("target_price",'g'),req.body.amount);
sql = sql.replace(new RegExp("target_month",'g'),req.body.month);
//console.log(sql);
Postgres.FirstRow(sql,[],res)
}
fs.readFile(path, 'utf8', function(err, data){
if (!err){
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
}
});
})
server.post('/scale_v', bodyParser.json(), function (req, res) {
var sql = "";
var w = "";
var c = 1;
var args = [];
var path = './route_sql/scale_vd.sql';
var callback = function(arg){
sql = arg
for (var i in req.body.scenario) {
//console.log(i);
///console.log(req.body[i]);
if (c > 1) {
w = w +
`
AND `
}
w = w + i + " = '" + req.body.scenario[i] + "'";
args.push(req.body.scenario[i]);
c = c + 1;
};
if (c == 1) {
res.send("no body was sent");
return;
}
console.log("-----------------------scale volume:------------------------------");
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
sql = sql.replace(new RegExp("target_increment",'g'),req.body.amount);
//console.log(sql);
Postgres.FirstRow(sql,[],res)
}
fs.readFile(path, 'utf8', function(err, data){
if (!err){
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
}
});
})
server.post('/scale_p', bodyParser.json(), function (req, res) {
var sql = "";
var w = "";
var c = 1;
var args = [];
var path = './route_sql/scale_pd.sql';
var callback = function(arg){
sql = arg
for (var i in req.body.scenario) {
//console.log(i);
///console.log(req.body[i]);
if (c > 1) {
w = w +
`
AND `
}
w = w + i + " = '" + req.body.scenario[i] + "'";
args.push(req.body.scenario[i]);
c = c + 1;
};
if (c == 1) {
res.send("no body was sent");
return;
}
console.log("--------------------scale price:-------------------");
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
sql = sql.replace(new RegExp("target_increment",'g'),req.body.amount);
//console.log(sql);
Postgres.FirstRow(sql,[],res)
}
fs.readFile(path, 'utf8', function(err, data){
if (!err){
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
}
});
})
server.post('/scale_vp', bodyParser.json(), function (req, res) {
var sql = "";
var w = "";
var c = 1;
var args = [];
var path = './route_sql/scale_vupd.sql';
var callback = function(arg){
sql = arg
for (var i in req.body.scenario) {
//console.log(i);
///console.log(req.body[i]);
if (c > 1) {
w = w +
`
AND `
}
w = w + i + " = '" + req.body.scenario[i] + "'";
args.push(req.body.scenario[i]);
c = c + 1;
};
if (c == 1) {
res.send("no body was sent");
return;
}
console.log("--------------------scale volume & price:-------------------");
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
sql = sql.replace(new RegExp("target_vol",'g'),req.body.qty);
sql = sql.replace(new RegExp("target_prc",'g'),req.body.amount);
//console.log(sql);
Postgres.FirstRow(sql,[],res)
}
fs.readFile(path, 'utf8', function(err, data){
if (!err){
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
}
});
})
2019-03-06 10:10:02 -05:00
server.listen(3000, () => console.log('started'))