From 8ab61b719922ec34812a75de1bce60ffa2dde8c7 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 15 Nov 2023 15:27:09 -0500 Subject: [PATCH] change structure of returned history --- apply_guidance.ts | 4 ++-- sql/get_hist.pg.sql | 40 ++++++++++++++++++--------------------- sql/get_hist_debug.pg.sql | 39 +++++++++++++++++--------------------- 3 files changed, 37 insertions(+), 46 deletions(-) diff --git a/apply_guidance.ts b/apply_guidance.ts index ccf150e..4deaae9 100644 --- a/apply_guidance.ts +++ b/apply_guidance.ts @@ -25,8 +25,8 @@ export function apply_guidance(doc: any) { const targetPrice = doc.pricing?.v1tp ?? doc.pricing?.v0tp; const priceBand = doc.pricing?.v1stdv ?? doc.pricing?.v0stdv; - const earlyPrice = doc.hist?.cust?.early_price; - const earlySeason = doc.hist?.cust?.early_season; + const earlyPrice = doc.hist?.cust?.[0]?.early_price; + const earlySeason = doc.hist?.cust?.[0]?.early_season; const bridgePremium = doc.pricing?.bridgePremium ?? 1.00000; const altHist = doc.hist?.cust?.ds; const iidx = doc.pricing?.iidx; diff --git a/sql/get_hist.pg.sql b/sql/get_hist.pg.sql index 46675ba..35dd30b 100644 --- a/sql/get_hist.pg.sql +++ b/sql/get_hist.pg.sql @@ -123,29 +123,24 @@ FROM --,flag.source --,rel.prefer --,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC) best - jsonb_strip_nulls( - jsonb_build_object( - flag.source - ,jsonb_build_object( - 'relevance' ,relevance - ,'avgunits' ,avgunits - ,'avgordcount' ,avgordcount - ,'avgcustcount' ,avgcustcount - ,'avgtargetprice' ,avgtargetprice - ,'early_season' ,early_season - ,'early_price' ,early_price - ,'recent_season' ,recent_season - ,'recent_price' ,recent_price - ,'last_season' ,last_season - ,'last_price' ,last_price - ,'ds' ,COALESCE(v1ds,v0ds) - ,'rank' ,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC) - ,'pricinghistory' ,season - ) - ) + flag.source + ,jsonb_build_object( + 'relevance' ,relevance + ,'avgunits' ,avgunits + ,'avgordcount' ,avgordcount + ,'avgcustcount' ,avgcustcount + ,'avgtargetprice' ,avgtargetprice + ,'early_season' ,early_season + ,'early_price' ,early_price + ,'recent_season' ,recent_season + ,'recent_price' ,recent_price + ,'last_season' ,last_season + ,'last_price' ,last_price + ,'ds' ,COALESCE(v1ds,v0ds) + ,'rank' ,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC) + ,'pricinghistory' ,season ) doc ,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC) rnk - ,season FROM flag LEFT OUTER JOIN rel ON @@ -154,7 +149,8 @@ FROM relevance ~ 'vol|exact' ) --SELECT jsonb_pretty(jsonb_agg(doc)) FROM rel_sort -SELECT jsonb_obj_aggc(doc) INTO _result FROM rel_sort WHERE rnk = 1; +SELECT jsonb_obj_aggc(doc) INTO _result FROM (SELECT jsonb_build_object(source, jsonb_agg(doc ORDER BY doc->>'rank' ASC)) doc FROM rel_sort GROUP BY source) x; +--SELECT jsonb_obj_aggc(doc) INTO _result FROM rel_sort WHERE rnk = 1; RETURN _result; diff --git a/sql/get_hist_debug.pg.sql b/sql/get_hist_debug.pg.sql index c0e081a..836d742 100644 --- a/sql/get_hist_debug.pg.sql +++ b/sql/get_hist_debug.pg.sql @@ -117,29 +117,24 @@ FROM --,flag.source --,rel.prefer --,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC) best - jsonb_strip_nulls( - jsonb_build_object( - flag.source - ,jsonb_build_object( - 'relevance' ,relevance - ,'avgunits' ,avgunits - ,'avgordcount' ,avgordcount - ,'avgcustcount' ,avgcustcount - ,'avgtargetprice' ,avgtargetprice - ,'early_season' ,early_season - ,'early_price' ,early_price - ,'recent_season' ,recent_season - ,'recent_price' ,recent_price - ,'last_season' ,last_season - ,'last_price' ,last_price - ,'ds' ,COALESCE(v1ds,v0ds) - ,'rank' ,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC) - ,'pricinghistory' ,season - ) - ) + flag.source + ,jsonb_build_object( + 'relevance' ,relevance + ,'avgunits' ,avgunits + ,'avgordcount' ,avgordcount + ,'avgcustcount' ,avgcustcount + ,'avgtargetprice' ,avgtargetprice + ,'early_season' ,early_season + ,'early_price' ,early_price + ,'recent_season' ,recent_season + ,'recent_price' ,recent_price + ,'last_season' ,last_season + ,'last_price' ,last_price + ,'ds' ,COALESCE(v1ds,v0ds) + ,'rank' ,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC) + ,'pricinghistory' ,season ) doc ,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC) rnk - ,season FROM flag LEFT OUTER JOIN rel ON @@ -150,4 +145,4 @@ FROM --SELECT rnk, jsonb_pretty(doc) doc, jsonb_pretty(season) season FROM rel_sort --SELECT jsonb_pretty(jsonb_agg(doc)) FROM rel_sort --SELECT jsonb_pretty(jsonb_obj_aggc(doc)) FROM rel_sort--INTO _result FROM rel_sort WHERE rnk = 1; -SELECT * FROM rel_sort; +SELECT jsonb_build_object(source, jsonb_agg(doc ORDER BY doc->>'rank' ASC)) FROM rel_sort GROUP BY source;