Compare commits

..

2 Commits

Author SHA1 Message Date
pt
1b8c101429 update history log 2020-02-14 02:24:52 -05:00
pt
6e8f026770 forgot to swap out alias 2020-02-14 00:15:11 -05:00
3 changed files with 162 additions and 148 deletions

236
index.js
View File

@ -18,8 +18,8 @@ var options = {
passprase: []
};
https.createServer(options,server).listen(process.env.nodeport, () => {
console.log('started on '+ process.env.nodeport)
https.createServer(options, server).listen(process.env.nodeport, () => {
console.log('started on ' + process.env.nodeport)
});
//server.listen(3000, () => console.log('started'))
@ -34,7 +34,7 @@ var Postgres = new pg.Client({
});
Postgres.connect();
Postgres.FirstRow = function (inSQL, args, inResponse) {
Postgres.FirstRow = function(inSQL, args, inResponse) {
Postgres.query(inSQL, args, (err, res) => {
if (err === null) {
inResponse.json(res.rows[0]);
@ -55,14 +55,14 @@ server.get('/pgbadger', (req, res) => res.sendFile(process.env.wd + 'logs.html')
server.get('/totals', (req, res) => res.sendFile(process.env.wd + 'totals.log'))
server.get('/test_sql', function(req, res){
server.get('/test_sql', function(req, res) {
var path = './route_meta/scenario_package.sql'
var callback = function(arg){
var callback = function(arg) {
res.send(arg)
};
fs.readFile(path, 'utf8', function(err, data){
if (!err){
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
callback(err);
@ -70,21 +70,21 @@ server.get('/test_sql', function(req, res){
});
});
server.get('/get_pool', bodyParser.json(), function (req, res) {
server.get('/get_pool', bodyParser.json(), function(req, res) {
var sql = "";
var args = [req.body.quota_rep];
var path = './route_sql/get_pool.sql';
var callback = function(arg){
var callback = function(arg) {
sql = arg;
console.log(new Date().toISOString() + "-------------------------get pool:----------------------------");
console.log(req.body.quota_rep);
sql = sql.replace("rep_replace",req.body.quota_rep);
Postgres.FirstRow(sql,[],res)
sql = sql.replace("rep_replace", req.body.quota_rep);
Postgres.FirstRow(sql, [], res)
};
fs.readFile(path, 'utf8', function(err, data){
if (!err){
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
@ -94,7 +94,7 @@ server.get('/get_pool', bodyParser.json(), function (req, res) {
})
server.get('/scenario_package', bodyParser.json(), function (req, res) {
server.get('/scenario_package', bodyParser.json(), function(req, res) {
var sql = "";
var w = "";
@ -103,8 +103,8 @@ server.get('/scenario_package', bodyParser.json(), function (req, res) {
var args = [];
var path = './route_sql/scenario_package.sql';
fs.readFile(path, 'utf8', function(err, data){
if (!err){
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
@ -112,7 +112,7 @@ server.get('/scenario_package', bodyParser.json(), function (req, res) {
}
});
var callback = function(arg){
var callback = function(arg) {
sql = arg;
//parse request body into a where clause
@ -126,14 +126,14 @@ server.get('/scenario_package', bodyParser.json(), function (req, res) {
console.log(new Date().toISOString() + "-------------------------get scenario:------------------------------")
console.log(req.body);
//parse the where clause into the main sql statement
sql = sql.replace(new RegExp("where_clause",'g'),w)
//execute the sql and send the result
//console.log(sql);
Postgres.FirstRow(sql,[],res)
sql = sql.replace(new RegExp("where_clause", 'g'), w)
//execute the sql and send the result
//console.log(sql);
Postgres.FirstRow(sql, [], res)
};
})
server.get('/list_changes', bodyParser.json(), function (req, res) {
server.get('/list_changes', bodyParser.json(), function(req, res) {
var sql = "";
var w = "";
@ -142,8 +142,8 @@ server.get('/list_changes', bodyParser.json(), function (req, res) {
var args = [];
var path = './route_sql/list_changes.sql';
fs.readFile(path, 'utf8', function(err, data){
if (!err){
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
@ -151,20 +151,20 @@ server.get('/list_changes', bodyParser.json(), function (req, res) {
}
});
var callback = function(arg){
var callback = function(arg) {
sql = arg;
console.log(new Date().toISOString() +"-------------------------list changes:------------------------------")
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))
//execute the sql and send the result
//console.log(sql);
Postgres.FirstRow(sql,[],res)
sql = sql.replace(new RegExp("replace_user", 'g'), JSON.stringify(req.body.quota_rep_descr))
//execute the sql and send the result
console.log(sql);
Postgres.FirstRow(sql, [], res)
};
})
server.post('/addmonth_v', bodyParser.json(), function (req, res) {
server.post('/addmonth_v', bodyParser.json(), function(req, res) {
var sql = "";
var w = "";
@ -173,8 +173,8 @@ server.post('/addmonth_v', bodyParser.json(), function (req, res) {
var args = [];
var path = './route_sql/addmonth_vd.sql';
fs.readFile(path, 'utf8', function(err, data){
if (!err){
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
@ -182,7 +182,7 @@ server.post('/addmonth_v', bodyParser.json(), function (req, res) {
}
});
var callback = function(arg){
var callback = function(arg) {
sql = arg;
//buile where clause expression
({ c, w, d } = build_where(req, c, w, d, args));
@ -191,22 +191,22 @@ server.post('/addmonth_v', bodyParser.json(), function (req, res) {
res.send("no body was sent");
return;
}
console.log(new Date().toISOString() +"-----------------------------add month volume:---------------------------------");
console.log(new Date().toISOString() + "-----------------------------add month volume:---------------------------------");
req.body.stamp = new Date().toISOString()
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("scenario = target_scenario",'g'),w);
sql = sql.replace(new RegExp("target_increment",'g'),req.body.qty);
sql = sql.replace(new RegExp("target_month",'g'),req.body.month);
sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
sql = sql.replace(new RegExp("scenario = target_scenario", 'g'), w);
sql = sql.replace(new RegExp("target_increment", 'g'), req.body.qty);
sql = sql.replace(new RegExp("target_month", 'g'), req.body.month);
sql = sql.replace(new RegExp("replace_version", 'g'), req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source);
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
console.log(sql)
Postgres.FirstRow(sql,[],res)
Postgres.FirstRow(sql, [], res)
}
})
server.post('/addmonth_vp', bodyParser.json(), function (req, res) {
server.post('/addmonth_vp', bodyParser.json(), function(req, res) {
var sql = "";
var w = "";
@ -215,7 +215,7 @@ server.post('/addmonth_vp', bodyParser.json(), function (req, res) {
var args = [];
var path = './route_sql/addmonth_vupd.sql';
var callback = function(arg){
var callback = function(arg) {
sql = arg;
({ c, w, d } = build_where(req, c, w, d, args));
@ -224,23 +224,23 @@ server.post('/addmonth_vp', bodyParser.json(), function (req, res) {
res.send("no body was sent");
return;
}
console.log(new Date().toISOString() +"------------------add month volume and price:-------------------");
console.log(new Date().toISOString() + "------------------add month volume and price:-------------------");
req.body.stamp = new Date().toISOString()
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
sql = sql.replace(new RegExp("target_volume",'g'),req.body.qty);
sql = sql.replace(new RegExp("target_price",'g'),req.body.amount);
sql = sql.replace(new RegExp("target_month",'g'),req.body.month);
sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
sql = sql.replace(new RegExp("where_clause", 'g'), w);
sql = sql.replace(new RegExp("target_volume", 'g'), req.body.qty);
sql = sql.replace(new RegExp("target_price", 'g'), req.body.amount);
sql = sql.replace(new RegExp("target_month", 'g'), req.body.month);
sql = sql.replace(new RegExp("replace_version", 'g'), req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source);
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
console.log(sql);
Postgres.FirstRow(sql,[],res)
Postgres.FirstRow(sql, [], res)
}
fs.readFile(path, 'utf8', function(err, data){
if (!err){
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
@ -249,7 +249,7 @@ server.post('/addmonth_vp', bodyParser.json(), function (req, res) {
});
})
server.post('/scale_v', bodyParser.json(), function (req, res) {
server.post('/scale_v', bodyParser.json(), function(req, res) {
var sql = "";
var w = "";
@ -258,7 +258,7 @@ server.post('/scale_v', bodyParser.json(), function (req, res) {
var args = [];
var path = './route_sql/scale_vd.sql';
var callback = function(arg){
var callback = function(arg) {
sql = arg;
({ c, w, d } = build_where(req, c, w, d, args));
@ -267,21 +267,21 @@ server.post('/scale_v', bodyParser.json(), function (req, res) {
res.send("no body was sent");
return;
}
console.log(new Date().toISOString() +"-----------------------scale volume:------------------------------");
console.log(new Date().toISOString() + "-----------------------scale volume:------------------------------");
req.body.stamp = new Date().toISOString()
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
sql = sql.replace(new RegExp("incr_qty",'g'),req.body.qty);
sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
sql = sql.replace(new RegExp("where_clause", 'g'), w);
sql = sql.replace(new RegExp("incr_qty", 'g'), req.body.qty);
sql = sql.replace(new RegExp("replace_version", 'g'), req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source);
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
console.log(sql);
Postgres.FirstRow(sql,[],res)
Postgres.FirstRow(sql, [], res)
}
fs.readFile(path, 'utf8', function(err, data){
if (!err){
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
@ -290,7 +290,7 @@ server.post('/scale_v', bodyParser.json(), function (req, res) {
});
})
server.post('/scale_p', bodyParser.json(), function (req, res) {
server.post('/scale_p', bodyParser.json(), function(req, res) {
var sql = "";
var w = "";
@ -299,7 +299,7 @@ server.post('/scale_p', bodyParser.json(), function (req, res) {
var args = [];
var path = './route_sql/scale_pd.sql';
var callback = function(arg){
var callback = function(arg) {
sql = arg;
({ c, w, d } = build_where(req, c, w, d, args));
@ -308,21 +308,21 @@ server.post('/scale_p', bodyParser.json(), function (req, res) {
res.send("no body was sent");
return;
}
console.log(new Date().toISOString() +"--------------------scale price:-------------------");
console.log(new Date().toISOString() + "--------------------scale price:-------------------");
req.body.stamp = new Date().toISOString()
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
sql = sql.replace(new RegExp("target_increment",'g'),req.body.amount);
sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
sql = sql.replace(new RegExp("where_clause", 'g'), w);
sql = sql.replace(new RegExp("target_increment", 'g'), req.body.amount);
sql = sql.replace(new RegExp("replace_version", 'g'), req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source);
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
console.log(sql);
Postgres.FirstRow(sql,[],res)
Postgres.FirstRow(sql, [], res)
}
fs.readFile(path, 'utf8', function(err, data){
if (!err){
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
@ -331,7 +331,7 @@ server.post('/scale_p', bodyParser.json(), function (req, res) {
});
})
server.post('/scale_vp', bodyParser.json(), function (req, res) {
server.post('/scale_vp', bodyParser.json(), function(req, res) {
var sql = "";
var w = "";
@ -340,7 +340,7 @@ server.post('/scale_vp', bodyParser.json(), function (req, res) {
var args = [];
var path = './route_sql/scale_vupd.sql';
var callback = function(arg){
var callback = function(arg) {
sql = arg;
({ c, w, d } = build_where(req, c, w, d, args));
@ -349,22 +349,22 @@ server.post('/scale_vp', bodyParser.json(), function (req, res) {
res.send("no body was sent");
return;
}
console.log(new Date().toISOString() +"--------------------scale volume & price:-------------------");
console.log(new Date().toISOString() + "--------------------scale volume & price:-------------------");
req.body.stamp = new Date().toISOString()
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
sql = sql.replace(new RegExp("target_vol",'g'),req.body.qty);
sql = sql.replace(new RegExp("target_prc",'g'),req.body.amount);
sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
sql = sql.replace(new RegExp("where_clause", 'g'), w);
sql = sql.replace(new RegExp("target_vol", 'g'), req.body.qty);
sql = sql.replace(new RegExp("target_prc", 'g'), req.body.amount);
sql = sql.replace(new RegExp("replace_version", 'g'), req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source);
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
console.log(sql);
Postgres.FirstRow(sql,[],res)
Postgres.FirstRow(sql, [], res)
}
fs.readFile(path, 'utf8', function(err, data){
if (!err){
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
@ -373,7 +373,7 @@ server.post('/scale_vp', bodyParser.json(), function (req, res) {
});
})
server.post('/new_part', bodyParser.json(), function (req, res) {
server.post('/new_part', bodyParser.json(), function(req, res) {
var sql = "";
var w = "";
@ -382,7 +382,7 @@ server.post('/new_part', bodyParser.json(), function (req, res) {
var args = [];
var path = './route_sql/new_part.sql';
var callback = function(arg){
var callback = function(arg) {
sql = arg;
({ c, w, d } = build_where(req, c, w, d, args));
@ -391,23 +391,23 @@ server.post('/new_part', bodyParser.json(), function (req, res) {
res.send("no body was sent");
return;
}
console.log(new Date().toISOString() +"--------------------new part:-------------------");
console.log(new Date().toISOString() + "--------------------new part:-------------------");
req.body.stamp = new Date().toISOString()
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
sql = sql.replace(new RegExp("target_vol",'g'),req.body.qty);
sql = sql.replace(new RegExp("target_prc",'g'),req.body.amount);
sql = sql.replace(new RegExp("replace_request",'g'),JSON.stringify(req.body));
sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
sql = sql.replace(new RegExp("where_clause", 'g'), w);
sql = sql.replace(new RegExp("target_vol", 'g'), req.body.qty);
sql = sql.replace(new RegExp("target_prc", 'g'), req.body.amount);
sql = sql.replace(new RegExp("replace_request", 'g'), JSON.stringify(req.body));
sql = sql.replace(new RegExp("replace_version", 'g'), req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source);
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
console.log(sql);
Postgres.FirstRow(sql,[],res)
Postgres.FirstRow(sql, [], res)
}
fs.readFile(path, 'utf8', function(err, data){
if (!err){
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
@ -416,7 +416,7 @@ server.post('/new_part', bodyParser.json(), function (req, res) {
});
})
server.post('/new_basket', bodyParser.json(), function (req, res) {
server.post('/new_basket', bodyParser.json(), function(req, res) {
var sql = "";
var w = "";
@ -425,7 +425,7 @@ server.post('/new_basket', bodyParser.json(), function (req, res) {
var args = [];
var path = './route_sql/new_basket.sql';
var callback = function(arg){
var callback = function(arg) {
sql = arg;
({ c, w, d } = build_where(req, c, w, d, args));
@ -434,23 +434,23 @@ server.post('/new_basket', bodyParser.json(), function (req, res) {
res.send("no body was sent");
return;
}
console.log(new Date().toISOString() +"--------------------new basket:-------------------");
console.log(new Date().toISOString() + "--------------------new basket:-------------------");
req.body.stamp = new Date().toISOString()
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
sql = sql.replace(new RegExp("target_vol",'g'),req.body.qty);
sql = sql.replace(new RegExp("target_prc",'g'),req.body.amount);
sql = sql.replace(new RegExp("replace_request",'g'),JSON.stringify(req.body));
sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
sql = sql.replace(new RegExp("where_clause", 'g'), w);
sql = sql.replace(new RegExp("target_vol", 'g'), req.body.qty);
sql = sql.replace(new RegExp("target_prc", 'g'), req.body.amount);
sql = sql.replace(new RegExp("replace_request", 'g'), JSON.stringify(req.body));
sql = sql.replace(new RegExp("replace_version", 'g'), req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source);
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
console.log(sql);
Postgres.FirstRow(sql,[],res)
Postgres.FirstRow(sql, [], res)
}
fs.readFile(path, 'utf8', function(err, data){
if (!err){
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
@ -480,13 +480,11 @@ function build_where(req, c, w, d, args) {
d = d + 1;
}
w = w + ")";
}
else {
} else {
w = w + i + " = '" + req.body.scenario[i] + "'";
}
args.push(req.body.scenario[i]);
c = c + 1;
}
;
};
return { c, w, d };
}

View File

@ -1,21 +1,37 @@
WITH
list AS (
list AS materialized (
SELECT
iterdef->>'user' "user",
iterdef->>'stamp' "stamp",
iterdef->>'mesasage' "comment",
round(SUM(fb_val_loc * r_rate)::numeric,2) sales,
jsonb_pretty(iterdef) def
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->>'message' "comment",
jsonb_pretty(l.doc) def,
l.id
FROM
rlarp.osm_fcpool
rlarp.osm_log l
WHERE
iterdef IS NOT NULL
AND iterdef @> $$replace_user$$::jsonb
GROUP BY
iterdef->>'user',
iterdef->>'stamp',
iterdef->>'message',
iterdef
l.doc @? '$.scenario.quota_rep_descr ? (@ == replace_user)'
)
,agg as (
SELECT
list.user
,list.quota_rep_descr
,list.stamp
,list.comment
,list.def
,list.id
,to_char(round(sum(value_usd),2),'$999,999,999D00') sales
from
LIST
inner join rlarp.osm_pool on
id = logid
group BY
list.user
,list.quota_rep_descr
,list.stamp
,list.comment
,list.def
,list.id
)
SELECT
jsonb_agg(row_to_json(list)::jsonb) x from list
jsonb_agg(row_to_json(agg)::jsonb ORDER BY agg.stamp desc) x from agg

View File

@ -254,9 +254,9 @@ SELECT
LEFT OUTER JOIN gld od ON
od.drange @> make_date(mxm.yr + 2021,mxm.cal,mxm.odom)
LEFT OUTER JOIN gld rd ON
od.drange @> (make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag)
rd.drange @> (make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag)
LEFT OUTER JOIN gld sd ON
od.drange @> (make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag + slag)
sd.drange @> (make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag + slag)
)
--SELECT * FROM basemix
,log AS (