From 89916bb1a30428ef43fc8e4f0ea87f8a0c4c50fc Mon Sep 17 00:00:00 2001 From: zeus Date: Thu, 10 Dec 2020 16:24:21 +0200 Subject: [PATCH] add rest --- swarmlab-app/src/package.json | 1 + swarmlab-app/src/run/app.js | 176 ++++++++++++++++++++++------------ 2 files changed, 117 insertions(+), 60 deletions(-) diff --git a/swarmlab-app/src/package.json b/swarmlab-app/src/package.json index 8a9baa0..ddf5956 100644 --- a/swarmlab-app/src/package.json +++ b/swarmlab-app/src/package.json @@ -17,6 +17,7 @@ "express": "^4.17.1", "express-validator": "^6.6.1", "helmet": "^4.1.1", + "luxon": "^1.25.0", "url-exist-sync": "^1.0.2" }, "devDependencies": {} diff --git a/swarmlab-app/src/run/app.js b/swarmlab-app/src/run/app.js index b330fbb..817f7d5 100755 --- a/swarmlab-app/src/run/app.js +++ b/swarmlab-app/src/run/app.js @@ -60,6 +60,8 @@ pubClient.on("connect", function() { }); const MongoClient = require('mongodb').MongoClient; +const { DateTime } = require("luxon"); + var async = require("async"); const { check, validationResult } = require('express-validator'); @@ -106,6 +108,7 @@ const corsOptions = { } + // *************************************************** // checktoken // *************************************************** @@ -233,6 +236,64 @@ async function getpipelines(token,pipelinename) { } } +// *************************************************** +// get user pipelines +// *************************************************** + +async function getuserpipelines(token,user) { + var pipelinename = user + const agent = new https.Agent({ + rejectUnauthorized: false, + }); + const instance = axios.create({ + baseURL: 'https://api.swarmlab.io', + withCredentials: true, + rejectUnauthorized: false, + crossdomain: true, + httpsAgent: agent, + headers: { + 'Accept': 'application/json', + 'Content-Type': 'multipart/form-data', + 'Authorization': 'Bearer '+token + } + }) + try { + + var pipelines = { + "querytokenFilter":'uWr4FKRqrmpCRkJ9WLuI0DNuDWOGTkfcSzyZkJirZvJwwFDffLWrraqzzSPLeuQqL3TF9', + "filter":pipelinename + } + //var params = { + // pipeline: pipelines + // } + var params = { + querytokenFilter:'uWr4FKRqrmpCRkJ9WLuI0DNuDWOGTkfcSzyZkJirZvJwwFDffLWrraqzzSPLeuQqL3TF9', + filter:pipelinename + } + + var options = { + params: params, + headers: { 'content-type': 'application/x-www-form-urlencoded',Authorization: `Bearer ${token}` }, + }; + + instance.defaults.timeout = 30000; + const res = await instance.get('/getuserplaygrounds',options); + if(res.status == 200){ + return res.data + }else{ + console.log("noerror: " + res) + return await res.status + + } + } + catch (err) { + console.error("error: "+err); + var error = new Object(); + error.action = '401' + return await error + } +} + global.online='ob'; global.pipelines=[]; @@ -285,71 +346,66 @@ global.pipelines=[]; // *************************************************** app.get('/get_log', [ - //check('access_token').isLength({ min: 40 }), - //check('llo').isBase64() + check('access_token').isLength({ min: 40 }) ], cors(corsOptions), (req, res, next) => { (async() => { - var RES = new Object(); - RES.token = req.query["token"] - RES.start = req.query["start"] - RES.end = req.query["end"] - RES.filter = req.query["filter"]; - var isvalid = await checkToken(RES.token); - if(isvalid.action == 'ok'){ - console.log("Authserver ok " + RES.token); - RES.error = 'ok' - }else{ - console.log("Authserver no " + RES.token); - RES.error = 'no' - } -if(RES.error == 'ok'){ - var mongourl = "mongodb://"+CONFIG.mongo.user+":"+CONFIG.mongo.password+"@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/fluent?replicaSet=rs1&authSource=swarmlabplaygroundstats" - const OPTS = { - useNewUrlParser: true, - useUnifiedTopology: true - }; - MongoClient.connect(mongourl, OPTS, function(err, client){ - if(err){ - console.log(err); - } else { - const db = client.db('fluent'); - //db.collection('log', onCollection); - console.log(JSON.stringify('mongo connected')) - -db.collection('logs').find({}, {}).toArray() - .then(item => { - console.log(item) - RES.error_msg = "ok" - RES.data = item - res.json(RES) - }) - .catch(err => { - console.error(err) - RES.error_msg = err - RES.data = stream - res.json(RES) - }) - - + var RES = new Object(); + RES.token = req.query["token"] + RES.start = req.query["start"] + RES.end = req.query["end"] + var isvalid = await checkToken(RES.token); + if(isvalid.action == 'ok'){ + console.log("Authserver ok " + RES.token); + RES.error = 'ok' + }else{ + console.log("Authserver no " + RES.token); + RES.error = 'no' + } + if(RES.error == 'ok'){ + var resdata = await getuserpipelines(token,isvalid.user) + var mongourl = "mongodb://"+CONFIG.mongo.user+":"+CONFIG.mongo.password+"@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/fluent?replicaSet=rs1&authSource=swarmlabplaygroundstats" + const OPTS = { + useNewUrlParser: true, + useUnifiedTopology: true + }; + MongoClient.connect(mongourl, OPTS, function(err, client){ + if(err){ + console.log(err); + } else { + const db = client.db('fluent'); + //usersession.SOCKET.user = isvalid.user + //usersession.SOCKET.scope = isvalid.scope // space delimeter + //usersession.SOCKET.token = isvalid.token + //db.collection('log', onCollection); + console.log(JSON.stringify('mongo connected')) + var datestart = DateTime.fromObject(RES.start).toISODate() + var dateend = DateTime.fromObject(RES.end).toISODate() + var search_term = { $and: [ { date: { $gt: datestart } }, { date: { $lt: dateend } } ] }; + + var resdataarray = [] + db.collection('logs').find({}, {}).toArray() + .then(item => { + console.log(item) + for (let i in resdata.data) { + var resdataobj = {} + resdataobj.lab = resdata.data[i].res25swarmlabname + resdataarray.push(resdataobj) + } + RES.error_msg = "ok" + RES.data = item + RES.dataserver = resdataarray + res.json(RES) + }) + .catch(err => { + console.error(err) + RES.error_msg = err + RES.data = stream + res.json(RES) + }) -/* -const find = async () => { - try { - var stream = await db.collection('logs').find({}, {}) - RES.error_msg = "ok" - RES.data = stream - res.json(RES) - } catch(err => { - RES.error_msg = err - RES.data = stream - res.json(RES) - }) -} -await find() -*/ //stream.on('data', function (doc) { // console.log(JSON.stringify(doc)) @@ -360,7 +416,7 @@ await find() }else{ RES.data = 'no' -RES.error_msg = "tokeni err" +RES.error_msg = "token err" res.json(RES) }