Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #3

Merged
merged 1 commit into from
Sep 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Communication.js
Original file line number Diff line number Diff line change
@@ -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
}
})
56 changes: 56 additions & 0 deletions MicroserviceHealth.js
Original file line number Diff line number Diff line change
@@ -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
}
});
259 changes: 228 additions & 31 deletions middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
// }
// }
42 changes: 42 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});