Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
feat: add API to get latest outages
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-barthelemy committed Aug 28, 2016
1 parent 3a0e95e commit 6db58c6
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/storage/providers/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,51 @@ StorageRedis.prototype.getServiceOutagesSince = function (service, timestamp, ca
});
};

/**
* Get outage history for all services
* @param services
* @param timestamp
* @param maxItems
* @param callback
*/

StorageRedis.prototype.getServicesOutagesSince = function (services, timestamp, maxItems, callback) {
// Creating a multi request to get each service outages.
var multi = this.redis.multi();
for (var i = 0; i < services.length; i++) {
multi.zrevrangebyscore(services[i].id + ':' + OUTAGES_KEY_SUFIX, '+inf', timestamp, 'LIMIT', 0, maxItems);
}

multi.exec(function (err, replies) {
if (err) {
callback(err, null);
return;
}

var servicesOutages = [];
for (var i = 0; i < services.length; i++) {
var svcOutages = replies[i].map(function (outage) {
outage = JSON.parse(outage);
outage['service'] = services[i].id;
return outage;
});

servicesOutages = servicesOutages.concat(svcOutages);
}

// Sorting by timestamp
servicesOutages.sort(function (a, b) {
if(a.timestamp < b.timestamp)
return -1;
if(a.timestamp < b.timestamp)
return 1;
return 0;
});

callback(err, servicesOutages.slice(0, maxItems));
})
};

/**
* Records ping latency
* @param service
Expand Down
57 changes: 57 additions & 0 deletions test/test-api-service-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,61 @@ describe('service route', function () {

});

describe('loading all outages', function () {

describe('with an anonymous user', function () {
before(function (done) {
agent.get('/logout').expect(302, done);
});

it('should require auth', function (done) {
storage.flush_database(function () {
storage.addService(validService, function (err, id) {
assert.ifError(err);
superAgentAssertions.shouldReturnStatusCode(agent, {
url : API_ROOT + '/services/outages',
statusCode: 401
}, done);
});
});
});
});

describe('with an authenticated normal user', function () {
before(function (done) {
agent.get('/login/test/2').expect(200, done);
});

it('should require auth', function (done) {
storage.flush_database(function () {
storage.addService(validService, function (err, id) {
assert.ifError(err);
superAgentAssertions.shouldReturnStatusCode(agent, {
url : API_ROOT + '/services/outages',
statusCode: 401
}, done);
});
});
});
});

describe('with an authenticated admin user', function () {
before(function (done) {
agent.get('/login/test/1').expect(200, done);
});

it('should load services', function (done) {
agent
.get(API_ROOT + '/services/outages')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.send()
.end(function (err, res) {
// TODO: Insert some outages and check against the result body
done(err);
});
});
});
});
});
29 changes: 29 additions & 0 deletions webserver/routes/api-service-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,35 @@ module.exports.getRoutes = function (storage) {
});
});

/**
* Get latest service outages.
*/

router.get('/services/outages', requireAdmin, function (req, res) {
var since = req.query.since;
if (!since) {
// Default to an hour ago.
since = new Date().getTime() - 3600000;
}

var maxItems = req.query['max-items'];
if(!maxItems) {
maxItems = 10;
}

storage.getServices({}, function (err, services) {
if (err) {
console.error(err);
return res.status(500).json({error: err});
}
var filteredServices = accessFilter.filterServices(services, req.user);

storage.getServicesOutagesSince(filteredServices, since, maxItems, function (err, outages) {
res.json(outages);
})
});
});

/**
* Load service
*/
Expand Down

0 comments on commit 6db58c6

Please sign in to comment.