Skip to content

Instantly share code, notes, and snippets.

@fuzzyfox
Created May 24, 2014 01:23
Show Gist options
  • Save fuzzyfox/bd09e0a9b38946de1c50 to your computer and use it in GitHub Desktop.
Save fuzzyfox/bd09e0a9b38946de1c50 to your computer and use it in GitHub Desktop.
JavaScript: hacky fix to solve a stupid problem (DO NOT USE)
request.get(env.get('EVENTS_SERVICE') + '/events?after=' + env.get('EVENTS_START_DATE'), function(error, response, events) {
if(!error && response.statusCode === 200) {
events = JSON.parse( events );
// array to dedupe organizers
var events_by_host = {};
var event_hosts = [];
// init stats object
var event_stats = {
hosts: 0,
attendees: 0,
events: 0,
byCountry: {}
};
// aleady know number of events :)
event_stats.events = events.length;
var geocodes = events.length;
// make access to other stats easier to figure
events.forEach( function( event, idx ) {
event_stats.attendees += event.attendees;
if(event_hosts.indexOf(event.organizerId) === -1){
event_hosts.push(event.organizerId);
}
request.get('http://open.mapquestapi.com/nominatim/v1/reverse.php?format=json&lat='+ event.latitude +'&lon=' + event.longitude, function(err, response, geodata){
geodata = JSON.parse(geodata);
events[ idx ].country = {
name: geodata.address.country,
code: geodata.address.country_code
};
geocodes -= 1;
if( geocodes === 0 ) {
secondPass();
}
});
});
function secondPass(){
// second pass w/ geodata
var countries = [];
events.forEach( function( event ) {
if( countries.indexOf( event.country.code ) === -1 ) {
event_stats.byCountry[ event.country.code ] = {
name: event.country.name,
code: event.country.code,
events: 0
};
countries.push( event.country.code );
}
event_stats.byCountry[ event.country.code ].events++;
});
// now we can count number organizers (deduped)
event_stats.hosts = event_hosts.length;
cache.set('eventStats', event_stats, 10000);
}
}
else if(error) {
cache.set('eventStats', {}, 10000);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment