diff --git a/swarmlab-app/src/run/app.js b/swarmlab-app/src/run/app.js index f8aba302..4bea50b6 100644 --- a/swarmlab-app/src/run/app.js +++ b/swarmlab-app/src/run/app.js @@ -4,7 +4,6 @@ var pathmodule = require("path"); var app = require("express")(); var http = require("http").Server(app); var https = require("https"); -const mongoose = require("mongoose"); var CONFIG = require(pathmodule.resolve(__dirname, "runconfig.js")); const io = require("socket.io")(http, { // pingTimeout: 30000, @@ -102,66 +101,70 @@ app.get("/length", cors(corsOptions), (req, res) => { var url = "mongodb://mongo:27017/"; - MongoClient.connect(url, function (err, db) { - if (err) throw err; - var dbo = db.db("fluentdb"); - dbo - .collection("test") - .find({}) - .toArray(function (err, result) { - if (err) throw err; - // EPIDI EXW NESTED JSON PREPEI NA TO KANW PARSE DUO FORES - var obj = JSON.parse(JSON.stringify(result)); - var appout = 0; - var apperror = 0; - var mongol = 0; - var mongolerror = 0; - var redis = 0; - var dumout = 0; - var dumerr = 0; - obj.forEach((value) => { - if (value.container_name == "/mongo") { - var tmp = JSON.parse(value.log); - if (tmp.s == "I") mongol++; - // count every informative log as an output log - else if (tmp.s == "W" || tmp.s == "E") mongolerror++; //count every warning and error log - } else if ( - value.container_name == "/readmongo_service" && - value.log.includes("app_name") - ) { - var test = JSON.parse(value.log); - var type = test.type; - if (type == "err") apperror++; - else appout++; - } else if (value.container_name == "/redisserver") { - redis++; - } else if ( - value.container_name == "/dummy_service" && - value.log.includes("app_name") - ) { - var test = JSON.parse(value.log); - var type = test.type; - if (type == "err") dumerr++; - else dumout++; - } + MongoClient.connect( + url, + { useNewUrlParser: true, useUnifiedTopology: true }, + function (err, db) { + if (err) throw err; + var dbo = db.db("fluentdb"); + dbo + .collection("test") + .find({}) + .toArray(function (err, result) { + if (err) throw err; + // EPIDI EXW NESTED JSON PREPEI NA TO KANW PARSE DUO FORES + var obj = JSON.parse(JSON.stringify(result)); + var appout = 0; + var apperror = 0; + var mongol = 0; + var mongolerror = 0; + var redis = 0; + var dumout = 0; + var dumerr = 0; + obj.forEach((value) => { + if (value.container_name == "/mongo") { + var tmp = JSON.parse(value.log); + if (tmp.s == "I") mongol++; + // count every informative log as an output log + else if (tmp.s == "W" || tmp.s == "E") mongolerror++; //count every warning and error log + } else if ( + value.container_name == "/readmongo_service" && + value.log.includes("app_name") + ) { + var test = JSON.parse(value.log); + var type = test.type; + if (type == "err") apperror++; + else appout++; + } else if (value.container_name == "/redisserver") { + redis++; + } else if ( + value.container_name == "/dummy_service" && + value.log.includes("app_name") + ) { + var test = JSON.parse(value.log); + var type = test.type; + if (type == "err") dumerr++; + else dumout++; + } + }); + var RES = new Object(); + var data = { + mongolength: mongol, + mongolerror: mongolerror, + appout: appout, + apperror: apperror, + redis: redis, + dumout: dumout, + dumerr: dumerr, + }; + RES.data = data; + + // console.log("Sending Data: " + jsonfinal); + res.json(RES.data); + db.close(); }); - var RES = new Object(); - var data = { - mongolength: mongol, - mongolerror: mongolerror, - appout: appout, - apperror: apperror, - redis: redis, - dumout: dumout, - dumerr: dumerr, - }; - RES.data = data; - - // console.log("Sending Data: " + jsonfinal); - res.json(RES.data); - db.close(); - }); - }); + } + ); }); // Lefos - mongo test read @@ -170,39 +173,43 @@ app.get("/test", cors(corsOptions), (req, res) => { var url = "mongodb://mongo:27017/"; - MongoClient.connect(url, function (err, db) { - if (err) throw err; - var dbo = db.db("fluentdb"); - dbo - .collection("test") - .find({}) - .toArray(function (err, result) { - if (err) throw err; - // EPIDI EXW NESTED JSON PREPEI NA TO KANW PARSE DUO FORES - var obj = JSON.parse(JSON.stringify(result)); - var jsonfinal = []; - var mongo = []; - var app = []; - obj.forEach((value) => { - if (value.container_name == "/redisserver") { - // jsonfinal.push(JSON.parse(value.log)); - jsonfinal.push(value); - } else if (value.container_name == "/mongo") { - mongo.push(JSON.parse(value.log)); - } else if (value.log.includes("app_name")) { - app.push(JSON.parse(value.log)); - } + MongoClient.connect( + url, + { useNewUrlParser: true, useUnifiedTopology: true }, + function (err, db) { + if (err) throw err; + var dbo = db.db("fluentdb"); + dbo + .collection("test") + .find({}) + .toArray(function (err, result) { + if (err) throw err; + // EPIDI EXW NESTED JSON PREPEI NA TO KANW PARSE DUO FORES + var obj = JSON.parse(JSON.stringify(result)); + var jsonfinal = []; + var mongo = []; + var app = []; + obj.forEach((value) => { + if (value.container_name == "/redisserver") { + // jsonfinal.push(JSON.parse(value.log)); + jsonfinal.push(value); + } else if (value.container_name == "/mongo") { + mongo.push(JSON.parse(value.log)); + } else if (value.log.includes("app_name")) { + app.push(JSON.parse(value.log)); + } + }); + var RES = new Object(); + RES.data = { + redis: jsonfinal, + mongo: mongo, + app: app, + }; + res.json(RES.data); + db.close(); }); - var RES = new Object(); - RES.data = { - redis: jsonfinal, - mongo: mongo, - app: app, - }; - res.json(RES.data); - db.close(); - }); - }); + } + ); }); app.get("/test2", cors(corsOptions), (req, res) => { @@ -219,112 +226,115 @@ app.get("/test2", cors(corsOptions), (req, res) => { var url = "mongodb://mongo:27017/"; var jsonfinal = []; - MongoClient.connect(url, function (err, db) { - if (err) throw err; - var dbo = db.db("fluentdb"); - dbo - .collection("test") - .find({}) - .toArray(function (err, result) { - if (err) throw err; - // EPIDI EXW NESTED JSON PREPEI NA TO KANW PARSE DUO FORES - var obj = JSON.parse(JSON.stringify(result)); - - obj.forEach((value) => { - if (value.log.includes("app_name")) { - var tmp = JSON.parse(value.log); - if (tmp.type == "out") { - tmp.type = '
' + tmp.type + "
"; - } else if (tmp.type == "err") { - tmp.type = '
' + tmp.type + "
"; - } - jsonfinal.push(tmp); - } else if (value.container_name == "/redisserver") { - var tmplog = value; - //timestamp - var time = tmplog.time; - var tmp = { - message: tmplog.log, - timestamp: time, - type: "
out
", - process_id: "Unknown", - app_name: "redis", - }; - jsonfinal.push(tmp); - } - // else if (value.container_name == "/mongo") { - // var tmplog = JSON.parse(value.log); - // var msg2 = tmplog.msg; - - // //timestam - // var time = tmplog.t; - // time = time.$date; - // var tmp = { - // message: msg2, - // timestamp: time, - // type: "
out
", - // process_id: "Unknown", - // app_name: "Mongo", - // }; - // jsonfinal.push(tmp); - // } - }); - //console.log("Sending Data: " + jsonfinal); - if (sortname == "type") { - jsonfinal.sort(function (a, b) { - if (sortorder == "asc") { - return a.type.localeCompare(b.type); - } else { - return b.type.localeCompare(a.type); - } - }); - } else if (sortname == "message") { - jsonfinal.sort(function (a, b) { - if (sortorder == "asc") { - return a.message.localeCompare(b.message); - } else { - return b.message.localeCompare(a.message); - } - }); - } else if (sortname == "time") { - jsonfinal.sort(function (a, b) { - if (sortorder == "asc") { - return a.timestamp.localeCompare(b.timestamp); - } else { - return b.timestamp.localeCompare(a.timestamp); - } - }); - } else if (sortname == "app_name") { - jsonfinal.sort(function (a, b) { - if (sortorder == "asc") { - return a.app_name.localeCompare(b.app_name); - } else { - return b.app_name.localeCompare(a.app_name); + MongoClient.connect( + url, + { useNewUrlParser: true, useUnifiedTopology: true }, + function (err, db) { + if (err) throw err; + var dbo = db.db("fluentdb"); + dbo + .collection("test") + .find({}) + .toArray(function (err, result) { + if (err) throw err; + // EPIDI EXW NESTED JSON PREPEI NA TO KANW PARSE DUO FORES + var obj = JSON.parse(JSON.stringify(result)); + + obj.forEach((value) => { + if (value.log.includes("app_name")) { + var tmp = JSON.parse(value.log); + if (tmp.type == "out") { + tmp.type = '
' + tmp.type + "
"; + } else if (tmp.type == "err") { + tmp.type = '
' + tmp.type + "
"; + } + jsonfinal.push(tmp); + } else if (value.container_name == "/redisserver") { + var tmplog = value; + //timestamp + var time = tmplog.time; + var tmp = { + message: tmplog.log, + timestamp: time, + type: "
out
", + process_id: "Unknown", + app_name: "redis", + }; + jsonfinal.push(tmp); } + // else if (value.container_name == "/mongo") { + // var tmplog = JSON.parse(value.log); + // var msg2 = tmplog.msg; + + // //timestam + // var time = tmplog.t; + // time = time.$date; + // var tmp = { + // message: msg2, + // timestamp: time, + // type: "
out
", + // process_id: "Unknown", + // app_name: "Mongo", + // }; + // jsonfinal.push(tmp); + // } }); - } - //Pagination - var total = jsonfinal.length; - var perpage = per_page; - var lastpage = total / perpage; - if (lastpage <= 1) { - lastpage = 1; - } else { - lastpage++; - } - lastpage = Math.trunc(lastpage); - var next = page + 1; - if (next >= lastpage) { - next = lastpage; - } - var prev = page - 1; - if (prev == 0) { - prev = 1; - } - var from = (page - 1) * perpage + 1; - var to = perpage * page; - var mypage = new Object(); - var links = ` + //console.log("Sending Data: " + jsonfinal); + if (sortname == "type") { + jsonfinal.sort(function (a, b) { + if (sortorder == "asc") { + return a.type.localeCompare(b.type); + } else { + return b.type.localeCompare(a.type); + } + }); + } else if (sortname == "message") { + jsonfinal.sort(function (a, b) { + if (sortorder == "asc") { + return a.message.localeCompare(b.message); + } else { + return b.message.localeCompare(a.message); + } + }); + } else if (sortname == "time") { + jsonfinal.sort(function (a, b) { + if (sortorder == "asc") { + return a.timestamp.localeCompare(b.timestamp); + } else { + return b.timestamp.localeCompare(a.timestamp); + } + }); + } else if (sortname == "app_name") { + jsonfinal.sort(function (a, b) { + if (sortorder == "asc") { + return a.app_name.localeCompare(b.app_name); + } else { + return b.app_name.localeCompare(a.app_name); + } + }); + } + //Pagination + var total = jsonfinal.length; + var perpage = per_page; + var lastpage = total / perpage; + if (lastpage <= 1) { + lastpage = 1; + } else { + lastpage++; + } + lastpage = Math.trunc(lastpage); + var next = page + 1; + if (next >= lastpage) { + next = lastpage; + } + var prev = page - 1; + if (prev == 0) { + prev = 1; + } + var from = (page - 1) * perpage + 1; + var to = perpage * page; + var mypage = new Object(); + var links = ` { "pagination": { "total": ${total}, @@ -340,21 +350,22 @@ app.get("/test2", cors(corsOptions), (req, res) => { } } `; - mypage.links = JSON.parse(links); - from--; - mypage.data = jsonfinal.slice(from, to); - var RES = new Object(); - RES.code = req.query["action"]; - RES.token = req.query["token"]; - RES.error = false; - RES.error_msg = "ok"; - RES.data = mypage; - - // console.log("Sending Data: " + jsonfinal); - res.json(RES.data); - db.close(); - }); - }); + mypage.links = JSON.parse(links); + from--; + mypage.data = jsonfinal.slice(from, to); + var RES = new Object(); + RES.code = req.query["action"]; + RES.token = req.query["token"]; + RES.error = false; + RES.error_msg = "ok"; + RES.data = mypage; + + // console.log("Sending Data: " + jsonfinal); + res.json(RES.data); + db.close(); + }); + } + ); }); console.log("reading from db...."); @@ -757,11 +768,15 @@ async function checkstream(data) { console.log("Creating Stream...."); var url = "mongodb://mongo:27017/"; - MongoClient.connect(url, function (err, db) { - if (err) throw err; - var dbo = db.db("fluentdb"); - dbo.collection("test", onCollectionNew.bind(data)); - }); + MongoClient.connect( + url, + { useNewUrlParser: true, useUnifiedTopology: true }, + function (err, db) { + if (err) throw err; + var dbo = db.db("fluentdb"); + dbo.collection("test", onCollectionNew.bind(data)); + } + ); } } // ***************************************************