Browse Source

add rest

master
zeus 4 years ago
parent
commit
89916bb1a3
  1. 1
      swarmlab-app/src/package.json
  2. 102
      swarmlab-app/src/run/app.js

1
swarmlab-app/src/package.json

@ -17,6 +17,7 @@
"express": "^4.17.1", "express": "^4.17.1",
"express-validator": "^6.6.1", "express-validator": "^6.6.1",
"helmet": "^4.1.1", "helmet": "^4.1.1",
"luxon": "^1.25.0",
"url-exist-sync": "^1.0.2" "url-exist-sync": "^1.0.2"
}, },
"devDependencies": {} "devDependencies": {}

102
swarmlab-app/src/run/app.js

@ -60,6 +60,8 @@ pubClient.on("connect", function() {
}); });
const MongoClient = require('mongodb').MongoClient; const MongoClient = require('mongodb').MongoClient;
const { DateTime } = require("luxon");
var async = require("async"); var async = require("async");
const { check, validationResult } = require('express-validator'); const { check, validationResult } = require('express-validator');
@ -106,6 +108,7 @@ const corsOptions = {
} }
// *************************************************** // ***************************************************
// checktoken // 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.online='ob';
global.pipelines=[]; global.pipelines=[];
@ -285,8 +346,7 @@ global.pipelines=[];
// *************************************************** // ***************************************************
app.get('/get_log', [ app.get('/get_log', [
//check('access_token').isLength({ min: 40 }), check('access_token').isLength({ min: 40 })
//check('llo').isBase64()
], ],
cors(corsOptions), (req, res, next) => { cors(corsOptions), (req, res, next) => {
@ -295,7 +355,6 @@ cors(corsOptions), (req, res, next) => {
RES.token = req.query["token"] RES.token = req.query["token"]
RES.start = req.query["start"] RES.start = req.query["start"]
RES.end = req.query["end"] RES.end = req.query["end"]
RES.filter = req.query["filter"];
var isvalid = await checkToken(RES.token); var isvalid = await checkToken(RES.token);
if(isvalid.action == 'ok'){ if(isvalid.action == 'ok'){
console.log("Authserver ok " + RES.token); console.log("Authserver ok " + RES.token);
@ -304,7 +363,8 @@ cors(corsOptions), (req, res, next) => {
console.log("Authserver no " + RES.token); console.log("Authserver no " + RES.token);
RES.error = 'no' RES.error = 'no'
} }
if(RES.error == 'ok'){ 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" 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 = { const OPTS = {
useNewUrlParser: true, useNewUrlParser: true,
@ -315,14 +375,27 @@ if(RES.error == 'ok'){
console.log(err); console.log(err);
} else { } else {
const db = client.db('fluent'); 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); //db.collection('log', onCollection);
console.log(JSON.stringify('mongo connected')) 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 } } ] };
db.collection('logs').find({}, {}).toArray() var resdataarray = []
db.collection('logs').find({}, {}).toArray()
.then(item => { .then(item => {
console.log(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.error_msg = "ok"
RES.data = item RES.data = item
RES.dataserver = resdataarray
res.json(RES) res.json(RES)
}) })
.catch(err => { .catch(err => {
@ -334,23 +407,6 @@ db.collection('logs').find({}, {}).toArray()
/*
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) { //stream.on('data', function (doc) {
// console.log(JSON.stringify(doc)) // console.log(JSON.stringify(doc))
//socket.write(JSON.stringify({'action': 'log','param': doc.log})); //socket.write(JSON.stringify({'action': 'log','param': doc.log}));
@ -360,7 +416,7 @@ await find()
}else{ }else{
RES.data = 'no' RES.data = 'no'
RES.error_msg = "tokeni err" RES.error_msg = "token err"
res.json(RES) res.json(RES)
} }

Loading…
Cancel
Save