work on building scenario package
This commit is contained in:
parent
a42aefc653
commit
0520775a00
106
index.js
106
index.js
@ -4,6 +4,11 @@ var bodyParser = require('body-parser');
|
||||
const server = express();
|
||||
const pg = require('pg');
|
||||
|
||||
//---------read sql files into variables----------------
|
||||
var fs = require('fs');
|
||||
var readline = require('readline');
|
||||
//-------------------------------------------------------
|
||||
|
||||
var Postgres = new pg.Client({
|
||||
user: process.env.user,
|
||||
password: process.env.password,
|
||||
@ -15,11 +20,9 @@ var Postgres = new pg.Client({
|
||||
});
|
||||
Postgres.connect();
|
||||
|
||||
Postgres.FirstRow = function(inSQL,args, inResponse)
|
||||
{
|
||||
Postgres.query(inSQL,args, (err, res) => {
|
||||
if (err === null)
|
||||
{
|
||||
Postgres.FirstRow = function (inSQL, args, inResponse) {
|
||||
Postgres.query(inSQL, args, (err, res) => {
|
||||
if (err === null) {
|
||||
inResponse.json(res.rows[0]);
|
||||
return;
|
||||
}
|
||||
@ -27,23 +30,18 @@ Postgres.FirstRow = function(inSQL,args, inResponse)
|
||||
});
|
||||
};
|
||||
|
||||
server.get('/list_sources', function (req,res) {
|
||||
var sql = "select jsonb_agg(defn) from tps.srce";
|
||||
console.log(req.query);
|
||||
Postgres.FirstRow(sql,[],res);
|
||||
})
|
||||
|
||||
server.get('/monthly_orders', bodyParser.json(), function (req,res) {
|
||||
server.get('/scenario_totals', bodyParser.json(), function (req, res) {
|
||||
|
||||
var w = "";
|
||||
var c = 1;
|
||||
var args = [];
|
||||
|
||||
for (var i in req.body) {
|
||||
//console.log(i);
|
||||
///console.log(req.body[i]);
|
||||
if (c > 1) {
|
||||
w = w +
|
||||
`
|
||||
`
|
||||
AND `
|
||||
}
|
||||
w = w + i + " = '" + req.body[i] + "'";
|
||||
@ -57,60 +55,7 @@ server.get('/monthly_orders', bodyParser.json(), function (req,res) {
|
||||
//console.log(w);
|
||||
//console.log(args);
|
||||
w =
|
||||
`
|
||||
WITH
|
||||
ini AS (
|
||||
SELECT
|
||||
oseas
|
||||
,extract('month' from odate) monthn
|
||||
,sum(qty) qty
|
||||
,sum(sales) sales
|
||||
,sum(stdcost) stdcost
|
||||
FROM
|
||||
rlarp.osm_ppfa_varto_mv
|
||||
WHERE
|
||||
` + w +
|
||||
`
|
||||
AND VERSION = 'Actual'
|
||||
GROUP BY
|
||||
oseas
|
||||
,extract('month' from odate)
|
||||
)
|
||||
SELECT
|
||||
jsonb_agg(row_to_json(ini)::jsonb)
|
||||
FROM
|
||||
ini`
|
||||
console.log(w);
|
||||
Postgres.FirstRow(w,[],res)
|
||||
//res.json("hi")
|
||||
})
|
||||
|
||||
|
||||
server.get('/scenario_totals', bodyParser.json(), function (req,res) {
|
||||
|
||||
var w = "";
|
||||
var c = 1;
|
||||
var args = [];
|
||||
for (var i in req.body) {
|
||||
//console.log(i);
|
||||
///console.log(req.body[i]);
|
||||
if (c > 1) {
|
||||
w = w +
|
||||
`
|
||||
AND `
|
||||
}
|
||||
w = w + i + " = '" + req.body[i] + "'";
|
||||
args.push(req.body[i]);
|
||||
c = c + 1;
|
||||
};
|
||||
if (c == 1) {
|
||||
res.send("no body was sent");
|
||||
return;
|
||||
}
|
||||
//console.log(w);
|
||||
//console.log(args);
|
||||
w =
|
||||
`
|
||||
WITH
|
||||
ini AS (
|
||||
SELECT
|
||||
@ -134,16 +79,16 @@ server.get('/scenario_totals', bodyParser.json(), function (req,res) {
|
||||
FROM
|
||||
ini`
|
||||
console.log(w);
|
||||
Postgres.FirstRow(w,[],res)
|
||||
Postgres.FirstRow(w, [], res)
|
||||
//res.json("hi")
|
||||
})
|
||||
|
||||
server.get('/get_pool', bodyParser.json(), function (req,res) {
|
||||
server.get('/get_pool', bodyParser.json(), function (req, res) {
|
||||
|
||||
var args = [req.body.quota_rep];
|
||||
//------------------------------------------set base SQL------------------------------------
|
||||
var w =
|
||||
`
|
||||
`
|
||||
WITH rows AS (
|
||||
SELECT
|
||||
---------customer info-----------------
|
||||
@ -226,10 +171,27 @@ server.get('/get_pool', bodyParser.json(), function (req,res) {
|
||||
rows`;
|
||||
//-----------------replace default quota_rep----------------------------------------------------
|
||||
console.log(w);
|
||||
Postgres.FirstRow(w,args,res)
|
||||
Postgres.FirstRow(w, args, res)
|
||||
//res.json("hi")
|
||||
})
|
||||
|
||||
server.get('/', (req,res) => res.send('node.js express is running: 🎉'))
|
||||
server.get('/', (req, res) => res.send('node.js express is running: 🎉'))
|
||||
|
||||
server.listen(3000,() => console.log('started'))
|
||||
|
||||
server.get('/test_sql', function(req, res){
|
||||
var path = './route_meta/scenario_totals.sql'
|
||||
var callback = function(arg){
|
||||
res.send(arg)
|
||||
};
|
||||
|
||||
fs.readFile(path, 'utf8', function(err, data){
|
||||
if (!err){
|
||||
callback(data);
|
||||
} else {
|
||||
callback(err);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
server.listen(3000, () => console.log('started'))
|
||||
|
@ -1,7 +1,14 @@
|
||||
--\timing
|
||||
--explain (analyze, buffers)
|
||||
WITH base AS (
|
||||
SELECT
|
||||
order_season
|
||||
,order_month
|
||||
,version
|
||||
,iter
|
||||
,part_descr
|
||||
,bill_cust_descr
|
||||
,ship_cust_descr
|
||||
,SUM(units) units
|
||||
,SUM(value_usd) value_usd
|
||||
FROM
|
||||
@ -13,5 +20,53 @@ WHERE
|
||||
AND order_month = 'May'
|
||||
GROUP BY
|
||||
order_season
|
||||
,order_month
|
||||
,version
|
||||
,iter
|
||||
,part_descr
|
||||
,bill_cust_descr
|
||||
,ship_cust_descr
|
||||
)
|
||||
,months AS (
|
||||
SELECT
|
||||
order_season
|
||||
,version
|
||||
,iter
|
||||
,order_month
|
||||
,sum(units) units
|
||||
,sum(value_usd) value_usd
|
||||
FROM
|
||||
base
|
||||
GROUP BY
|
||||
order_season
|
||||
,version
|
||||
,iter
|
||||
,order_month
|
||||
)
|
||||
,totals AS (
|
||||
SELECT
|
||||
order_season
|
||||
,version
|
||||
,iter
|
||||
,sum(units) units
|
||||
,sum(value_usd) value_usd
|
||||
FROM
|
||||
months
|
||||
GROUP BY
|
||||
order_season
|
||||
,version
|
||||
,iter
|
||||
)
|
||||
SELECT
|
||||
jsonb_build_object(
|
||||
'months'
|
||||
,(SELECT jsonb_agg(row_to_json(months)::jsonb) FROM months)
|
||||
)
|
||||
,jsonb_build_object(
|
||||
'base'
|
||||
,(SELECT jsonb_agg(row_to_json(base)::jsonb) FROM base)
|
||||
)
|
||||
,jsonb_build_object(
|
||||
'totals'
|
||||
,(SELECT jsonb_agg(row_to_json(totals)::jsonb) FROM totals)
|
||||
)
|
Loading…
Reference in New Issue
Block a user