From e69e3e0789f657a811b0aea54c965ada24a55500 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 30 Aug 2023 12:30:04 +0000 Subject: [PATCH] work on history --- api.ts | 27 +++++++++++------------ sql/dev.sql | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ sql/hist.sql | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 13 deletions(-) create mode 100644 sql/dev.sql create mode 100644 sql/hist.sql diff --git a/api.ts b/api.ts index 7cf4767..6860cf5 100644 --- a/api.ts +++ b/api.ts @@ -15,9 +15,6 @@ const password = env["PASSWORD"]; const database = env["DATABASE"]; const app_port = env["APP_PORT"]; -console.log(password); -// "Geheimnis" -//--------------------------------- // Configure database connection const client = new Client({ @@ -30,16 +27,20 @@ const client = new Client({ await client.connect(); -// Define a route to retrieve values from the database -router.get('/', async (ctx) => { - ctx.response.body = "live"; -}); -// Define a route to retrieve values from the database -router.get('/api/data', async (ctx) => { - const result = await client.queryObject("SELECT * FROM rlarp.pl LIMIT 10"); - console.log(result.rows); // [{id: 1, name: 'Carlos'}, {id: 2, name: 'Johnru'}, ...] - //const result = await client.query('SELECT 1'); - ctx.response.body = result.rows; +// Load SQL from file +const query = await Deno.readTextFile("sql/hist.sql"); + +// Define a route to retrieve values from the database using parameters +router.get('/price_info/part_cust/:partcode/:customer', async (ctx) => { + + const partcode = ctx.params.partcode; // Extract the partcode parameter from the route + const customer = ctx.params.customer; // Extract the customer parameter from the route + + //console.log(partcode) + //console.log(customer) + + const result = await client.queryObject({args: [partcode, customer], text: query} ); + }); app.use(router.routes()); diff --git a/sql/dev.sql b/sql/dev.sql new file mode 100644 index 0000000..faa025d --- /dev/null +++ b/sql/dev.sql @@ -0,0 +1,60 @@ +------------real history on target scenario------------ +WITH +partcodes AS ( + SELECT + item + ,'v1:' || COALESCE(i.coltier,'') || '.' || COALESCE(substring(i.branding,1,1),'') || '.' || coalesce(i.uomp,'') || '.' || coalesce(i.suffix,'') || '.' || coalesce(i.accs_ps,'') v1dataseg + ,_ds.dataseg v0dataseg + ,part_group + FROM + "CMS.CUSLG".itemm i + LEFT OUTER JOIN rlarp.molds m ON + m.stlc = i.stlc + LEFT OUTER JOIN _ds ON + _ds.colgrp = i.colgrp + AND _ds.brand = i.branding + WHERE + i.item ~ 'ULH12000' + AND part_group IS NOT NULL +) +,customers AS ( + SELECT + dba + FROM + rlarp.cust c + WHERE + c.dba ~ 'SUNSHINE GREENH' +) +,allscenarios AS ( + SELECT DISTINCT + jsonb_build_object('baseitem',part_group,'customer',dba) sc + FROM + partcodes + CROSS JOIN customers +) +,v1scenarios AS ( + SELECT DISTINCT + jsonb_build_object('baseitem',part_group,'customer',dba,'v1dataseg',v1dataseg) sc + FROM + partcodes + CROSS JOIN customers +) +,v0scenarios AS ( + SELECT DISTINCT + jsonb_build_object('baseitem',part_group,'customer',dba,'v0dataseg',v0dataseg) sc + FROM + partcodes + CROSS JOIN customers +) +,hist AS ( + SELECT + s.sc + ,p.gset + ,(SELECT string_agg(substring(ok,1,2),'') FROM (SELECT * FROM jsonb_object_keys(p.gset) jok(ok)) x ) + ,p.season + FROM + allscenarios s + LEFT OUTER JOIN rlarp.price_pool_r1 p ON + p.gset @> s.sc +) +SELECT * FROM hist diff --git a/sql/hist.sql b/sql/hist.sql new file mode 100644 index 0000000..65069f4 --- /dev/null +++ b/sql/hist.sql @@ -0,0 +1,60 @@ +------------real history on target scenario------------ +WITH +partcodes AS ( + SELECT + item + ,'v1:' || COALESCE(i.coltier,'') || '.' || COALESCE(substring(i.branding,1,1),'') || '.' || coalesce(i.uomp,'') || '.' || coalesce(i.suffix,'') || '.' || coalesce(i.accs_ps,'') v1dataseg + ,_ds.dataseg v0dataseg + ,part_group + FROM + "CMS.CUSLG".itemm i + LEFT OUTER JOIN rlarp.molds m ON + m.stlc = i.stlc + LEFT OUTER JOIN _ds ON + _ds.colgrp = i.colgrp + AND _ds.brand = i.branding + WHERE + i.item ~ $1 + AND part_group IS NOT NULL +) +,customers AS ( + SELECT + dba + FROM + rlarp.cust c + WHERE + c.dba ~ $2 +) +,allscenarios AS ( + SELECT DISTINCT + jsonb_build_object('baseitem',part_group,'customer',dba) sc + FROM + partcodes + CROSS JOIN customers +) +,v1scenarios AS ( + SELECT DISTINCT + jsonb_build_object('baseitem',part_group,'customer',dba,'v1dataseg',v1dataseg) sc + FROM + partcodes + CROSS JOIN customers +) +,v0scenarios AS ( + SELECT DISTINCT + jsonb_build_object('baseitem',part_group,'customer',dba,'v0dataseg',v0dataseg) sc + FROM + partcodes + CROSS JOIN customers +) +,hist AS ( + SELECT + s.sc + ,p.gset + ,(SELECT string_agg(substring(ok,1,2),'') FROM (SELECT * FROM jsonb_object_keys(p.gset) jok(ok)) x ) agglevel + ,p.season + FROM + allscenarios s + LEFT OUTER JOIN rlarp.price_pool_r1 p ON + p.gset @> s.sc +) +SELECT * FROM hist