Skip to content

Instantly share code, notes, and snippets.

@th3hunt
Last active August 29, 2015 14:05
Show Gist options
  • Save th3hunt/22d7992d166fec0cf562 to your computer and use it in GitHub Desktop.
Save th3hunt/22d7992d166fec0cf562 to your computer and use it in GitHub Desktop.
Import certain honeybadger faults into a mongo DB
var args = process.argv.slice(2),
PROJECT_ID = args[0],
FAULT_ID = args[1],
// MONGO
MONGODB_URI = 'mongodb://localhost:27017/honeybadger',
mongodb = require('mongodb'),
mongoclient,
collection,
// REST
rest = require('rest'),
mime = require('rest/interceptor/mime'),
client,
// API
API_HOST = 'https://api.honeybadger.io/v1/',
API_KEY = 'MY API KEY'
// Other
_ = require('underscore'),
urlHelpers = {
authenticatedURL: function (url) {
var suffix = 'auth_token=' + API_KEY;
return url.indexOf('?') >= 0 ? url + '&' + suffix : url + '?' + suffix;
},
noticesURL: function (page) {
var url = API_HOST + 'projects/' + PROJECT_ID + '/faults/' + FAULT_ID + '/notices?page=' + (page || 1);
return this.authenticatedURL(url);
}
};
function fetchNotices(page) {
var url = urlHelpers.noticesURL(page);
console.log('Requesting %s', url);
client = rest.wrap(mime);
return client({ path: url }).then(function(response) {
var entity = response.entity;
console.log('Fetched %s notices', entity.results.length);
return entity;
});
}
function updateNotices(notices) {
notices.forEach(function (notice) {
console.log('Inserting notice %s', notice.id);
collection.update({id: notice.id}, notice, { w:1, upsert: true }, function (err, item) {
if (err) console.log(err);
});
});
}
mongoclient = mongodb.MongoClient.connect(MONGODB_URI, function(err, db) {
if(err) throw err;
collection = db.collection('notices');
function fetchAndUpdate(page) {
page = page || 1;
return fetchNotices(page).then(function (entity) {
updateNotices(entity.results);
if (entity.current_page < entity.num_pages) {
return fetchAndUpdate(page + 1);
}
});
}
fetchAndUpdate().done(function () {
setTimeout(function () {
db.close();
}, 100);
});
});
// remove aborted uploads
db.notices.remove({ 'request.context.audit_log.events': { $elemMatch: { 'message.event': 'definitefail', 'message.xhr.statusText': 'abort'} } })
// group by browser and version
db.notices.aggregate([{ $group: { _id: {browser: '$request.context.audit_log.browser.name', version: '$request.context.audit_log.browser.version' }, count: { $sum: 1 } } }])
// browser by name
db.notices.find().map(function (n) { return {browser: n.request.context.audit_log.browser.name, version: n.request.context.audit_log.browser.version, filename: n.request.context.audit_log.events[1].message.file.name} })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment