work on history

This commit is contained in:
Paul Trowbridge 2023-08-30 12:30:04 +00:00
parent 35faea37cc
commit e69e3e0789
3 changed files with 134 additions and 13 deletions

27
api.ts
View File

@ -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());

60
sql/dev.sql Normal file
View File

@ -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

60
sql/hist.sql Normal file
View File

@ -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