diff --git a/baseline.sql b/baseline.sql new file mode 100644 index 0000000..4251ecd --- /dev/null +++ b/baseline.sql @@ -0,0 +1 @@ +{"num_rows":"257845"} \ No newline at end of file diff --git a/curl b/curl deleted file mode 100755 index a602616..0000000 --- a/curl +++ /dev/null @@ -1 +0,0 @@ -curl -H "Content-Type: application/json" -X GET -d@./sample_request.json https://localhost:8082/baseline --insecure > baseline.sql diff --git a/generate_sql/exctract_params.sql b/exctract_params.sql similarity index 100% rename from generate_sql/exctract_params.sql rename to exctract_params.sql diff --git a/index.js b/index.js index ede9c35..6149eff 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,7 @@ var Postgres = new pg.Client({ port: process.env.port, database: process.env.database, ssl: false, - application_name: "osm_api" + application_name: "ps_api" }); Postgres.connect(); @@ -52,7 +52,7 @@ server.get('/', (req, res) => res.send('pivotscale api is running')) server.get('/baseline', bodyParser.json(), function(req, res) { var sql = ""; - var path = './route_sql/baseline.sql'; + var path = './routes/baseline/baseline.sql'; var args = []; fs.readFile(path, 'utf8', function(err, data) { diff --git a/routes/baseline/baseline.sql b/routes/baseline/baseline.sql new file mode 100644 index 0000000..911925d --- /dev/null +++ b/routes/baseline/baseline.sql @@ -0,0 +1,363 @@ +DELETE FROM fc.live WHERE version = 'forecast_name'; +WITH +baseline AS ( +SELECT + o."ddord#" + ,o."dditm#" + ,o."fgbol#" + ,o."fgent#" + ,o."diinv#" + ,o."dilin#" + ,o.quoten + ,o.quotel + ,o.dcodat + ,o.ddqdat + ,o.dcmdat + ,o.fesdat + ,o.dhidat + ,o.fesind + ,o.dhpost + ,o.fspr + ,o.ddqtoi + ,o.ddqtsi + ,o.fgqshp + ,o.diqtsh + ,o.diext + ,o.ditdis + ,o.discj + ,o.dhincr + ,o.plnt + ,o.promo + ,o.return_reas + ,o.terms + ,o.custpo + ,o.remit_to + ,o.bill_class + ,o.bill_cust + ,o.bill_rep + ,o.bill_terr + ,o.ship_class + ,o.ship_cust + ,o.ship_rep + ,o.ship_terr + ,o.dsm + ,o.account + ,o.shipgrp + ,o.geo + ,o.chan + ,o.chansub + ,o.orig_ctry + ,o.orig_prov + ,o.orig_post + ,o.bill_ctry + ,o.bill_prov + ,o.bill_post + ,o.dest_ctry + ,o.dest_prov + ,o.dest_post + ,o.part + ,o.styc + ,o.colc + ,o.colgrp + ,o.coltier + ,o.colstat + ,o.sizc + ,o.pckg + ,o.kit + ,o.brnd + ,o.majg + ,o.ming + ,o.majs + ,o.mins + ,o.gldco + ,o.gldc + ,o.glec + ,o.harm + ,o.clss + ,o.brand + ,o.assc + ,o.ddunit + ,o.unti + ,o.lbs + ,o.plt + ,o.plcd + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.fb_qty + ,o.fb_val_loc + ,o.fb_val_loc_dis + ,o.fb_val_loc_qt + ,o.fb_val_loc_pl + ,o.fb_val_loc_tar + ,o.fb_cst_loc + ,o.fb_cst_loc_cur + ,o.fb_cst_loc_fut + ,o.calc_status + ,o.flag + ,o.odate + ,o.oseas + ,o.rdate + ,o.rseas + ,o.sdate + ,o.sseas + ,'forecast_name' "version" + ,'actuals' iter +FROM + fc.live o +WHERE + ( + --base period orders booked.... + odate BETWEEN 'app_baseline_from_date'::date AND 'app_baseline_to_date'::date + --...or any open orders currently booked before cutoff.... + OR (calc_status IN (app_openstatus_code) and odate <= 'app_openorder_cutoff'::date) + --...or anything that shipped in that period + OR (sdate 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."ddord#" + ,o."dditm#" + ,o."fgbol#" + ,o."fgent#" + ,o."diinv#" + ,o."dilin#" + ,o.quoten + ,o.quotel + ,o.dcodat + ,o.ddqdat + ,o.dcmdat + ,o.fesdat + ,o.dhidat + ,o.fesind + ,o.dhpost + ,o.fspr + ,o.ddqtoi + ,o.ddqtsi + ,o.fgqshp + ,o.diqtsh + ,o.diext + ,o.ditdis + ,o.discj + ,o.dhincr + ,o.plnt + ,o.promo + ,o.return_reas + ,o.terms + ,o.custpo + ,o.remit_to + ,o.bill_class + ,o.bill_cust + ,o.bill_rep + ,o.bill_terr + ,o.ship_class + ,o.ship_cust + ,o.ship_rep + ,o.ship_terr + ,o.dsm + ,o.account + ,o.shipgrp + ,o.geo + ,o.chan + ,o.chansub + ,o.orig_ctry + ,o.orig_prov + ,o.orig_post + ,o.bill_ctry + ,o.bill_prov + ,o.bill_post + ,o.dest_ctry + ,o.dest_prov + ,o.dest_post + ,o.part + ,o.styc + ,o.colc + ,o.colgrp + ,o.coltier + ,o.colstat + ,o.sizc + ,o.pckg + ,o.kit + ,o.brnd + ,o.majg + ,o.ming + ,o.majs + ,o.mins + ,o.gldco + ,o.gldc + ,o.glec + ,o.harm + ,o.clss + ,o.brand + ,o.assc + ,o.ddunit + ,o.unti + ,o.lbs + ,o.plt + ,o.plcd + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.fb_qty + ,o.fb_val_loc + ,o.fb_val_loc_dis + ,o.fb_val_loc_qt + ,o.fb_val_loc_pl + ,o.fb_val_loc_tar + ,o.fb_cst_loc + ,o.fb_cst_loc_cur + ,o.fb_cst_loc_fut + ,o.calc_status + ,o.flag + ,o.odate + interval '1 year' AS odate + ,odate.ssyr + ,o.rdate + interval '1 year' AS rdate + ,rdate.ssyr + ,o.sdate + interval '1 year' AS sdate + ,sdate.ssyr + ,'forecast_name' "version" + ,'plug' iter +FROM + fc.live o +LEFT OUTER JOIN fc.perd rdate ON + (o.rdate + interval '1 year' )::date <@ rdate.drange +LEFT OUTER JOIN fc.perd odate ON + (o.odate + interval '1 year' )::date <@ odate.drange +LEFT OUTER JOIN fc.perd sdate ON + (o.sdate + interval '1 year' )::date <@ sdate.drange +WHERE + odate 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 +) +,incr AS ( +SELECT + o."ddord#" + ,o."dditm#" + ,o."fgbol#" + ,o."fgent#" + ,o."diinv#" + ,o."dilin#" + ,o.quoten + ,o.quotel + ,o.dcodat + ,o.ddqdat + ,o.dcmdat + ,o.fesdat + ,o.dhidat + ,o.fesind + ,o.dhpost + ,o.fspr + ,o.ddqtoi + ,o.ddqtsi + ,o.fgqshp + ,o.diqtsh + ,o.diext + ,o.ditdis + ,o.discj + ,o.dhincr + ,o.plnt + ,o.promo + ,o.return_reas + ,o.terms + ,o.custpo + ,o.remit_to + ,o.bill_class + ,o.bill_cust + ,o.bill_rep + ,o.bill_terr + ,o.ship_class + ,o.ship_cust + ,o.ship_rep + ,o.ship_terr + ,o.dsm + ,o.account + ,o.shipgrp + ,o.geo + ,o.chan + ,o.chansub + ,o.orig_ctry + ,o.orig_prov + ,o.orig_post + ,o.bill_ctry + ,o.bill_prov + ,o.bill_post + ,o.dest_ctry + ,o.dest_prov + ,o.dest_post + ,o.part + ,o.styc + ,o.colc + ,o.colgrp + ,o.coltier + ,o.colstat + ,o.sizc + ,o.pckg + ,o.kit + ,o.brnd + ,o.majg + ,o.ming + ,o.majs + ,o.mins + ,o.gldco + ,o.gldc + ,o.glec + ,o.harm + ,o.clss + ,o.brand + ,o.assc + ,o.ddunit + ,o.unti + ,o.lbs + ,o.plt + ,o.plcd + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.fb_qty + ,o.fb_val_loc + ,o.fb_val_loc_dis + ,o.fb_val_loc_qt + ,o.fb_val_loc_pl + ,o.fb_val_loc_tar + ,o.fb_cst_loc + ,o.fb_cst_loc_cur + ,o.fb_cst_loc_fut + ,o.calc_status + ,o.flag + ,o.odate + interval '1 year' AS odate + ,odate.ssyr + ,o.rdate + interval '1 year' AS rdate + ,rdate.ssyr + ,o.sdate + interval '1 year' AS sdate + ,sdate.ssyr + ,'forecast_name' "version" + ,'baseline' iter +FROM + baseline o +LEFT OUTER JOIN fc.perd rdate ON + (o.rdate + interval '1 year' )::date <@ rdate.drange +LEFT OUTER JOIN fc.perd odate ON + (o.odate + interval '1 year' )::date <@ odate.drange +LEFT OUTER JOIN fc.perd sdate ON + (o.sdate + interval '1 year' )::date <@ sdate.drange +) +,ins AS ( +INSERT INTO + fc.live +SELECT + * +FROM + incr i +WHERE + i.odate >= 'app_first_forecast_date'::date + OR i.sdate >= 'app_first_forecast_date'::date +RETURNING * +) +SELECT COUNT(*) num_rows FROM ins diff --git a/routes/baseline/curl.sh b/routes/baseline/curl.sh new file mode 100755 index 0000000..5d9f6cf --- /dev/null +++ b/routes/baseline/curl.sh @@ -0,0 +1 @@ +curl -H "Content-Type: application/json" -X GET -d@./routes/baseline/req.json https://localhost:8082/baseline --insecure > baseline.sql diff --git a/generate_sql/gen_baseline.sql b/routes/baseline/gen_baseline.sql similarity index 100% rename from generate_sql/gen_baseline.sql rename to routes/baseline/gen_baseline.sql diff --git a/generate_sql/generate_route_sql.sh b/routes/baseline/generate_route_sql.sh similarity index 74% rename from generate_sql/generate_route_sql.sh rename to routes/baseline/generate_route_sql.sh index 7636d88..5466890 100755 --- a/generate_sql/generate_route_sql.sh +++ b/routes/baseline/generate_route_sql.sh @@ -1,4 +1,4 @@ # execure the sql for baseline which builds the sql and inserts into a table -$PGD -f generate_sql/gen_baseline.sql +$PGD -f routes/baseline/gen_baseline.sql # pull the sql out of the table and write it to route directory -$PGD -c "SELECT t FROM fc.sql WHERE cmd = 'baseline'" -t -A -o route_sql/baseline.sql +$PGD -c "SELECT t FROM fc.sql WHERE cmd = 'baseline'" -t -A -o routes/baseline/baseline.sql diff --git a/sample_request.json b/routes/baseline/req.json similarity index 100% rename from sample_request.json rename to routes/baseline/req.json