From fcc95ecb405cf4dd0e81ed246313cb7e435642a2 Mon Sep 17 00:00:00 2001 From: Trowbridge Date: Thu, 20 Feb 2020 14:26:39 -0500 Subject: [PATCH] validate forecast matches actual, build in log entries for initial build --- build/build_fcpool.sql | 12 ++++++--- build/build_forecast.sql | 5 +++- build/rep_totals.sql | 12 +++++---- build/snap_fx.sql | 4 +-- index.js | 2 +- route_sql/list_changes.sql | 51 +++++++++++++++----------------------- 6 files changed, 42 insertions(+), 44 deletions(-) diff --git a/build/build_fcpool.sql b/build/build_fcpool.sql index 1d924a9..df61601 100644 --- a/build/build_fcpool.sql +++ b/build/build_fcpool.sql @@ -50,6 +50,9 @@ repc AS ( ,SI AS ( SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> '' ) +,logload AS ( + INSERT INTO rlarp.osm_log (doc) SELECT jsonb_build_object('user',current_user,'tag','Initial Build','type','build_pool','stamp',current_timestamp,'source','top level','message','don''t undo') RETURNING * +) SELECT fspr ,plnt @@ -106,12 +109,13 @@ SELECT ,sseas ship_season ,version ,iter - ,null::int logid - ,''::text tag - ,''::text "comment" - ,''::text module + ,logload.id logid + ,logload.doc->>'tag' tag + ,logload.doc->>'message' "comment" + ,logload.doc->>'type' module FROM rlarp.osmf_dev o + CROSS JOIN logload LEFT OUTER JOIN lgdat.cust bc ON bc.bvcust = o.bill_cust LEFT OUTER JOIN lgdat.cust sc ON diff --git a/build/build_forecast.sql b/build/build_forecast.sql index 22e5f5c..688e573 100644 --- a/build/build_forecast.sql +++ b/build/build_forecast.sql @@ -144,8 +144,11 @@ gld AS ( greatest(least(o.sdate,gld.edat),gld.sdat) BETWEEN ss.sdat AND ss.edat WHERE ( - o.odate BETWEEN '2019-06-01' AND '2019-01-31' + --base period orders booked.... + o.odate BETWEEN '2019-06-01' AND '2020-01-31' + --...or any open orders currently booked before cutoff.... OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < '2020-02-01') + --...or anything that shipped in that period OR o.fspr BETWEEN '2001' AND '2008' ) AND fs_line = '41010' diff --git a/build/rep_totals.sql b/build/rep_totals.sql index 7e7defd..c822ed6 100644 --- a/build/rep_totals.sql +++ b/build/rep_totals.sql @@ -9,6 +9,7 @@ SELECT ,order_month ,doc->>'stamp' stamp ,logid + ,tag ,sum(value_usd) FILTER (WHERE iter = 'copy') sales ,sum(cost_usd) FILTER (WHERE iter = 'copy') scogs ,sum(value_usd) FILTER (WHERE iter <> 'copy') sales_adj @@ -28,17 +29,18 @@ GROUP BY ,order_month ,doc->>'stamp' ,logid + ,tag ) SELECT director ,quota_rep_descr ,to_char(sum(sales),'999,999,999') sales ,to_char(sum(sales - scogs),'999,999,999') margin - ,to_char(sum(sales_adj),'999,999,999') sales_adj - ,to_char(sum(sales_adj - scogs_adj),'999,999,999') margin_adj - ,count(DISTINCT logid) "# of changes" - ,COUNT(logid) "# of rows added" - ,(max(stamp))::timestamptz "last edit" + ,to_char(sum(sales_adj) FILTER (WHERE tag <> 'Initial Build'),'999,999,999') sales_adj + ,to_char(sum(sales_adj - scogs_adj) FILTER (WHERE tag <> 'Initial Build'),'999,999,999') margin_adj + ,count(DISTINCT logid) FILTER (WHERE tag <> 'Initial Build') "# of changes" + ,COUNT(logid) FILTER (WHERE tag <> 'Initial Build') "# of rows added" + ,(max(stamp) FILTER (WHERE tag <> 'Initial Build'))::timestamptz "last edit" FROM preagg GROUP BY diff --git a/build/snap_fx.sql b/build/snap_fx.sql index fa3e698..07ca5c1 100644 --- a/build/snap_fx.sql +++ b/build/snap_fx.sql @@ -1,7 +1,7 @@ UPDATE rlarp.osmf_dev SET - r_rate = .75 + r_rate = .7597 WHERE r_currency = 'CA'; @@ -15,7 +15,7 @@ WHERE UPDATE rlarp.osmf_dev SET - c_rate = .75 + c_rate = .7597 WHERE c_currency = 'CA'; diff --git a/index.js b/index.js index c068170..65128c8 100644 --- a/index.js +++ b/index.js @@ -158,7 +158,7 @@ server.get('/list_changes', bodyParser.json(), function(req, res) { console.log(new Date().toISOString() + "-------------------------list changes:------------------------------") console.log(req.body); //parse the where clause into the main sql statement - sql = sql.replace(new RegExp("replace_user", 'g'), JSON.stringify(req.body.quota_rep_descr)) + sql = sql.replace(new RegExp("replace_user", 'g'), req.body.quota_rep_descr) //execute the sql and send the result console.log(sql); Postgres.FirstRow(sql, [], res) diff --git a/route_sql/list_changes.sql b/route_sql/list_changes.sql index d710609..7e6e8f1 100644 --- a/route_sql/list_changes.sql +++ b/route_sql/list_changes.sql @@ -1,40 +1,29 @@ WITH -list AS materialized ( +agg as ( SELECT - l.doc->>'user' "user", - jsonb_path_query_first(l.doc,'$.scenario.quota_rep_descr')->>0 quota_rep_descr, - to_char((l.doc->>'stamp')::timestamptz,'YYYY-MM-DD HH:MI:SS') "stamp", - l.doc->>'tag' "tag", - l.doc->>'message' "comment", - jsonb_pretty(l.doc) def, - l.id -FROM - rlarp.osm_log l -WHERE - l.doc @? '$.scenario.quota_rep_descr ? (@ == replace_user)' -) -,agg as ( -SELECT - list.user - ,list.quota_rep_descr - ,list.stamp - ,list.tag - ,list.comment - ,list.def - ,list.id + ol.doc->>'user' "user" + ,quota_rep_descr + ,ol.doc->>'stamp' stamp + ,ol.doc->>'tag' tag + ,ol.doc->>'message' "comment" + ,ol.doc + ,ol.id ,to_char(round(sum(value_usd),2),'$999,999,999D00') sales from - LIST + rlarp.osm_log ol inner join rlarp.osm_pool on id = logid -group BY - list.user - ,list.quota_rep_descr - ,list.stamp - ,list.tag - ,list.comment - ,list.def - ,list.id +WHERE + quota_rep_descr = 'replace_user' + AND tag <> 'Initial Build' +group BY + ol.doc->>'user' + ,quota_rep_descr + ,ol.doc->>'stamp' + ,ol.doc->>'tag' + ,ol.doc->>'comment' + ,ol.doc + ,ol.id ) SELECT jsonb_agg(row_to_json(agg)::jsonb ORDER BY agg.stamp desc) x from agg \ No newline at end of file