diff --git a/index.js b/index.js index 3d11637..1f617ee 100644 --- a/index.js +++ b/index.js @@ -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]); @@ -42,7 +42,7 @@ Postgres.FirstRow = function (inSQL, args, inResponse) { } console.log(err.stack); inResponse.json(err.stack); - }); + }); }; server.get('/', (req, res) => res.send('node.js express is up and running')) @@ -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,9 +112,9 @@ 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 ({ c, w, d } = build_where(req, c, w, d, args)); @@ -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,16 +173,16 @@ 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") callback(err); } }); - - 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 = ""; @@ -214,8 +214,8 @@ server.post('/addmonth_vp', bodyParser.json(), function (req, res) { var d = 1; 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 = ""; @@ -257,8 +257,8 @@ server.post('/scale_v', bodyParser.json(), function (req, res) { var d = 1; 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 = ""; @@ -298,8 +298,8 @@ server.post('/scale_p', bodyParser.json(), function (req, res) { var d = 1; 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 = ""; @@ -339,8 +339,8 @@ server.post('/scale_vp', bodyParser.json(), function (req, res) { var d = 1; 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 = ""; @@ -381,8 +381,8 @@ server.post('/new_part', bodyParser.json(), function (req, res) { var d = 1; 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 = ""; @@ -424,8 +424,8 @@ server.post('/new_basket', bodyParser.json(), function (req, res) { var d = 1; 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 }; -} +} \ No newline at end of file diff --git a/route_sql/list_changes.sql b/route_sql/list_changes.sql index 596c056..64dfabe 100644 --- a/route_sql/list_changes.sql +++ b/route_sql/list_changes.sql @@ -1,21 +1,37 @@ -WITH -list AS ( +WITH +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 \ No newline at end of file + jsonb_agg(row_to_json(agg)::jsonb ORDER BY agg.stamp desc) x from agg \ No newline at end of file