diff --git a/readmongo/swarmlab-app/src/run/app.js b/readmongo/swarmlab-app/src/run/app.js index dda2936..bece0c4 100644 --- a/readmongo/swarmlab-app/src/run/app.js +++ b/readmongo/swarmlab-app/src/run/app.js @@ -9,6 +9,8 @@ const dotenv = require("dotenv"); const check123 = dotenv.config(); const { DATABASE, COLLECTION, URL, REDIS, REDIS_PORT, PORT } = process.env; +const fs = require('fs'); + const io = require("socket.io")(http, { // pingTimeout: 30000, // allowUpgrades: false, @@ -51,6 +53,10 @@ pubClient.on("connect", function () { const MongoClient = require("mongodb").MongoClient; const { DateTime } = require("luxon"); +const loadDB = require('./db'); + + + var async = require("async"); const { check, validationResult } = require("express-validator"); const urlExistSync = require("url-exist-sync"); @@ -103,104 +109,26 @@ const corsOptions = { // ------------------------------ // Lefos - get length of logs app.get("/length", cors(corsOptions), (req, res) => { - console.error("getting length of logs"); var url = URL; - MongoClient.connect( - url, - { useNewUrlParser: true, useUnifiedTopology: true }, - function (err, db) { - if (err) throw err; - var dbo = db.db(DATABASE); - dbo - .collection(COLLECTION) - .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)); - let containers = []; - var type; - var found = 0; - obj.forEach((value) => { - //new code - if (containers.length == 0) { - type = checkService(); - if (type == "out") { + var RES = new Object(); + var containers = []; + +(async() => { + var RES = new Object() + var RESL = new Object() + const mydb = await loadDB() + RES.data = await mydb.collection(COLLECTION).find({}).toArray() + var jsonfinal = []; + RES.data.forEach((value) => { containers.push({ - name: value.container_name, + name: value, lengtho: 1, lengthe: 0, }); - } else if (type == "err") { - containers.push({ - name: value.container_name, - lengtho: 0, - lengthe: 1, - }); - } - } else { - containers.forEach((val) => { - if (val.name == value.container_name) { - found = 1; - type = checkService(); - if (type == "out") { - val.lengtho++; - } else if (type == "err") { - val.lengthe++; - } - } - }); - if (found == 0) { - type = checkService(); - if (type == "out") { - containers.push({ - name: value.container_name, - lengtho: 1, - lengthe: 0, - }); - } else if ("error") { - containers.push({ - name: value.container_name, - lengtho: 0, - lengthe: 1, - }); - } - } - found = 0; - } - - // new code end - - function checkService() { - if (value.tag.includes("mongodb")) { - var tmp = JSON.parse(value.log); - if (tmp.s == "I") return "out"; - // count every informative log as an output log - else if (tmp.s == "W" || tmp.s == "E") return "err"; //count every warning and error log - } else if (value.tag.includes("node")) { - if (IsJsonString(value.log)) var test = JSON.parse(value.log); - else { - if (value.source == "stdout") return "out"; - else return "err"; - } - - var type = test.type; - if (type == "out") return "out"; - else return "err"; - } else if (value.tag.includes("redis")) { - return "out"; - } - } - }); - var RES = new Object(); - RES.data = containers; - - // console.log("Sending Data: " + jsonfinal); - res.json(containers); - db.close(); - }); - } - ); + }); + RESL.data = containers; + res.json(RESL) +})() }); // έλεγχος αν η δομή είναι κατάλληλη για μετατροπή σε json @@ -216,74 +144,26 @@ function IsJsonString(str) { // endpoint για την αναγνώριση των υπηρεσιών στο δίκτυο app.get("/services", cors(corsOptions), (req, res) => { console.error("getting length of logs"); - - var url = URL; - - MongoClient.connect( - url, - { useNewUrlParser: true, useUnifiedTopology: true }, - function (err, db) { - if (err) throw err; - var dbo = db.db(DATABASE); - dbo - .collection(COLLECTION) - .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 services = []; - var flag = false; - obj.forEach((value) => { - if (flag == false) { - services.push(value.container_name); - flag = true; - } else { - var tmp = "none"; - var count = 0; - services.forEach((val) => { - var length = services.length; - - var found = false; - if ( - val != value.container_name && - count + 1 < length && - found == false - ) { - count++; - //services.push(value.container_name) - } else if ( - val != value.container_name && - count + 1 == length && - found == false - ) { - //found =true; - tmp = value.container_name; - } else if (val == value.container_name) { - found = true; - } - }); - - if (tmp != "none") { - services.push(tmp); - } - } - }); - var RES = new Object(); - - RES.data = services; - // console.log("Sending Data: " + jsonfinal); - res.json(RES.data); - db.close(); - }); - } - ); + +(async() => { + var RES = new Object() + const mydb = await loadDB() + RES.data = await mydb.collection(COLLECTION).find({}).toArray() + var jsonfinal = []; + RES.data.forEach((value) => { + jsonfinal.push(value.tailed_path); + }); + //var services = Object.keys(jsonfinal); + var uniq = [...new Set(jsonfinal)]; + res.json(uniq) +})() }); // Lefos - mongo test read app.get("/test", cors(corsOptions), (req, res) => { console.log("reading from db...."); + fs.writeFileSync('/tmp/test-sync', 'db', {flag:'a+'}); var url = URL; MongoClient.connect( @@ -303,20 +183,13 @@ app.get("/test", cors(corsOptions), (req, res) => { 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, + redis: '', + mongo: '', + app: jsonfinal }; res.json(RES.data); db.close(); @@ -329,34 +202,20 @@ app.get("/test", cors(corsOptions), (req, res) => { app.get("/raw", cors(corsOptions), (req, res) => { console.log("reading from db...."); - var url = URL; - - MongoClient.connect( - url, - { useNewUrlParser: true, useUnifiedTopology: true }, - function (err, db) { - if (err) throw err; - var dbo = db.db(DATABASE); - dbo - .collection(COLLECTION) - .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 = []; - obj.forEach((value) => { - jsonfinal.push(value); - }); - var RES = new Object(); - RES.data = { +// fs.writeFileSync('/tmp/test-sync', 'db1', {flag:'a+'}); +(async() => { + var RES = new Object() + const mydb = await loadDB() + RES.data = await mydb.collection(COLLECTION).find({}).toArray() + var jsonfinal = []; + RES.data.forEach((value) => { + jsonfinal.push(value.tailed_path); + }); + RES.src = { all: jsonfinal, }; - res.json(RES.data); - db.close(); - }); - } - ); + res.json(RES.src) +})() }); // βασικό endpoint που επιστρέφει κατάλληλα τα logs στην ευρετηρίαση του web-client @@ -379,162 +238,22 @@ app.get("/test2", cors(corsOptions), (req, res) => { console.error("EXTRA PARAMS: " + logtext); //console.log("TEST LOG"); - var url = URL; +(async() => { + var RES = new Object() + const mydb = await loadDB() + RES.data = await mydb.collection(COLLECTION).find({}).toArray() var jsonfinal = []; - MongoClient.connect( - url, - { useNewUrlParser: true, useUnifiedTopology: true }, - function (err, db) { - if (err) throw err; - var dbo = db.db(DATABASE); - dbo - .collection(COLLECTION) - .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) => { - var test = IsJsonString(value.log); - if (value.tag.includes("node") && IsJsonString(value.log)) { - var tmp = JSON.parse(value.log); - var container_name = value.container_name; - var tmptype; - tmp.app_name = container_name; - if (tmp.type == "out") { - tmptype = '