Skip to content

Commit

Permalink
remove eventemitter dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
tortuvshin committed Aug 28, 2019
1 parent 31368c7 commit d062be4
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 85 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"release": "release-it"
},
"dependencies": {
"eventemitter3": "4.0.0",
"request": "2.88.0",
"intelligo": "1.2.1"
},
Expand Down
179 changes: 95 additions & 84 deletions src/Uptime.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,113 @@
'use strict';
"use strict";

const EventEmitter = require('eventemitter3'),
request = require('request');
const request = require("request");

class Uptime extends EventEmitter{

constructor(options) {
super();
if (!options || (options && (!options.SLACK_WEBHOOK_URL))) {
throw new Error("You need to specify an SLACK_WEBHOOK_URL");
}
this.SLACK_WEBHOOK_URL = options.SLACK_WEBHOOK_URL;
this.pingInterval = 1*1000*60
this.serviceStatus = {}
class Uptime {
constructor(options) {
super();
if (!options || (options && !options.SLACK_WEBHOOK_URL)) {
throw new Error("You need to specify an SLACK_WEBHOOK_URL");
}

pingService(url, cb){

request({
method: 'GET',
this.SLACK_WEBHOOK_URL = options.SLACK_WEBHOOK_URL;
this.pingInterval = 1 * 1000 * 60;
this.serviceStatus = {};
}

pingService(url, cb) {
request(
{
method: "GET",
uri: url,
time: true
}, (err, res, body) => {
},
(err, res, body) => {
if (!err && res.statusCode == 200) {
// we'll use the time from the point we try to establish a connection with
// the service until the first byte is received
cb(res.timingPhases.firstByte)
cb(res.timingPhases.firstByte);
} else {
cb('OUTAGE')
cb("OUTAGE");
}
})
}

postToSlack(serviceUrl){

var message = "";
if (this.serviceStatus[serviceUrl].status == 'DEGRADED'){
message = "`Degraded System Service !!!` :skull: ";
} else if (this.serviceStatus[serviceUrl].status == 'OPERATIONAL') {
message = "System Operational :robot_face:";
} else if (this.serviceStatus[serviceUrl].status == 'OUTAGE') {
message = "System Outage :zzz:";
}
let slackPayload = {
text: `*${message}*\n_${serviceUrl}_`
}

request({
method: 'POST',
);
}

postToSlack(serviceUrl) {
var message = "";
if (this.serviceStatus[serviceUrl].status == "DEGRADED") {
message = "`Degraded System Service !!!` :skull: ";
} else if (this.serviceStatus[serviceUrl].status == "OPERATIONAL") {
message = "System Operational :robot_face:";
} else if (this.serviceStatus[serviceUrl].status == "OUTAGE") {
message = "System Outage :zzz:";
}
let slackPayload = {
text: `*${message}*\n_${serviceUrl}_`
};

request(
{
method: "POST",
uri: this.SLACK_WEBHOOK_URL,
body: slackPayload,
json: true
}, (err, res, body) => {
if (err) console.log(`Error posting to Slack: ${err}`)
})
}

monitor(websites){
websites.forEach(service => {
this.serviceStatus[service.url] = {
status: 'OPERATIONAL', // initialize all services as operational when we start
responseTimes: [], // array containing the responses times for last 3 pings
timeout: service.timeout // load up the timout from the config
}

setInterval(() => {
this.pingService(service.url, (serviceResponse) => {
if (serviceResponse === 'OUTAGE' && this.serviceStatus[service.url].status !== 'OUTAGE') {
// only update and post to Slack on state change
this.serviceStatus[service.url].status = 'OUTAGE'
this.postToSlack(service.url)
} else {
let responseTimes = this.serviceStatus[service.url].responseTimes
responseTimes.push(serviceResponse)

// check degraded performance if we have 3 responses so we can average them
if (responseTimes.length > 3) {
// remove the oldest response time (beginning of array)
responseTimes.shift()

// compute average of last 3 response times
let avgResTime = responseTimes.reduce((a, b) => a + b, 0) / responseTimes.length
let currService = this.serviceStatus[service.url]

if (avgResTime > currService.timeout && currService.status !== 'DEGRADED') {
currService.status = 'DEGRADED'
this.postToSlack(service.url)
} else if (avgResTime < currService.timeout && currService.status !== 'OPERATIONAL') {
currService.status = 'OPERATIONAL'
this.postToSlack(service.url)
}
},
(err, res, body) => {
if (err) console.log(`Error posting to Slack: ${err}`);
}
);
}

monitor(websites) {
websites.forEach(service => {
this.serviceStatus[service.url] = {
status: "OPERATIONAL", // initialize all services as operational when we start
responseTimes: [], // array containing the responses times for last 3 pings
timeout: service.timeout // load up the timout from the config
};

setInterval(() => {
this.pingService(service.url, serviceResponse => {
if (
serviceResponse === "OUTAGE" &&
this.serviceStatus[service.url].status !== "OUTAGE"
) {
// only update and post to Slack on state change
this.serviceStatus[service.url].status = "OUTAGE";
this.postToSlack(service.url);
} else {
let responseTimes = this.serviceStatus[service.url].responseTimes;
responseTimes.push(serviceResponse);

// check degraded performance if we have 3 responses so we can average them
if (responseTimes.length > 3) {
// remove the oldest response time (beginning of array)
responseTimes.shift();

// compute average of last 3 response times
let avgResTime =
responseTimes.reduce((a, b) => a + b, 0) / responseTimes.length;
let currService = this.serviceStatus[service.url];

if (
avgResTime > currService.timeout &&
currService.status !== "DEGRADED"
) {
currService.status = "DEGRADED";
this.postToSlack(service.url);
} else if (
avgResTime < currService.timeout &&
currService.status !== "OPERATIONAL"
) {
currService.status = "OPERATIONAL";
this.postToSlack(service.url);
}

}
})
}, this.pingInterval)
})
}
}
});
}, this.pingInterval);
});
}
}

module.exports = Uptime;
module.exports = Uptime;

0 comments on commit d062be4

Please sign in to comment.