Skip to content

Instantly share code, notes, and snippets.

@lloydmeta
Last active December 17, 2015 03:39
Show Gist options
  • Save lloydmeta/5545084 to your computer and use it in GitHub Desktop.
Save lloydmeta/5545084 to your computer and use it in GitHub Desktop.
Javascript API function for RockMongo to find slowish endpoints based on nginx access logs gathered from instances. Now sorts endpoints based on number of results in descending order !
// Contains questionable Javascript and Regex. You have been warned.
function () {
var counting_object = {
count_hash : {},
clean_endpoint : function(endpoint) {
return endpoint.replace(/(\/[\w]+\/[\d]+)/, function(v){return v.replace(/\/[\d]+/, "/:id")})
.replace(/max_id=[\d]+/,"{some_max_id}")
.replace(/since_id=[\d]+/,"{some_since_id}")
.replace(/time_from=[\d]+/,"{some_time_from}")
.replace(/time_until=[\d]+/,"{some_time_until}")
.replace(/count=[\d]+/,"{some_count}")
.replace(/_=[\d]+/,"")
.replace(/code=[\a-zA-Z0-9_]+/,"{some_code}")
.replace(/frm_id=.*/,"{some_frm_id}")
.replace("&{some_frm_id}", "")
.replace("{some_frm_id?", "")
.replace(/\?id=.*/,'{some_redis_id}')
},
increment_for_endpoint : function(endpoint){
var that = this,
clean_endpoint = that.clean_endpoint(endpoint);
if (that.count_hash[clean_endpoint] && that.count_hash[clean_endpoint] >= 1){
that.count_hash[clean_endpoint] = that.count_hash[clean_endpoint] + 1;
}else{
that.count_hash[clean_endpoint] = 1;
}
return that.count_hash[clean_endpoint];
},
sorted_results : function(){
var that = this,
sortable = [];
for (var count_key in that.count_hash) {
sortable.push([count_key, that.count_hash[count_key]])
};
sortable.sort(function(a,b){return b[1] - a[1]});
return sortable;
}
};
var cur = db.access.find(
{
"upstream_request_time":{
$gte: 5
}
}
);
var out = [];
cur.forEach(function(x){
counting_object.increment_for_endpoint((x['path']+' -> '+x['method']))});
return(counting_object.sorted_results());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment