From f0f1bab0fe1f17cf10d16bc78fd1e77f38ba7bb4 Mon Sep 17 00:00:00 2001 From: Natalie <44040061+nmczormick@users.noreply.github.com> Date: Sat, 28 Sep 2019 17:14:40 -0400 Subject: [PATCH] Add files via upload --- Communication.js | 20 ++++ MicroserviceHealth.js | 56 +++++++++ middleware.js | 259 +++++++++++++++++++++++++++++++++++++----- test.js | 42 +++++++ 4 files changed, 346 insertions(+), 31 deletions(-) create mode 100644 Communication.js create mode 100644 MicroserviceHealth.js create mode 100644 test.js diff --git a/Communication.js b/Communication.js new file mode 100644 index 000000000..fe50f99b8 --- /dev/null +++ b/Communication.js @@ -0,0 +1,20 @@ +const mongoose = require('mongoose'); + +mongoose.model('Communication', { + currentMicroservice: { + type: String, + required: true + }, + targetedEndpoint: { + type: String, + required: true + }, + reqType: { + type: String, + required: true + }, + timeSent: { + type: Date, + required: true + } +}) \ No newline at end of file diff --git a/MicroserviceHealth.js b/MicroserviceHealth.js new file mode 100644 index 000000000..315223f62 --- /dev/null +++ b/MicroserviceHealth.js @@ -0,0 +1,56 @@ +const mongoose = require("mongoose"); + +mongoose.model("MicroserviceHealth", { + currentMicroservice: { + type: String, + required: true + }, + cpuCurrentSpeed: { + type: Number, + required: true + }, + cpuTemperature: { + type: Number, + required: true + }, + totalMemory: { + type: Number, + required: true + }, + freeMemory: { + type: Number, + required: true + }, + usedMemory: { + type: Number, + required: true + }, + activeMemory: { + type: Number, + required: true + }, + totalNumProcesses: { + type: Number, + required: true + }, + numRunningProcesses: { + type: Number, + required: true + }, + numBlockedProcesses: { + type: Number, + required: true + }, + numSleepingProcesses: { + type: Number, + required: true + }, + latency: { + type: Number, + required: false + }, + timestamp: { + type: Date, + required: true + } +}); diff --git a/middleware.js b/middleware.js index 1c57d617f..507f1b5e0 100644 --- a/middleware.js +++ b/middleware.js @@ -2,42 +2,239 @@ const express = require('express'); const app = express(); const mongoose = require('mongoose'); const bodyParser = require('body-parser'); -const path = require('path'); app.use(bodyParser.json()); +const si = require('systeminformation'); //Required to get rid of deprecation warnings mongoose.set("useUnifiedTopology", true); mongoose.set("useNewUrlParser", true); - module.exports = function(currentMicroservice) { - return function (req,res,next) { - mongoose.connect( - "mongodb+srv://numanzor:Nu121692.@microservice-tutorial-hq75f.mongodb.net/chronos-access", - () => { - console.log("Chronos database is connected..."); - } - ) - const currentMicroservicePath = currentMicroservice; - - require('./HealthInfo.js') - const HealthInfo = mongoose.model("HealthInfo") - - const newHealthPoint = { - currentMicroservice: currentMicroservicePath, - targetedEndpoint: req.originalUrl, - reqType: req.method, - timeSent: Date.now() - }; - - const healthPoint = new HealthInfo(newHealthPoint); - - healthPoint.save().then(()=> { - console.log('New Health Point Created') - }).catch((err) => { - if (err) { - throw err; - } - }) - next(); +const chronos = {}; + +chronos.connectDB = () => { + mongoose.connect( + "mongodb+srv://numanzor:Nu121692.@microservice-tutorial-hq75f.mongodb.net/chronos-access", + () => { + console.log("Chronos database is connected..."); } + ); +} + +chronos.microCom = (currentMicroservice) => { + chronos.connectDB() + return function (req, res, next) { + console.log('HEY DO WE HIT THIS?') + const currentMicroservicePath = currentMicroservice; + + require('./Communication.js') + const Communication = mongoose.model("Communication") + + const newCommunication = { + currentMicroservice: currentMicroservicePath, + targetedEndpoint: req.originalUrl, + reqType: req.method, + timeSent: Date.now(), + }; + + const communication = new Communication(newCommunication); + + communication.save().then(()=> { + console.log('New Microservice Communication Created') + next(); + }).catch((err) => { + if (err) { + throw err; + } + }) } +}, + +chronos.microHealth = (currentMicroservice) => { + require('./MicroserviceHealth.js') + const MicroserviceHealth = mongoose.model('MicroserviceHealth') + let cpuCurrentSpeed, cpuTemperature, totalMemory, freeMemory, usedMemory, activeMemory, latency, timestamp; + let currentMicroservicePath, totalNumProcesses, numBlockedProcesses, numRunningProcesses, numSleepingProcesses; + + chronos.connectDB(); + currentMicroservicePath = currentMicroservice; + + setInterval(() => { + si.cpuCurrentspeed() + .then(data => { + cpuCurrentSpeed = data.avg; + }) + .catch((err) => { + console.log(err) + }) + + si.cpuTemperature() + .then(data => { + cpuTemperature = data.main + }) + .catch((err) => { + console.log(err) + }) + + si.mem() + .then(data => { + totalMemory = data.total + freeMemory = data.free + usedMemory = data.used + activeMemory = data.active + }) + .catch((err) => { + console.log(err) + }) + + si.processes() + .then(data => { + totalNumProcesses = data.all + numBlockedProcesses = data.blocked + numRunningProcesses = data.running + numSleepingProcesses = data.sleeping + }) + .catch((err) => { + console.log(err) + }) + + si.inetLatency() + .then(data => { + latency = data + }) + .catch((err) => { + console.log(err) + }) + + const newHealthPoint = { + timestamp: Date.now(), + currentMicroservice: currentMicroservice, + cpuCurrentSpeed: cpuCurrentSpeed, + cpuTemperature: cpuTemperature, + totalMemory: totalMemory, + freeMemory: freeMemory, + usedMemory: usedMemory, + activeMemory: activeMemory, + totalNumProcesses: totalNumProcesses, + numRunningProcesses: numRunningProcesses, + numBlockedProcesses: numBlockedProcesses, + numSleepingProcesses: numSleepingProcesses, + latency: latency + } + + const healthPoint = new MicroserviceHealth(newHealthPoint) + healthPoint.save() + .then(() => { + console.log('New Microservice Health Point Created') + }) + .catch((err) => { + if (err) { + throw err; + } + }) + }, 1000) +} + +module.exports = chronos; + + // module.exports = function(currentMicroservice) { + // return function (req,res,next) { + // mongoose.connect( + // "mongodb+srv://numanzor:Nu121692.@microservice-tutorial-hq75f.mongodb.net/chronos-access", + // () => { + // console.log("Chronos database is connected..."); + // } + // ); + // const currentMicroservicePath = currentMicroservice; + + // require('./Communication.js') + // const Communication = mongoose.model("Communication") + + // const newCommunication = { + // currentMicroservice: currentMicroservicePath, + // targetedEndpoint: req.originalUrl, + // reqType: req.method, + // timeSent: Date.now(), + // }; + + // const communication = new Communication(newCommunication); + + // communication.save().then(()=> { + // console.log('New Microservice Communication Created') + // }).catch((err) => { + // if (err) { + // throw err; + // } + // }) + + // require('./MicroserviceHealth.js') + // const MicroserviceHealth = mongoose.model('MicroserviceHealth') + // let cpuCurrentSpeed, cpuTemperature, totalMemory, freeMemory, usedMemory, activeMemory; + // let totalNumProcesses, numBlockedProcesses, numRunningProcesses, numSleepingProcesses; + + // si.cpuCurrentspeed() + // .then(data => { + // console.log(data) + // }) + // .catch((err) => { + // console.log(err) + // }) + + // si.cpuTemperature() + // .then(data => { + // cpuTemperature = data.main + // }) + // .catch((err) => { + // console.log(err) + // }) + + // si.mem() + // .then(data => { + // totalMemory = data.total + // freeMemory = data.free + // usedMemory = data.used + // activeMemory = data.active + // }) + // .catch((err) => { + // console.log(err) + // }) + + // si.processes() + // .then(data => { + // totalNumProcesses = data.all + // numBlockedProcesses = data.blocked + // numRunningProcesses = data.running + // numSleepingProcesses = data.sleeping + // }) + // .catch((err) => { + // console.log(err) + // }) + + // const newHealthPoint = { + // currentMicroservice: currentMicroservice, + // cpuCurrentSpeed: cpuCurrentSpeed, + // cpuTemperature: cpuTemperature, + // totalMemory: totalMemory, + // freeMemory: freeMemory, + // usedMemory: usedMemory, + // activeMemory: activeMemory, + // totalNumProcesses: totalNumProcesses, + // numRunningProcesses: numRunningProcesses, + // numBlockedProcesses: numBlockedProcesses, + // numSleepingProcesses: numSleepingProcesses + // } + + // const healthPoint = new MicroserviceHealth(newHealthPoint) + // healthPoint.save() + // .then(() => { + // console.log('New Microservice Communication Created') + // }) + // .catch((err) => { + // if (err) { + // throw err; + // } + // }) + // // We are currently able to access the responses, but are not sending status codes or messages + // // This is due to the way the microservices are written. + // next(); + // } + // } diff --git a/test.js b/test.js new file mode 100644 index 000000000..e5cc75ffd --- /dev/null +++ b/test.js @@ -0,0 +1,42 @@ +var expect = require('chai').expect; +var MongoClient = require('mongodb').MongoClient; + +describe('middleware', () => { + var healthInfo, db; + + beforeAll(function (done) { + MongoClient.connect( + 'mongodb+srv://numanzor:Nu121692.@microservice-tutorial-hq75f.mongodb.net/chronos-access', { useNewUrlParser: true }, function(err, client) { + if (err) throw new Error(err); + done(); + db = client.db('chronos-access'); + healthInfo = db.collection('healthinfos'); + } + ); + }); + + test('should have records in the "healthinfos" collection', done => { + healthInfo.countDocuments(function (err, num) { + expect(err).to.not.exist; + expect(num).to.be.above(0); + done(); + }); + }); + + test('should have the right string and date-time fields', done => { + healthInfo.find().toArray(function (err, data) { + expect(err).to.not.exist; + expect(data).to.be.an('Array'); + var dataPoint = data[0]; + expect(dataPoint.currentMicroservice).to.be.a('string').and.not.eql(''); + expect(dataPoint.targetedEndpoint).to.be.a('string').and.not.eql(''); + expect(dataPoint.reqType).to.be.a('string').and.not.eql(''); + expect(dataPoint.timeSent).to.be.a('date').and.not.eql(''); + done(); + }); + }); + + afterAll(function () { + db.close(); + }); +}); \ No newline at end of file