diff --git a/index.js b/index.js index dc08116..710c64a 100644 --- a/index.js +++ b/index.js @@ -55,7 +55,7 @@ server.get('/baseline', bodyParser.json(), function(req, res) { var path = './routes/baseline/baseline.sql'; var args = []; - + var app_req = JSON.stringify(req.body); 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; @@ -71,6 +71,7 @@ server.get('/baseline', bodyParser.json(), function(req, res) { console.log(req.body); //parse the where clause into the main sql statement //sql = sql.replace(new RegExp("where_clause", 'g'), w) + sql = sql.replace(new RegExp("app_req", 'g'), app_req); 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); diff --git a/readme.md b/readme.md index a97e863..bc0f506 100644 --- a/readme.md +++ b/readme.md @@ -48,6 +48,7 @@ setup * logid (added if missing) issues + - [ ] baseline request json is not depostiing correctly into the sql - [ ] what if you create a func table but some of the values are null? (orders without quotes) - [x] what if you want certain things like invoice numbers included in the baseline, is a func table necesary? no, just leave fkey null and no table will be setup - [ ] baseline needs to populate all columns in sequential order like the scale route diff --git a/routes/baseline/baseline.sql b/routes/baseline/baseline.sql index b1b9c69..15403e1 100644 --- a/routes/baseline/baseline.sql +++ b/routes/baseline/baseline.sql @@ -1,20 +1,30 @@ DELETE FROM tpsv.dcard WHERE app_version = 'app_forecast_name'; WITH -baseline AS ( +req AS (SELECT $$app_req$$::jsonb j) +,ilog AS ( + INSERT INTO + fc.log (doc) + SELECT + req.j + FROM + req + RETURNING * +) +,baseline AS ( SELECT - o.id - ,o.logid + null::integer AS id + ,null::integer AS logid ,o."Trans. Date" ,o."Post Date" ,o."Description" - ,o."Amount" + ,sum(o."Amount") AS "Amount" ,o."Category" ,o."Party" ,o."Reason" - ,o.app_units - ,'app_forecast_name' "version" - ,'actuals' iter - ,null::bigint app_logid + ,sum(o.app_units) AS app_units + ,'app_forecast_name' AS app_version + ,'baseline' AS app_iter + ,(SELECT id FROM ilog) AS app_logid FROM tpsv.dcard o WHERE @@ -27,52 +37,72 @@ WHERE OR ("Post Date" BETWEEN 'app_baseline_from_date'::date AND 'app_baseline_to_date'::date) ) --be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships -UNION ALL -SELECT - o.id - ,o.logid - ,o."Trans. Date" + interval '1 year' AS "Trans. Date" - ,o."Post Date" + interval '1 year' AS "Post Date" +GROUP BY + o."Trans. Date" + ,o."Post Date" ,o."Description" - ,o."Amount" ,o."Category" ,o."Party" ,o."Reason" - ,o.app_units - ,'app_forecast_name' "version" - ,'plug' iter - ,null::bigint app_logid +UNION ALL +SELECT + null::integer AS id + ,null::integer AS logid + ,o."Trans. Date" + interval '1 year' AS "Trans. Date" + ,o."Post Date" + interval '1 year' AS "Post Date" + ,o."Description" + ,sum(o."Amount") AS "Amount" + ,o."Category" + ,o."Party" + ,o."Reason" + ,sum(o.app_units) AS app_units + ,'app_forecast_name' AS app_version + ,'baseline' AS app_iter + ,(SELECT id FROM ilog) AS app_logid FROM tpsv.dcard o -LEFT OUTER JOIN fc.perd pdate ON - (o."Post Date" + interval '1 year' )::date <@ pdate.drange -LEFT OUTER JOIN fc.perd tdate ON + LEFT OUTER JOIN fc.perd tdate ON (o."Trans. Date" + interval '1 year' )::date <@ tdate.drange + LEFT OUTER JOIN fc.perd pdate ON + (o."Post Date" + interval '1 year' )::date <@ pdate.drange WHERE "Trans. Date" BETWEEN 'app_plug_fromdate'::date AND 'app_plug_todate'::date --be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships -) +GROUP BY + o."Trans. Date" + ,o."Post Date" + ,o."Description" + ,o."Category" + ,o."Party" + ,o."Reason") ,incr AS ( SELECT - o.id - ,o.logid + null::integer AS id + ,null::integer AS logid ,o."Trans. Date" + interval '1 year' AS "Trans. Date" ,o."Post Date" + interval '1 year' AS "Post Date" ,o."Description" - ,o."Amount" + ,sum(o."Amount") AS "Amount" ,o."Category" ,o."Party" ,o."Reason" - ,o.app_units - ,'app_forecast_name' "version" - ,'baseline' iter - ,null::bigint app_logid + ,sum(o.app_units) AS app_units + ,'app_forecast_name' AS app_version + ,'baseline' AS app_iter + ,(SELECT id FROM ilog) AS app_logid FROM baseline o -LEFT OUTER JOIN fc.perd pdate ON - (o."Post Date" + interval '1 year' )::date <@ pdate.drange LEFT OUTER JOIN fc.perd tdate ON (o."Trans. Date" + interval '1 year' )::date <@ tdate.drange + LEFT OUTER JOIN fc.perd pdate ON + (o."Post Date" + interval '1 year' )::date <@ pdate.drange +GROUP BY + o."Trans. Date" + ,o."Post Date" + ,o."Description" + ,o."Category" + ,o."Party" + ,o."Reason" ) ,ins AS ( INSERT INTO diff --git a/routes/baseline/gen_baseline.sql b/routes/baseline/gen_baseline.sql index 0b44ad2..988097b 100644 --- a/routes/baseline/gen_baseline.sql +++ b/routes/baseline/gen_baseline.sql @@ -219,13 +219,11 @@ SELECT -- fc.live $$,incr AS ( SELECT - $$||_clist_inc|| - $$ - ,'app_forecast_name' "version" - ,'baseline' iter - ,null::bigint app_logid + $$||_clist_inc||$$ FROM baseline o$$||E'\n'||_perd_joins||$$ +GROUP BY + $$||_clist_group||$$ ) ,ins AS ( INSERT INTO @@ -251,7 +249,8 @@ RAISE NOTICE E'_baseline -------> \n%',_baseline; SELECT $$DELETE FROM $$||_target_table||$$ WHERE $$||_version_col||$$ = 'app_forecast_name'; WITH -ilog AS ( +req AS (SELECT $$||'$$app_req$$::jsonb j)'||$$ +,ilog AS ( INSERT INTO fc.log (doc) SELECT