From 2352a898bea6ed6ad5417e2c0cdd9acf9ee28ad7 Mon Sep 17 00:00:00 2001 From: Trowbridge Date: Wed, 13 Mar 2019 13:07:07 -0400 Subject: [PATCH] update addmonth_v to manifest iteration changes --- index.js | 25 +++++++++++++++++++++++-- route_meta/adjust.json | 11 +++++------ route_sql/addmonth_vd.sql | 12 ++++-------- route_sql/scenario_package.sql | 8 ++++---- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 2b56de6..80db038 100644 --- a/index.js +++ b/index.js @@ -104,6 +104,7 @@ server.get('/scenario_package', bodyParser.json(), function (req, res) { //parse the where clause into the main sql statement sql = sql.replace(new RegExp("where_clause",'g'),w) //execute the sql and send the result + //console.log(sql); Postgres.FirstRow(sql,[],res) }; @@ -122,7 +123,8 @@ server.post('/addmonth_v', bodyParser.json(), function (req, res) { var sql = ""; var w = ""; - var c = 1; + var c = 1; //counts iterations through each scaenario key + var d = 1; //counts cycles in scenario key values which are arrays var args = []; var path = './route_sql/addmonth_vd.sql'; @@ -137,7 +139,21 @@ server.post('/addmonth_v', bodyParser.json(), function (req, res) { ` AND ` } - w = w + i + " = '" + req.body.scenario[i] + "'"; + 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] + "'"; + } args.push(req.body.scenario[i]); c = c + 1; }; @@ -151,7 +167,12 @@ server.post('/addmonth_v', bodyParser.json(), function (req, res) { 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)); //console.log(sql); + console.log(sql) Postgres.FirstRow(sql,[],res) } diff --git a/route_meta/adjust.json b/route_meta/adjust.json index 786d23f..7c24978 100644 --- a/route_meta/adjust.json +++ b/route_meta/adjust.json @@ -4,12 +4,11 @@ "ship_month": "Mar", "order_season": 2019, "order_month": "Mar", - "version": "9p3" + "version": "9p3", + "iter": [ + "forecast" + ] }, - "version": "9p3", - "iter": [ - "forecast" - ], "adj_amount": -1248994, "adj_qty": -2828623, "tar_amount": 5335749, @@ -17,5 +16,5 @@ "stamp": "2019-03-01 10:00:00", "user": "Trowbridge, Paul", "type": "scale_vp", - "source":"forecast ui" + "source": "forecast ui" } \ No newline at end of file diff --git a/route_sql/addmonth_vd.sql b/route_sql/addmonth_vd.sql index 7ff5b79..e885e3c 100644 --- a/route_sql/addmonth_vd.sql +++ b/route_sql/addmonth_vd.sql @@ -213,8 +213,6 @@ SELECT ,null::date adj_orderdate --history ,null::date adj_requestdate --history ,null::date adj_shipdate --history - ,'b20' "version" --calculated - ,'adjustment' iter --calculated ---------------ui columns------------------------- ,null::numeric order_season ,null::text order_month @@ -251,8 +249,6 @@ SELECT -----------------scenario---------------------------- scenario = target_scenario -----------------additional params------------------- - AND version = 'b20' - AND iter = 'copy' AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND adj_orderdate <= adj_shipdate GROUP BY @@ -398,11 +394,11 @@ SELECT ,make_date(mseq.yr + 2020,mseq.cal,m.odom) adj_orderdate ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag adj_requestdate ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag adj_shipdate - ,b.version --calculated - ,b.iter --calculated + ,'replace_version' "version" --calculated + ,'replace_iteration' iter --calculated -----------------------ui columns-------------------------------- - ,'adjust volume' iterdet - ,null::jsonb iterdef + ,'replace_iterdet' iterdet + ,$$replace_iterdef$$::jsonb iterdef ,od.ssyr order_season ,to_char(make_date(mseq.yr + 2020,mseq.cal,m.odom),'Mon') order_month ,sd.ssyr ship_season diff --git a/route_sql/scenario_package.sql b/route_sql/scenario_package.sql index b08b859..0d3ceac 100644 --- a/route_sql/scenario_package.sql +++ b/route_sql/scenario_package.sql @@ -71,12 +71,12 @@ GROUP BY ,seq ,SUM(units) FILTER (WHERE order_season = 2019) "2019 qty" ,SUM(units) FILTER (WHERE order_season = 2020 AND iter = 'copy') "2020 base qty" - ,SUM(units) FILTER (WHERE order_season = 2020 AND iter = 'adjustment') "2020 adj qty" - ,SUM(units) FILTER (WHERE order_season = 2020 AND iter IN ('copy','adjustment')) "2020 tot qty" + ,SUM(units) FILTER (WHERE order_season = 2020 AND iter <> 'copy') "2020 adj qty" + ,SUM(units) FILTER (WHERE order_season = 2020) "2020 tot qty" ,SUM(value_usd) FILTER (WHERE order_season = 2019) "2019 value_usd" ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter = 'copy') "2020 base value_usd" - ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter = 'adjustment') "2020 adj value_usd" - ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter IN ('copy','adjustment')) "2020 tot value_usd" + ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter <> 'copy') "2020 adj value_usd" + ,SUM(value_usd) FILTER (WHERE order_season = 2020) "2020 tot value_usd" FROM months GROUP BY