Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
tortuvshin committed Jan 17, 2018
1 parent 5b30c94 commit 6a00ade
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "techstar-uptime",
"version": "1.0.1",
"version": "1.0.3",
"description": "Uptime monitor",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 2 additions & 4 deletions src/helpers/default-config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module.exports = {
websites: {
url: 'https://www.techstar.cloud', // URL of service we'll be pining
timeout: 200 // threshold in milliseconds above which is considered degraded performance
},
url: 'https://www.techstar.cloud', // URL of service we'll be pining
timeout: 200, // threshold in milliseconds above which is considered degraded performance
SLACK_WEBHOOK_URL: ""
};
4 changes: 3 additions & 1 deletion src/helpers/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ module.exports = config => {
return defaultConfig;
}

config.websites = (typeof config.websites === 'object') ? config.websites : defaultConfig.websites;
config.url = (typeof config.url === 'object') ? config.url : defaultConfig.url;
config.timeout = (typeof config.timeout === 'number') ? config.timeout : defaultConfig.timeout;
config.SLACK_WEBHOOK_URL = (typeof config.SLACK_WEBHOOK_URL === 'string') ? config.SLACK_WEBHOOK_URL : defaultConfig.SLACK_WEBHOOK_URL;

return config;
};
22 changes: 11 additions & 11 deletions src/middleware-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ const middlewareWrapper = config => {
const pingInterval = 1*1000*60 // 1 minutes
let serviceStatus = {}

validatedConfig.websites.forEach(service => {
serviceStatus[service.url] = {
monitor => {
serviceStatus[validatedConfig.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
timeout: validatedConfig.timeout // load up the timout from the config
}

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

// check degraded performance if we have 3 responses so we can average them
Expand All @@ -47,21 +47,21 @@ const middlewareWrapper = config => {

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

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

}
})
}, pingInterval)
})
}

const postToSlack = (serviceUrl) => {
var message = "";
Expand Down

0 comments on commit 6a00ade

Please sign in to comment.