From b5e5a0d9d299b612c34076ab225257086e95c64c Mon Sep 17 00:00:00 2001 From: zeus Date: Tue, 24 Nov 2020 15:54:48 +0200 Subject: [PATCH] mongo tail --- swarmlab-app/src/run/app.js | 66 +++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/swarmlab-app/src/run/app.js b/swarmlab-app/src/run/app.js index 009f645..5c60734 100755 --- a/swarmlab-app/src/run/app.js +++ b/swarmlab-app/src/run/app.js @@ -6,13 +6,16 @@ var http = require('http').Server(app); var https = require('https'); var io = require('socket.io')(http); -var chokidar = require("chokidar"); -var logpath = "/var/lab/playground-serverlogs"; -var watcher = chokidar.watch(logpath, { - ignored: /[\/\\]\./, - awaitWriteFinish: true, - persistent: true - }); +var mongodb = require('mongodb'); + MongoClient = mongodb.MongoClient; + +//var chokidar = require("chokidar"); +//var logpath = "/var/lab/playground-serverlogs"; +//var watcher = chokidar.watch(logpath, { +// ignored: /[\/\\]\./, +// awaitWriteFinish: true, +// persistent: true +// }); var async = require("async"); @@ -251,8 +254,14 @@ app.get('/run', [ cors(corsOptions), (req, res, next) => { var RES = new Object(); - RES.code = req.query["code"] - console.error('socket GET from client ' + RES.code); + RES.code = req.query["filter"] + RES.token = req.query["filter"] + var isvalid = await checkToken(RES.token); + if(isvalid.action == 'ok'){ + console.log("Authserver ok " + RES.token); + }else{ + console.log("Authserver no " + RES.token); + } RES.error = false RES.error_msg = "ok" res.json(RES) @@ -274,9 +283,9 @@ cors(corsOptions), (req, res, next) => { //console.log(JSON.stringify(req.headers)); //console.log(JSON.stringify(req.body)); - console.log("mongo "+JSON.stringify(req.body)); - console.log("LOG "+JSON.stringify(req.body[0].message)); - console.log("PATH "+JSON.stringify(req.body[0].tailed_path)); + //console.log("mongo "+JSON.stringify(req.body)); + //console.log("LOG "+JSON.stringify(req.body[0].message)); + //console.log("PATH "+JSON.stringify(req.body[0].tailed_path)); for (var i = 0; i < req.body.length; i++){ //var getpath = await onlogfile(req.body[i].tailed_path) @@ -352,11 +361,42 @@ cors(corsOptions), (req, res, next) => { }); // *************************************************** -// rocket +// socket // *************************************************** io.origins('*:*') // for latest version + +function onCollection(err, collection) { + var cursor = collection.find({}, { tailable: true, awaitdata: true }), + cursorStream = cursor.stream(), + itemsProcessed = 0; + + cursorStream.on('data', function (data) { + console.log(data.value); + itemsProcessed++; + }); + + setInterval(function () { + console.log('itemsProcessed', itemsProcessed); + }, 1000); +} + +//function onConnected(err, db) { +// db.collection('log', onCollection); +//} + +var mongourl = "mongodb://playgrounduser:efvvnuioervefSDFSGYGHRDFVsdfergvssppiiedifhwincvinviw_dbfjbsifbsdkjfswuunscfudfgbbfvibqefwrvnine@ondemand_playground_mongo1:27017,ondemand_playground_mongo2:27017,ondemand_playground_mongo3:27017,ondemand_playground_mongo4:27017,ondemand_playground_mongo5:27017,ondemand_playground_mongo6:27017,ondemand_playground_mongo7:27017?replicaSet=rs1&tls=false" +//MongoClient.connect(mongourl, options).then((client) => { +MongoClient.connect(mongourl).then((client) => { + const db = client.db('fluent'); + db.collection('log', onCollection); + // do database things +}).catch((error) => { + console.log("error " +JSON.stringify(error)) + // handle connection errors +}); + io.on('connection', s => { console.error('socket connection');