From 0520775a0063b5f0e1b02d7084942dd1af178b7b Mon Sep 17 00:00:00 2001 From: Trowbridge Date: Fri, 1 Mar 2019 15:31:31 -0500 Subject: [PATCH] work on building scenario package --- index.js | 112 +++++++++++---------------------- route_meta/scenario_totals.sql | 57 ++++++++++++++++- 2 files changed, 93 insertions(+), 76 deletions(-) diff --git a/index.js b/index.js index 99d47d7..46cd573 100644 --- a/index.js +++ b/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 + - ` + w = w + + ` AND ` } w = w + i + " = '" + req.body[i] + "'"; @@ -56,61 +54,8 @@ 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 + + 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 = - ` + 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')) diff --git a/route_meta/scenario_totals.sql b/route_meta/scenario_totals.sql index b636ff4..e52bd75 100644 --- a/route_meta/scenario_totals.sql +++ b/route_meta/scenario_totals.sql @@ -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 \ No newline at end of file + ,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) + ) \ No newline at end of file